blob: 540bc8ef607c6a0a60a39928990802c7d0a492f7 [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"
Erik Verbruggen888d52a2014-01-15 09:15:43 +000016#include "clang/AST/ASTContext.h"
Chandler Carruth757fcd62014-03-04 10:05:20 +000017#include "clang/AST/DeclTemplate.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000018#include "clang/Basic/AddressSpaces.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000019#include "clang/Basic/CharInfo.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) {
Aaron Ballman35db2b32014-01-29 22:13:45 +000072#define CLANG_ATTR_LATE_PARSED_LIST
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000073 return llvm::StringSwitch<bool>(II.getName())
Aaron Ballman35db2b32014-01-29 22:13:45 +000074#include "clang/Parse/AttrParserStringSwitches.inc"
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000075 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +000076#undef CLANG_ATTR_LATE_PARSED_LIST
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000077}
78
Alexis Hunt96d5c762009-11-21 08:43:09 +000079/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000080///
81/// [GNU] attributes:
82/// attribute
83/// attributes attribute
84///
85/// [GNU] attribute:
86/// '__attribute__' '(' '(' attribute-list ')' ')'
87///
88/// [GNU] attribute-list:
89/// attrib
90/// attribute_list ',' attrib
91///
92/// [GNU] attrib:
93/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000094/// attrib-name
95/// attrib-name '(' identifier ')'
96/// attrib-name '(' identifier ',' nonempty-expr-list ')'
97/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000098///
Steve Naroff0f2fe172007-06-01 17:11:19 +000099/// [GNU] attrib-name:
100/// identifier
101/// typespec
102/// typequal
103/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +0000104///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000105/// Whether an attribute takes an 'identifier' is determined by the
106/// attrib-name. GCC's behavior here is not worth imitating:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000107///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000108/// * In C mode, if the attribute argument list starts with an identifier
109/// followed by a ',' or an ')', and the identifier doesn't resolve to
110/// a type, it is parsed as an identifier. If the attribute actually
111/// wanted an expression, it's out of luck (but it turns out that no
112/// attributes work that way, because C constant expressions are very
113/// limited).
114/// * In C++ mode, if the attribute argument list starts with an identifier,
115/// and the attribute *wants* an identifier, it is parsed as an identifier.
116/// At block scope, any additional tokens between the identifier and the
117/// ',' or ')' are ignored, otherwise they produce a parse error.
Richard Smithb12bf692011-10-17 21:20:17 +0000118///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000119/// We follow the C++ model, but don't allow junk after the identifier.
John McCall53fa7142010-12-24 02:08:15 +0000120void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000121 SourceLocation *endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000122 LateParsedAttrList *LateAttrs,
123 Declarator *D) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000124 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000125
Chris Lattner76c72282007-10-09 17:33:22 +0000126 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000127 ConsumeToken();
128 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
129 "attribute")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000130 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000131 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000132 }
133 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000134 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000135 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000136 }
137 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Alp Toker094e5212014-01-05 03:27:11 +0000138 while (true) {
139 // Allow empty/non-empty attributes. ((__vector_size__(16),,,,))
140 if (TryConsumeToken(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000141 continue;
Alp Toker094e5212014-01-05 03:27:11 +0000142
143 // Expect an identifier or declaration specifier (const, int, etc.)
144 if (Tok.isNot(tok::identifier) && !isDeclarationSpecifier())
145 break;
146
Steve Naroff0f2fe172007-06-01 17:11:19 +0000147 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
148 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000149
Alp Toker094e5212014-01-05 03:27:11 +0000150 if (Tok.isNot(tok::l_paren)) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000151 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
152 AttributeList::AS_GNU);
Alp Toker094e5212014-01-05 03:27:11 +0000153 continue;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000154 }
Alp Toker094e5212014-01-05 03:27:11 +0000155
156 // Handle "parameterized" attributes
157 if (!LateAttrs || !isAttributeLateParsed(*AttrName)) {
158 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, 0,
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000159 SourceLocation(), AttributeList::AS_GNU, D);
Alp Toker094e5212014-01-05 03:27:11 +0000160 continue;
161 }
162
163 // Handle attributes with arguments that require late parsing.
164 LateParsedAttribute *LA =
165 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
166 LateAttrs->push_back(LA);
167
168 // Attributes in a class are parsed at the end of the class, along
169 // with other late-parsed declarations.
170 if (!ClassStack.empty() && !LateAttrs->parseSoon())
171 getCurrentClass().LateParsedDeclarations.push_back(LA);
172
173 // consume everything up to and including the matching right parens
174 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
175
176 Token Eof;
177 Eof.startToken();
178 Eof.setLocation(Tok.getLocation());
179 LA->Toks.push_back(Eof);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000180 }
Alp Toker094e5212014-01-05 03:27:11 +0000181
Alp Toker383d2c42014-01-01 03:08:43 +0000182 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000183 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000184 SourceLocation Loc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000185 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000186 SkipUntil(tok::r_paren, StopAtSemi);
John McCall53fa7142010-12-24 02:08:15 +0000187 if (endLoc)
188 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000189 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000190}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000191
Aaron Ballman4768b312013-11-04 12:55:56 +0000192/// \brief Normalizes an attribute name by dropping prefixed and suffixed __.
193static StringRef normalizeAttrName(StringRef Name) {
Richard Smith66e71682013-10-24 01:07:54 +0000194 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
195 Name = Name.drop_front(2).drop_back(2);
Aaron Ballman4768b312013-11-04 12:55:56 +0000196 return Name;
197}
198
199/// \brief Determine whether the given attribute has an identifier argument.
200static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +0000201#define CLANG_ATTR_IDENTIFIER_ARG_LIST
Aaron Ballman4768b312013-11-04 12:55:56 +0000202 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +0000203#include "clang/Parse/AttrParserStringSwitches.inc"
Douglas Gregord2472d42013-05-02 23:25:32 +0000204 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +0000205#undef CLANG_ATTR_IDENTIFIER_ARG_LIST
Douglas Gregord2472d42013-05-02 23:25:32 +0000206}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000207
Aaron Ballman4768b312013-11-04 12:55:56 +0000208/// \brief Determine whether the given attribute parses a type argument.
209static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +0000210#define CLANG_ATTR_TYPE_ARG_LIST
Aaron Ballman4768b312013-11-04 12:55:56 +0000211 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +0000212#include "clang/Parse/AttrParserStringSwitches.inc"
Aaron Ballman4768b312013-11-04 12:55:56 +0000213 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +0000214#undef CLANG_ATTR_TYPE_ARG_LIST
Aaron Ballman4768b312013-11-04 12:55:56 +0000215}
216
Aaron Ballman15b27b92014-01-09 19:39:35 +0000217/// \brief Determine whether the given attribute requires parsing its arguments
218/// in an unevaluated context or not.
219static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +0000220#define CLANG_ATTR_ARG_CONTEXT_LIST
Aaron Ballman15b27b92014-01-09 19:39:35 +0000221 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +0000222#include "clang/Parse/AttrParserStringSwitches.inc"
Aaron Ballman15b27b92014-01-09 19:39:35 +0000223 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +0000224#undef CLANG_ATTR_ARG_CONTEXT_LIST
Aaron Ballman15b27b92014-01-09 19:39:35 +0000225}
226
Richard Smithfeefaf52013-09-03 18:01:40 +0000227IdentifierLoc *Parser::ParseIdentifierLoc() {
228 assert(Tok.is(tok::identifier) && "expected an identifier");
229 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
230 Tok.getLocation(),
231 Tok.getIdentifierInfo());
232 ConsumeToken();
233 return IL;
234}
235
Richard Smithb1f9a282013-10-31 01:56:18 +0000236void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
237 SourceLocation AttrNameLoc,
238 ParsedAttributes &Attrs,
239 SourceLocation *EndLoc) {
240 BalancedDelimiterTracker Parens(*this, tok::l_paren);
241 Parens.consumeOpen();
242
243 TypeResult T;
244 if (Tok.isNot(tok::r_paren))
245 T = ParseTypeName();
246
247 if (Parens.consumeClose())
248 return;
249
250 if (T.isInvalid())
251 return;
252
253 if (T.isUsable())
254 Attrs.addNewTypeAttr(&AttrName,
255 SourceRange(AttrNameLoc, Parens.getCloseLocation()), 0,
256 AttrNameLoc, T.get(), AttributeList::AS_GNU);
257 else
258 Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
259 0, AttrNameLoc, 0, 0, AttributeList::AS_GNU);
260}
261
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000262void Parser::ParseAttributeArgsCommon(
263 IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
264 ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
265 SourceLocation ScopeLoc, AttributeList::Syntax Syntax) {
266 // Ignore the left paren location for now.
267 ConsumeParen();
268
269 ArgsVector ArgExprs;
270 if (Tok.is(tok::identifier)) {
271 // If this attribute wants an 'identifier' argument, make it so.
272 bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName);
273 AttributeList::Kind AttrKind =
274 AttributeList::getKind(AttrName, ScopeName, Syntax);
275
276 // If we don't know how to parse this attribute, but this is the only
277 // token in this argument, assume it's meant to be an identifier.
278 if (AttrKind == AttributeList::UnknownAttribute ||
279 AttrKind == AttributeList::IgnoredAttribute) {
280 const Token &Next = NextToken();
281 IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma);
282 }
283
284 if (IsIdentifierArg)
285 ArgExprs.push_back(ParseIdentifierLoc());
286 }
287
288 if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
289 // Eat the comma.
290 if (!ArgExprs.empty())
291 ConsumeToken();
292
293 // Parse the non-empty comma-separated list of expressions.
294 do {
295 std::unique_ptr<EnterExpressionEvaluationContext> Unevaluated;
296 if (attributeParsedArgsUnevaluated(*AttrName))
297 Unevaluated.reset(
298 new EnterExpressionEvaluationContext(Actions, Sema::Unevaluated));
299
300 ExprResult ArgExpr(ParseAssignmentExpression());
301 if (ArgExpr.isInvalid()) {
302 SkipUntil(tok::r_paren, StopAtSemi);
303 return;
304 }
305 ArgExprs.push_back(ArgExpr.release());
306 // Eat the comma, move to the next argument
307 } while (TryConsumeToken(tok::comma));
308 }
309
310 SourceLocation RParen = Tok.getLocation();
311 if (!ExpectAndConsume(tok::r_paren)) {
312 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
313 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
314 ArgExprs.data(), ArgExprs.size(), Syntax);
315 }
316
317 if (EndLoc)
318 *EndLoc = RParen;
319}
320
Michael Han23214e52012-10-03 01:56:22 +0000321/// Parse the arguments to a parameterized GNU attribute or
322/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000323void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
324 SourceLocation AttrNameLoc,
325 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000326 SourceLocation *EndLoc,
327 IdentifierInfo *ScopeName,
328 SourceLocation ScopeLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000329 AttributeList::Syntax Syntax,
330 Declarator *D) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000331
332 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
333
Richard Smith66e71682013-10-24 01:07:54 +0000334 AttributeList::Kind AttrKind =
Richard Smithb1f9a282013-10-31 01:56:18 +0000335 AttributeList::getKind(AttrName, ScopeName, Syntax);
Richard Smith66e71682013-10-24 01:07:54 +0000336
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000337 // Availability attributes have their own grammar.
Richard Smithb1f9a282013-10-31 01:56:18 +0000338 // FIXME: All these cases fail to pass in the syntax and scope, and might be
339 // written as C++11 gnu:: attributes.
Richard Smith66e71682013-10-24 01:07:54 +0000340 if (AttrKind == AttributeList::AT_Availability) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000341 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
342 return;
343 }
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000344
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000345 if (AttrKind == AttributeList::AT_ObjCBridgeRelated) {
346 ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
347 return;
348 }
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000349
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000350 // Type safety attributes have their own grammar.
Richard Smith66e71682013-10-24 01:07:54 +0000351 if (AttrKind == AttributeList::AT_TypeTagForDatatype) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000352 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
353 return;
354 }
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000355
Aaron Ballman4768b312013-11-04 12:55:56 +0000356 // Some attributes expect solely a type parameter.
357 if (attributeIsTypeArgAttr(*AttrName)) {
Richard Smithb1f9a282013-10-31 01:56:18 +0000358 ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc);
359 return;
360 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000361
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000362 // These may refer to the function arguments, but need to be parsed early to
363 // participate in determining whether it's a redeclaration.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000364 std::unique_ptr<ParseScope> PrototypeScope;
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000365 if (AttrName->isStr("enable_if") && D && D->isFunctionDeclarator()) {
366 DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo();
367 PrototypeScope.reset(new ParseScope(this, Scope::FunctionPrototypeScope |
368 Scope::FunctionDeclarationScope |
369 Scope::DeclScope));
Alp Tokerc5350722014-02-26 22:27:52 +0000370 for (unsigned i = 0; i != FTI.NumParams; ++i) {
371 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param);
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000372 Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param);
373 }
374 }
375
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000376 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
377 ScopeLoc, Syntax);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000378}
379
Chad Rosierc1183952012-06-26 22:30:43 +0000380/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000381/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000382void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000383 SourceLocation AttrNameLoc,
384 ParsedAttributes &Attrs)
385{
386 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000387 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000388 AttrName->getNameStart(), tok::r_paren))
389 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000390
Aaron Ballman478faed2012-06-19 22:09:27 +0000391 ExprResult ArgExpr(ParseConstantExpression());
392 if (ArgExpr.isInvalid()) {
393 T.skipToEnd();
394 return;
395 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000396 ArgsUnion ExprList = ArgExpr.take();
397 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
398 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000399
400 T.consumeClose();
401}
402
Chad Rosierc1183952012-06-26 22:30:43 +0000403/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000404/// arguments.
405bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
406 return llvm::StringSwitch<bool>(Ident->getName())
407 .Case("dllimport", true)
408 .Case("dllexport", true)
409 .Case("noreturn", true)
410 .Case("nothrow", true)
411 .Case("noinline", true)
412 .Case("naked", true)
413 .Case("appdomain", true)
414 .Case("process", true)
415 .Case("jitintrinsic", true)
416 .Case("noalias", true)
417 .Case("restrict", true)
418 .Case("novtable", true)
419 .Case("selectany", true)
420 .Case("thread", true)
Aaron Ballman444eb6e2013-05-04 16:58:37 +0000421 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000422 .Default(false);
423}
424
Chad Rosierc1183952012-06-26 22:30:43 +0000425/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000426/// parameters). Will return false if we properly handled the declspec, or
427/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000428void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000429 SourceLocation Loc,
430 ParsedAttributes &Attrs) {
431 // Try to handle the easy case first -- these declspecs all take a single
432 // parameter as their argument.
433 if (llvm::StringSwitch<bool>(Ident->getName())
434 .Case("uuid", true)
435 .Case("align", true)
436 .Case("allocate", true)
437 .Default(false)) {
438 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
439 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000440 // The deprecated declspec has an optional single argument, so we will
441 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000442 // not.
443 if (Tok.getKind() == tok::l_paren)
444 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
445 else
Aaron Ballman00e99962013-08-31 01:11:41 +0000446 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000447 } else if (Ident->getName() == "property") {
448 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000449 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000450 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000451 if (Tok.isNot(tok::l_paren)) {
452 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
453 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000454 return;
John McCall5e77d762013-04-16 07:28:30 +0000455 }
456 BalancedDelimiterTracker T(*this, tok::l_paren);
457 T.expectAndConsume(diag::err_expected_lparen_after,
458 Ident->getNameStart(), tok::r_paren);
459
460 enum AccessorKind {
461 AK_Invalid = -1,
462 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
463 };
464 IdentifierInfo *AccessorNames[] = { 0, 0 };
465 bool HasInvalidAccessor = false;
466
467 // Parse the accessor specifications.
468 while (true) {
469 // Stop if this doesn't look like an accessor spec.
470 if (!Tok.is(tok::identifier)) {
471 // If the user wrote a completely empty list, use a special diagnostic.
472 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
473 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
474 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
475 break;
476 }
477
478 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
479 break;
480 }
481
482 AccessorKind Kind;
483 SourceLocation KindLoc = Tok.getLocation();
484 StringRef KindStr = Tok.getIdentifierInfo()->getName();
485 if (KindStr == "get") {
486 Kind = AK_Get;
487 } else if (KindStr == "put") {
488 Kind = AK_Put;
489
490 // Recover from the common mistake of using 'set' instead of 'put'.
491 } else if (KindStr == "set") {
492 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
493 << FixItHint::CreateReplacement(KindLoc, "put");
494 Kind = AK_Put;
495
496 // Handle the mistake of forgetting the accessor kind by skipping
497 // this accessor.
498 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
499 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
500 ConsumeToken();
501 HasInvalidAccessor = true;
502 goto next_property_accessor;
503
504 // Otherwise, complain about the unknown accessor kind.
505 } else {
506 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
507 HasInvalidAccessor = true;
508 Kind = AK_Invalid;
509
510 // Try to keep parsing unless it doesn't look like an accessor spec.
511 if (!NextToken().is(tok::equal)) break;
512 }
513
514 // Consume the identifier.
515 ConsumeToken();
516
517 // Consume the '='.
Alp Toker8fbec672013-12-17 23:29:36 +0000518 if (!TryConsumeToken(tok::equal)) {
John McCall5e77d762013-04-16 07:28:30 +0000519 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
520 << KindStr;
521 break;
522 }
523
524 // Expect the method name.
525 if (!Tok.is(tok::identifier)) {
526 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
527 break;
528 }
529
530 if (Kind == AK_Invalid) {
531 // Just drop invalid accessors.
532 } else if (AccessorNames[Kind] != NULL) {
533 // Complain about the repeated accessor, ignore it, and keep parsing.
534 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
535 } else {
536 AccessorNames[Kind] = Tok.getIdentifierInfo();
537 }
538 ConsumeToken();
539
540 next_property_accessor:
541 // Keep processing accessors until we run out.
Alp Toker094e5212014-01-05 03:27:11 +0000542 if (TryConsumeToken(tok::comma))
John McCall5e77d762013-04-16 07:28:30 +0000543 continue;
544
545 // If we run into the ')', stop without consuming it.
Alp Toker094e5212014-01-05 03:27:11 +0000546 if (Tok.is(tok::r_paren))
John McCall5e77d762013-04-16 07:28:30 +0000547 break;
Alp Toker094e5212014-01-05 03:27:11 +0000548
549 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
550 break;
John McCall5e77d762013-04-16 07:28:30 +0000551 }
552
553 // Only add the property attribute if it was well-formed.
554 if (!HasInvalidAccessor) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000555 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000556 AccessorNames[AK_Get], AccessorNames[AK_Put],
557 AttributeList::AS_Declspec);
558 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000559 T.skipToEnd();
560 } else {
561 // We don't recognize this as a valid declspec, but instead of creating the
562 // attribute and allowing sema to warn about it, we will warn here instead.
563 // This is because some attributes have multiple spellings, but we need to
564 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000565 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000566 // both locations.
567 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
568
569 // If there's an open paren, we should eat the open and close parens under
570 // the assumption that this unknown declspec has parameters.
571 BalancedDelimiterTracker T(*this, tok::l_paren);
572 if (!T.consumeOpen())
573 T.skipToEnd();
574 }
575}
576
Eli Friedman06de2b52009-06-08 07:21:15 +0000577/// [MS] decl-specifier:
578/// __declspec ( extended-decl-modifier-seq )
579///
580/// [MS] extended-decl-modifier-seq:
581/// extended-decl-modifier[opt]
582/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000583void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000584 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000585
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000586 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000587 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000588 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000589 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000590 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000591
Chad Rosierc1183952012-06-26 22:30:43 +0000592 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000593 // you can specify multiple attributes per declspec.
594 while (Tok.getKind() != tok::r_paren) {
595 // We expect either a well-known identifier or a generic string. Anything
596 // else is a malformed declspec.
597 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000598 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000599 Tok.getKind() != tok::kw_restrict) {
600 Diag(Tok, diag::err_ms_declspec_type);
601 T.skipToEnd();
602 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000603 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000604
605 IdentifierInfo *AttrName;
606 SourceLocation AttrNameLoc;
607 if (IsString) {
608 SmallString<8> StrBuffer;
609 bool Invalid = false;
610 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
611 if (Invalid) {
612 T.skipToEnd();
613 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000614 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000615 AttrName = PP.getIdentifierInfo(Str);
616 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000617 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000618 AttrName = Tok.getIdentifierInfo();
619 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000620 }
Chad Rosierc1183952012-06-26 22:30:43 +0000621
Aaron Ballman478faed2012-06-19 22:09:27 +0000622 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000623 // If we have a generic string, we will allow it because there is no
624 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000625 // (for instance, SAL declspecs in older versions of MSVC).
626 //
Chad Rosierc1183952012-06-26 22:30:43 +0000627 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000628 // arguments and can be turned into an attribute directly.
Aaron Ballman00e99962013-08-31 01:11:41 +0000629 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
630 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000631 else
632 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000633 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000634 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000635}
636
John McCall53fa7142010-12-24 02:08:15 +0000637void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000638 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000639 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000640 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000641 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballman317a77f2013-05-22 23:25:32 +0000642 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
643 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000644 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
645 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000646 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
647 AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000648 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000649}
650
John McCall53fa7142010-12-24 02:08:15 +0000651void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000652 // Treat these like attributes
653 while (Tok.is(tok::kw___pascal)) {
654 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
655 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000656 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
657 AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000658 }
John McCall53fa7142010-12-24 02:08:15 +0000659}
660
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000661void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
662 // Treat these like attributes
663 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000664 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000665 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000666 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
667 AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000668 }
669}
670
Aaron Ballman05d76ea2014-01-14 01:29:54 +0000671void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) {
672 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
673 SourceLocation AttrNameLoc = Tok.getLocation();
Aaron Ballman26891332014-01-14 17:41:53 +0000674 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
675 AttributeList::AS_Keyword);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000676}
677
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000678/// \brief Parse a version number.
679///
680/// version:
681/// simple-integer
682/// simple-integer ',' simple-integer
683/// simple-integer ',' simple-integer ',' simple-integer
684VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
685 Range = Tok.getLocation();
686
687 if (!Tok.is(tok::numeric_constant)) {
688 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000689 SkipUntil(tok::comma, tok::r_paren,
690 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000691 return VersionTuple();
692 }
693
694 // Parse the major (and possibly minor and subminor) versions, which
695 // are stored in the numeric constant. We utilize a quirk of the
696 // lexer, which is that it handles something like 1.2.3 as a single
697 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000698 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000699 Buffer.resize(Tok.getLength()+1);
700 const char *ThisTokBegin = &Buffer[0];
701
702 // Get the spelling of the token, which eliminates trigraphs, etc.
703 bool Invalid = false;
704 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
705 if (Invalid)
706 return VersionTuple();
707
708 // Parse the major version.
709 unsigned AfterMajor = 0;
710 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000711 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000712 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
713 ++AfterMajor;
714 }
715
716 if (AfterMajor == 0) {
717 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000718 SkipUntil(tok::comma, tok::r_paren,
719 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000720 return VersionTuple();
721 }
722
723 if (AfterMajor == ActualLength) {
724 ConsumeToken();
725
726 // We only had a single version component.
727 if (Major == 0) {
728 Diag(Tok, diag::err_zero_version);
729 return VersionTuple();
730 }
731
732 return VersionTuple(Major);
733 }
734
735 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
736 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000737 SkipUntil(tok::comma, tok::r_paren,
738 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000739 return VersionTuple();
740 }
741
742 // Parse the minor version.
743 unsigned AfterMinor = AfterMajor + 1;
744 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000745 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000746 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
747 ++AfterMinor;
748 }
749
750 if (AfterMinor == ActualLength) {
751 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000752
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000753 // We had major.minor.
754 if (Major == 0 && Minor == 0) {
755 Diag(Tok, diag::err_zero_version);
756 return VersionTuple();
757 }
758
Chad Rosierc1183952012-06-26 22:30:43 +0000759 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000760 }
761
762 // If what follows is not a '.', we have a problem.
763 if (ThisTokBegin[AfterMinor] != '.') {
764 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000765 SkipUntil(tok::comma, tok::r_paren,
766 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Chad Rosierc1183952012-06-26 22:30:43 +0000767 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000768 }
769
770 // Parse the subminor version.
771 unsigned AfterSubminor = AfterMinor + 1;
772 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000773 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000774 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
775 ++AfterSubminor;
776 }
777
778 if (AfterSubminor != ActualLength) {
779 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000780 SkipUntil(tok::comma, tok::r_paren,
781 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000782 return VersionTuple();
783 }
784 ConsumeToken();
785 return VersionTuple(Major, Minor, Subminor);
786}
787
788/// \brief Parse the contents of the "availability" attribute.
789///
790/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000791/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000792///
793/// platform:
794/// identifier
795///
796/// version-arg-list:
797/// version-arg
798/// version-arg ',' version-arg-list
799///
800/// version-arg:
801/// 'introduced' '=' version
802/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000803/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000804/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000805/// opt-message:
806/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000807void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
808 SourceLocation AvailabilityLoc,
809 ParsedAttributes &attrs,
810 SourceLocation *endLoc) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000811 enum { Introduced, Deprecated, Obsoleted, Unknown };
812 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000813 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000814
815 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000816 BalancedDelimiterTracker T(*this, tok::l_paren);
817 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000818 Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000819 return;
820 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000821
822 // Parse the platform name,
823 if (Tok.isNot(tok::identifier)) {
824 Diag(Tok, diag::err_availability_expected_platform);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000825 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000826 return;
827 }
Richard Smithfeefaf52013-09-03 18:01:40 +0000828 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000829
830 // Parse the ',' following the platform name.
Alp Toker383d2c42014-01-01 03:08:43 +0000831 if (ExpectAndConsume(tok::comma)) {
832 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000833 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000834 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000835
836 // If we haven't grabbed the pointers for the identifiers
837 // "introduced", "deprecated", and "obsoleted", do so now.
838 if (!Ident_introduced) {
839 Ident_introduced = PP.getIdentifierInfo("introduced");
840 Ident_deprecated = PP.getIdentifierInfo("deprecated");
841 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000842 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000843 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000844 }
845
846 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000847 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000848 do {
849 if (Tok.isNot(tok::identifier)) {
850 Diag(Tok, diag::err_availability_expected_change);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000851 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000852 return;
853 }
854 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
855 SourceLocation KeywordLoc = ConsumeToken();
856
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000857 if (Keyword == Ident_unavailable) {
858 if (UnavailableLoc.isValid()) {
859 Diag(KeywordLoc, diag::err_availability_redundant)
860 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000861 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000862 UnavailableLoc = KeywordLoc;
Alp Toker97650562014-01-10 11:19:30 +0000863 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000864 }
865
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000866 if (Tok.isNot(tok::equal)) {
Alp Tokerec543272013-12-24 09:48:30 +0000867 Diag(Tok, diag::err_expected_after) << Keyword << tok::equal;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000868 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000869 return;
870 }
871 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000872 if (Keyword == Ident_message) {
Benjamin Kramera9dfa922013-09-13 17:31:48 +0000873 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000874 Diag(Tok, diag::err_expected_string_literal)
875 << /*Source='availability attribute'*/2;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000876 SkipUntil(tok::r_paren, StopAtSemi);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000877 return;
878 }
879 MessageExpr = ParseStringLiteralExpression();
880 break;
881 }
Chad Rosierc1183952012-06-26 22:30:43 +0000882
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000883 SourceRange VersionRange;
884 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000885
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000886 if (Version.empty()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000887 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000888 return;
889 }
890
891 unsigned Index;
892 if (Keyword == Ident_introduced)
893 Index = Introduced;
894 else if (Keyword == Ident_deprecated)
895 Index = Deprecated;
896 else if (Keyword == Ident_obsoleted)
897 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000898 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000899 Index = Unknown;
900
901 if (Index < Unknown) {
902 if (!Changes[Index].KeywordLoc.isInvalid()) {
903 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000904 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000905 << SourceRange(Changes[Index].KeywordLoc,
906 Changes[Index].VersionRange.getEnd());
907 }
908
909 Changes[Index].KeywordLoc = KeywordLoc;
910 Changes[Index].Version = Version;
911 Changes[Index].VersionRange = VersionRange;
912 } else {
913 Diag(KeywordLoc, diag::err_availability_unknown_change)
914 << Keyword << VersionRange;
915 }
916
Alp Toker97650562014-01-10 11:19:30 +0000917 } while (TryConsumeToken(tok::comma));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000918
919 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000920 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000921 return;
922
923 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000924 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000925
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000926 // The 'unavailable' availability cannot be combined with any other
927 // availability changes. Make sure that hasn't happened.
928 if (UnavailableLoc.isValid()) {
929 bool Complained = false;
930 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
931 if (Changes[Index].KeywordLoc.isValid()) {
932 if (!Complained) {
933 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
934 << SourceRange(Changes[Index].KeywordLoc,
935 Changes[Index].VersionRange.getEnd());
936 Complained = true;
937 }
938
939 // Clear out the availability.
940 Changes[Index] = AvailabilityChange();
941 }
942 }
943 }
944
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000945 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000946 attrs.addNew(&Availability,
947 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000948 0, AvailabilityLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +0000949 Platform,
John McCall084e83d2011-03-24 11:26:52 +0000950 Changes[Introduced],
951 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000952 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000953 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000954 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000955}
956
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000957/// \brief Parse the contents of the "objc_bridge_related" attribute.
958/// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')'
959/// related_class:
960/// Identifier
961///
962/// opt-class_method:
963/// Identifier: | <empty>
964///
965/// opt-instance_method:
966/// Identifier | <empty>
967///
968void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated,
969 SourceLocation ObjCBridgeRelatedLoc,
970 ParsedAttributes &attrs,
971 SourceLocation *endLoc) {
972 // Opening '('.
973 BalancedDelimiterTracker T(*this, tok::l_paren);
974 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000975 Diag(Tok, diag::err_expected) << tok::l_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000976 return;
977 }
978
979 // Parse the related class name.
980 if (Tok.isNot(tok::identifier)) {
981 Diag(Tok, diag::err_objcbridge_related_expected_related_class);
982 SkipUntil(tok::r_paren, StopAtSemi);
983 return;
984 }
985 IdentifierLoc *RelatedClass = ParseIdentifierLoc();
Alp Toker97650562014-01-10 11:19:30 +0000986 if (ExpectAndConsume(tok::comma)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000987 SkipUntil(tok::r_paren, StopAtSemi);
988 return;
989 }
Alp Toker8fbec672013-12-17 23:29:36 +0000990
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000991 // Parse optional class method name.
992 IdentifierLoc *ClassMethod = 0;
993 if (Tok.is(tok::identifier)) {
994 ClassMethod = ParseIdentifierLoc();
Alp Toker8fbec672013-12-17 23:29:36 +0000995 if (!TryConsumeToken(tok::colon)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000996 Diag(Tok, diag::err_objcbridge_related_selector_name);
997 SkipUntil(tok::r_paren, StopAtSemi);
998 return;
999 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001000 }
Alp Toker8fbec672013-12-17 23:29:36 +00001001 if (!TryConsumeToken(tok::comma)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001002 if (Tok.is(tok::colon))
1003 Diag(Tok, diag::err_objcbridge_related_selector_name);
1004 else
Alp Tokerec543272013-12-24 09:48:30 +00001005 Diag(Tok, diag::err_expected) << tok::comma;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001006 SkipUntil(tok::r_paren, StopAtSemi);
1007 return;
1008 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001009
1010 // Parse optional instance method name.
1011 IdentifierLoc *InstanceMethod = 0;
1012 if (Tok.is(tok::identifier))
1013 InstanceMethod = ParseIdentifierLoc();
1014 else if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001015 Diag(Tok, diag::err_expected) << tok::r_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001016 SkipUntil(tok::r_paren, StopAtSemi);
1017 return;
1018 }
1019
1020 // Closing ')'.
1021 if (T.consumeClose())
1022 return;
1023
1024 if (endLoc)
1025 *endLoc = T.getCloseLocation();
1026
1027 // Record this attribute
1028 attrs.addNew(&ObjCBridgeRelated,
1029 SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
1030 0, ObjCBridgeRelatedLoc,
1031 RelatedClass,
1032 ClassMethod,
1033 InstanceMethod,
1034 AttributeList::AS_GNU);
1035
1036}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001037
Bill Wendling44426052012-12-20 19:22:21 +00001038// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001039// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
1040
1041void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
1042
1043void Parser::LateParsedClass::ParseLexedAttributes() {
1044 Self->ParseLexedAttributes(*Class);
1045}
1046
1047void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001048 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001049}
1050
1051/// Wrapper class which calls ParseLexedAttribute, after setting up the
1052/// scope appropriately.
1053void Parser::ParseLexedAttributes(ParsingClass &Class) {
1054 // Deal with templates
1055 // FIXME: Test cases to make sure this does the right thing for templates.
1056 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
1057 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
1058 HasTemplateScope);
1059 if (HasTemplateScope)
1060 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
1061
Douglas Gregor3024f072012-04-16 07:05:22 +00001062 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001063 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +00001064 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001065 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1066 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1067
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001068 // Enter the scope of nested classes
1069 if (!AlreadyHasClassScope)
1070 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1071 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001072 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001073 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1074 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1075 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001076 }
Chad Rosierc1183952012-06-26 22:30:43 +00001077
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001078 if (!AlreadyHasClassScope)
1079 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1080 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001081}
1082
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001083
1084/// \brief Parse all attributes in LAs, and attach them to Decl D.
1085void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1086 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001087 assert(LAs.parseSoon() &&
1088 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001089 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001090 if (D)
1091 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001092 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001093 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001094 }
1095 LAs.clear();
1096}
1097
1098
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001099/// \brief Finish parsing an attribute for which parsing was delayed.
1100/// This will be called at the end of parsing a class declaration
1101/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001102/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001103/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001104void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1105 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001106 // Save the current token position.
1107 SourceLocation OrigLoc = Tok.getLocation();
1108
1109 // Append the current token at the end of the new token stream so that it
1110 // doesn't get lost.
1111 LA.Toks.push_back(Tok);
1112 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1113 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001114 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001115
1116 ParsedAttributes Attrs(AttrFactory);
1117 SourceLocation endLoc;
1118
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001119 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001120 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001121 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1122 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001123
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001124 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001125 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1126 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001127
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001128 if (LA.Decls.size() == 1) {
1129 // If the Decl is templatized, add template parameters to scope.
1130 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1131 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1132 if (HasTemplateScope)
1133 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001134
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001135 // If the Decl is on a function, add function parameters to the scope.
1136 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1137 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1138 if (HasFunScope)
1139 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001140
Michael Han23214e52012-10-03 01:56:22 +00001141 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001142 0, SourceLocation(), AttributeList::AS_GNU, 0);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001143
1144 if (HasFunScope) {
1145 Actions.ActOnExitFunctionContext();
1146 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1147 }
1148 if (HasTemplateScope) {
1149 TempScope.Exit();
1150 }
1151 } else {
1152 // If there are multiple decls, then the decl cannot be within the
1153 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001154 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001155 0, SourceLocation(), AttributeList::AS_GNU, 0);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001156 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001157 } else {
1158 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001159 }
1160
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00001161 const AttributeList *AL = Attrs.getList();
1162 if (OnDefinition && AL && !AL->isCXX11Attribute() &&
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001163 AL->isKnownToGCC())
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00001164 Diag(Tok, diag::warn_attribute_on_function_definition)
1165 << &LA.AttrName;
1166
1167 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i)
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001168 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001169
1170 if (Tok.getLocation() != OrigLoc) {
1171 // Due to a parsing error, we either went over the cached tokens or
1172 // there are still cached tokens left, so we skip the leftover tokens.
1173 // Since this is an uncommon situation that should be avoided, use the
1174 // expensive isBeforeInTranslationUnit call.
1175 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1176 OrigLoc))
1177 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001178 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001179 }
1180}
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)) {
Alp Tokerec543272013-12-24 09:48:30 +00001192 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001193 T.skipToEnd();
1194 return;
1195 }
Richard Smithfeefaf52013-09-03 18:01:40 +00001196 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001197
Alp Toker094e5212014-01-05 03:27:11 +00001198 if (ExpectAndConsume(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001199 T.skipToEnd();
1200 return;
1201 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001202
1203 SourceRange MatchingCTypeRange;
1204 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1205 if (MatchingCType.isInvalid()) {
1206 T.skipToEnd();
1207 return;
1208 }
1209
1210 bool LayoutCompatible = false;
1211 bool MustBeNull = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001212 while (TryConsumeToken(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001213 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001214 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001215 T.skipToEnd();
1216 return;
1217 }
1218 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1219 if (Flag->isStr("layout_compatible"))
1220 LayoutCompatible = true;
1221 else if (Flag->isStr("must_be_null"))
1222 MustBeNull = true;
1223 else {
1224 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1225 T.skipToEnd();
1226 return;
1227 }
1228 ConsumeToken(); // consume flag
1229 }
1230
1231 if (!T.consumeClose()) {
1232 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +00001233 ArgumentKind, MatchingCType.release(),
1234 LayoutCompatible, MustBeNull,
1235 AttributeList::AS_GNU);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001236 }
1237
1238 if (EndLoc)
1239 *EndLoc = T.getCloseLocation();
1240}
1241
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001242/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1243/// of a C++11 attribute-specifier in a location where an attribute is not
1244/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1245/// situation.
1246///
1247/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1248/// this doesn't appear to actually be an attribute-specifier, and the caller
1249/// should try to parse it.
1250bool Parser::DiagnoseProhibitedCXX11Attribute() {
1251 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1252
1253 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1254 case CAK_NotAttributeSpecifier:
1255 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1256 return false;
1257
1258 case CAK_InvalidAttributeSpecifier:
1259 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1260 return false;
1261
1262 case CAK_AttributeSpecifier:
1263 // Parse and discard the attributes.
1264 SourceLocation BeginLoc = ConsumeBracket();
1265 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001266 SkipUntil(tok::r_square);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001267 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1268 SourceLocation EndLoc = ConsumeBracket();
1269 Diag(BeginLoc, diag::err_attributes_not_allowed)
1270 << SourceRange(BeginLoc, EndLoc);
1271 return true;
1272 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001273 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001274}
1275
Richard Smith98155ad2013-02-20 01:17:14 +00001276/// \brief We have found the opening square brackets of a C++11
1277/// attribute-specifier in a location where an attribute is not permitted, but
1278/// we know where the attributes ought to be written. Parse them anyway, and
1279/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001280void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1281 SourceLocation CorrectLocation) {
1282 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1283 Tok.is(tok::kw_alignas));
1284
1285 // Consume the attributes.
1286 SourceLocation Loc = Tok.getLocation();
1287 ParseCXX11Attributes(Attrs);
1288 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1289
1290 Diag(Loc, diag::err_attributes_not_allowed)
1291 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1292 << FixItHint::CreateRemoval(AttrRange);
1293}
1294
John McCall53fa7142010-12-24 02:08:15 +00001295void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1296 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1297 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001298}
1299
Michael Han64536a62012-11-06 19:34:54 +00001300void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1301 AttributeList *AttrList = attrs.getList();
1302 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001303 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001304 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001305 << AttrList->getName();
1306 AttrList->setInvalid();
1307 }
1308 AttrList = AttrList->getNext();
1309 }
1310}
1311
Chris Lattner53361ac2006-08-10 05:19:57 +00001312/// ParseDeclaration - Parse a full 'declaration', which consists of
1313/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001314/// 'Context' should be a Declarator::TheContext value. This returns the
1315/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001316///
1317/// declaration: [C99 6.7]
1318/// block-declaration ->
1319/// simple-declaration
1320/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001321/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001322/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001323/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001324/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001325/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001326/// others... [FIXME]
1327///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001328Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1329 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001330 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001331 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001332 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001333 // Must temporarily exit the objective-c container scope for
1334 // parsing c none objective-c decls.
1335 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001336
John McCall48871652010-08-21 09:40:31 +00001337 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001338 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001339 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001340 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001341 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001342 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001343 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001344 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001345 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001346 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001347 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001348 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001349 SourceLocation InlineLoc = ConsumeToken();
1350 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1351 break;
1352 }
Chad Rosierc1183952012-06-26 22:30:43 +00001353 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001354 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001355 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001356 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001357 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001358 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001359 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001360 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001361 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001362 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001363 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001364 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001365 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001366 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001367 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001368 default:
John McCall53fa7142010-12-24 02:08:15 +00001369 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001370 }
Chad Rosierc1183952012-06-26 22:30:43 +00001371
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001372 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001373 // single decl, convert it now. Alias declarations can also declare a type;
1374 // include that too if it is present.
1375 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001376}
1377
1378/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1379/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001380/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1381/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001382///[C90/C++]init-declarator-list ';' [TODO]
1383/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001384///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001385/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001386/// attribute-specifier-seq[opt] type-specifier-seq declarator
1387///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001388/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001389/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001390///
1391/// If FRI is non-null, we might be parsing a for-range-declaration instead
1392/// of a simple-declaration. If we find that we are, we also parse the
1393/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001394Parser::DeclGroupPtrTy
1395Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1396 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001397 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001398 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001399 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001400 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001401
Richard Smith404dfb42013-11-19 22:47:36 +00001402 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
1403 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
1404
1405 // If we had a free-standing type definition with a missing semicolon, we
1406 // may get this far before the problem becomes obvious.
1407 if (DS.hasTagDefinition() &&
1408 DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
1409 return DeclGroupPtrTy();
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001410
Chris Lattner0e894622006-08-13 19:58:17 +00001411 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1412 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001413 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001414 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001415 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001416 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001417 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001418 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001419 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001420 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001421 }
Chad Rosierc1183952012-06-26 22:30:43 +00001422
Richard Smith2386c8b2013-02-22 09:06:26 +00001423 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001424 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001425}
Mike Stump11289f42009-09-09 15:08:12 +00001426
Richard Smith09f76ee2011-10-19 21:33:05 +00001427/// Returns true if this might be the start of a declarator, or a common typo
1428/// for a declarator.
1429bool Parser::MightBeDeclarator(unsigned Context) {
1430 switch (Tok.getKind()) {
1431 case tok::annot_cxxscope:
1432 case tok::annot_template_id:
1433 case tok::caret:
1434 case tok::code_completion:
1435 case tok::coloncolon:
1436 case tok::ellipsis:
1437 case tok::kw___attribute:
1438 case tok::kw_operator:
1439 case tok::l_paren:
1440 case tok::star:
1441 return true;
1442
1443 case tok::amp:
1444 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001445 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001446
Richard Smithc8a79032012-01-09 22:31:44 +00001447 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001448 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001449 NextToken().is(tok::l_square);
1450
1451 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001452 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001453
Richard Smith09f76ee2011-10-19 21:33:05 +00001454 case tok::identifier:
1455 switch (NextToken().getKind()) {
1456 case tok::code_completion:
1457 case tok::coloncolon:
1458 case tok::comma:
1459 case tok::equal:
1460 case tok::equalequal: // Might be a typo for '='.
1461 case tok::kw_alignas:
1462 case tok::kw_asm:
1463 case tok::kw___attribute:
1464 case tok::l_brace:
1465 case tok::l_paren:
1466 case tok::l_square:
1467 case tok::less:
1468 case tok::r_brace:
1469 case tok::r_paren:
1470 case tok::r_square:
1471 case tok::semi:
1472 return true;
1473
1474 case tok::colon:
1475 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001476 // and in block scope it's probably a label. Inside a class definition,
1477 // this is a bit-field.
1478 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001479 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001480
1481 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001482 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001483
1484 default:
1485 return false;
1486 }
1487
1488 default:
1489 return false;
1490 }
1491}
1492
Richard Smithb8caac82012-04-11 20:59:20 +00001493/// Skip until we reach something which seems like a sensible place to pick
1494/// up parsing after a malformed declaration. This will sometimes stop sooner
1495/// than SkipUntil(tok::r_brace) would, but will never stop later.
1496void Parser::SkipMalformedDecl() {
1497 while (true) {
1498 switch (Tok.getKind()) {
1499 case tok::l_brace:
1500 // Skip until matching }, then stop. We've probably skipped over
1501 // a malformed class or function definition or similar.
1502 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001503 SkipUntil(tok::r_brace);
Richard Smithb8caac82012-04-11 20:59:20 +00001504 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1505 // This declaration isn't over yet. Keep skipping.
1506 continue;
1507 }
Alp Toker8fbec672013-12-17 23:29:36 +00001508 TryConsumeToken(tok::semi);
Richard Smithb8caac82012-04-11 20:59:20 +00001509 return;
1510
1511 case tok::l_square:
1512 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001513 SkipUntil(tok::r_square);
Richard Smithb8caac82012-04-11 20:59:20 +00001514 continue;
1515
1516 case tok::l_paren:
1517 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001518 SkipUntil(tok::r_paren);
Richard Smithb8caac82012-04-11 20:59:20 +00001519 continue;
1520
1521 case tok::r_brace:
1522 return;
1523
1524 case tok::semi:
1525 ConsumeToken();
1526 return;
1527
1528 case tok::kw_inline:
1529 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001530 // a good place to pick back up parsing, except in an Objective-C
1531 // @interface context.
1532 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1533 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001534 return;
1535 break;
1536
1537 case tok::kw_namespace:
1538 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001539 // place to pick back up parsing, except in an Objective-C
1540 // @interface context.
1541 if (Tok.isAtStartOfLine() &&
1542 (!ParsingInObjCContainer || CurParsedObjCImpl))
1543 return;
1544 break;
1545
1546 case tok::at:
1547 // @end is very much like } in Objective-C contexts.
1548 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1549 ParsingInObjCContainer)
1550 return;
1551 break;
1552
1553 case tok::minus:
1554 case tok::plus:
1555 // - and + probably start new method declarations in Objective-C contexts.
1556 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001557 return;
1558 break;
1559
1560 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001561 case tok::annot_module_begin:
1562 case tok::annot_module_end:
1563 case tok::annot_module_include:
Richard Smithb8caac82012-04-11 20:59:20 +00001564 return;
1565
1566 default:
1567 break;
1568 }
1569
1570 ConsumeAnyToken();
1571 }
1572}
1573
John McCalld5a36322009-11-03 19:26:08 +00001574/// ParseDeclGroup - Having concluded that this is either a function
1575/// definition or a group of object declarations, actually parse the
1576/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001577Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1578 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001579 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001580 SourceLocation *DeclEnd,
1581 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001582 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001583 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001584 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001585
John McCalld5a36322009-11-03 19:26:08 +00001586 // Bail out if the first declarator didn't seem well-formed.
1587 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001588 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001589 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001590 }
Mike Stump11289f42009-09-09 15:08:12 +00001591
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001592 // Save late-parsed attributes for now; they need to be parsed in the
1593 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001594 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1595 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001596 if (D.isFunctionDeclarator())
1597 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1598
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001599 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001600 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001601 // Look at the next token to make sure that this isn't a function
1602 // declaration. We have to check this because __attribute__ might be the
1603 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001604 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001605
Douglas Gregor012efe22013-04-16 16:01:32 +00001606 if (AllowFunctionDefinitions) {
1607 if (isStartOfFunctionDefinition(D)) {
1608 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1609 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001610
Douglas Gregor012efe22013-04-16 16:01:32 +00001611 // Recover by treating the 'typedef' as spurious.
1612 DS.ClearStorageClassSpecs();
1613 }
1614
1615 Decl *TheDecl =
1616 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1617 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001618 }
1619
Douglas Gregor012efe22013-04-16 16:01:32 +00001620 if (isDeclarationSpecifier()) {
1621 // If there is an invalid declaration specifier right after the function
1622 // prototype, then we must be in a missing semicolon case where this isn't
1623 // actually a body. Just fall through into the code that handles it as a
1624 // prototype, and let the top-level code handle the erroneous declspec
1625 // where it would otherwise expect a comma or semicolon.
1626 } else {
1627 Diag(Tok, diag::err_expected_fn_body);
1628 SkipUntil(tok::semi);
1629 return DeclGroupPtrTy();
1630 }
John McCalld5a36322009-11-03 19:26:08 +00001631 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001632 if (Tok.is(tok::l_brace)) {
1633 Diag(Tok, diag::err_function_definition_not_allowed);
Serge Pavlov1de51512013-12-09 05:25:47 +00001634 SkipMalformedDecl();
1635 return DeclGroupPtrTy();
Douglas Gregor012efe22013-04-16 16:01:32 +00001636 }
John McCalld5a36322009-11-03 19:26:08 +00001637 }
1638 }
1639
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001640 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001641 return DeclGroupPtrTy();
1642
1643 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1644 // must parse and analyze the for-range-initializer before the declaration is
1645 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001646 //
1647 // Handle the Objective-C for-in loop variable similarly, although we
1648 // don't need to parse the container in advance.
1649 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1650 bool IsForRangeLoop = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001651 if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001652 IsForRangeLoop = true;
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001653 if (Tok.is(tok::l_brace))
1654 FRI->RangeExpr = ParseBraceInitializer();
1655 else
1656 FRI->RangeExpr = ParseExpression();
1657 }
1658
Richard Smith02e85f32011-04-14 22:09:26 +00001659 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001660 if (IsForRangeLoop)
1661 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001662 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001663 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001664 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001665 }
1666
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001667 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001668 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001669 if (LateParsedAttrs.size() > 0)
1670 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001671 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001672 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001673 DeclsInGroup.push_back(FirstDecl);
1674
Richard Smith09f76ee2011-10-19 21:33:05 +00001675 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001676
John McCalld5a36322009-11-03 19:26:08 +00001677 // If we don't have a comma, it is either the end of the list (a ';') or an
1678 // error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00001679 SourceLocation CommaLoc;
1680 while (TryConsumeToken(tok::comma, CommaLoc)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001681 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1682 // This comma was followed by a line-break and something which can't be
1683 // the start of a declarator. The comma was probably a typo for a
1684 // semicolon.
1685 Diag(CommaLoc, diag::err_expected_semi_declaration)
1686 << FixItHint::CreateReplacement(CommaLoc, ";");
1687 ExpectSemi = false;
1688 break;
1689 }
John McCalld5a36322009-11-03 19:26:08 +00001690
1691 // Parse the next declarator.
1692 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001693 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001694
1695 // Accept attributes in an init-declarator. In the first declarator in a
1696 // declaration, these would be part of the declspec. In subsequent
1697 // declarators, they become part of the declarator itself, so that they
1698 // don't apply to declarators after *this* one. Examples:
1699 // short __attribute__((common)) var; -> declspec
1700 // short var __attribute__((common)); -> declarator
1701 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001702 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001703
1704 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001705 if (!D.isInvalidType()) {
1706 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1707 D.complete(ThisDecl);
1708 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001709 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001710 }
John McCalld5a36322009-11-03 19:26:08 +00001711 }
1712
1713 if (DeclEnd)
1714 *DeclEnd = Tok.getLocation();
1715
Richard Smith09f76ee2011-10-19 21:33:05 +00001716 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001717 ExpectAndConsumeSemi(Context == Declarator::FileContext
1718 ? diag::err_invalid_token_after_toplevel_declarator
1719 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001720 // Okay, there was no semicolon and one was expected. If we see a
1721 // declaration specifier, just assume it was missing and continue parsing.
1722 // Otherwise things are very confused and we skip to recover.
1723 if (!isDeclarationSpecifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001724 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Alp Toker8fbec672013-12-17 23:29:36 +00001725 TryConsumeToken(tok::semi);
Chris Lattner13901342010-07-11 22:42:07 +00001726 }
John McCalld5a36322009-11-03 19:26:08 +00001727 }
1728
Rafael Espindolaab417692013-07-09 12:05:01 +00001729 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001730}
1731
Richard Smith02e85f32011-04-14 22:09:26 +00001732/// Parse an optional simple-asm-expr and attributes, and attach them to a
1733/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001734bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001735 // If a simple-asm-expr is present, parse it.
1736 if (Tok.is(tok::kw_asm)) {
1737 SourceLocation Loc;
1738 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1739 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001740 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smith02e85f32011-04-14 22:09:26 +00001741 return true;
1742 }
1743
1744 D.setAsmLabel(AsmLabel.release());
1745 D.SetRangeEnd(Loc);
1746 }
1747
1748 MaybeParseGNUAttributes(D);
1749 return false;
1750}
1751
Douglas Gregor23996282009-05-12 21:31:51 +00001752/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1753/// declarator'. This method parses the remainder of the declaration
1754/// (including any attributes or initializer, among other things) and
1755/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001756///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001757/// init-declarator: [C99 6.7]
1758/// declarator
1759/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001760/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1761/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001762/// [C++] declarator initializer[opt]
1763///
1764/// [C++] initializer:
1765/// [C++] '=' initializer-clause
1766/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001767/// [C++0x] '=' 'default' [TODO]
1768/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001769/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001770///
1771/// According to the standard grammar, =default and =delete are function
1772/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001773///
John McCall48871652010-08-21 09:40:31 +00001774Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001775 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001776 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001777 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001778
Richard Smith02e85f32011-04-14 22:09:26 +00001779 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1780}
Mike Stump11289f42009-09-09 15:08:12 +00001781
Richard Smith02e85f32011-04-14 22:09:26 +00001782Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1783 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001784 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001785 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001786 switch (TemplateInfo.Kind) {
1787 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001788 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001789 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001790
Douglas Gregor450f00842009-09-25 18:43:00 +00001791 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001792 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001793 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001794 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001795 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00001796 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001797 // Re-direct this decl to refer to the templated decl so that we can
1798 // initialize it.
1799 ThisDecl = VT->getTemplatedDecl();
1800 break;
1801 }
1802 case ParsedTemplateInfo::ExplicitInstantiation: {
1803 if (Tok.is(tok::semi)) {
1804 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1805 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1806 if (ThisRes.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001807 SkipUntil(tok::semi, StopBeforeMatch);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001808 return 0;
1809 }
1810 ThisDecl = ThisRes.get();
1811 } else {
1812 // FIXME: This check should be for a variable template instantiation only.
1813
1814 // Check that this is a valid instantiation
1815 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1816 // If the declarator-id is not a template-id, issue a diagnostic and
1817 // recover by ignoring the 'template' keyword.
1818 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1819 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1820 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1821 } else {
1822 SourceLocation LAngleLoc =
1823 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1824 Diag(D.getIdentifierLoc(),
1825 diag::err_explicit_instantiation_with_definition)
1826 << SourceRange(TemplateInfo.TemplateLoc)
1827 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1828
1829 // Recover as if it were an explicit specialization.
1830 TemplateParameterLists FakedParamLists;
1831 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1832 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1833 LAngleLoc));
1834
1835 ThisDecl =
1836 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1837 }
1838 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001839 break;
1840 }
1841 }
Mike Stump11289f42009-09-09 15:08:12 +00001842
Richard Smith74aeef52013-04-26 16:15:35 +00001843 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001844
Douglas Gregor23996282009-05-12 21:31:51 +00001845 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001846 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001847 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001848 ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001849
Anders Carlsson991285e2010-09-24 21:25:25 +00001850 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001851 if (D.isFunctionDeclarator())
1852 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1853 << 1 /* delete */;
1854 else
1855 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001856 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001857 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001858 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1859 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001860 else
1861 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001862 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001863 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001864 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001865 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001866 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001867
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001868 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001869 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001870 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001871 cutOffParsing();
1872 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001873 }
Chad Rosierc1183952012-06-26 22:30:43 +00001874
John McCalldadc5752010-08-24 06:29:42 +00001875 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001876
David Blaikiebbafb8a2012-03-11 07:00:24 +00001877 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001878 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001879 ExitScope();
1880 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001881
Douglas Gregor23996282009-05-12 21:31:51 +00001882 if (Init.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001883 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor604c3022010-03-01 18:27:54 +00001884 Actions.ActOnInitializerError(ThisDecl);
1885 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001886 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1887 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001888 }
1889 } else if (Tok.is(tok::l_paren)) {
1890 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001891 BalancedDelimiterTracker T(*this, tok::l_paren);
1892 T.consumeOpen();
1893
Benjamin Kramerf0623432012-08-23 22:51:59 +00001894 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001895 CommaLocsTy CommaLocs;
1896
David Blaikiebbafb8a2012-03-11 07:00:24 +00001897 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001898 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001899 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001900 }
1901
Douglas Gregor23996282009-05-12 21:31:51 +00001902 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001903 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001904 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor613bf102009-12-22 17:47:17 +00001905
David Blaikiebbafb8a2012-03-11 07:00:24 +00001906 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001907 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001908 ExitScope();
1909 }
Douglas Gregor23996282009-05-12 21:31:51 +00001910 } else {
1911 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001912 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001913
1914 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1915 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001916
David Blaikiebbafb8a2012-03-11 07:00:24 +00001917 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001918 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001919 ExitScope();
1920 }
1921
Sebastian Redla9351792012-02-11 23:51:47 +00001922 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1923 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001924 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001925 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1926 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001927 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001928 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001929 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001930 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001931 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1932
Sebastian Redl3da34892011-06-05 12:23:16 +00001933 if (D.getCXXScopeSpec().isSet()) {
1934 EnterScope(0);
1935 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1936 }
1937
1938 ExprResult Init(ParseBraceInitializer());
1939
1940 if (D.getCXXScopeSpec().isSet()) {
1941 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1942 ExitScope();
1943 }
1944
1945 if (Init.isInvalid()) {
1946 Actions.ActOnInitializerError(ThisDecl);
1947 } else
1948 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1949 /*DirectInit=*/true, TypeContainsAuto);
1950
Douglas Gregor23996282009-05-12 21:31:51 +00001951 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001952 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001953 }
1954
Richard Smithb2bc2e62011-02-21 20:05:19 +00001955 Actions.FinalizeDeclaration(ThisDecl);
1956
Douglas Gregor23996282009-05-12 21:31:51 +00001957 return ThisDecl;
1958}
1959
Chris Lattner1890ac82006-08-13 01:16:23 +00001960/// ParseSpecifierQualifierList
1961/// specifier-qualifier-list:
1962/// type-specifier specifier-qualifier-list[opt]
1963/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001964/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001965///
Richard Smithc5b05522012-03-12 07:56:15 +00001966void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1967 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001968 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1969 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001970 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001971 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001972
Chris Lattner1890ac82006-08-13 01:16:23 +00001973 // Validate declspec for type-name.
1974 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith649c7b062014-01-08 00:56:48 +00001975 if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001976 Diag(Tok, diag::err_expected_type);
1977 DS.SetTypeSpecError();
1978 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1979 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001980 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001981 if (!DS.hasTypeSpecifier())
1982 DS.SetTypeSpecError();
1983 }
Mike Stump11289f42009-09-09 15:08:12 +00001984
Chris Lattner1b22eed2006-11-28 05:12:07 +00001985 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001986 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001987 if (DS.getStorageClassSpecLoc().isValid())
1988 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1989 else
Richard Smithb4a9e862013-04-12 22:46:28 +00001990 Diag(DS.getThreadStorageClassSpecLoc(),
1991 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001992 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001993 }
Mike Stump11289f42009-09-09 15:08:12 +00001994
Chris Lattner1b22eed2006-11-28 05:12:07 +00001995 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001996 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001997 if (DS.isInlineSpecified())
1998 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1999 if (DS.isVirtualSpecified())
2000 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2001 if (DS.isExplicitSpecified())
2002 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002003 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002004 }
Richard Smithc5b05522012-03-12 07:56:15 +00002005
2006 // Issue diagnostic and remove constexpr specfier if present.
2007 if (DS.isConstexprSpecified()) {
2008 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2009 DS.ClearConstexprSpec();
2010 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002011}
Chris Lattner53361ac2006-08-10 05:19:57 +00002012
Chris Lattner6cc055a2009-04-12 20:42:31 +00002013/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2014/// specified token is valid after the identifier in a declarator which
2015/// immediately follows the declspec. For example, these things are valid:
2016///
2017/// int x [ 4]; // direct-declarator
2018/// int x ( int y); // direct-declarator
2019/// int(int x ) // direct-declarator
2020/// int x ; // simple-declaration
2021/// int x = 17; // init-declarator-list
2022/// int x , y; // init-declarator-list
2023/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002024/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002025/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002026///
2027/// This is not, because 'x' does not immediately follow the declspec (though
2028/// ')' happens to be valid anyway).
2029/// int (x)
2030///
2031static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2032 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2033 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002034 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002035}
2036
Chris Lattner20a0c612009-04-14 21:34:55 +00002037
2038/// ParseImplicitInt - This method is called when we have an non-typename
2039/// identifier in a declspec (which normally terminates the decl spec) when
2040/// the declspec has no type specifier. In this case, the declspec is either
2041/// malformed or is "implicit int" (in K&R and C89).
2042///
2043/// This method handles diagnosing this prettily and returns false if the
2044/// declspec is done being processed. If it recovers and thinks there may be
2045/// other pieces of declspec after it, it returns true.
2046///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002047bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002048 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002049 AccessSpecifier AS, DeclSpecContext DSC,
2050 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002051 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002052
Chris Lattner20a0c612009-04-14 21:34:55 +00002053 SourceLocation Loc = Tok.getLocation();
2054 // If we see an identifier that is not a type name, we normally would
2055 // parse it as the identifer being declared. However, when a typename
2056 // is typo'd or the definition is not included, this will incorrectly
2057 // parse the typename as the identifier name and fall over misparsing
2058 // later parts of the diagnostic.
2059 //
2060 // As such, we try to do some look-ahead in cases where this would
2061 // otherwise be an "implicit-int" case to see if this is invalid. For
2062 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2063 // an identifier with implicit int, we'd get a parse error because the
2064 // next token is obviously invalid for a type. Parse these as a case
2065 // with an invalid type specifier.
2066 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002067
Chris Lattner20a0c612009-04-14 21:34:55 +00002068 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002069 // error, do lookahead to try to do better recovery. This never applies
2070 // within a type specifier. Outside of C++, we allow this even if the
2071 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002072 // implicit int as an extension in C99 and C11.
Richard Smith649c7b062014-01-08 00:56:48 +00002073 if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002074 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002075 // If this token is valid for implicit int, e.g. "static x = 4", then
2076 // we just avoid eating the identifier, so it will be parsed as the
2077 // identifier in the declarator.
2078 return false;
2079 }
Mike Stump11289f42009-09-09 15:08:12 +00002080
Richard Smitha952ebb2012-05-15 21:01:51 +00002081 if (getLangOpts().CPlusPlus &&
2082 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2083 // Don't require a type specifier if we have the 'auto' storage class
2084 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002085 if (SS)
2086 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002087 return false;
2088 }
2089
Chris Lattner20a0c612009-04-14 21:34:55 +00002090 // Otherwise, if we don't consume this token, we are going to emit an
2091 // error anyway. Try to recover from various common problems. Check
2092 // to see if this was a reference to a tag name without a tag specified.
2093 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002094 //
2095 // C++ doesn't need this, and isTagName doesn't take SS.
2096 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002097 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002098 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002099
Douglas Gregor0be31a22010-07-02 17:43:08 +00002100 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002101 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002102 case DeclSpec::TST_enum:
2103 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2104 case DeclSpec::TST_union:
2105 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2106 case DeclSpec::TST_struct:
2107 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002108 case DeclSpec::TST_interface:
2109 TagName="__interface"; FixitTagName = "__interface ";
2110 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002111 case DeclSpec::TST_class:
2112 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002113 }
Mike Stump11289f42009-09-09 15:08:12 +00002114
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002115 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002116 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2117 LookupResult R(Actions, TokenName, SourceLocation(),
2118 Sema::LookupOrdinaryName);
2119
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002120 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002121 << TokenName << TagName << getLangOpts().CPlusPlus
2122 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2123
2124 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2125 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2126 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002127 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002128 << TokenName << TagName;
2129 }
Mike Stump11289f42009-09-09 15:08:12 +00002130
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002131 // Parse this as a tag as if the missing tag were present.
2132 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002133 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002134 else
Richard Smithc5b05522012-03-12 07:56:15 +00002135 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002136 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002137 return true;
2138 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002139 }
Mike Stump11289f42009-09-09 15:08:12 +00002140
Richard Smithfe904f02012-05-15 21:29:55 +00002141 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002142 // being declared (with a missing type).
Richard Smith649c7b062014-01-08 00:56:48 +00002143 if (!isTypeSpecifier(DSC) &&
Richard Smithfe904f02012-05-15 21:29:55 +00002144 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002145 // Look ahead to the next token to try to figure out what this declaration
2146 // was supposed to be.
2147 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002148 case tok::l_paren: {
2149 // static x(4); // 'x' is not a type
2150 // x(int n); // 'x' is not a type
2151 // x (*p)[]; // 'x' is a type
2152 //
2153 // Since we're in an error case (or the rare 'implicit int in C++' MS
2154 // extension), we can afford to perform a tentative parse to determine
2155 // which case we're in.
2156 TentativeParsingAction PA(*this);
2157 ConsumeToken();
2158 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2159 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002160
2161 if (TPR != TPResult::False()) {
2162 // The identifier is followed by a parenthesized declarator.
2163 // It's supposed to be a type.
2164 break;
2165 }
2166
2167 // If we're in a context where we could be declaring a constructor,
2168 // check whether this is a constructor declaration with a bogus name.
2169 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2170 IdentifierInfo *II = Tok.getIdentifierInfo();
2171 if (Actions.isCurrentClassNameTypo(II, SS)) {
2172 Diag(Loc, diag::err_constructor_bad_name)
2173 << Tok.getIdentifierInfo() << II
2174 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2175 Tok.setIdentifierInfo(II);
2176 }
2177 }
2178 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002179 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002180 case tok::comma:
2181 case tok::equal:
2182 case tok::kw_asm:
2183 case tok::l_brace:
2184 case tok::l_square:
2185 case tok::semi:
2186 // This looks like a variable or function declaration. The type is
2187 // probably missing. We're done parsing decl-specifiers.
2188 if (SS)
2189 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2190 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002191
2192 default:
2193 // This is probably supposed to be a type. This includes cases like:
2194 // int f(itn);
2195 // struct S { unsinged : 4; };
2196 break;
2197 }
2198 }
2199
Chad Rosierc1183952012-06-26 22:30:43 +00002200 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002201 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002202 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002203 IdentifierInfo *II = Tok.getIdentifierInfo();
Kaelyn Uhrain67b44c92014-02-13 20:14:07 +00002204 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T,
2205 getLangOpts().CPlusPlus &&
2206 NextToken().is(tok::less))) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002207 // The action emitted a diagnostic, so we don't have to.
2208 if (T) {
2209 // The action has suggested that the type T could be used. Set that as
2210 // the type in the declaration specifiers, consume the would-be type
2211 // name token, and we're done.
2212 const char *PrevSpec;
2213 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002214 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
2215 Actions.getASTContext().getPrintingPolicy());
Douglas Gregor15e56022009-10-13 23:27:22 +00002216 DS.SetRangeEnd(Tok.getLocation());
2217 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002218 // There may be other declaration specifiers after this.
2219 return true;
2220 } else if (II != Tok.getIdentifierInfo()) {
2221 // If no type was suggested, the correction is to a keyword
2222 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002223 // There may be other declaration specifiers after this.
2224 return true;
2225 }
Chad Rosierc1183952012-06-26 22:30:43 +00002226
Douglas Gregor15e56022009-10-13 23:27:22 +00002227 // Fall through; the action had no suggestion for us.
2228 } else {
2229 // The action did not emit a diagnostic, so emit one now.
2230 SourceRange R;
2231 if (SS) R = SS->getRange();
2232 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2233 }
Mike Stump11289f42009-09-09 15:08:12 +00002234
Douglas Gregor15e56022009-10-13 23:27:22 +00002235 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002236 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002237 DS.SetRangeEnd(Tok.getLocation());
2238 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002239
Chris Lattner20a0c612009-04-14 21:34:55 +00002240 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2241 // avoid rippling error messages on subsequent uses of the same type,
2242 // could be useful if #include was forgotten.
2243 return false;
2244}
2245
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002246/// \brief Determine the declaration specifier context from the declarator
2247/// context.
2248///
2249/// \param Context the declarator context, which is one of the
2250/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002251Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002252Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2253 if (Context == Declarator::MemberContext)
2254 return DSC_class;
2255 if (Context == Declarator::FileContext)
2256 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002257 if (Context == Declarator::TrailingReturnContext)
2258 return DSC_trailing;
Richard Smith649c7b062014-01-08 00:56:48 +00002259 if (Context == Declarator::AliasDeclContext ||
2260 Context == Declarator::AliasTemplateContext)
2261 return DSC_alias_declaration;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002262 return DSC_normal;
2263}
2264
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002265/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2266///
2267/// FIXME: Simply returns an alignof() expression if the argument is a
2268/// type. Ideally, the type should be propagated directly into Sema.
2269///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002270/// [C11] type-id
2271/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002272/// [C++0x] type-id ...[opt]
2273/// [C++0x] assignment-expression ...[opt]
2274ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2275 SourceLocation &EllipsisLoc) {
2276 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002277 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002278 SourceLocation TypeLoc = Tok.getLocation();
2279 ParsedType Ty = ParseTypeName().get();
2280 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002281 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2282 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002283 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002284 ER = ParseConstantExpression();
2285
Alp Toker8fbec672013-12-17 23:29:36 +00002286 if (getLangOpts().CPlusPlus11)
2287 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002288
2289 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002290}
2291
2292/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2293/// attribute to Attrs.
2294///
2295/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002296/// [C11] '_Alignas' '(' type-id ')'
2297/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002298/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2299/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002300void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002301 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002302 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2303 "Not an alignment-specifier!");
2304
Richard Smithd11c7a12013-01-29 01:48:07 +00002305 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2306 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002307
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002308 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002309 if (T.expectAndConsume())
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002310 return;
2311
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002312 SourceLocation EllipsisLoc;
2313 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002314 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002315 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002316 return;
2317 }
2318
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002319 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002320 if (EndLoc)
2321 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002322
Aaron Ballman00e99962013-08-31 01:11:41 +00002323 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002324 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002325 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2326 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002327}
2328
Richard Smith404dfb42013-11-19 22:47:36 +00002329/// Determine whether we're looking at something that might be a declarator
2330/// in a simple-declaration. If it can't possibly be a declarator, maybe
2331/// diagnose a missing semicolon after a prior tag definition in the decl
2332/// specifier.
2333///
2334/// \return \c true if an error occurred and this can't be any kind of
2335/// declaration.
2336bool
2337Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2338 DeclSpecContext DSContext,
2339 LateParsedAttrList *LateAttrs) {
2340 assert(DS.hasTagDefinition() && "shouldn't call this");
2341
2342 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002343
2344 if (getLangOpts().CPlusPlus &&
2345 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2346 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
2347 TryAnnotateCXXScopeToken(EnteringContext)) {
2348 SkipMalformedDecl();
2349 return true;
2350 }
2351
Richard Smith698875a2013-11-20 23:40:57 +00002352 bool HasScope = Tok.is(tok::annot_cxxscope);
2353 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2354 Token AfterScope = HasScope ? NextToken() : Tok;
2355
Richard Smith404dfb42013-11-19 22:47:36 +00002356 // Determine whether the following tokens could possibly be a
2357 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002358 bool MightBeDeclarator = true;
2359 if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) {
2360 // A declarator-id can't start with 'typename'.
2361 MightBeDeclarator = false;
2362 } else if (AfterScope.is(tok::annot_template_id)) {
2363 // If we have a type expressed as a template-id, this cannot be a
2364 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2365 TemplateIdAnnotation *Annot =
2366 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2367 if (Annot->Kind == TNK_Type_template)
2368 MightBeDeclarator = false;
2369 } else if (AfterScope.is(tok::identifier)) {
2370 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2371
Richard Smith404dfb42013-11-19 22:47:36 +00002372 // These tokens cannot come after the declarator-id in a
2373 // simple-declaration, and are likely to come after a type-specifier.
Richard Smith698875a2013-11-20 23:40:57 +00002374 if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) ||
2375 Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) ||
2376 Next.is(tok::coloncolon)) {
2377 // Missing a semicolon.
2378 MightBeDeclarator = false;
2379 } else if (HasScope) {
2380 // If the declarator-id has a scope specifier, it must redeclare a
2381 // previously-declared entity. If that's a type (and this is not a
2382 // typedef), that's an error.
2383 CXXScopeSpec SS;
2384 Actions.RestoreNestedNameSpecifierAnnotation(
2385 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2386 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2387 Sema::NameClassification Classification = Actions.ClassifyName(
2388 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2389 /*IsAddressOfOperand*/false);
2390 switch (Classification.getKind()) {
2391 case Sema::NC_Error:
2392 SkipMalformedDecl();
2393 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002394
Richard Smith698875a2013-11-20 23:40:57 +00002395 case Sema::NC_Keyword:
2396 case Sema::NC_NestedNameSpecifier:
2397 llvm_unreachable("typo correction and nested name specifiers not "
2398 "possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002399
Richard Smith698875a2013-11-20 23:40:57 +00002400 case Sema::NC_Type:
2401 case Sema::NC_TypeTemplate:
2402 // Not a previously-declared non-type entity.
2403 MightBeDeclarator = false;
2404 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002405
Richard Smith698875a2013-11-20 23:40:57 +00002406 case Sema::NC_Unknown:
2407 case Sema::NC_Expression:
2408 case Sema::NC_VarTemplate:
2409 case Sema::NC_FunctionTemplate:
2410 // Might be a redeclaration of a prior entity.
2411 break;
2412 }
Richard Smith404dfb42013-11-19 22:47:36 +00002413 }
Richard Smith404dfb42013-11-19 22:47:36 +00002414 }
2415
Richard Smith698875a2013-11-20 23:40:57 +00002416 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002417 return false;
2418
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002419 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Richard Smith404dfb42013-11-19 22:47:36 +00002420 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
Alp Toker383d2c42014-01-01 03:08:43 +00002421 diag::err_expected_after)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002422 << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi;
Richard Smith404dfb42013-11-19 22:47:36 +00002423
2424 // Try to recover from the typo, by dropping the tag definition and parsing
2425 // the problematic tokens as a type.
2426 //
2427 // FIXME: Split the DeclSpec into pieces for the standalone
2428 // declaration and pieces for the following declaration, instead
2429 // of assuming that all the other pieces attach to new declaration,
2430 // and call ParsedFreeStandingDeclSpec as appropriate.
2431 DS.ClearTypeSpecType();
2432 ParsedTemplateInfo NotATemplate;
2433 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2434 return false;
2435}
2436
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002437/// ParseDeclarationSpecifiers
2438/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002439/// storage-class-specifier declaration-specifiers[opt]
2440/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002441/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002442/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002443/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002444/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002445///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002446/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002447/// 'typedef'
2448/// 'extern'
2449/// 'static'
2450/// 'auto'
2451/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002452/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002453/// [C++11] 'thread_local'
2454/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002455/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002456/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002457/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002458/// [C++] 'virtual'
2459/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002460/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002461/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002462/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002463
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002464///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002465void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002466 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002467 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002468 DeclSpecContext DSContext,
2469 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002470 if (DS.getSourceRange().isInvalid()) {
2471 DS.SetRangeStart(Tok.getLocation());
2472 DS.SetRangeEnd(Tok.getLocation());
2473 }
Chad Rosierc1183952012-06-26 22:30:43 +00002474
Douglas Gregordf593fb2011-11-07 17:33:42 +00002475 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002476 bool AttrsLastTime = false;
2477 ParsedAttributesWithRange attrs(AttrFactory);
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002478 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
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.
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002502 DS.Finish(Diags, PP, Policy);
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)) {
Richard Smith446161b2014-03-03 21:12:53 +00002604 if (isConstructorDeclarator(/*Unqualified*/false)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002605 // 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(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002636 PrevSpec, DiagID, T, Policy);
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)) {
Richard Smith446161b2014-03-03 21:12:53 +00002654 if (isConstructorDeclarator(/*Unqualified*/false))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002655 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,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002695 DiagID, TypeRep, Policy);
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,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002714 DiagID, T, Policy);
John McCallba7bf592010-08-24 05:47:05 +00002715 } 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()) &&
Richard Smith446161b2014-03-03 21:12:53 +00002801 isConstructorDeclarator(/*Unqualified*/true))
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,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002805 DiagID, TypeRep, Policy);
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()) &&
Richard Smith446161b2014-03-03 21:12:53 +00002837 isConstructorDeclarator(TemplateId->SS.isEmpty()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002838 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();
Aaron Ballman00e99962013-08-31 01:11:41 +00002861 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
Aaron Ballman3fe6ed52014-01-13 21:40:16 +00002862 AttributeList::AS_Keyword);
Richard Smithda837032012-09-14 18:27:01 +00002863 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002864 }
Eli Friedman53339e02009-06-08 23:27:34 +00002865
Aaron Ballman317a77f2013-05-22 23:25:32 +00002866 case tok::kw___sptr:
2867 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002868 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002869 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002870 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002871 case tok::kw___cdecl:
2872 case tok::kw___stdcall:
2873 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002874 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002875 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002876 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002877 continue;
2878
Dawn Perchik335e16b2010-09-03 01:29:35 +00002879 // Borland single token adornments.
2880 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002881 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002882 continue;
2883
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002884 // OpenCL single token adornments.
2885 case tok::kw___kernel:
2886 ParseOpenCLAttributes(DS.getAttributes());
2887 continue;
2888
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002889 // storage-class-specifier
2890 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002891 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002892 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002893 break;
2894 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002895 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002896 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002897 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002898 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002899 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002900 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002901 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002902 Loc, PrevSpec, DiagID, Policy);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002903 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002904 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002905 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002906 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002907 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002908 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002909 break;
2910 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002911 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002912 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002913 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002914 PrevSpec, DiagID, Policy);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002915 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002916 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002917 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002918 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002919 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002920 DiagID, Policy);
Richard Smith58c74332011-09-04 19:54:14 +00002921 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002922 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002923 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002924 break;
2925 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002926 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002927 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002928 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002929 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002930 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002931 PrevSpec, DiagID, Policy);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002932 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002933 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002934 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2935 PrevSpec, DiagID);
2936 break;
2937 case tok::kw_thread_local:
2938 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2939 PrevSpec, DiagID);
2940 break;
2941 case tok::kw__Thread_local:
2942 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2943 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002944 break;
Mike Stump11289f42009-09-09 15:08:12 +00002945
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002946 // function-specifier
2947 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00002948 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002949 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002950 case tok::kw_virtual:
Serge Pavlov750db652013-11-13 06:57:53 +00002951 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002952 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002953 case tok::kw_explicit:
Serge Pavlov750db652013-11-13 06:57:53 +00002954 isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002955 break;
Richard Smith0015f092013-01-17 22:16:11 +00002956 case tok::kw__Noreturn:
2957 if (!getLangOpts().C11)
2958 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlov750db652013-11-13 06:57:53 +00002959 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00002960 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002961
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002962 // alignment-specifier
2963 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002964 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002965 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002966 ParseAlignmentSpecifier(DS.getAttributes());
2967 continue;
2968
Anders Carlssoncd8db412009-05-06 04:46:28 +00002969 // friend
2970 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002971 if (DSContext == DSC_class)
2972 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2973 else {
2974 PrevSpec = ""; // not actually used by the diagnostic
2975 DiagID = diag::err_friend_invalid_in_context;
2976 isInvalid = true;
2977 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002978 break;
Mike Stump11289f42009-09-09 15:08:12 +00002979
Douglas Gregor26701a42011-09-09 02:06:17 +00002980 // Modules
2981 case tok::kw___module_private__:
2982 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2983 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002984
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002985 // constexpr
2986 case tok::kw_constexpr:
2987 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2988 break;
2989
Chris Lattnere387d9e2009-01-21 19:48:37 +00002990 // type-specifier
2991 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002992 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002993 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002994 break;
2995 case tok::kw_long:
2996 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002997 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002998 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002999 else
John McCall49bfce42009-08-03 20:12:06 +00003000 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003001 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003002 break;
Francois Pichet84133e42011-04-28 01:59:37 +00003003 case tok::kw___int64:
3004 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003005 DiagID, Policy);
Francois Pichet84133e42011-04-28 01:59:37 +00003006 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003007 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00003008 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3009 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003010 break;
3011 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003012 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3013 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003014 break;
3015 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00003016 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3017 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003018 break;
3019 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00003020 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3021 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003022 break;
3023 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003024 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003025 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003026 break;
3027 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003028 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003029 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003030 break;
3031 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003032 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003033 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003034 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003035 case tok::kw___int128:
3036 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003037 DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00003038 break;
3039 case tok::kw_half:
3040 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003041 DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00003042 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003043 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003044 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003045 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003046 break;
3047 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003048 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003049 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003050 break;
3051 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003052 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003053 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003054 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003055 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003056 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003057 DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003058 break;
3059 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003060 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003061 DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003062 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003063 case tok::kw_bool:
3064 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003065 if (Tok.is(tok::kw_bool) &&
3066 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3067 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3068 PrevSpec = ""; // Not used by the diagnostic.
3069 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003070 // For better error recovery.
3071 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003072 isInvalid = true;
3073 } else {
3074 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003075 DiagID, Policy);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003076 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003077 break;
3078 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003079 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003080 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003081 break;
3082 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003083 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003084 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003085 break;
3086 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003087 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003088 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003089 break;
John Thompson22334602010-02-05 00:12:22 +00003090 case tok::kw___vector:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003091 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
John Thompson22334602010-02-05 00:12:22 +00003092 break;
3093 case tok::kw___pixel:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003094 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
John Thompson22334602010-02-05 00:12:22 +00003095 break;
John McCall39439732011-04-09 22:50:59 +00003096 case tok::kw___unknown_anytype:
3097 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003098 PrevSpec, DiagID, Policy);
John McCall39439732011-04-09 22:50:59 +00003099 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003100
3101 // class-specifier:
3102 case tok::kw_class:
3103 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003104 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003105 case tok::kw_union: {
3106 tok::TokenKind Kind = Tok.getKind();
3107 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003108
3109 // These are attributes following class specifiers.
3110 // To produce better diagnostic, we parse them when
3111 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003112 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003113 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003114 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003115
3116 // If there are attributes following class specifier,
3117 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003118 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003119 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003120 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003121 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003122 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003123 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003124
3125 // enum-specifier:
3126 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003127 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003128 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003129 continue;
3130
3131 // cv-qualifier:
3132 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003133 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003134 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003135 break;
3136 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003137 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003138 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003139 break;
3140 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003141 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003142 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003143 break;
3144
Douglas Gregor333489b2009-03-27 23:10:48 +00003145 // C++ typename-specifier:
3146 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003147 if (TryAnnotateTypeOrScopeToken()) {
3148 DS.SetTypeSpecError();
3149 goto DoneWithDeclSpec;
3150 }
3151 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003152 continue;
3153 break;
3154
Chris Lattnere387d9e2009-01-21 19:48:37 +00003155 // GNU typeof support.
3156 case tok::kw_typeof:
3157 ParseTypeofSpecifier(DS);
3158 continue;
3159
David Blaikie15a430a2011-12-04 05:04:18 +00003160 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003161 ParseDecltypeSpecifier(DS);
3162 continue;
3163
Alexis Hunt4a257072011-05-19 05:37:45 +00003164 case tok::kw___underlying_type:
3165 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003166 continue;
3167
3168 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003169 // C11 6.7.2.4/4:
3170 // If the _Atomic keyword is immediately followed by a left parenthesis,
3171 // it is interpreted as a type specifier (with a type name), not as a
3172 // type qualifier.
3173 if (NextToken().is(tok::l_paren)) {
3174 ParseAtomicSpecifier(DS);
3175 continue;
3176 }
3177 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3178 getLangOpts());
3179 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003180
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003181 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003182 case tok::kw___private:
3183 case tok::kw___global:
3184 case tok::kw___local:
3185 case tok::kw___constant:
3186 case tok::kw___read_only:
3187 case tok::kw___write_only:
3188 case tok::kw___read_write:
Aaron Ballman05d76ea2014-01-14 01:29:54 +00003189 ParseOpenCLQualifiers(DS.getAttributes());
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003190 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003191
Steve Naroffcfdf6162008-06-05 00:02:44 +00003192 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003193 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003194 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3195 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003196 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003197 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003198
Douglas Gregor3a001f42010-11-19 17:10:50 +00003199 if (!ParseObjCProtocolQualifiers(DS))
3200 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3201 << FixItHint::CreateInsertion(Loc, "id")
3202 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003203
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003204 // Need to support trailing type qualifiers (e.g. "id<p> const").
3205 // If a type specifier follows, it will be diagnosed elsewhere.
3206 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003207 }
John McCall49bfce42009-08-03 20:12:06 +00003208 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003209 if (isInvalid) {
3210 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003211 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003212
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003213 if (DiagID == diag::ext_duplicate_declspec)
3214 Diag(Tok, DiagID)
3215 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3216 else
3217 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003218 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003219
Chris Lattner2e232092008-03-13 06:29:04 +00003220 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003221 if (DiagID != diag::err_bool_redeclaration)
3222 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003223
3224 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003225 }
3226}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003227
Chris Lattner70ae4912007-10-29 04:42:53 +00003228/// ParseStructDeclaration - Parse a struct declaration without the terminating
3229/// semicolon.
3230///
Chris Lattner90a26b02007-01-23 04:38:16 +00003231/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003232/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003233/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003234/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003235/// struct-declarator-list:
3236/// struct-declarator
3237/// struct-declarator-list ',' struct-declarator
3238/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3239/// struct-declarator:
3240/// declarator
3241/// [GNU] declarator attributes[opt]
3242/// declarator[opt] ':' constant-expression
3243/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3244///
Chris Lattnera12405b2008-04-10 06:46:29 +00003245void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003246ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003247
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003248 if (Tok.is(tok::kw___extension__)) {
3249 // __extension__ silences extension warnings in the subexpression.
3250 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003251 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003252 return ParseStructDeclaration(DS, Fields);
3253 }
Mike Stump11289f42009-09-09 15:08:12 +00003254
Steve Naroff97170802007-08-20 22:28:22 +00003255 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003256 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003257
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003258 // If there are no declarators, this is a free-standing declaration
3259 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003260 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003261 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3262 DS);
3263 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003264 return;
3265 }
3266
3267 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003268 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003269 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003270 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003271 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003272 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003273
Bill Wendling44426052012-12-20 19:22:21 +00003274 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003275 if (!FirstDeclarator)
3276 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003277
Steve Naroff97170802007-08-20 22:28:22 +00003278 /// struct-declarator: declarator
3279 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003280 if (Tok.isNot(tok::colon)) {
3281 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3282 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003283 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003284 }
Mike Stump11289f42009-09-09 15:08:12 +00003285
Alp Toker8fbec672013-12-17 23:29:36 +00003286 if (TryConsumeToken(tok::colon)) {
John McCalldadc5752010-08-24 06:29:42 +00003287 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003288 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003289 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00003290 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003291 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003292 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003293
Steve Naroff97170802007-08-20 22:28:22 +00003294 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003295 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003296
John McCallcfefb6d2009-11-03 02:38:08 +00003297 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003298 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003299
Steve Naroff97170802007-08-20 22:28:22 +00003300 // If we don't have a comma, it is either the end of the list (a ';')
3301 // or an error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00003302 if (!TryConsumeToken(tok::comma, CommaLoc))
Chris Lattner70ae4912007-10-29 04:42:53 +00003303 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003304
John McCallcfefb6d2009-11-03 02:38:08 +00003305 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003306 }
Steve Naroff97170802007-08-20 22:28:22 +00003307}
3308
3309/// ParseStructUnionBody
3310/// struct-contents:
3311/// struct-declaration-list
3312/// [EXT] empty
3313/// [GNU] "struct-declaration-list" without terminatoring ';'
3314/// struct-declaration-list:
3315/// struct-declaration
3316/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003317/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003318///
Chris Lattner1300fb92007-01-23 23:42:53 +00003319void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003320 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003321 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3322 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003323 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003324
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003325 BalancedDelimiterTracker T(*this, tok::l_brace);
3326 if (T.consumeOpen())
3327 return;
Mike Stump11289f42009-09-09 15:08:12 +00003328
Douglas Gregor658b9552009-01-09 22:42:13 +00003329 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003330 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003331
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003332 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003333
Chris Lattner7b9ace62007-01-23 20:11:08 +00003334 // While we still have something to read, read the declarations in the struct.
Richard Smith34f30512013-11-23 04:06:09 +00003335 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003336 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003337
Chris Lattner736ed5d2007-06-09 05:59:07 +00003338 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003339 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003340 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003341 continue;
3342 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003343
Andy Gibbsc804e082013-04-03 09:46:04 +00003344 // Parse _Static_assert declaration.
3345 if (Tok.is(tok::kw__Static_assert)) {
3346 SourceLocation DeclEnd;
3347 ParseStaticAssertDeclaration(DeclEnd);
3348 continue;
3349 }
3350
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003351 if (Tok.is(tok::annot_pragma_pack)) {
3352 HandlePragmaPack();
3353 continue;
3354 }
3355
3356 if (Tok.is(tok::annot_pragma_align)) {
3357 HandlePragmaAlign();
3358 continue;
3359 }
3360
John McCallcfefb6d2009-11-03 02:38:08 +00003361 if (!Tok.is(tok::at)) {
3362 struct CFieldCallback : FieldCallback {
3363 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003364 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003365 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003366
John McCall48871652010-08-21 09:40:31 +00003367 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003368 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003369 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3370
Craig Topper2b07f022014-03-12 05:09:18 +00003371 void invoke(ParsingFieldDeclarator &FD) override {
John McCallcfefb6d2009-11-03 02:38:08 +00003372 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003373 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003374 FD.D.getDeclSpec().getSourceRange().getBegin(),
3375 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003376 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003377 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003378 }
John McCallcfefb6d2009-11-03 02:38:08 +00003379 } Callback(*this, TagDecl, FieldDecls);
3380
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003381 // Parse all the comma separated declarators.
3382 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003383 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003384 } else { // Handle @defs
3385 ConsumeToken();
3386 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3387 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003388 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003389 continue;
3390 }
3391 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003392 ExpectAndConsume(tok::l_paren);
Chris Lattner535b8302008-06-21 19:39:06 +00003393 if (!Tok.is(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003394 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003395 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003396 continue;
3397 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003398 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003399 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003400 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003401 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3402 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003403 ExpectAndConsume(tok::r_paren);
Mike Stump11289f42009-09-09 15:08:12 +00003404 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003405
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003406 if (TryConsumeToken(tok::semi))
3407 continue;
3408
3409 if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003410 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003411 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003412 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003413
3414 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3415 // Skip to end of block or statement to avoid ext-warning on extra ';'.
3416 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
3417 // If we stopped at a ';', eat it.
3418 TryConsumeToken(tok::semi);
Chris Lattner90a26b02007-01-23 04:38:16 +00003419 }
Mike Stump11289f42009-09-09 15:08:12 +00003420
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003421 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003422
John McCall084e83d2011-03-24 11:26:52 +00003423 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003424 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003425 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003426
Douglas Gregor0be31a22010-07-02 17:43:08 +00003427 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003428 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003429 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003430 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003431 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003432 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3433 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003434}
3435
Chris Lattner3b561a32006-08-13 00:12:11 +00003436/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003437/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003438/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003439///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003440/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3441/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003442/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3443/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003444/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003445/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003446///
Richard Smith7d137e32012-03-23 03:33:32 +00003447/// [C++11] enum-head '{' enumerator-list[opt] '}'
3448/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003449///
Richard Smith7d137e32012-03-23 03:33:32 +00003450/// enum-head: [C++11]
3451/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3452/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3453/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003454///
Richard Smith7d137e32012-03-23 03:33:32 +00003455/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003456/// 'enum'
3457/// 'enum' 'class'
3458/// 'enum' 'struct'
3459///
Richard Smith7d137e32012-03-23 03:33:32 +00003460/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003461/// ':' type-specifier-seq
3462///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003463/// [C++] elaborated-type-specifier:
3464/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3465///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003466void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003467 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003468 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003469 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003470 if (Tok.is(tok::code_completion)) {
3471 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003472 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003473 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003474 }
John McCallcb432fa2011-07-06 05:58:41 +00003475
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003476 // If attributes exist after tag, parse them.
3477 ParsedAttributesWithRange attrs(AttrFactory);
3478 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003479 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003480
3481 // If declspecs exist after tag, parse them.
3482 while (Tok.is(tok::kw___declspec))
3483 ParseMicrosoftDeclSpec(attrs);
3484
Richard Smith0f8ee222012-01-10 01:33:14 +00003485 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003486 bool IsScopedUsingClassTag = false;
3487
John McCallbeae29a2012-06-23 22:30:04 +00003488 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003489 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3490 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3491 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003492 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003493 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003494
Bill Wendling44426052012-12-20 19:22:21 +00003495 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003496 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003497 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003498
3499 // They are allowed afterwards, though.
3500 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003501 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003502 while (Tok.is(tok::kw___declspec))
3503 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003504 }
Richard Smith7d137e32012-03-23 03:33:32 +00003505
John McCall6347b682012-05-07 06:16:58 +00003506 // C++11 [temp.explicit]p12:
3507 // The usual access controls do not apply to names used to specify
3508 // explicit instantiations.
3509 // We extend this to also cover explicit specializations. Note that
3510 // we don't suppress if this turns out to be an elaborated type
3511 // specifier.
3512 bool shouldDelayDiagsInTag =
3513 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3514 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3515 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003516
Richard Smithbfdb1082012-03-12 08:56:40 +00003517 // Enum definitions should not be parsed in a trailing-return-type.
3518 bool AllowDeclaration = DSC != DSC_trailing;
3519
3520 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003521 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003522 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003523
Abramo Bagnarad7548482010-05-19 21:37:53 +00003524 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003525 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003526 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3527 // if a fixed underlying type is allowed.
3528 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003529
3530 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003531 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003532 return;
3533
3534 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003535 Diag(Tok, diag::err_expected) << tok::identifier;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003536 if (Tok.isNot(tok::l_brace)) {
3537 // Has no name and is not a definition.
3538 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003539 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003540 return;
3541 }
3542 }
3543 }
Mike Stump11289f42009-09-09 15:08:12 +00003544
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003545 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003546 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003547 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Alp Tokerec543272013-12-24 09:48:30 +00003548 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
Mike Stump11289f42009-09-09 15:08:12 +00003549
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003550 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003551 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00003552 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003553 }
Mike Stump11289f42009-09-09 15:08:12 +00003554
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003555 // If an identifier is present, consume and remember it.
3556 IdentifierInfo *Name = 0;
3557 SourceLocation NameLoc;
3558 if (Tok.is(tok::identifier)) {
3559 Name = Tok.getIdentifierInfo();
3560 NameLoc = ConsumeToken();
3561 }
Mike Stump11289f42009-09-09 15:08:12 +00003562
Richard Smith0f8ee222012-01-10 01:33:14 +00003563 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003564 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3565 // declaration of a scoped enumeration.
3566 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003567 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003568 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003569 }
3570
John McCall6347b682012-05-07 06:16:58 +00003571 // Okay, end the suppression area. We'll decide whether to emit the
3572 // diagnostics in a second.
3573 if (shouldDelayDiagsInTag)
3574 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003575
Douglas Gregor0bf31402010-10-08 23:50:27 +00003576 TypeResult BaseType;
3577
Douglas Gregord1f69f62010-12-01 17:42:47 +00003578 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003579 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003580 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003581 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003582 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003583 // If we're in class scope, this can either be an enum declaration with
3584 // an underlying type, or a declaration of a bitfield member. We try to
3585 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003586 // (integer literal, sizeof); if it's still ambiguous, we then consider
3587 // anything that's a simple-type-specifier followed by '(' as an
3588 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003589 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003590 EnterExpressionEvaluationContext Unevaluated(Actions,
3591 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003592 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003593 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003594 // bit-field. This is the common case.
3595 if (TPR == TPResult::True())
3596 PossibleBitfield = true;
3597 // If the next token starts a type-specifier-seq, it may be either a
3598 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003599 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003600 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003601 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003602 GetLookAheadToken(2).getKind() == tok::semi) {
3603 // Consume the ':'.
3604 ConsumeToken();
3605 } else {
3606 // We have the start of a type-specifier-seq, so we have to perform
3607 // tentative parsing to determine whether we have an expression or a
3608 // type.
3609 TentativeParsingAction TPA(*this);
3610
3611 // Consume the ':'.
3612 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003613
3614 // If we see a type specifier followed by an open-brace, we have an
3615 // ambiguity between an underlying type and a C++11 braced
3616 // function-style cast. Resolve this by always treating it as an
3617 // underlying type.
3618 // FIXME: The standard is not entirely clear on how to disambiguate in
3619 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003620 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003621 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003622 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003623 // We'll parse this as a bitfield later.
3624 PossibleBitfield = true;
3625 TPA.Revert();
3626 } else {
3627 // We have a type-specifier-seq.
3628 TPA.Commit();
3629 }
3630 }
3631 } else {
3632 // Consume the ':'.
3633 ConsumeToken();
3634 }
3635
3636 if (!PossibleBitfield) {
3637 SourceRange Range;
3638 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003639
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003640 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003641 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003642 } else if (!getLangOpts().ObjC2) {
3643 if (getLangOpts().CPlusPlus)
3644 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3645 else
3646 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3647 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003648 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003649 }
3650
Richard Smith0f8ee222012-01-10 01:33:14 +00003651 // There are four options here. If we have 'friend enum foo;' then this is a
3652 // friend declaration, and cannot have an accompanying definition. If we have
3653 // 'enum foo;', then this is a forward declaration. If we have
3654 // 'enum foo {...' then this is a definition. Otherwise we have something
3655 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003656 //
3657 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3658 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3659 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3660 //
John McCallfaf5fb42010-08-26 23:41:50 +00003661 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003662 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003663 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003664 } else if (Tok.is(tok::l_brace)) {
3665 if (DS.isFriendSpecified()) {
3666 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3667 << SourceRange(DS.getFriendSpecLoc());
3668 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003669 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00003670 TUK = Sema::TUK_Friend;
3671 } else {
3672 TUK = Sema::TUK_Definition;
3673 }
Richard Smith649c7b062014-01-08 00:56:48 +00003674 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00003675 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003676 (Tok.isAtStartOfLine() &&
3677 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003678 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3679 if (Tok.isNot(tok::semi)) {
3680 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00003681 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003682 PP.EnterToken(Tok);
3683 Tok.setKind(tok::semi);
3684 }
John McCall6347b682012-05-07 06:16:58 +00003685 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003686 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003687 }
3688
3689 // If this is an elaborated type specifier, and we delayed
3690 // diagnostics before, just merge them into the current pool.
3691 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3692 diagsFromTag.redelay();
3693 }
Richard Smith7d137e32012-03-23 03:33:32 +00003694
3695 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003696 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003697 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003698 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003699 // Skip the rest of this declarator, up until the comma or semicolon.
3700 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003701 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00003702 return;
3703 }
3704
3705 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3706 // Enumerations can't be explicitly instantiated.
3707 DS.SetTypeSpecError();
3708 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3709 return;
3710 }
3711
3712 assert(TemplateInfo.TemplateParams && "no template parameters");
3713 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3714 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003715 }
Chad Rosierc1183952012-06-26 22:30:43 +00003716
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003717 if (TUK == Sema::TUK_Reference)
3718 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003719
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003720 if (!Name && TUK != Sema::TUK_Definition) {
3721 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003722
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003723 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003724 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003725 return;
3726 }
Richard Smith7d137e32012-03-23 03:33:32 +00003727
Douglas Gregord6ab8742009-05-28 23:31:59 +00003728 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003729 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003730 const char *PrevSpec = 0;
3731 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003732 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003733 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003734 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003735 Owned, IsDependent, ScopedEnumKWLoc,
Richard Smith649c7b062014-01-08 00:56:48 +00003736 IsScopedUsingClassTag, BaseType,
3737 DSC == DSC_type_specifier);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003738
Douglas Gregorba41d012010-04-24 16:38:41 +00003739 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003740 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003741 // dependent tag.
3742 if (!Name) {
3743 DS.SetTypeSpecError();
3744 Diag(Tok, diag::err_expected_type_name_after_typename);
3745 return;
3746 }
Chad Rosierc1183952012-06-26 22:30:43 +00003747
Douglas Gregor0be31a22010-07-02 17:43:08 +00003748 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003749 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003750 NameLoc);
3751 if (Type.isInvalid()) {
3752 DS.SetTypeSpecError();
3753 return;
3754 }
Chad Rosierc1183952012-06-26 22:30:43 +00003755
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003756 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3757 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003758 PrevSpec, DiagID, Type.get(),
3759 Actions.getASTContext().getPrintingPolicy()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003760 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003761
Douglas Gregorba41d012010-04-24 16:38:41 +00003762 return;
3763 }
Mike Stump11289f42009-09-09 15:08:12 +00003764
John McCall48871652010-08-21 09:40:31 +00003765 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003766 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003767 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003768 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003769 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003770 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00003771 }
Chad Rosierc1183952012-06-26 22:30:43 +00003772
Douglas Gregorba41d012010-04-24 16:38:41 +00003773 DS.SetTypeSpecError();
3774 return;
3775 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003776
Richard Smith369b9f92012-06-25 21:37:02 +00003777 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003778 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003779
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003780 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3781 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003782 PrevSpec, DiagID, TagDecl, Owned,
3783 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00003784 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003785}
3786
Chris Lattnerc1915e22007-01-25 07:29:02 +00003787/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3788/// enumerator-list:
3789/// enumerator
3790/// enumerator-list ',' enumerator
3791/// enumerator:
3792/// enumeration-constant
3793/// enumeration-constant '=' constant-expression
3794/// enumeration-constant:
3795/// identifier
3796///
John McCall48871652010-08-21 09:40:31 +00003797void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003798 // Enter the scope of the enum body and start the definition.
3799 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003800 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003801
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003802 BalancedDelimiterTracker T(*this, tok::l_brace);
3803 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003804
Chris Lattner37256fb2007-08-27 17:24:30 +00003805 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003806 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003807 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003808
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003809 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003810
John McCall48871652010-08-21 09:40:31 +00003811 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003812
Chris Lattnerc1915e22007-01-25 07:29:02 +00003813 // Parse the enumerator-list.
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003814 while (Tok.isNot(tok::r_brace)) {
3815 // Parse enumerator. If failed, try skipping till the start of the next
3816 // enumerator definition.
3817 if (Tok.isNot(tok::identifier)) {
3818 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
3819 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
3820 TryConsumeToken(tok::comma))
3821 continue;
3822 break;
3823 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003824 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3825 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003826
John McCall811a0f52010-10-22 23:36:17 +00003827 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003828 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003829 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003830 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003831 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003832
Chris Lattnerc1915e22007-01-25 07:29:02 +00003833 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003834 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003835 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003836
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003837 if (TryConsumeToken(tok::equal, EqualLoc)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003838 AssignedVal = ParseConstantExpression();
3839 if (AssignedVal.isInvalid())
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003840 SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003841 }
Mike Stump11289f42009-09-09 15:08:12 +00003842
Chris Lattnerc1915e22007-01-25 07:29:02 +00003843 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003844 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3845 LastEnumConstDecl,
3846 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003847 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003848 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003849 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003850
Chris Lattner4ef40012007-06-11 01:28:17 +00003851 EnumConstantDecls.push_back(EnumConstDecl);
3852 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003853
Douglas Gregorce66d022010-09-07 14:51:08 +00003854 if (Tok.is(tok::identifier)) {
3855 // We're missing a comma between enumerators.
3856 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003857 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003858 << FixItHint::CreateInsertion(Loc, ", ");
3859 continue;
3860 }
Chad Rosierc1183952012-06-26 22:30:43 +00003861
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003862 // Emumerator definition must be finished, only comma or r_brace are
3863 // allowed here.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003864 SourceLocation CommaLoc;
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003865 if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
3866 if (EqualLoc.isValid())
3867 Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
3868 << tok::comma;
3869 else
3870 Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
3871 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
3872 if (TryConsumeToken(tok::comma, CommaLoc))
3873 continue;
3874 } else {
3875 break;
3876 }
3877 }
Mike Stump11289f42009-09-09 15:08:12 +00003878
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003879 // If comma is followed by r_brace, emit appropriate warning.
3880 if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003881 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003882 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3883 diag::ext_enumerator_list_comma_cxx :
3884 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003885 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003886 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003887 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3888 << FixItHint::CreateRemoval(CommaLoc);
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003889 break;
Richard Smith5d164bc2011-10-15 05:09:34 +00003890 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003891 }
Mike Stump11289f42009-09-09 15:08:12 +00003892
Chris Lattnerc1915e22007-01-25 07:29:02 +00003893 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003894 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003895
Chris Lattnerc1915e22007-01-25 07:29:02 +00003896 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003897 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003898 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003899
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003900 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003901 EnumDecl, EnumConstantDecls,
3902 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003903 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003904
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003905 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003906 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3907 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003908
3909 // The next token must be valid after an enum definition. If not, a ';'
3910 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003911 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3912 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Alp Toker383d2c42014-01-01 03:08:43 +00003913 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003914 // Push this token back into the preprocessor and change our current token
3915 // to ';' so that the rest of the code recovers as though there were an
3916 // ';' after the definition.
3917 PP.EnterToken(Tok);
3918 Tok.setKind(tok::semi);
3919 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003920}
Chris Lattner3b561a32006-08-13 00:12:11 +00003921
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003922/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003923/// start of a type-qualifier-list.
3924bool Parser::isTypeQualifier() const {
3925 switch (Tok.getKind()) {
3926 default: return false;
Alp Tokerde50ff32013-12-17 18:17:46 +00003927 // type-qualifier
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003928 case tok::kw_const:
3929 case tok::kw_volatile:
3930 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003931 case tok::kw___private:
3932 case tok::kw___local:
3933 case tok::kw___global:
3934 case tok::kw___constant:
3935 case tok::kw___read_only:
3936 case tok::kw___read_write:
3937 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003938 return true;
3939 }
3940}
3941
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003942/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3943/// is definitely a type-specifier. Return false if it isn't part of a type
3944/// specifier or if we're not sure.
3945bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3946 switch (Tok.getKind()) {
3947 default: return false;
3948 // type-specifiers
3949 case tok::kw_short:
3950 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003951 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003952 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003953 case tok::kw_signed:
3954 case tok::kw_unsigned:
3955 case tok::kw__Complex:
3956 case tok::kw__Imaginary:
3957 case tok::kw_void:
3958 case tok::kw_char:
3959 case tok::kw_wchar_t:
3960 case tok::kw_char16_t:
3961 case tok::kw_char32_t:
3962 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003963 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003964 case tok::kw_float:
3965 case tok::kw_double:
3966 case tok::kw_bool:
3967 case tok::kw__Bool:
3968 case tok::kw__Decimal32:
3969 case tok::kw__Decimal64:
3970 case tok::kw__Decimal128:
3971 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003972
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003973 // struct-or-union-specifier (C99) or class-specifier (C++)
3974 case tok::kw_class:
3975 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003976 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003977 case tok::kw_union:
3978 // enum-specifier
3979 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003980
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003981 // typedef-name
3982 case tok::annot_typename:
3983 return true;
3984 }
3985}
3986
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003987/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003988/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003989bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003990 switch (Tok.getKind()) {
3991 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003992
Chris Lattner020bab92009-01-04 23:41:41 +00003993 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003994 if (TryAltiVecVectorToken())
3995 return true;
3996 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003997 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003998 // Annotate typenames and C++ scope specifiers. If we get one, just
3999 // recurse to handle whatever we get.
4000 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004001 return true;
4002 if (Tok.is(tok::identifier))
4003 return false;
4004 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00004005
Chris Lattner020bab92009-01-04 23:41:41 +00004006 case tok::coloncolon: // ::foo::bar
4007 if (NextToken().is(tok::kw_new) || // ::new
4008 NextToken().is(tok::kw_delete)) // ::delete
4009 return false;
4010
Chris Lattner020bab92009-01-04 23:41:41 +00004011 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004012 return true;
4013 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004014
Chris Lattnere37e2332006-08-15 04:50:22 +00004015 // GNU attributes support.
4016 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004017 // GNU typeof support.
4018 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004019
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004020 // type-specifiers
4021 case tok::kw_short:
4022 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004023 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004024 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004025 case tok::kw_signed:
4026 case tok::kw_unsigned:
4027 case tok::kw__Complex:
4028 case tok::kw__Imaginary:
4029 case tok::kw_void:
4030 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004031 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004032 case tok::kw_char16_t:
4033 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004034 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004035 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004036 case tok::kw_float:
4037 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004038 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004039 case tok::kw__Bool:
4040 case tok::kw__Decimal32:
4041 case tok::kw__Decimal64:
4042 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004043 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004044
Chris Lattner861a2262008-04-13 18:59:07 +00004045 // struct-or-union-specifier (C99) or class-specifier (C++)
4046 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004047 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004048 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004049 case tok::kw_union:
4050 // enum-specifier
4051 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004052
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004053 // type-qualifier
4054 case tok::kw_const:
4055 case tok::kw_volatile:
4056 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004057
John McCallea0a39e2012-11-14 00:49:39 +00004058 // Debugger support.
4059 case tok::kw___unknown_anytype:
4060
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004061 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004062 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004063 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004064
Chris Lattner409bf7d2008-10-20 00:25:30 +00004065 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4066 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004067 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004068
Steve Naroff44ac7772008-12-25 14:16:32 +00004069 case tok::kw___cdecl:
4070 case tok::kw___stdcall:
4071 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004072 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004073 case tok::kw___w64:
4074 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004075 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004076 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004077 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004078
4079 case tok::kw___private:
4080 case tok::kw___local:
4081 case tok::kw___global:
4082 case tok::kw___constant:
4083 case tok::kw___read_only:
4084 case tok::kw___read_write:
4085 case tok::kw___write_only:
4086
Eli Friedman53339e02009-06-08 23:27:34 +00004087 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004088
Richard Smith8e1ac332013-03-28 01:55:44 +00004089 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004090 case tok::kw__Atomic:
4091 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004092 }
4093}
4094
Chris Lattneracd58a32006-08-06 17:24:14 +00004095/// isDeclarationSpecifier() - Return true if the current token is part of a
4096/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004097///
4098/// \param DisambiguatingWithExpression True to indicate that the purpose of
4099/// this check is to disambiguate between an expression and a declaration.
4100bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004101 switch (Tok.getKind()) {
4102 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004103
Chris Lattner020bab92009-01-04 23:41:41 +00004104 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004105 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004106 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004107 return false;
John Thompson22334602010-02-05 00:12:22 +00004108 if (TryAltiVecVectorToken())
4109 return true;
4110 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004111 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004112 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004113 // Annotate typenames and C++ scope specifiers. If we get one, just
4114 // recurse to handle whatever we get.
4115 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004116 return true;
4117 if (Tok.is(tok::identifier))
4118 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004119
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004120 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004121 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004122 // expression is permitted, then this is probably a class message send
4123 // missing the initial '['. In this case, we won't consider this to be
4124 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004125 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004126 isStartOfObjCClassMessageMissingOpenBracket())
4127 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004128
John McCall1f476a12010-02-26 08:45:28 +00004129 return isDeclarationSpecifier();
4130
Chris Lattner020bab92009-01-04 23:41:41 +00004131 case tok::coloncolon: // ::foo::bar
4132 if (NextToken().is(tok::kw_new) || // ::new
4133 NextToken().is(tok::kw_delete)) // ::delete
4134 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004135
Chris Lattner020bab92009-01-04 23:41:41 +00004136 // Annotate typenames and C++ scope specifiers. If we get one, just
4137 // recurse to handle whatever we get.
4138 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004139 return true;
4140 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004141
Chris Lattneracd58a32006-08-06 17:24:14 +00004142 // storage-class-specifier
4143 case tok::kw_typedef:
4144 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004145 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004146 case tok::kw_static:
4147 case tok::kw_auto:
4148 case tok::kw_register:
4149 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004150 case tok::kw_thread_local:
4151 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004152
Douglas Gregor26701a42011-09-09 02:06:17 +00004153 // Modules
4154 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004155
John McCallea0a39e2012-11-14 00:49:39 +00004156 // Debugger support
4157 case tok::kw___unknown_anytype:
4158
Chris Lattneracd58a32006-08-06 17:24:14 +00004159 // type-specifiers
4160 case tok::kw_short:
4161 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004162 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004163 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004164 case tok::kw_signed:
4165 case tok::kw_unsigned:
4166 case tok::kw__Complex:
4167 case tok::kw__Imaginary:
4168 case tok::kw_void:
4169 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004170 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004171 case tok::kw_char16_t:
4172 case tok::kw_char32_t:
4173
Chris Lattneracd58a32006-08-06 17:24:14 +00004174 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004175 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004176 case tok::kw_float:
4177 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004178 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004179 case tok::kw__Bool:
4180 case tok::kw__Decimal32:
4181 case tok::kw__Decimal64:
4182 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004183 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004184
Chris Lattner861a2262008-04-13 18:59:07 +00004185 // struct-or-union-specifier (C99) or class-specifier (C++)
4186 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004187 case tok::kw_struct:
4188 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004189 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004190 // enum-specifier
4191 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004192
Chris Lattneracd58a32006-08-06 17:24:14 +00004193 // type-qualifier
4194 case tok::kw_const:
4195 case tok::kw_volatile:
4196 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004197
Chris Lattneracd58a32006-08-06 17:24:14 +00004198 // function-specifier
4199 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004200 case tok::kw_virtual:
4201 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004202 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004203
Richard Smith1dba27c2013-01-29 09:02:09 +00004204 // alignment-specifier
4205 case tok::kw__Alignas:
4206
Richard Smithd16fe122012-10-25 00:00:53 +00004207 // friend keyword.
4208 case tok::kw_friend:
4209
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004210 // static_assert-declaration
4211 case tok::kw__Static_assert:
4212
Chris Lattner599e47e2007-08-09 17:01:07 +00004213 // GNU typeof support.
4214 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004215
Chris Lattner599e47e2007-08-09 17:01:07 +00004216 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004217 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004218
Richard Smithd16fe122012-10-25 00:00:53 +00004219 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004220 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004221 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004222
Richard Smith8e1ac332013-03-28 01:55:44 +00004223 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004224 case tok::kw__Atomic:
4225 return true;
4226
Chris Lattner8b2ec162008-07-26 03:38:44 +00004227 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4228 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004229 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004230
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004231 // typedef-name
4232 case tok::annot_typename:
4233 return !DisambiguatingWithExpression ||
4234 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004235
Steve Narofff192fab2009-01-06 19:34:12 +00004236 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004237 case tok::kw___cdecl:
4238 case tok::kw___stdcall:
4239 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004240 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004241 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004242 case tok::kw___sptr:
4243 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004244 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004245 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004246 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004247 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004248 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004249
4250 case tok::kw___private:
4251 case tok::kw___local:
4252 case tok::kw___global:
4253 case tok::kw___constant:
4254 case tok::kw___read_only:
4255 case tok::kw___read_write:
4256 case tok::kw___write_only:
4257
Eli Friedman53339e02009-06-08 23:27:34 +00004258 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004259 }
4260}
4261
Richard Smith446161b2014-03-03 21:12:53 +00004262bool Parser::isConstructorDeclarator(bool IsUnqualified) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004263 TentativeParsingAction TPA(*this);
4264
4265 // Parse the C++ scope specifier.
4266 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004267 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004268 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004269 TPA.Revert();
4270 return false;
4271 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004272
4273 // Parse the constructor name.
4274 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4275 // We already know that we have a constructor name; just consume
4276 // the token.
4277 ConsumeToken();
4278 } else {
4279 TPA.Revert();
4280 return false;
4281 }
4282
Richard Smith43f340f2012-03-27 23:05:05 +00004283 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004284 if (Tok.isNot(tok::l_paren)) {
4285 TPA.Revert();
4286 return false;
4287 }
4288 ConsumeParen();
4289
Richard Smith43f340f2012-03-27 23:05:05 +00004290 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4291 // that we have a constructor.
4292 if (Tok.is(tok::r_paren) ||
4293 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004294 TPA.Revert();
4295 return true;
4296 }
4297
Richard Smithf2163662013-09-06 00:12:20 +00004298 // A C++11 attribute here signals that we have a constructor, and is an
4299 // attribute on the first constructor parameter.
4300 if (getLangOpts().CPlusPlus11 &&
4301 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4302 /*OuterMightBeMessageSend*/ true)) {
4303 TPA.Revert();
4304 return true;
4305 }
4306
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004307 // If we need to, enter the specified scope.
4308 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004309 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004310 DeclScopeObj.EnterDeclaratorScope();
4311
Francois Pichet79f3a872011-01-31 04:54:32 +00004312 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004313 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004314 MaybeParseMicrosoftAttributes(Attrs);
4315
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004316 // Check whether the next token(s) are part of a declaration
4317 // specifier, in which case we have the start of a parameter and,
4318 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004319 bool IsConstructor = false;
4320 if (isDeclarationSpecifier())
4321 IsConstructor = true;
4322 else if (Tok.is(tok::identifier) ||
4323 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4324 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4325 // This might be a parenthesized member name, but is more likely to
4326 // be a constructor declaration with an invalid argument type. Keep
4327 // looking.
4328 if (Tok.is(tok::annot_cxxscope))
4329 ConsumeToken();
4330 ConsumeToken();
4331
4332 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004333 // which must have one of the following syntactic forms (see the
4334 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004335 switch (Tok.getKind()) {
4336 case tok::l_paren:
4337 // C(X ( int));
4338 case tok::l_square:
4339 // C(X [ 5]);
4340 // C(X [ [attribute]]);
4341 case tok::coloncolon:
4342 // C(X :: Y);
4343 // C(X :: *p);
Richard Smithefd009d2012-03-27 00:56:56 +00004344 // Assume this isn't a constructor, rather than assuming it's a
4345 // constructor with an unnamed parameter of an ill-formed type.
4346 break;
4347
Richard Smith446161b2014-03-03 21:12:53 +00004348 case tok::r_paren:
4349 // C(X )
4350 if (NextToken().is(tok::colon) || NextToken().is(tok::kw_try)) {
4351 // Assume these were meant to be constructors:
4352 // C(X) : (the name of a bit-field cannot be parenthesized).
4353 // C(X) try (this is otherwise ill-formed).
4354 IsConstructor = true;
4355 }
4356 if (NextToken().is(tok::semi) || NextToken().is(tok::l_brace)) {
4357 // If we have a constructor name within the class definition,
4358 // assume these were meant to be constructors:
4359 // C(X) {
4360 // C(X) ;
4361 // ... because otherwise we would be declaring a non-static data
4362 // member that is ill-formed because it's of the same type as its
4363 // surrounding class.
4364 //
4365 // FIXME: We can actually do this whether or not the name is qualified,
4366 // because if it is qualified in this context it must be being used as
4367 // a constructor name. However, we do not implement that rule correctly
4368 // currently, so we're somewhat conservative here.
4369 IsConstructor = IsUnqualified;
4370 }
4371 break;
4372
Richard Smithefd009d2012-03-27 00:56:56 +00004373 default:
4374 IsConstructor = true;
4375 break;
4376 }
4377 }
4378
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004379 TPA.Revert();
4380 return IsConstructor;
4381}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004382
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004383/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004384/// type-qualifier-list: [C99 6.7.5]
4385/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004386/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004387/// [ only if VendorAttributesAllowed=true ]
4388/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004389/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004390/// [ only if VendorAttributesAllowed=true ]
4391/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004392/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004393/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004394///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004395void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4396 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004397 bool CXX11AttributesAllowed,
Alp Toker62c5b572013-11-26 01:30:10 +00004398 bool AtomicAllowed,
4399 bool IdentifierRequired) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004400 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004401 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004402 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004403 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004404 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004405 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004406
4407 SourceLocation EndLoc;
4408
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004409 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004410 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004411 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004412 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004413 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004414
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004415 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004416 case tok::code_completion:
4417 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004418 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004419
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004420 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004421 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004422 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004423 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004424 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004425 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004426 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004427 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004428 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004429 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004430 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004431 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004432 case tok::kw__Atomic:
4433 if (!AtomicAllowed)
4434 goto DoneWithTypeQuals;
4435 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4436 getLangOpts());
4437 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004438
4439 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004440 case tok::kw___private:
4441 case tok::kw___global:
4442 case tok::kw___local:
4443 case tok::kw___constant:
4444 case tok::kw___read_only:
4445 case tok::kw___write_only:
4446 case tok::kw___read_write:
Aaron Ballman05d76ea2014-01-14 01:29:54 +00004447 ParseOpenCLQualifiers(DS.getAttributes());
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004448 break;
4449
Aaron Ballman317a77f2013-05-22 23:25:32 +00004450 case tok::kw___uptr:
Alp Toker62c5b572013-11-26 01:30:10 +00004451 // GNU libc headers in C mode use '__uptr' as an identifer which conflicts
4452 // with the MS modifier keyword.
4453 if (VendorAttributesAllowed && !getLangOpts().CPlusPlus &&
Alp Toker47642d22013-12-03 06:13:01 +00004454 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
4455 if (TryKeywordIdentFallback(false))
4456 continue;
Alp Toker62c5b572013-11-26 01:30:10 +00004457 }
4458 case tok::kw___sptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004459 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004460 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004461 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004462 case tok::kw___cdecl:
4463 case tok::kw___stdcall:
4464 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004465 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004466 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004467 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004468 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004469 continue;
4470 }
4471 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004472 case tok::kw___pascal:
4473 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004474 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004475 continue;
4476 }
4477 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004478 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004479 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004480 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004481 continue; // do *not* consume the next token!
4482 }
4483 // otherwise, FALL THROUGH!
4484 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004485 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004486 // If this is not a type-qualifier token, we're done reading type
4487 // qualifiers. First verify that DeclSpec's are consistent.
Erik Verbruggen888d52a2014-01-15 09:15:43 +00004488 DS.Finish(Diags, PP, Actions.getASTContext().getPrintingPolicy());
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004489 if (EndLoc.isValid())
4490 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004491 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004492 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004493
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004494 // If the specifier combination wasn't legal, issue a diagnostic.
4495 if (isInvalid) {
4496 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004497 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004498 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004499 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004500 }
4501}
4502
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004503
4504/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4505///
4506void Parser::ParseDeclarator(Declarator &D) {
4507 /// This implements the 'declarator' production in the C grammar, then checks
4508 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004509 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004510}
4511
Richard Smith0efa75c2012-03-29 01:16:42 +00004512static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4513 if (Kind == tok::star || Kind == tok::caret)
4514 return true;
4515
4516 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4517 if (!Lang.CPlusPlus)
4518 return false;
4519
4520 return Kind == tok::amp || Kind == tok::ampamp;
4521}
4522
Sebastian Redlbd150f42008-11-21 19:14:01 +00004523/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4524/// is parsed by the function passed to it. Pass null, and the direct-declarator
4525/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004526/// ptr-operator production.
4527///
Richard Smith09f76ee2011-10-19 21:33:05 +00004528/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004529/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4530/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004531///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004532/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4533/// [C] pointer[opt] direct-declarator
4534/// [C++] direct-declarator
4535/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004536///
4537/// pointer: [C99 6.7.5]
4538/// '*' type-qualifier-list[opt]
4539/// '*' type-qualifier-list[opt] pointer
4540///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004541/// ptr-operator:
4542/// '*' cv-qualifier-seq[opt]
4543/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004544/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004545/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004546/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004547/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004548void Parser::ParseDeclaratorInternal(Declarator &D,
4549 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004550 if (Diags.hasAllExtensionsSilenced())
4551 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004552
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004553 // C++ member pointers start with a '::' or a nested-name.
4554 // Member pointers get special handling, since there's no place for the
4555 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004556 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004557 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4558 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004559 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4560 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004561 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004562 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004563
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004564 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004565 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004566 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004567 if (D.mayHaveIdentifier())
4568 D.getCXXScopeSpec() = SS;
4569 else
4570 AnnotateScopeToken(SS, true);
4571
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004572 if (DirectDeclParser)
4573 (this->*DirectDeclParser)(D);
4574 return;
4575 }
4576
4577 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004578 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004579 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004580 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004581 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004582
4583 // Recurse to parse whatever is left.
4584 ParseDeclaratorInternal(D, DirectDeclParser);
4585
4586 // Sema will have to catch (syntactically invalid) pointers into global
4587 // scope. It has to catch pointers into namespace scope anyway.
4588 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004589 Loc),
4590 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004591 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004592 return;
4593 }
4594 }
4595
4596 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004597 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004598 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004599 if (DirectDeclParser)
4600 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004601 return;
4602 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004603
Sebastian Redled0f3b02009-03-15 22:02:01 +00004604 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4605 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004606 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004607 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004608
Chris Lattner9eac9312009-03-27 04:18:06 +00004609 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004610 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004611 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004612
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004613 // FIXME: GNU attributes are not allowed here in a new-type-id.
Alp Toker62c5b572013-11-26 01:30:10 +00004614 ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004615 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004616
Bill Wendling3708c182007-05-27 10:15:43 +00004617 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004618 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004619 if (Kind == tok::star)
4620 // Remember that we parsed a pointer type, and remember the type-quals.
4621 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004622 DS.getConstSpecLoc(),
4623 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004624 DS.getRestrictSpecLoc()),
4625 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004626 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004627 else
4628 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004629 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004630 Loc),
4631 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004632 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004633 } else {
4634 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004635 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004636
Sebastian Redl3b27be62009-03-23 00:00:23 +00004637 // Complain about rvalue references in C++03, but then go on and build
4638 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004639 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004640 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004641 diag::warn_cxx98_compat_rvalue_reference :
4642 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004643
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004644 // GNU-style and C++11 attributes are allowed here, as is restrict.
4645 ParseTypeQualifierListOpt(DS);
4646 D.ExtendWithDeclSpec(DS);
4647
Bill Wendling93efb222007-06-02 23:28:54 +00004648 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4649 // cv-qualifiers are introduced through the use of a typedef or of a
4650 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004651 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4652 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4653 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004654 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004655 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4656 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004657 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004658 // 'restrict' is permitted as an extension.
4659 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4660 Diag(DS.getAtomicSpecLoc(),
4661 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004662 }
Bill Wendling3708c182007-05-27 10:15:43 +00004663
4664 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004665 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004666
Douglas Gregor66583c52008-11-03 15:51:28 +00004667 if (D.getNumTypeObjects() > 0) {
4668 // C++ [dcl.ref]p4: There shall be no references to references.
4669 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4670 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004671 if (const IdentifierInfo *II = D.getIdentifier())
4672 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4673 << II;
4674 else
4675 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4676 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004677
Sebastian Redlbd150f42008-11-21 19:14:01 +00004678 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004679 // can go ahead and build the (technically ill-formed)
4680 // declarator: reference collapsing will take care of it.
4681 }
4682 }
4683
Richard Smith8e1ac332013-03-28 01:55:44 +00004684 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004685 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004686 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004687 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004688 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004689 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004690}
4691
Richard Smith0efa75c2012-03-29 01:16:42 +00004692static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4693 SourceLocation EllipsisLoc) {
4694 if (EllipsisLoc.isValid()) {
4695 FixItHint Insertion;
4696 if (!D.getEllipsisLoc().isValid()) {
4697 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4698 D.setEllipsisLoc(EllipsisLoc);
4699 }
4700 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4701 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4702 }
4703}
4704
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004705/// ParseDirectDeclarator
4706/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004707/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004708/// '(' declarator ')'
4709/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004710/// [C90] direct-declarator '[' constant-expression[opt] ']'
4711/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4712/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4713/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4714/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004715/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4716/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004717/// direct-declarator '(' parameter-type-list ')'
4718/// direct-declarator '(' identifier-list[opt] ')'
4719/// [GNU] direct-declarator '(' parameter-forward-declarations
4720/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004721/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4722/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004723/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4724/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4725/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004726/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004727/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004728///
4729/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004730/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004731/// '::'[opt] nested-name-specifier[opt] type-name
4732///
4733/// id-expression: [C++ 5.1]
4734/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004735/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004736///
4737/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004738/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004739/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004740/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004741/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004742/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004743///
Richard Smith1453e312012-03-27 01:42:32 +00004744/// Note, any additional constructs added here may need corresponding changes
4745/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004746void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004747 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004748
David Blaikiebbafb8a2012-03-11 07:00:24 +00004749 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004750 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004751 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004752 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4753 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004754 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004755 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004756 }
4757
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004758 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004759 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004760 // Change the declaration context for name lookup, until this function
4761 // is exited (and the declarator has been parsed).
4762 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004763 }
4764
Douglas Gregor27b4c162010-12-23 22:44:42 +00004765 // C++0x [dcl.fct]p14:
4766 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004767 // of a parameter-declaration-clause without a preceding comma. In
4768 // this case, the ellipsis is parsed as part of the
4769 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004770 // parameter pack that has not been expanded; otherwise, it is parsed
4771 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004772 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004773 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004774 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004775 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004776 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004777 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004778 !Actions.containsUnexpandedParameterPacks(D))) {
4779 SourceLocation EllipsisLoc = ConsumeToken();
4780 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4781 // The ellipsis was put in the wrong place. Recover, and explain to
4782 // the user what they should have done.
4783 ParseDeclarator(D);
4784 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4785 return;
4786 } else
4787 D.setEllipsisLoc(EllipsisLoc);
4788
4789 // The ellipsis can't be followed by a parenthesized declarator. We
4790 // check for that in ParseParenDeclarator, after we have disambiguated
4791 // the l_paren token.
4792 }
4793
Douglas Gregor7861a802009-11-03 01:35:08 +00004794 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4795 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4796 // We found something that indicates the start of an unqualified-id.
4797 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004798 bool AllowConstructorName;
4799 if (D.getDeclSpec().hasTypeSpecifier())
4800 AllowConstructorName = false;
4801 else if (D.getCXXScopeSpec().isSet())
4802 AllowConstructorName =
4803 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004804 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004805 else
4806 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4807
Abramo Bagnara7945c982012-01-27 09:46:47 +00004808 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004809 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4810 /*EnteringContext=*/true,
4811 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004812 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004813 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004814 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004815 D.getName()) ||
4816 // Once we're past the identifier, if the scope was bad, mark the
4817 // whole declarator bad.
4818 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004819 D.SetIdentifier(0, Tok.getLocation());
4820 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004821 } else {
4822 // Parsed the unqualified-id; update range information and move along.
4823 if (D.getSourceRange().getBegin().isInvalid())
4824 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4825 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004826 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004827 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004828 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004829 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004830 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004831 "There's a C++-specific check for tok::identifier above");
4832 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4833 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4834 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004835 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004836 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004837 // A virt-specifier isn't treated as an identifier if it appears after a
4838 // trailing-return-type.
4839 if (D.getContext() != Declarator::TrailingReturnContext ||
4840 !isCXX11VirtSpecifier(Tok)) {
4841 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4842 << FixItHint::CreateRemoval(Tok.getLocation());
4843 D.SetIdentifier(0, Tok.getLocation());
4844 ConsumeToken();
4845 goto PastIdentifier;
4846 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004847 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004848
Douglas Gregor7861a802009-11-03 01:35:08 +00004849 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004850 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004851 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004852 // Example: 'char (*X)' or 'int (*XX)(void)'
4853 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004854
4855 // If the declarator was parenthesized, we entered the declarator
4856 // scope when parsing the parenthesized declarator, then exited
4857 // the scope already. Re-enter the scope, if we need to.
4858 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004859 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004860 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004861 if (!D.isInvalidType() &&
4862 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004863 // Change the declaration context for name lookup, until this function
4864 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004865 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004866 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004867 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004868 // This could be something simple like "int" (in which case the declarator
4869 // portion is empty), if an abstract-declarator is allowed.
4870 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004871
4872 // The grammar for abstract-pack-declarator does not allow grouping parens.
4873 // FIXME: Revisit this once core issue 1488 is resolved.
4874 if (D.hasEllipsis() && D.hasGroupingParens())
4875 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4876 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004877 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004878 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004879 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004880 if (D.getContext() == Declarator::MemberContext)
4881 Diag(Tok, diag::err_expected_member_name_or_semi)
4882 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004883 else if (getLangOpts().CPlusPlus) {
4884 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4885 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004886 else {
4887 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4888 if (Tok.isAtStartOfLine() && Loc.isValid())
4889 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4890 << getLangOpts().CPlusPlus;
4891 else
4892 Diag(Tok, diag::err_expected_unqualified_id)
4893 << getLangOpts().CPlusPlus;
4894 }
Richard Trieu9c672672013-01-26 02:31:38 +00004895 } else
Alp Tokerec543272013-12-24 09:48:30 +00004896 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_paren;
Chris Lattnereec40f92006-08-06 21:55:29 +00004897 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004898 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004899 }
Mike Stump11289f42009-09-09 15:08:12 +00004900
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004901 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004902 assert(D.isPastIdentifier() &&
4903 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004904
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004905 // Don't parse attributes unless we have parsed an unparenthesized name.
4906 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004907 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004908
Chris Lattneracd58a32006-08-06 17:24:14 +00004909 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004910 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004911 // Enter function-declaration scope, limiting any declarators to the
4912 // function prototype scope, including parameter declarators.
4913 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004914 Scope::FunctionPrototypeScope|Scope::DeclScope|
4915 (D.isFunctionDeclaratorAFunctionDeclaration()
4916 ? Scope::FunctionDeclarationScope : 0));
4917
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004918 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4919 // In such a case, check if we actually have a function declarator; if it
4920 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004921 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004922 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4923 // The name of the declarator, if any, is tentatively declared within
4924 // a possible direct initializer.
4925 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4926 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4927 TentativelyDeclaredIdentifiers.pop_back();
4928 if (!IsFunctionDecl)
4929 break;
4930 }
John McCall084e83d2011-03-24 11:26:52 +00004931 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004932 BalancedDelimiterTracker T(*this, tok::l_paren);
4933 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004934 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004935 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004936 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004937 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004938 } else {
4939 break;
4940 }
4941 }
Chad Rosierc1183952012-06-26 22:30:43 +00004942}
Chris Lattneracd58a32006-08-06 17:24:14 +00004943
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004944/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4945/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004946/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004947/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4948///
4949/// direct-declarator:
4950/// '(' declarator ')'
4951/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004952/// direct-declarator '(' parameter-type-list ')'
4953/// direct-declarator '(' identifier-list[opt] ')'
4954/// [GNU] direct-declarator '(' parameter-forward-declarations
4955/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004956///
4957void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004958 BalancedDelimiterTracker T(*this, tok::l_paren);
4959 T.consumeOpen();
4960
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004961 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004962
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004963 // Eat any attributes before we look at whether this is a grouping or function
4964 // declarator paren. If this is a grouping paren, the attribute applies to
4965 // the type being built up, for example:
4966 // int (__attribute__(()) *x)(long y)
4967 // If this ends up not being a grouping paren, the attribute applies to the
4968 // first argument, for example:
4969 // int (__attribute__(()) int x)
4970 // In either case, we need to eat any attributes to be able to determine what
4971 // sort of paren this is.
4972 //
John McCall084e83d2011-03-24 11:26:52 +00004973 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004974 bool RequiresArg = false;
4975 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004976 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004977
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004978 // We require that the argument list (if this is a non-grouping paren) be
4979 // present even if the attribute list was empty.
4980 RequiresArg = true;
4981 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004982
Steve Naroff44ac7772008-12-25 14:16:32 +00004983 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004984 ParseMicrosoftTypeAttributes(attrs);
4985
Dawn Perchik335e16b2010-09-03 01:29:35 +00004986 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004987 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004988 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004989
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004990 // If we haven't past the identifier yet (or where the identifier would be
4991 // stored, if this is an abstract declarator), then this is probably just
4992 // grouping parens. However, if this could be an abstract-declarator, then
4993 // this could also be the start of function arguments (consider 'void()').
4994 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004995
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004996 if (!D.mayOmitIdentifier()) {
4997 // If this can't be an abstract-declarator, this *must* be a grouping
4998 // paren, because we haven't seen the identifier yet.
4999 isGrouping = true;
5000 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00005001 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
5002 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00005003 isDeclarationSpecifier() || // 'int(int)' is a function.
5004 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005005 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
5006 // considered to be a type, not a K&R identifier-list.
5007 isGrouping = false;
5008 } else {
5009 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
5010 isGrouping = true;
5011 }
Mike Stump11289f42009-09-09 15:08:12 +00005012
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005013 // If this is a grouping paren, handle:
5014 // direct-declarator: '(' declarator ')'
5015 // direct-declarator: '(' attributes declarator ')'
5016 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005017 SourceLocation EllipsisLoc = D.getEllipsisLoc();
5018 D.setEllipsisLoc(SourceLocation());
5019
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005020 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005021 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00005022 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005023 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005024 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00005025 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005026 T.getCloseLocation()),
5027 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005028
5029 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00005030
5031 // An ellipsis cannot be placed outside parentheses.
5032 if (EllipsisLoc.isValid())
5033 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
5034
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005035 return;
5036 }
Mike Stump11289f42009-09-09 15:08:12 +00005037
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005038 // Okay, if this wasn't a grouping paren, it must be the start of a function
5039 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005040 // identifier (and remember where it would have been), then call into
5041 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005042 D.SetIdentifier(0, Tok.getLocation());
5043
David Blaikie15a430a2011-12-04 05:04:18 +00005044 // Enter function-declaration scope, limiting any declarators to the
5045 // function prototype scope, including parameter declarators.
5046 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005047 Scope::FunctionPrototypeScope | Scope::DeclScope |
5048 (D.isFunctionDeclaratorAFunctionDeclaration()
5049 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00005050 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00005051 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005052}
5053
5054/// ParseFunctionDeclarator - We are after the identifier and have parsed the
5055/// declarator D up to a paren, which indicates that we are parsing function
5056/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00005057///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005058/// If FirstArgAttrs is non-null, then the caller parsed those arguments
5059/// immediately after the open paren - they should be considered to be the
5060/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005061///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005062/// If RequiresArg is true, then the first argument of the function is required
5063/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005064///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005065/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5066/// (C++11) ref-qualifier[opt], exception-specification[opt],
5067/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5068///
5069/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00005070/// dynamic-exception-specification
5071/// noexcept-specification
5072///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005073void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005074 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005075 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00005076 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005077 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00005078 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00005079 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00005080 // lparen is already consumed!
5081 assert(D.isPastIdentifier() && "Should not call before identifier!");
5082
5083 // This should be true when the function has typed arguments.
5084 // Otherwise, it is treated as a K&R-style function.
5085 bool HasProto = false;
5086 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005087 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005088 // Remember where we see an ellipsis, if any.
5089 SourceLocation EllipsisLoc;
5090
5091 DeclSpec DS(AttrFactory);
5092 bool RefQualifierIsLValueRef = true;
5093 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00005094 SourceLocation ConstQualifierLoc;
5095 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005096 ExceptionSpecificationType ESpecType = EST_None;
5097 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005098 SmallVector<ParsedType, 2> DynamicExceptions;
5099 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005100 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005101 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00005102 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005103
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005104 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5105 EndLoc is the end location for the function declarator.
5106 They differ for trailing return types. */
5107 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005108 SourceLocation LParenLoc, RParenLoc;
5109 LParenLoc = Tracker.getOpenLocation();
5110 StartLoc = LParenLoc;
5111
Douglas Gregor9e66af42011-07-05 16:44:18 +00005112 if (isFunctionDeclaratorIdentifierList()) {
5113 if (RequiresArg)
5114 Diag(Tok, diag::err_argument_required_after_attribute);
5115
5116 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5117
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005118 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005119 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005120 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005121 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005122 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005123 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005124 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5125 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005126 else if (RequiresArg)
5127 Diag(Tok, diag::err_argument_required_after_attribute);
5128
David Blaikiebbafb8a2012-03-11 07:00:24 +00005129 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005130
5131 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005132 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005133 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005134 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005135 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005136
David Blaikiebbafb8a2012-03-11 07:00:24 +00005137 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005138 // FIXME: Accept these components in any order, and produce fixits to
5139 // correct the order if the user gets it wrong. Ideally we should deal
5140 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005141
5142 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005143 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5144 /*CXX11AttributesAllowed*/ false,
5145 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005146 if (!DS.getSourceRange().getEnd().isInvalid()) {
5147 EndLoc = DS.getSourceRange().getEnd();
5148 ConstQualifierLoc = DS.getConstSpecLoc();
5149 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5150 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005151
5152 // Parse ref-qualifier[opt].
5153 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005154 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005155 diag::warn_cxx98_compat_ref_qualifier :
5156 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005157
Douglas Gregor9e66af42011-07-05 16:44:18 +00005158 RefQualifierIsLValueRef = Tok.is(tok::amp);
5159 RefQualifierLoc = ConsumeToken();
5160 EndLoc = RefQualifierLoc;
5161 }
5162
Douglas Gregor3024f072012-04-16 07:05:22 +00005163 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005164 // If a declaration declares a member function or member function
5165 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005166 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005167 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005168 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005169 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005170 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005171 getLangOpts().CPlusPlus11 &&
Richard Smith990a6922014-01-17 21:01:18 +00005172 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005173 (D.getContext() == Declarator::MemberContext
5174 ? !D.getDeclSpec().isFriendSpecified()
5175 : D.getContext() == Declarator::FileContext &&
5176 D.getCXXScopeSpec().isValid() &&
5177 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005178 Sema::CXXThisScopeRAII ThisScope(Actions,
5179 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005180 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005181 (D.getDeclSpec().isConstexprSpecified() &&
5182 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005183 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005184 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005185
Douglas Gregor9e66af42011-07-05 16:44:18 +00005186 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005187 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005188 DynamicExceptions,
5189 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005190 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005191 if (ESpecType != EST_None)
5192 EndLoc = ESpecRange.getEnd();
5193
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005194 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5195 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005196 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005197
Douglas Gregor9e66af42011-07-05 16:44:18 +00005198 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005199 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005200 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005201 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005202 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5203 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005204 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005205 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005206 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005207 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005208 }
5209 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005210 }
5211
5212 // Remember that we parsed a function type, and remember the attributes.
5213 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005214 IsAmbiguous,
5215 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005216 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005217 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005218 DS.getTypeQualifiers(),
5219 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005220 RefQualifierLoc, ConstQualifierLoc,
5221 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005222 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005223 ESpecType, ESpecRange.getBegin(),
5224 DynamicExceptions.data(),
5225 DynamicExceptionRanges.data(),
5226 DynamicExceptions.size(),
5227 NoexceptExpr.isUsable() ?
5228 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005229 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005230 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005231 FnAttrs, EndLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005232}
5233
5234/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5235/// identifier list form for a K&R-style function: void foo(a,b,c)
5236///
5237/// Note that identifier-lists are only allowed for normal declarators, not for
5238/// abstract-declarators.
5239bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005240 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005241 && Tok.is(tok::identifier)
5242 && !TryAltiVecVectorToken()
5243 // K&R identifier lists can't have typedefs as identifiers, per C99
5244 // 6.7.5.3p11.
5245 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5246 // Identifier lists follow a really simple grammar: the identifiers can
5247 // be followed *only* by a ", identifier" or ")". However, K&R
5248 // identifier lists are really rare in the brave new modern world, and
5249 // it is very common for someone to typo a type in a non-K&R style
5250 // list. If we are presented with something like: "void foo(intptr x,
5251 // float y)", we don't want to start parsing the function declarator as
5252 // though it is a K&R style declarator just because intptr is an
5253 // invalid type.
5254 //
5255 // To handle this, we check to see if the token after the first
5256 // identifier is a "," or ")". Only then do we parse it as an
5257 // identifier list.
5258 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5259}
5260
5261/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5262/// we found a K&R-style identifier list instead of a typed parameter list.
5263///
5264/// After returning, ParamInfo will hold the parsed parameters.
5265///
5266/// identifier-list: [C99 6.7.5]
5267/// identifier
5268/// identifier-list ',' identifier
5269///
5270void Parser::ParseFunctionDeclaratorIdentifierList(
5271 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005272 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005273 // If there was no identifier specified for the declarator, either we are in
5274 // an abstract-declarator, or we are in a parameter declarator which was found
5275 // to be abstract. In abstract-declarators, identifier lists are not valid:
5276 // diagnose this.
5277 if (!D.getIdentifier())
5278 Diag(Tok, diag::ext_ident_list_in_param);
5279
5280 // Maintain an efficient lookup of params we have seen so far.
5281 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5282
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005283 do {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005284 // If this isn't an identifier, report the error and skip until ')'.
5285 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00005286 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00005287 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005288 // Forget we parsed anything.
5289 ParamInfo.clear();
5290 return;
5291 }
5292
5293 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5294
5295 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5296 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5297 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5298
5299 // Verify that the argument identifier has not already been mentioned.
5300 if (!ParamsSoFar.insert(ParmII)) {
5301 Diag(Tok, diag::err_param_redefinition) << ParmII;
5302 } else {
5303 // Remember this identifier in ParamInfo.
5304 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5305 Tok.getLocation(),
5306 0));
5307 }
5308
5309 // Eat the identifier.
5310 ConsumeToken();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005311 // The list continues if we see a comma.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005312 } while (TryConsumeToken(tok::comma));
Douglas Gregor9e66af42011-07-05 16:44:18 +00005313}
5314
5315/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5316/// after the opening parenthesis. This function will not parse a K&R-style
5317/// identifier list.
5318///
Richard Smith2620cd92012-04-11 04:01:28 +00005319/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5320/// caller parsed those arguments immediately after the open paren - they should
5321/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005322///
5323/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5324/// be the location of the ellipsis, if any was parsed.
5325///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005326/// parameter-type-list: [C99 6.7.5]
5327/// parameter-list
5328/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005329/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005330///
5331/// parameter-list: [C99 6.7.5]
5332/// parameter-declaration
5333/// parameter-list ',' parameter-declaration
5334///
5335/// parameter-declaration: [C99 6.7.5]
5336/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005337/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005338/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005339/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005340/// declaration-specifiers abstract-declarator[opt]
5341/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005342/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005343/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005344/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005345///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005346void Parser::ParseParameterDeclarationClause(
5347 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005348 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005349 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005350 SourceLocation &EllipsisLoc) {
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005351 do {
5352 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5353 // before deciding this was a parameter-declaration-clause.
5354 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Chris Lattner371ed4e2008-04-06 06:57:35 +00005355 break;
Mike Stump11289f42009-09-09 15:08:12 +00005356
Chris Lattner371ed4e2008-04-06 06:57:35 +00005357 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005358 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005359 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005360
Richard Smith2620cd92012-04-11 04:01:28 +00005361 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005362 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005363
John McCall53fa7142010-12-24 02:08:15 +00005364 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005365 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005366
5367 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005368
5369 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005370 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005371 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005372 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5373 // too much hassle.
5374 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005375
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005376 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005377
Faisal Vali2b391ab2013-09-26 19:54:12 +00005378
5379 // Parse the declarator. This is "PrototypeContext" or
5380 // "LambdaExprParameterContext", because we must accept either
5381 // 'declarator' or 'abstract-declarator' here.
5382 Declarator ParmDeclarator(DS,
5383 D.getContext() == Declarator::LambdaExprContext ?
5384 Declarator::LambdaExprParameterContext :
5385 Declarator::PrototypeContext);
5386 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005387
5388 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005389 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005390
Chris Lattner371ed4e2008-04-06 06:57:35 +00005391 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005392 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005393
Douglas Gregor4d87df52008-12-16 21:30:33 +00005394 // DefArgToks is used when the parsing of default arguments needs
5395 // to be delayed.
5396 CachedTokens *DefArgToks = 0;
5397
Chris Lattner371ed4e2008-04-06 06:57:35 +00005398 // If no parameter was specified, verify that *something* was specified,
5399 // otherwise we have a missing type and identifier.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005400 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5401 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005402 // Completely missing, emit error.
5403 Diag(DSStart, diag::err_missing_param);
5404 } else {
5405 // Otherwise, we have something. Add it and let semantic analysis try
5406 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005407
Chris Lattner371ed4e2008-04-06 06:57:35 +00005408 // Inform the actions module about the parameter declarator, so it gets
5409 // added to the current scope.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005410 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5411 ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005412 // Parse the default argument, if any. We parse the default
5413 // arguments in all dialects; the semantic analysis in
5414 // ActOnParamDefaultArgument will reject the default argument in
5415 // C.
5416 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005417 SourceLocation EqualLoc = Tok.getLocation();
5418
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005419 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005420 if (D.getContext() == Declarator::MemberContext) {
5421 // If we're inside a class definition, cache the tokens
5422 // corresponding to the default argument. We'll actually parse
5423 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005424 // FIXME: Can we use a smart pointer for Toks?
5425 DefArgToks = new CachedTokens;
5426
Richard Smith1fff95c2013-09-12 23:28:08 +00005427 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005428 delete DefArgToks;
5429 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005430 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005431 } else {
5432 // Mark the end of the default argument so that we know when to
5433 // stop when we parse it later on.
5434 Token DefArgEnd;
5435 DefArgEnd.startToken();
5436 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5437 DefArgEnd.setLocation(Tok.getLocation());
5438 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005439 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005440 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005441 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005442 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005443 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005444 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005445
Chad Rosierc1183952012-06-26 22:30:43 +00005446 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005447 // used.
5448 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005449 Sema::PotentiallyEvaluatedIfUsed,
5450 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005451
Sebastian Redldb63af22012-03-14 15:54:00 +00005452 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005453 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005454 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005455 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005456 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005457 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005458 if (DefArgResult.isInvalid()) {
5459 Actions.ActOnParamDefaultArgumentError(Param);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005460 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00005461 } else {
5462 // Inform the actions module about the default argument
5463 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005464 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005465 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005466 }
5467 }
Mike Stump11289f42009-09-09 15:08:12 +00005468
5469 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005470 ParmDeclarator.getIdentifierLoc(),
5471 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005472 }
5473
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005474 if (TryConsumeToken(tok::ellipsis, EllipsisLoc) &&
5475 !getLangOpts().CPlusPlus) {
5476 // We have ellipsis without a preceding ',', which is ill-formed
5477 // in C. Complain and provide the fix.
5478 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
5479 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005480 break;
5481 }
Mike Stump11289f42009-09-09 15:08:12 +00005482
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005483 // If the next token is a comma, consume it and keep reading arguments.
5484 } while (TryConsumeToken(tok::comma));
Chris Lattner6c940e62008-04-06 06:34:08 +00005485}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005486
Chris Lattnere8074e62006-08-06 18:30:15 +00005487/// [C90] direct-declarator '[' constant-expression[opt] ']'
5488/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5489/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5490/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5491/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005492/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5493/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005494void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005495 if (CheckProhibitedCXX11Attribute())
5496 return;
5497
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005498 BalancedDelimiterTracker T(*this, tok::l_square);
5499 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005500
Chris Lattner84a11622008-12-18 07:27:21 +00005501 // C array syntax has many features, but by-far the most common is [] and [4].
5502 // This code does a fast path to handle some of the most obvious cases.
5503 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005504 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005505 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005506 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005507
Chris Lattner84a11622008-12-18 07:27:21 +00005508 // Remember that we parsed the empty array type.
John McCall084e83d2011-03-24 11:26:52 +00005509 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005510 T.getOpenLocation(),
5511 T.getCloseLocation()),
5512 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005513 return;
5514 } else if (Tok.getKind() == tok::numeric_constant &&
5515 GetLookAheadToken(1).is(tok::r_square)) {
5516 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005517 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005518 ConsumeToken();
5519
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005520 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005521 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005522 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005523
Chris Lattner84a11622008-12-18 07:27:21 +00005524 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005525 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005526 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005527 T.getOpenLocation(),
5528 T.getCloseLocation()),
5529 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005530 return;
5531 }
Mike Stump11289f42009-09-09 15:08:12 +00005532
Chris Lattnere8074e62006-08-06 18:30:15 +00005533 // If valid, this location is the position where we read the 'static' keyword.
5534 SourceLocation StaticLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005535 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005536
Chris Lattnere8074e62006-08-06 18:30:15 +00005537 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005538 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005539 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005540 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005541
Chris Lattnere8074e62006-08-06 18:30:15 +00005542 // If we haven't already read 'static', check to see if there is one after the
5543 // type-qualifier-list.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005544 if (!StaticLoc.isValid())
5545 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005546
Chris Lattnere8074e62006-08-06 18:30:15 +00005547 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005548 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005549 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005550
Chris Lattner521ff2b2008-04-06 05:26:30 +00005551 // Handle the case where we have '[*]' as the array size. However, a leading
5552 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005553 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005554 // infrequent, use of lookahead is not costly here.
5555 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005556 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005557
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005558 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005559 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005560 StaticLoc = SourceLocation(); // Drop the static.
5561 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005562 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005563 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005564 // Note, in C89, this production uses the constant-expr production instead
5565 // of assignment-expr. The only difference is that assignment-expr allows
5566 // things like '=' and '*='. Sema rejects these in C89 mode because they
5567 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005568
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005569 // Parse the constant-expression or assignment-expression now (depending
5570 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005571 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005572 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005573 } else {
5574 EnterExpressionEvaluationContext Unevaluated(Actions,
5575 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005576 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005577 }
Chris Lattner62591722006-08-12 18:40:58 +00005578 }
Mike Stump11289f42009-09-09 15:08:12 +00005579
Chris Lattner62591722006-08-12 18:40:58 +00005580 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005581 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005582 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005583 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00005584 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00005585 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005586 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005587
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005588 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005589
John McCall084e83d2011-03-24 11:26:52 +00005590 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005591 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005592
Chris Lattner84a11622008-12-18 07:27:21 +00005593 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005594 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005595 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005596 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005597 T.getOpenLocation(),
5598 T.getCloseLocation()),
5599 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005600}
5601
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005602/// [GNU] typeof-specifier:
5603/// typeof ( expressions )
5604/// typeof ( type-name )
5605/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005606///
5607void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005608 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005609 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005610 SourceLocation StartLoc = ConsumeToken();
5611
John McCalle8595032010-01-13 20:03:27 +00005612 const bool hasParens = Tok.is(tok::l_paren);
5613
Eli Friedman15681d62012-09-26 04:34:21 +00005614 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5615 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005616
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005617 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005618 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005619 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005620 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5621 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005622 if (hasParens)
5623 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005624
5625 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005626 // FIXME: Not accurate, the range gets one token more than it should.
5627 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005628 else
5629 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005630
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005631 if (isCastExpr) {
5632 if (!CastTy) {
5633 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005634 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005635 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005636
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005637 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005638 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005639 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5640 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005641 DiagID, CastTy,
5642 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00005643 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005644 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005645 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005646
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005647 // If we get here, the operand to the typeof was an expresion.
5648 if (Operand.isInvalid()) {
5649 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005650 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005651 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005652
Eli Friedmane0afc982012-01-21 01:01:51 +00005653 // We might need to transform the operand if it is potentially evaluated.
5654 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5655 if (Operand.isInvalid()) {
5656 DS.SetTypeSpecError();
5657 return;
5658 }
5659
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005660 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005661 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005662 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5663 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005664 DiagID, Operand.get(),
5665 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00005666 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005667}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005668
Benjamin Kramere56f3932011-12-23 17:00:35 +00005669/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005670/// _Atomic ( type-name )
5671///
5672void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005673 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5674 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005675
5676 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005677 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005678 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005679 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005680
5681 TypeResult Result = ParseTypeName();
5682 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00005683 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00005684 return;
5685 }
5686
5687 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005688 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005689
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005690 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005691 return;
5692
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005693 DS.setTypeofParensRange(T.getRange());
5694 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005695
5696 const char *PrevSpec = 0;
5697 unsigned DiagID;
5698 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005699 DiagID, Result.release(),
5700 Actions.getASTContext().getPrintingPolicy()))
Eli Friedman0dfb8892011-10-06 23:00:33 +00005701 Diag(StartLoc, DiagID) << PrevSpec;
5702}
5703
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005704
5705/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5706/// from TryAltiVecVectorToken.
5707bool Parser::TryAltiVecVectorTokenOutOfLine() {
5708 Token Next = NextToken();
5709 switch (Next.getKind()) {
5710 default: return false;
5711 case tok::kw_short:
5712 case tok::kw_long:
5713 case tok::kw_signed:
5714 case tok::kw_unsigned:
5715 case tok::kw_void:
5716 case tok::kw_char:
5717 case tok::kw_int:
5718 case tok::kw_float:
5719 case tok::kw_double:
5720 case tok::kw_bool:
5721 case tok::kw___pixel:
5722 Tok.setKind(tok::kw___vector);
5723 return true;
5724 case tok::identifier:
5725 if (Next.getIdentifierInfo() == Ident_pixel) {
5726 Tok.setKind(tok::kw___vector);
5727 return true;
5728 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005729 if (Next.getIdentifierInfo() == Ident_bool) {
5730 Tok.setKind(tok::kw___vector);
5731 return true;
5732 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005733 return false;
5734 }
5735}
5736
5737bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5738 const char *&PrevSpec, unsigned &DiagID,
5739 bool &isInvalid) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005740 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005741 if (Tok.getIdentifierInfo() == Ident_vector) {
5742 Token Next = NextToken();
5743 switch (Next.getKind()) {
5744 case tok::kw_short:
5745 case tok::kw_long:
5746 case tok::kw_signed:
5747 case tok::kw_unsigned:
5748 case tok::kw_void:
5749 case tok::kw_char:
5750 case tok::kw_int:
5751 case tok::kw_float:
5752 case tok::kw_double:
5753 case tok::kw_bool:
5754 case tok::kw___pixel:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005755 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005756 return true;
5757 case tok::identifier:
5758 if (Next.getIdentifierInfo() == Ident_pixel) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005759 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005760 return true;
5761 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005762 if (Next.getIdentifierInfo() == Ident_bool) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005763 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
Bill Schmidt99a084b2013-07-03 20:54:09 +00005764 return true;
5765 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005766 break;
5767 default:
5768 break;
5769 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005770 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005771 DS.isTypeAltiVecVector()) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005772 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005773 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005774 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5775 DS.isTypeAltiVecVector()) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005776 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
Bill Schmidt99a084b2013-07-03 20:54:09 +00005777 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005778 }
5779 return false;
5780}