blob: e007998be389d938619b763a9d0f6eec7ec34c61 [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
Michael Han23214e52012-10-03 01:56:22 +0000262/// Parse the arguments to a parameterized GNU attribute or
263/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000264void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
265 SourceLocation AttrNameLoc,
266 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000267 SourceLocation *EndLoc,
268 IdentifierInfo *ScopeName,
269 SourceLocation ScopeLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000270 AttributeList::Syntax Syntax,
271 Declarator *D) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000272
273 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
274
Richard Smith66e71682013-10-24 01:07:54 +0000275 AttributeList::Kind AttrKind =
Richard Smithb1f9a282013-10-31 01:56:18 +0000276 AttributeList::getKind(AttrName, ScopeName, Syntax);
Richard Smith66e71682013-10-24 01:07:54 +0000277
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000278 // Availability attributes have their own grammar.
Richard Smithb1f9a282013-10-31 01:56:18 +0000279 // FIXME: All these cases fail to pass in the syntax and scope, and might be
280 // written as C++11 gnu:: attributes.
Richard Smith66e71682013-10-24 01:07:54 +0000281 if (AttrKind == AttributeList::AT_Availability) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000282 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
283 return;
284 }
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000285
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000286 if (AttrKind == AttributeList::AT_ObjCBridgeRelated) {
287 ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
288 return;
289 }
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000290
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000291 // Type safety attributes have their own grammar.
Richard Smith66e71682013-10-24 01:07:54 +0000292 if (AttrKind == AttributeList::AT_TypeTagForDatatype) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000293 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
294 return;
295 }
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000296
Aaron Ballman4768b312013-11-04 12:55:56 +0000297 // Some attributes expect solely a type parameter.
298 if (attributeIsTypeArgAttr(*AttrName)) {
Richard Smithb1f9a282013-10-31 01:56:18 +0000299 ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc);
300 return;
301 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000302
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000303 // These may refer to the function arguments, but need to be parsed early to
304 // participate in determining whether it's a redeclaration.
305 llvm::OwningPtr<ParseScope> PrototypeScope;
306 if (AttrName->isStr("enable_if") && D && D->isFunctionDeclarator()) {
307 DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo();
308 PrototypeScope.reset(new ParseScope(this, Scope::FunctionPrototypeScope |
309 Scope::FunctionDeclarationScope |
310 Scope::DeclScope));
Alp Tokerc5350722014-02-26 22:27:52 +0000311 for (unsigned i = 0; i != FTI.NumParams; ++i) {
312 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param);
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000313 Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param);
314 }
315 }
316
Richard Smith66e71682013-10-24 01:07:54 +0000317 // Ignore the left paren location for now.
318 ConsumeParen();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000319
Aaron Ballman00e99962013-08-31 01:11:41 +0000320 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000321
Richard Smithb1f9a282013-10-31 01:56:18 +0000322 if (Tok.is(tok::identifier)) {
Richard Smith66e71682013-10-24 01:07:54 +0000323 // If this attribute wants an 'identifier' argument, make it so.
Richard Smithb1f9a282013-10-31 01:56:18 +0000324 bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName);
Richard Smith66e71682013-10-24 01:07:54 +0000325
326 // If we don't know how to parse this attribute, but this is the only
327 // token in this argument, assume it's meant to be an identifier.
Aaron Ballman66037472013-12-04 15:32:26 +0000328 if (AttrKind == AttributeList::UnknownAttribute ||
329 AttrKind == AttributeList::IgnoredAttribute) {
Richard Smith66e71682013-10-24 01:07:54 +0000330 const Token &Next = NextToken();
Richard Smithb1f9a282013-10-31 01:56:18 +0000331 IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma);
Richard Smith66e71682013-10-24 01:07:54 +0000332 }
Richard Smithb12bf692011-10-17 21:20:17 +0000333
Richard Smithb1f9a282013-10-31 01:56:18 +0000334 if (IsIdentifierArg)
335 ArgExprs.push_back(ParseIdentifierLoc());
Richard Smithb12bf692011-10-17 21:20:17 +0000336 }
337
Richard Smithb1f9a282013-10-31 01:56:18 +0000338 if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
Richard Smithb12bf692011-10-17 21:20:17 +0000339 // Eat the comma.
Aaron Ballman00e99962013-08-31 01:11:41 +0000340 if (!ArgExprs.empty())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000341 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000342
Richard Smithb12bf692011-10-17 21:20:17 +0000343 // Parse the non-empty comma-separated list of expressions.
Alp Toker8fbec672013-12-17 23:29:36 +0000344 do {
Aaron Ballman7c1fcf82014-01-09 20:12:12 +0000345 OwningPtr<EnterExpressionEvaluationContext> Unevaluated;
346 if (attributeParsedArgsUnevaluated(*AttrName))
347 Unevaluated.reset(new EnterExpressionEvaluationContext(Actions,
348 Sema::Unevaluated));
349
Richard Smithb12bf692011-10-17 21:20:17 +0000350 ExprResult ArgExpr(ParseAssignmentExpression());
351 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000352 SkipUntil(tok::r_paren, StopAtSemi);
Richard Smithb12bf692011-10-17 21:20:17 +0000353 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000354 }
Richard Smithb12bf692011-10-17 21:20:17 +0000355 ArgExprs.push_back(ArgExpr.release());
Alp Toker8fbec672013-12-17 23:29:36 +0000356 // Eat the comma, move to the next argument
357 } while (TryConsumeToken(tok::comma));
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000358 }
Richard Smithb12bf692011-10-17 21:20:17 +0000359
360 SourceLocation RParen = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000361 if (!ExpectAndConsume(tok::r_paren)) {
Michael Han360d2252012-10-04 16:42:52 +0000362 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithb1f9a282013-10-31 01:56:18 +0000363 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
364 ArgExprs.data(), ArgExprs.size(), Syntax);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000365 }
Aaron Ballman7c1fcf82014-01-09 20:12:12 +0000366
367 if (EndLoc)
368 *EndLoc = RParen;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000369}
370
Chad Rosierc1183952012-06-26 22:30:43 +0000371/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000372/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000373void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000374 SourceLocation AttrNameLoc,
375 ParsedAttributes &Attrs)
376{
377 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000378 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000379 AttrName->getNameStart(), tok::r_paren))
380 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000381
Aaron Ballman478faed2012-06-19 22:09:27 +0000382 ExprResult ArgExpr(ParseConstantExpression());
383 if (ArgExpr.isInvalid()) {
384 T.skipToEnd();
385 return;
386 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000387 ArgsUnion ExprList = ArgExpr.take();
388 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
389 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000390
391 T.consumeClose();
392}
393
Chad Rosierc1183952012-06-26 22:30:43 +0000394/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000395/// arguments.
396bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
397 return llvm::StringSwitch<bool>(Ident->getName())
398 .Case("dllimport", true)
399 .Case("dllexport", true)
400 .Case("noreturn", true)
401 .Case("nothrow", true)
402 .Case("noinline", true)
403 .Case("naked", true)
404 .Case("appdomain", true)
405 .Case("process", true)
406 .Case("jitintrinsic", true)
407 .Case("noalias", true)
408 .Case("restrict", true)
409 .Case("novtable", true)
410 .Case("selectany", true)
411 .Case("thread", true)
Aaron Ballman444eb6e2013-05-04 16:58:37 +0000412 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000413 .Default(false);
414}
415
Chad Rosierc1183952012-06-26 22:30:43 +0000416/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000417/// parameters). Will return false if we properly handled the declspec, or
418/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000419void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000420 SourceLocation Loc,
421 ParsedAttributes &Attrs) {
422 // Try to handle the easy case first -- these declspecs all take a single
423 // parameter as their argument.
424 if (llvm::StringSwitch<bool>(Ident->getName())
425 .Case("uuid", true)
426 .Case("align", true)
427 .Case("allocate", true)
428 .Default(false)) {
429 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
430 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000431 // The deprecated declspec has an optional single argument, so we will
432 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000433 // not.
434 if (Tok.getKind() == tok::l_paren)
435 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
436 else
Aaron Ballman00e99962013-08-31 01:11:41 +0000437 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000438 } else if (Ident->getName() == "property") {
439 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000440 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000441 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000442 if (Tok.isNot(tok::l_paren)) {
443 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
444 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000445 return;
John McCall5e77d762013-04-16 07:28:30 +0000446 }
447 BalancedDelimiterTracker T(*this, tok::l_paren);
448 T.expectAndConsume(diag::err_expected_lparen_after,
449 Ident->getNameStart(), tok::r_paren);
450
451 enum AccessorKind {
452 AK_Invalid = -1,
453 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
454 };
455 IdentifierInfo *AccessorNames[] = { 0, 0 };
456 bool HasInvalidAccessor = false;
457
458 // Parse the accessor specifications.
459 while (true) {
460 // Stop if this doesn't look like an accessor spec.
461 if (!Tok.is(tok::identifier)) {
462 // If the user wrote a completely empty list, use a special diagnostic.
463 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
464 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
465 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
466 break;
467 }
468
469 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
470 break;
471 }
472
473 AccessorKind Kind;
474 SourceLocation KindLoc = Tok.getLocation();
475 StringRef KindStr = Tok.getIdentifierInfo()->getName();
476 if (KindStr == "get") {
477 Kind = AK_Get;
478 } else if (KindStr == "put") {
479 Kind = AK_Put;
480
481 // Recover from the common mistake of using 'set' instead of 'put'.
482 } else if (KindStr == "set") {
483 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
484 << FixItHint::CreateReplacement(KindLoc, "put");
485 Kind = AK_Put;
486
487 // Handle the mistake of forgetting the accessor kind by skipping
488 // this accessor.
489 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
490 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
491 ConsumeToken();
492 HasInvalidAccessor = true;
493 goto next_property_accessor;
494
495 // Otherwise, complain about the unknown accessor kind.
496 } else {
497 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
498 HasInvalidAccessor = true;
499 Kind = AK_Invalid;
500
501 // Try to keep parsing unless it doesn't look like an accessor spec.
502 if (!NextToken().is(tok::equal)) break;
503 }
504
505 // Consume the identifier.
506 ConsumeToken();
507
508 // Consume the '='.
Alp Toker8fbec672013-12-17 23:29:36 +0000509 if (!TryConsumeToken(tok::equal)) {
John McCall5e77d762013-04-16 07:28:30 +0000510 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
511 << KindStr;
512 break;
513 }
514
515 // Expect the method name.
516 if (!Tok.is(tok::identifier)) {
517 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
518 break;
519 }
520
521 if (Kind == AK_Invalid) {
522 // Just drop invalid accessors.
523 } else if (AccessorNames[Kind] != NULL) {
524 // Complain about the repeated accessor, ignore it, and keep parsing.
525 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
526 } else {
527 AccessorNames[Kind] = Tok.getIdentifierInfo();
528 }
529 ConsumeToken();
530
531 next_property_accessor:
532 // Keep processing accessors until we run out.
Alp Toker094e5212014-01-05 03:27:11 +0000533 if (TryConsumeToken(tok::comma))
John McCall5e77d762013-04-16 07:28:30 +0000534 continue;
535
536 // If we run into the ')', stop without consuming it.
Alp Toker094e5212014-01-05 03:27:11 +0000537 if (Tok.is(tok::r_paren))
John McCall5e77d762013-04-16 07:28:30 +0000538 break;
Alp Toker094e5212014-01-05 03:27:11 +0000539
540 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
541 break;
John McCall5e77d762013-04-16 07:28:30 +0000542 }
543
544 // Only add the property attribute if it was well-formed.
545 if (!HasInvalidAccessor) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000546 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000547 AccessorNames[AK_Get], AccessorNames[AK_Put],
548 AttributeList::AS_Declspec);
549 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000550 T.skipToEnd();
551 } else {
552 // We don't recognize this as a valid declspec, but instead of creating the
553 // attribute and allowing sema to warn about it, we will warn here instead.
554 // This is because some attributes have multiple spellings, but we need to
555 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000556 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000557 // both locations.
558 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
559
560 // If there's an open paren, we should eat the open and close parens under
561 // the assumption that this unknown declspec has parameters.
562 BalancedDelimiterTracker T(*this, tok::l_paren);
563 if (!T.consumeOpen())
564 T.skipToEnd();
565 }
566}
567
Eli Friedman06de2b52009-06-08 07:21:15 +0000568/// [MS] decl-specifier:
569/// __declspec ( extended-decl-modifier-seq )
570///
571/// [MS] extended-decl-modifier-seq:
572/// extended-decl-modifier[opt]
573/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000574void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000575 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000576
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000577 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000578 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000579 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000580 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000581 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000582
Chad Rosierc1183952012-06-26 22:30:43 +0000583 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000584 // you can specify multiple attributes per declspec.
585 while (Tok.getKind() != tok::r_paren) {
586 // We expect either a well-known identifier or a generic string. Anything
587 // else is a malformed declspec.
588 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000589 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000590 Tok.getKind() != tok::kw_restrict) {
591 Diag(Tok, diag::err_ms_declspec_type);
592 T.skipToEnd();
593 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000594 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000595
596 IdentifierInfo *AttrName;
597 SourceLocation AttrNameLoc;
598 if (IsString) {
599 SmallString<8> StrBuffer;
600 bool Invalid = false;
601 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
602 if (Invalid) {
603 T.skipToEnd();
604 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000605 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000606 AttrName = PP.getIdentifierInfo(Str);
607 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000608 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000609 AttrName = Tok.getIdentifierInfo();
610 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000611 }
Chad Rosierc1183952012-06-26 22:30:43 +0000612
Aaron Ballman478faed2012-06-19 22:09:27 +0000613 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000614 // If we have a generic string, we will allow it because there is no
615 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000616 // (for instance, SAL declspecs in older versions of MSVC).
617 //
Chad Rosierc1183952012-06-26 22:30:43 +0000618 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000619 // arguments and can be turned into an attribute directly.
Aaron Ballman00e99962013-08-31 01:11:41 +0000620 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
621 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000622 else
623 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000624 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000625 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000626}
627
John McCall53fa7142010-12-24 02:08:15 +0000628void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000629 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000630 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000631 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000632 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballman317a77f2013-05-22 23:25:32 +0000633 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
634 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000635 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
636 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000637 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
638 AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000639 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000640}
641
John McCall53fa7142010-12-24 02:08:15 +0000642void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000643 // Treat these like attributes
644 while (Tok.is(tok::kw___pascal)) {
645 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
646 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000647 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
648 AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000649 }
John McCall53fa7142010-12-24 02:08:15 +0000650}
651
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000652void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
653 // Treat these like attributes
654 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000655 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000656 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000657 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
658 AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000659 }
660}
661
Aaron Ballman05d76ea2014-01-14 01:29:54 +0000662void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) {
663 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
664 SourceLocation AttrNameLoc = Tok.getLocation();
Aaron Ballman26891332014-01-14 17:41:53 +0000665 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
666 AttributeList::AS_Keyword);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000667}
668
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000669/// \brief Parse a version number.
670///
671/// version:
672/// simple-integer
673/// simple-integer ',' simple-integer
674/// simple-integer ',' simple-integer ',' simple-integer
675VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
676 Range = Tok.getLocation();
677
678 if (!Tok.is(tok::numeric_constant)) {
679 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000680 SkipUntil(tok::comma, tok::r_paren,
681 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000682 return VersionTuple();
683 }
684
685 // Parse the major (and possibly minor and subminor) versions, which
686 // are stored in the numeric constant. We utilize a quirk of the
687 // lexer, which is that it handles something like 1.2.3 as a single
688 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000689 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000690 Buffer.resize(Tok.getLength()+1);
691 const char *ThisTokBegin = &Buffer[0];
692
693 // Get the spelling of the token, which eliminates trigraphs, etc.
694 bool Invalid = false;
695 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
696 if (Invalid)
697 return VersionTuple();
698
699 // Parse the major version.
700 unsigned AfterMajor = 0;
701 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000702 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000703 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
704 ++AfterMajor;
705 }
706
707 if (AfterMajor == 0) {
708 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000709 SkipUntil(tok::comma, tok::r_paren,
710 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000711 return VersionTuple();
712 }
713
714 if (AfterMajor == ActualLength) {
715 ConsumeToken();
716
717 // We only had a single version component.
718 if (Major == 0) {
719 Diag(Tok, diag::err_zero_version);
720 return VersionTuple();
721 }
722
723 return VersionTuple(Major);
724 }
725
726 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
727 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000728 SkipUntil(tok::comma, tok::r_paren,
729 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000730 return VersionTuple();
731 }
732
733 // Parse the minor version.
734 unsigned AfterMinor = AfterMajor + 1;
735 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000736 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000737 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
738 ++AfterMinor;
739 }
740
741 if (AfterMinor == ActualLength) {
742 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000743
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000744 // We had major.minor.
745 if (Major == 0 && Minor == 0) {
746 Diag(Tok, diag::err_zero_version);
747 return VersionTuple();
748 }
749
Chad Rosierc1183952012-06-26 22:30:43 +0000750 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000751 }
752
753 // If what follows is not a '.', we have a problem.
754 if (ThisTokBegin[AfterMinor] != '.') {
755 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000756 SkipUntil(tok::comma, tok::r_paren,
757 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Chad Rosierc1183952012-06-26 22:30:43 +0000758 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000759 }
760
761 // Parse the subminor version.
762 unsigned AfterSubminor = AfterMinor + 1;
763 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000764 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000765 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
766 ++AfterSubminor;
767 }
768
769 if (AfterSubminor != ActualLength) {
770 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000771 SkipUntil(tok::comma, tok::r_paren,
772 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000773 return VersionTuple();
774 }
775 ConsumeToken();
776 return VersionTuple(Major, Minor, Subminor);
777}
778
779/// \brief Parse the contents of the "availability" attribute.
780///
781/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000782/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000783///
784/// platform:
785/// identifier
786///
787/// version-arg-list:
788/// version-arg
789/// version-arg ',' version-arg-list
790///
791/// version-arg:
792/// 'introduced' '=' version
793/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000794/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000795/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000796/// opt-message:
797/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000798void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
799 SourceLocation AvailabilityLoc,
800 ParsedAttributes &attrs,
801 SourceLocation *endLoc) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000802 enum { Introduced, Deprecated, Obsoleted, Unknown };
803 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000804 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000805
806 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000807 BalancedDelimiterTracker T(*this, tok::l_paren);
808 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000809 Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000810 return;
811 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000812
813 // Parse the platform name,
814 if (Tok.isNot(tok::identifier)) {
815 Diag(Tok, diag::err_availability_expected_platform);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000816 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000817 return;
818 }
Richard Smithfeefaf52013-09-03 18:01:40 +0000819 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000820
821 // Parse the ',' following the platform name.
Alp Toker383d2c42014-01-01 03:08:43 +0000822 if (ExpectAndConsume(tok::comma)) {
823 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000824 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000825 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000826
827 // If we haven't grabbed the pointers for the identifiers
828 // "introduced", "deprecated", and "obsoleted", do so now.
829 if (!Ident_introduced) {
830 Ident_introduced = PP.getIdentifierInfo("introduced");
831 Ident_deprecated = PP.getIdentifierInfo("deprecated");
832 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000833 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000834 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000835 }
836
837 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000838 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000839 do {
840 if (Tok.isNot(tok::identifier)) {
841 Diag(Tok, diag::err_availability_expected_change);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000842 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000843 return;
844 }
845 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
846 SourceLocation KeywordLoc = ConsumeToken();
847
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000848 if (Keyword == Ident_unavailable) {
849 if (UnavailableLoc.isValid()) {
850 Diag(KeywordLoc, diag::err_availability_redundant)
851 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000852 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000853 UnavailableLoc = KeywordLoc;
Alp Toker97650562014-01-10 11:19:30 +0000854 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000855 }
856
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000857 if (Tok.isNot(tok::equal)) {
Alp Tokerec543272013-12-24 09:48:30 +0000858 Diag(Tok, diag::err_expected_after) << Keyword << tok::equal;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000859 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000860 return;
861 }
862 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000863 if (Keyword == Ident_message) {
Benjamin Kramera9dfa922013-09-13 17:31:48 +0000864 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000865 Diag(Tok, diag::err_expected_string_literal)
866 << /*Source='availability attribute'*/2;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000867 SkipUntil(tok::r_paren, StopAtSemi);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000868 return;
869 }
870 MessageExpr = ParseStringLiteralExpression();
871 break;
872 }
Chad Rosierc1183952012-06-26 22:30:43 +0000873
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000874 SourceRange VersionRange;
875 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000876
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000877 if (Version.empty()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000878 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000879 return;
880 }
881
882 unsigned Index;
883 if (Keyword == Ident_introduced)
884 Index = Introduced;
885 else if (Keyword == Ident_deprecated)
886 Index = Deprecated;
887 else if (Keyword == Ident_obsoleted)
888 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000889 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000890 Index = Unknown;
891
892 if (Index < Unknown) {
893 if (!Changes[Index].KeywordLoc.isInvalid()) {
894 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000895 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000896 << SourceRange(Changes[Index].KeywordLoc,
897 Changes[Index].VersionRange.getEnd());
898 }
899
900 Changes[Index].KeywordLoc = KeywordLoc;
901 Changes[Index].Version = Version;
902 Changes[Index].VersionRange = VersionRange;
903 } else {
904 Diag(KeywordLoc, diag::err_availability_unknown_change)
905 << Keyword << VersionRange;
906 }
907
Alp Toker97650562014-01-10 11:19:30 +0000908 } while (TryConsumeToken(tok::comma));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000909
910 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000911 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000912 return;
913
914 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000915 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000916
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000917 // The 'unavailable' availability cannot be combined with any other
918 // availability changes. Make sure that hasn't happened.
919 if (UnavailableLoc.isValid()) {
920 bool Complained = false;
921 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
922 if (Changes[Index].KeywordLoc.isValid()) {
923 if (!Complained) {
924 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
925 << SourceRange(Changes[Index].KeywordLoc,
926 Changes[Index].VersionRange.getEnd());
927 Complained = true;
928 }
929
930 // Clear out the availability.
931 Changes[Index] = AvailabilityChange();
932 }
933 }
934 }
935
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000936 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000937 attrs.addNew(&Availability,
938 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000939 0, AvailabilityLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +0000940 Platform,
John McCall084e83d2011-03-24 11:26:52 +0000941 Changes[Introduced],
942 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000943 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000944 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000945 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000946}
947
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000948/// \brief Parse the contents of the "objc_bridge_related" attribute.
949/// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')'
950/// related_class:
951/// Identifier
952///
953/// opt-class_method:
954/// Identifier: | <empty>
955///
956/// opt-instance_method:
957/// Identifier | <empty>
958///
959void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated,
960 SourceLocation ObjCBridgeRelatedLoc,
961 ParsedAttributes &attrs,
962 SourceLocation *endLoc) {
963 // Opening '('.
964 BalancedDelimiterTracker T(*this, tok::l_paren);
965 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000966 Diag(Tok, diag::err_expected) << tok::l_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000967 return;
968 }
969
970 // Parse the related class name.
971 if (Tok.isNot(tok::identifier)) {
972 Diag(Tok, diag::err_objcbridge_related_expected_related_class);
973 SkipUntil(tok::r_paren, StopAtSemi);
974 return;
975 }
976 IdentifierLoc *RelatedClass = ParseIdentifierLoc();
Alp Toker97650562014-01-10 11:19:30 +0000977 if (ExpectAndConsume(tok::comma)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000978 SkipUntil(tok::r_paren, StopAtSemi);
979 return;
980 }
Alp Toker8fbec672013-12-17 23:29:36 +0000981
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000982 // Parse optional class method name.
983 IdentifierLoc *ClassMethod = 0;
984 if (Tok.is(tok::identifier)) {
985 ClassMethod = ParseIdentifierLoc();
Alp Toker8fbec672013-12-17 23:29:36 +0000986 if (!TryConsumeToken(tok::colon)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000987 Diag(Tok, diag::err_objcbridge_related_selector_name);
988 SkipUntil(tok::r_paren, StopAtSemi);
989 return;
990 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000991 }
Alp Toker8fbec672013-12-17 23:29:36 +0000992 if (!TryConsumeToken(tok::comma)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000993 if (Tok.is(tok::colon))
994 Diag(Tok, diag::err_objcbridge_related_selector_name);
995 else
Alp Tokerec543272013-12-24 09:48:30 +0000996 Diag(Tok, diag::err_expected) << tok::comma;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000997 SkipUntil(tok::r_paren, StopAtSemi);
998 return;
999 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001000
1001 // Parse optional instance method name.
1002 IdentifierLoc *InstanceMethod = 0;
1003 if (Tok.is(tok::identifier))
1004 InstanceMethod = ParseIdentifierLoc();
1005 else if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001006 Diag(Tok, diag::err_expected) << tok::r_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001007 SkipUntil(tok::r_paren, StopAtSemi);
1008 return;
1009 }
1010
1011 // Closing ')'.
1012 if (T.consumeClose())
1013 return;
1014
1015 if (endLoc)
1016 *endLoc = T.getCloseLocation();
1017
1018 // Record this attribute
1019 attrs.addNew(&ObjCBridgeRelated,
1020 SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
1021 0, ObjCBridgeRelatedLoc,
1022 RelatedClass,
1023 ClassMethod,
1024 InstanceMethod,
1025 AttributeList::AS_GNU);
1026
1027}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001028
Bill Wendling44426052012-12-20 19:22:21 +00001029// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001030// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
1031
1032void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
1033
1034void Parser::LateParsedClass::ParseLexedAttributes() {
1035 Self->ParseLexedAttributes(*Class);
1036}
1037
1038void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001039 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001040}
1041
1042/// Wrapper class which calls ParseLexedAttribute, after setting up the
1043/// scope appropriately.
1044void Parser::ParseLexedAttributes(ParsingClass &Class) {
1045 // Deal with templates
1046 // FIXME: Test cases to make sure this does the right thing for templates.
1047 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
1048 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
1049 HasTemplateScope);
1050 if (HasTemplateScope)
1051 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
1052
Douglas Gregor3024f072012-04-16 07:05:22 +00001053 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001054 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +00001055 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001056 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1057 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1058
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001059 // Enter the scope of nested classes
1060 if (!AlreadyHasClassScope)
1061 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1062 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001063 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001064 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1065 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1066 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001067 }
Chad Rosierc1183952012-06-26 22:30:43 +00001068
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001069 if (!AlreadyHasClassScope)
1070 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1071 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001072}
1073
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001074
1075/// \brief Parse all attributes in LAs, and attach them to Decl D.
1076void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1077 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001078 assert(LAs.parseSoon() &&
1079 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001080 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001081 if (D)
1082 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001083 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001084 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001085 }
1086 LAs.clear();
1087}
1088
1089
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001090/// \brief Finish parsing an attribute for which parsing was delayed.
1091/// This will be called at the end of parsing a class declaration
1092/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001093/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001094/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001095void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1096 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001097 // Save the current token position.
1098 SourceLocation OrigLoc = Tok.getLocation();
1099
1100 // Append the current token at the end of the new token stream so that it
1101 // doesn't get lost.
1102 LA.Toks.push_back(Tok);
1103 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1104 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001105 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001106
1107 ParsedAttributes Attrs(AttrFactory);
1108 SourceLocation endLoc;
1109
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001110 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001111 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001112 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1113 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001114
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001115 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001116 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1117 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001118
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001119 if (LA.Decls.size() == 1) {
1120 // If the Decl is templatized, add template parameters to scope.
1121 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1122 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1123 if (HasTemplateScope)
1124 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001125
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001126 // If the Decl is on a function, add function parameters to the scope.
1127 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1128 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1129 if (HasFunScope)
1130 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001131
Michael Han23214e52012-10-03 01:56:22 +00001132 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001133 0, SourceLocation(), AttributeList::AS_GNU, 0);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001134
1135 if (HasFunScope) {
1136 Actions.ActOnExitFunctionContext();
1137 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1138 }
1139 if (HasTemplateScope) {
1140 TempScope.Exit();
1141 }
1142 } else {
1143 // If there are multiple decls, then the decl cannot be within the
1144 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001145 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001146 0, SourceLocation(), AttributeList::AS_GNU, 0);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001147 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001148 } else {
1149 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001150 }
1151
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00001152 const AttributeList *AL = Attrs.getList();
1153 if (OnDefinition && AL && !AL->isCXX11Attribute() &&
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001154 AL->isKnownToGCC())
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00001155 Diag(Tok, diag::warn_attribute_on_function_definition)
1156 << &LA.AttrName;
1157
1158 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i)
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001159 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001160
1161 if (Tok.getLocation() != OrigLoc) {
1162 // Due to a parsing error, we either went over the cached tokens or
1163 // there are still cached tokens left, so we skip the leftover tokens.
1164 // Since this is an uncommon situation that should be avoided, use the
1165 // expensive isBeforeInTranslationUnit call.
1166 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1167 OrigLoc))
1168 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001169 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001170 }
1171}
1172
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001173void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1174 SourceLocation AttrNameLoc,
1175 ParsedAttributes &Attrs,
1176 SourceLocation *EndLoc) {
1177 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1178
1179 BalancedDelimiterTracker T(*this, tok::l_paren);
1180 T.consumeOpen();
1181
1182 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001183 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001184 T.skipToEnd();
1185 return;
1186 }
Richard Smithfeefaf52013-09-03 18:01:40 +00001187 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001188
Alp Toker094e5212014-01-05 03:27:11 +00001189 if (ExpectAndConsume(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001190 T.skipToEnd();
1191 return;
1192 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001193
1194 SourceRange MatchingCTypeRange;
1195 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1196 if (MatchingCType.isInvalid()) {
1197 T.skipToEnd();
1198 return;
1199 }
1200
1201 bool LayoutCompatible = false;
1202 bool MustBeNull = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001203 while (TryConsumeToken(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001204 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001205 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001206 T.skipToEnd();
1207 return;
1208 }
1209 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1210 if (Flag->isStr("layout_compatible"))
1211 LayoutCompatible = true;
1212 else if (Flag->isStr("must_be_null"))
1213 MustBeNull = true;
1214 else {
1215 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1216 T.skipToEnd();
1217 return;
1218 }
1219 ConsumeToken(); // consume flag
1220 }
1221
1222 if (!T.consumeClose()) {
1223 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +00001224 ArgumentKind, MatchingCType.release(),
1225 LayoutCompatible, MustBeNull,
1226 AttributeList::AS_GNU);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001227 }
1228
1229 if (EndLoc)
1230 *EndLoc = T.getCloseLocation();
1231}
1232
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001233/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1234/// of a C++11 attribute-specifier in a location where an attribute is not
1235/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1236/// situation.
1237///
1238/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1239/// this doesn't appear to actually be an attribute-specifier, and the caller
1240/// should try to parse it.
1241bool Parser::DiagnoseProhibitedCXX11Attribute() {
1242 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1243
1244 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1245 case CAK_NotAttributeSpecifier:
1246 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1247 return false;
1248
1249 case CAK_InvalidAttributeSpecifier:
1250 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1251 return false;
1252
1253 case CAK_AttributeSpecifier:
1254 // Parse and discard the attributes.
1255 SourceLocation BeginLoc = ConsumeBracket();
1256 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001257 SkipUntil(tok::r_square);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001258 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1259 SourceLocation EndLoc = ConsumeBracket();
1260 Diag(BeginLoc, diag::err_attributes_not_allowed)
1261 << SourceRange(BeginLoc, EndLoc);
1262 return true;
1263 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001264 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001265}
1266
Richard Smith98155ad2013-02-20 01:17:14 +00001267/// \brief We have found the opening square brackets of a C++11
1268/// attribute-specifier in a location where an attribute is not permitted, but
1269/// we know where the attributes ought to be written. Parse them anyway, and
1270/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001271void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1272 SourceLocation CorrectLocation) {
1273 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1274 Tok.is(tok::kw_alignas));
1275
1276 // Consume the attributes.
1277 SourceLocation Loc = Tok.getLocation();
1278 ParseCXX11Attributes(Attrs);
1279 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1280
1281 Diag(Loc, diag::err_attributes_not_allowed)
1282 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1283 << FixItHint::CreateRemoval(AttrRange);
1284}
1285
John McCall53fa7142010-12-24 02:08:15 +00001286void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1287 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1288 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001289}
1290
Michael Han64536a62012-11-06 19:34:54 +00001291void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1292 AttributeList *AttrList = attrs.getList();
1293 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001294 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001295 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001296 << AttrList->getName();
1297 AttrList->setInvalid();
1298 }
1299 AttrList = AttrList->getNext();
1300 }
1301}
1302
Chris Lattner53361ac2006-08-10 05:19:57 +00001303/// ParseDeclaration - Parse a full 'declaration', which consists of
1304/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001305/// 'Context' should be a Declarator::TheContext value. This returns the
1306/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001307///
1308/// declaration: [C99 6.7]
1309/// block-declaration ->
1310/// simple-declaration
1311/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001312/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001313/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001314/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001315/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001316/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001317/// others... [FIXME]
1318///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001319Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1320 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001321 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001322 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001323 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001324 // Must temporarily exit the objective-c container scope for
1325 // parsing c none objective-c decls.
1326 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001327
John McCall48871652010-08-21 09:40:31 +00001328 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001329 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001330 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001331 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001332 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001333 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001334 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001335 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001336 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001337 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001338 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001339 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001340 SourceLocation InlineLoc = ConsumeToken();
1341 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1342 break;
1343 }
Chad Rosierc1183952012-06-26 22:30:43 +00001344 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001345 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001346 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001347 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001348 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001349 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001350 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001351 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001352 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001353 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001354 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001355 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001356 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001357 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001358 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001359 default:
John McCall53fa7142010-12-24 02:08:15 +00001360 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001361 }
Chad Rosierc1183952012-06-26 22:30:43 +00001362
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001363 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001364 // single decl, convert it now. Alias declarations can also declare a type;
1365 // include that too if it is present.
1366 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001367}
1368
1369/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1370/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001371/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1372/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001373///[C90/C++]init-declarator-list ';' [TODO]
1374/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001375///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001376/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001377/// attribute-specifier-seq[opt] type-specifier-seq declarator
1378///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001379/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001380/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001381///
1382/// If FRI is non-null, we might be parsing a for-range-declaration instead
1383/// of a simple-declaration. If we find that we are, we also parse the
1384/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001385Parser::DeclGroupPtrTy
1386Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1387 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001388 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001389 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001390 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001391 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001392
Richard Smith404dfb42013-11-19 22:47:36 +00001393 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
1394 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
1395
1396 // If we had a free-standing type definition with a missing semicolon, we
1397 // may get this far before the problem becomes obvious.
1398 if (DS.hasTagDefinition() &&
1399 DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
1400 return DeclGroupPtrTy();
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001401
Chris Lattner0e894622006-08-13 19:58:17 +00001402 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1403 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001404 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001405 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001406 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001407 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001408 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001409 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001410 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001411 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001412 }
Chad Rosierc1183952012-06-26 22:30:43 +00001413
Richard Smith2386c8b2013-02-22 09:06:26 +00001414 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001415 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001416}
Mike Stump11289f42009-09-09 15:08:12 +00001417
Richard Smith09f76ee2011-10-19 21:33:05 +00001418/// Returns true if this might be the start of a declarator, or a common typo
1419/// for a declarator.
1420bool Parser::MightBeDeclarator(unsigned Context) {
1421 switch (Tok.getKind()) {
1422 case tok::annot_cxxscope:
1423 case tok::annot_template_id:
1424 case tok::caret:
1425 case tok::code_completion:
1426 case tok::coloncolon:
1427 case tok::ellipsis:
1428 case tok::kw___attribute:
1429 case tok::kw_operator:
1430 case tok::l_paren:
1431 case tok::star:
1432 return true;
1433
1434 case tok::amp:
1435 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001436 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001437
Richard Smithc8a79032012-01-09 22:31:44 +00001438 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001439 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001440 NextToken().is(tok::l_square);
1441
1442 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001443 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001444
Richard Smith09f76ee2011-10-19 21:33:05 +00001445 case tok::identifier:
1446 switch (NextToken().getKind()) {
1447 case tok::code_completion:
1448 case tok::coloncolon:
1449 case tok::comma:
1450 case tok::equal:
1451 case tok::equalequal: // Might be a typo for '='.
1452 case tok::kw_alignas:
1453 case tok::kw_asm:
1454 case tok::kw___attribute:
1455 case tok::l_brace:
1456 case tok::l_paren:
1457 case tok::l_square:
1458 case tok::less:
1459 case tok::r_brace:
1460 case tok::r_paren:
1461 case tok::r_square:
1462 case tok::semi:
1463 return true;
1464
1465 case tok::colon:
1466 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001467 // and in block scope it's probably a label. Inside a class definition,
1468 // this is a bit-field.
1469 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001470 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001471
1472 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001473 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001474
1475 default:
1476 return false;
1477 }
1478
1479 default:
1480 return false;
1481 }
1482}
1483
Richard Smithb8caac82012-04-11 20:59:20 +00001484/// Skip until we reach something which seems like a sensible place to pick
1485/// up parsing after a malformed declaration. This will sometimes stop sooner
1486/// than SkipUntil(tok::r_brace) would, but will never stop later.
1487void Parser::SkipMalformedDecl() {
1488 while (true) {
1489 switch (Tok.getKind()) {
1490 case tok::l_brace:
1491 // Skip until matching }, then stop. We've probably skipped over
1492 // a malformed class or function definition or similar.
1493 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001494 SkipUntil(tok::r_brace);
Richard Smithb8caac82012-04-11 20:59:20 +00001495 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1496 // This declaration isn't over yet. Keep skipping.
1497 continue;
1498 }
Alp Toker8fbec672013-12-17 23:29:36 +00001499 TryConsumeToken(tok::semi);
Richard Smithb8caac82012-04-11 20:59:20 +00001500 return;
1501
1502 case tok::l_square:
1503 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001504 SkipUntil(tok::r_square);
Richard Smithb8caac82012-04-11 20:59:20 +00001505 continue;
1506
1507 case tok::l_paren:
1508 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001509 SkipUntil(tok::r_paren);
Richard Smithb8caac82012-04-11 20:59:20 +00001510 continue;
1511
1512 case tok::r_brace:
1513 return;
1514
1515 case tok::semi:
1516 ConsumeToken();
1517 return;
1518
1519 case tok::kw_inline:
1520 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001521 // a good place to pick back up parsing, except in an Objective-C
1522 // @interface context.
1523 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1524 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001525 return;
1526 break;
1527
1528 case tok::kw_namespace:
1529 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001530 // place to pick back up parsing, except in an Objective-C
1531 // @interface context.
1532 if (Tok.isAtStartOfLine() &&
1533 (!ParsingInObjCContainer || CurParsedObjCImpl))
1534 return;
1535 break;
1536
1537 case tok::at:
1538 // @end is very much like } in Objective-C contexts.
1539 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1540 ParsingInObjCContainer)
1541 return;
1542 break;
1543
1544 case tok::minus:
1545 case tok::plus:
1546 // - and + probably start new method declarations in Objective-C contexts.
1547 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001548 return;
1549 break;
1550
1551 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001552 case tok::annot_module_begin:
1553 case tok::annot_module_end:
1554 case tok::annot_module_include:
Richard Smithb8caac82012-04-11 20:59:20 +00001555 return;
1556
1557 default:
1558 break;
1559 }
1560
1561 ConsumeAnyToken();
1562 }
1563}
1564
John McCalld5a36322009-11-03 19:26:08 +00001565/// ParseDeclGroup - Having concluded that this is either a function
1566/// definition or a group of object declarations, actually parse the
1567/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001568Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1569 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001570 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001571 SourceLocation *DeclEnd,
1572 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001573 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001574 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001575 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001576
John McCalld5a36322009-11-03 19:26:08 +00001577 // Bail out if the first declarator didn't seem well-formed.
1578 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001579 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001580 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001581 }
Mike Stump11289f42009-09-09 15:08:12 +00001582
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001583 // Save late-parsed attributes for now; they need to be parsed in the
1584 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001585 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1586 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001587 if (D.isFunctionDeclarator())
1588 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1589
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001590 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001591 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001592 // Look at the next token to make sure that this isn't a function
1593 // declaration. We have to check this because __attribute__ might be the
1594 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001595 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001596
Douglas Gregor012efe22013-04-16 16:01:32 +00001597 if (AllowFunctionDefinitions) {
1598 if (isStartOfFunctionDefinition(D)) {
1599 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1600 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001601
Douglas Gregor012efe22013-04-16 16:01:32 +00001602 // Recover by treating the 'typedef' as spurious.
1603 DS.ClearStorageClassSpecs();
1604 }
1605
1606 Decl *TheDecl =
1607 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1608 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001609 }
1610
Douglas Gregor012efe22013-04-16 16:01:32 +00001611 if (isDeclarationSpecifier()) {
1612 // If there is an invalid declaration specifier right after the function
1613 // prototype, then we must be in a missing semicolon case where this isn't
1614 // actually a body. Just fall through into the code that handles it as a
1615 // prototype, and let the top-level code handle the erroneous declspec
1616 // where it would otherwise expect a comma or semicolon.
1617 } else {
1618 Diag(Tok, diag::err_expected_fn_body);
1619 SkipUntil(tok::semi);
1620 return DeclGroupPtrTy();
1621 }
John McCalld5a36322009-11-03 19:26:08 +00001622 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001623 if (Tok.is(tok::l_brace)) {
1624 Diag(Tok, diag::err_function_definition_not_allowed);
Serge Pavlov1de51512013-12-09 05:25:47 +00001625 SkipMalformedDecl();
1626 return DeclGroupPtrTy();
Douglas Gregor012efe22013-04-16 16:01:32 +00001627 }
John McCalld5a36322009-11-03 19:26:08 +00001628 }
1629 }
1630
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001631 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001632 return DeclGroupPtrTy();
1633
1634 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1635 // must parse and analyze the for-range-initializer before the declaration is
1636 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001637 //
1638 // Handle the Objective-C for-in loop variable similarly, although we
1639 // don't need to parse the container in advance.
1640 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1641 bool IsForRangeLoop = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001642 if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001643 IsForRangeLoop = true;
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001644 if (Tok.is(tok::l_brace))
1645 FRI->RangeExpr = ParseBraceInitializer();
1646 else
1647 FRI->RangeExpr = ParseExpression();
1648 }
1649
Richard Smith02e85f32011-04-14 22:09:26 +00001650 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001651 if (IsForRangeLoop)
1652 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001653 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001654 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001655 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001656 }
1657
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001658 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001659 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001660 if (LateParsedAttrs.size() > 0)
1661 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001662 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001663 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001664 DeclsInGroup.push_back(FirstDecl);
1665
Richard Smith09f76ee2011-10-19 21:33:05 +00001666 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001667
John McCalld5a36322009-11-03 19:26:08 +00001668 // If we don't have a comma, it is either the end of the list (a ';') or an
1669 // error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00001670 SourceLocation CommaLoc;
1671 while (TryConsumeToken(tok::comma, CommaLoc)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001672 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1673 // This comma was followed by a line-break and something which can't be
1674 // the start of a declarator. The comma was probably a typo for a
1675 // semicolon.
1676 Diag(CommaLoc, diag::err_expected_semi_declaration)
1677 << FixItHint::CreateReplacement(CommaLoc, ";");
1678 ExpectSemi = false;
1679 break;
1680 }
John McCalld5a36322009-11-03 19:26:08 +00001681
1682 // Parse the next declarator.
1683 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001684 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001685
1686 // Accept attributes in an init-declarator. In the first declarator in a
1687 // declaration, these would be part of the declspec. In subsequent
1688 // declarators, they become part of the declarator itself, so that they
1689 // don't apply to declarators after *this* one. Examples:
1690 // short __attribute__((common)) var; -> declspec
1691 // short var __attribute__((common)); -> declarator
1692 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001693 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001694
1695 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001696 if (!D.isInvalidType()) {
1697 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1698 D.complete(ThisDecl);
1699 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001700 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001701 }
John McCalld5a36322009-11-03 19:26:08 +00001702 }
1703
1704 if (DeclEnd)
1705 *DeclEnd = Tok.getLocation();
1706
Richard Smith09f76ee2011-10-19 21:33:05 +00001707 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001708 ExpectAndConsumeSemi(Context == Declarator::FileContext
1709 ? diag::err_invalid_token_after_toplevel_declarator
1710 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001711 // Okay, there was no semicolon and one was expected. If we see a
1712 // declaration specifier, just assume it was missing and continue parsing.
1713 // Otherwise things are very confused and we skip to recover.
1714 if (!isDeclarationSpecifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001715 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Alp Toker8fbec672013-12-17 23:29:36 +00001716 TryConsumeToken(tok::semi);
Chris Lattner13901342010-07-11 22:42:07 +00001717 }
John McCalld5a36322009-11-03 19:26:08 +00001718 }
1719
Rafael Espindolaab417692013-07-09 12:05:01 +00001720 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001721}
1722
Richard Smith02e85f32011-04-14 22:09:26 +00001723/// Parse an optional simple-asm-expr and attributes, and attach them to a
1724/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001725bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001726 // If a simple-asm-expr is present, parse it.
1727 if (Tok.is(tok::kw_asm)) {
1728 SourceLocation Loc;
1729 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1730 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001731 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smith02e85f32011-04-14 22:09:26 +00001732 return true;
1733 }
1734
1735 D.setAsmLabel(AsmLabel.release());
1736 D.SetRangeEnd(Loc);
1737 }
1738
1739 MaybeParseGNUAttributes(D);
1740 return false;
1741}
1742
Douglas Gregor23996282009-05-12 21:31:51 +00001743/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1744/// declarator'. This method parses the remainder of the declaration
1745/// (including any attributes or initializer, among other things) and
1746/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001747///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001748/// init-declarator: [C99 6.7]
1749/// declarator
1750/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001751/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1752/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001753/// [C++] declarator initializer[opt]
1754///
1755/// [C++] initializer:
1756/// [C++] '=' initializer-clause
1757/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001758/// [C++0x] '=' 'default' [TODO]
1759/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001760/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001761///
1762/// According to the standard grammar, =default and =delete are function
1763/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001764///
John McCall48871652010-08-21 09:40:31 +00001765Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001766 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001767 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001768 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001769
Richard Smith02e85f32011-04-14 22:09:26 +00001770 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1771}
Mike Stump11289f42009-09-09 15:08:12 +00001772
Richard Smith02e85f32011-04-14 22:09:26 +00001773Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1774 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001775 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001776 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001777 switch (TemplateInfo.Kind) {
1778 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001779 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001780 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001781
Douglas Gregor450f00842009-09-25 18:43:00 +00001782 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001783 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001784 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001785 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001786 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00001787 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001788 // Re-direct this decl to refer to the templated decl so that we can
1789 // initialize it.
1790 ThisDecl = VT->getTemplatedDecl();
1791 break;
1792 }
1793 case ParsedTemplateInfo::ExplicitInstantiation: {
1794 if (Tok.is(tok::semi)) {
1795 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1796 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1797 if (ThisRes.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001798 SkipUntil(tok::semi, StopBeforeMatch);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001799 return 0;
1800 }
1801 ThisDecl = ThisRes.get();
1802 } else {
1803 // FIXME: This check should be for a variable template instantiation only.
1804
1805 // Check that this is a valid instantiation
1806 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1807 // If the declarator-id is not a template-id, issue a diagnostic and
1808 // recover by ignoring the 'template' keyword.
1809 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1810 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1811 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1812 } else {
1813 SourceLocation LAngleLoc =
1814 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1815 Diag(D.getIdentifierLoc(),
1816 diag::err_explicit_instantiation_with_definition)
1817 << SourceRange(TemplateInfo.TemplateLoc)
1818 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1819
1820 // Recover as if it were an explicit specialization.
1821 TemplateParameterLists FakedParamLists;
1822 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1823 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1824 LAngleLoc));
1825
1826 ThisDecl =
1827 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1828 }
1829 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001830 break;
1831 }
1832 }
Mike Stump11289f42009-09-09 15:08:12 +00001833
Richard Smith74aeef52013-04-26 16:15:35 +00001834 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001835
Douglas Gregor23996282009-05-12 21:31:51 +00001836 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001837 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001838 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001839 ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001840
Anders Carlsson991285e2010-09-24 21:25:25 +00001841 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001842 if (D.isFunctionDeclarator())
1843 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1844 << 1 /* delete */;
1845 else
1846 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001847 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001848 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001849 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1850 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001851 else
1852 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001853 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001854 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001855 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001856 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001857 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001858
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001859 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001860 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001861 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001862 cutOffParsing();
1863 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001864 }
Chad Rosierc1183952012-06-26 22:30:43 +00001865
John McCalldadc5752010-08-24 06:29:42 +00001866 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001867
David Blaikiebbafb8a2012-03-11 07:00:24 +00001868 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001869 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001870 ExitScope();
1871 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001872
Douglas Gregor23996282009-05-12 21:31:51 +00001873 if (Init.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001874 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor604c3022010-03-01 18:27:54 +00001875 Actions.ActOnInitializerError(ThisDecl);
1876 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001877 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1878 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001879 }
1880 } else if (Tok.is(tok::l_paren)) {
1881 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001882 BalancedDelimiterTracker T(*this, tok::l_paren);
1883 T.consumeOpen();
1884
Benjamin Kramerf0623432012-08-23 22:51:59 +00001885 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001886 CommaLocsTy CommaLocs;
1887
David Blaikiebbafb8a2012-03-11 07:00:24 +00001888 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001889 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001890 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001891 }
1892
Douglas Gregor23996282009-05-12 21:31:51 +00001893 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001894 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001895 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor613bf102009-12-22 17:47:17 +00001896
David Blaikiebbafb8a2012-03-11 07:00:24 +00001897 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001898 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001899 ExitScope();
1900 }
Douglas Gregor23996282009-05-12 21:31:51 +00001901 } else {
1902 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001903 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001904
1905 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1906 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001907
David Blaikiebbafb8a2012-03-11 07:00:24 +00001908 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001909 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001910 ExitScope();
1911 }
1912
Sebastian Redla9351792012-02-11 23:51:47 +00001913 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1914 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001915 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001916 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1917 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001918 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001919 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001920 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001921 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001922 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1923
Sebastian Redl3da34892011-06-05 12:23:16 +00001924 if (D.getCXXScopeSpec().isSet()) {
1925 EnterScope(0);
1926 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1927 }
1928
1929 ExprResult Init(ParseBraceInitializer());
1930
1931 if (D.getCXXScopeSpec().isSet()) {
1932 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1933 ExitScope();
1934 }
1935
1936 if (Init.isInvalid()) {
1937 Actions.ActOnInitializerError(ThisDecl);
1938 } else
1939 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1940 /*DirectInit=*/true, TypeContainsAuto);
1941
Douglas Gregor23996282009-05-12 21:31:51 +00001942 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001943 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001944 }
1945
Richard Smithb2bc2e62011-02-21 20:05:19 +00001946 Actions.FinalizeDeclaration(ThisDecl);
1947
Douglas Gregor23996282009-05-12 21:31:51 +00001948 return ThisDecl;
1949}
1950
Chris Lattner1890ac82006-08-13 01:16:23 +00001951/// ParseSpecifierQualifierList
1952/// specifier-qualifier-list:
1953/// type-specifier specifier-qualifier-list[opt]
1954/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001955/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001956///
Richard Smithc5b05522012-03-12 07:56:15 +00001957void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1958 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001959 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1960 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001961 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001962 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001963
Chris Lattner1890ac82006-08-13 01:16:23 +00001964 // Validate declspec for type-name.
1965 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith649c7b062014-01-08 00:56:48 +00001966 if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001967 Diag(Tok, diag::err_expected_type);
1968 DS.SetTypeSpecError();
1969 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1970 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001971 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001972 if (!DS.hasTypeSpecifier())
1973 DS.SetTypeSpecError();
1974 }
Mike Stump11289f42009-09-09 15:08:12 +00001975
Chris Lattner1b22eed2006-11-28 05:12:07 +00001976 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001977 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001978 if (DS.getStorageClassSpecLoc().isValid())
1979 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1980 else
Richard Smithb4a9e862013-04-12 22:46:28 +00001981 Diag(DS.getThreadStorageClassSpecLoc(),
1982 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001983 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001984 }
Mike Stump11289f42009-09-09 15:08:12 +00001985
Chris Lattner1b22eed2006-11-28 05:12:07 +00001986 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001987 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001988 if (DS.isInlineSpecified())
1989 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1990 if (DS.isVirtualSpecified())
1991 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1992 if (DS.isExplicitSpecified())
1993 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001994 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001995 }
Richard Smithc5b05522012-03-12 07:56:15 +00001996
1997 // Issue diagnostic and remove constexpr specfier if present.
1998 if (DS.isConstexprSpecified()) {
1999 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2000 DS.ClearConstexprSpec();
2001 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002002}
Chris Lattner53361ac2006-08-10 05:19:57 +00002003
Chris Lattner6cc055a2009-04-12 20:42:31 +00002004/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2005/// specified token is valid after the identifier in a declarator which
2006/// immediately follows the declspec. For example, these things are valid:
2007///
2008/// int x [ 4]; // direct-declarator
2009/// int x ( int y); // direct-declarator
2010/// int(int x ) // direct-declarator
2011/// int x ; // simple-declaration
2012/// int x = 17; // init-declarator-list
2013/// int x , y; // init-declarator-list
2014/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002015/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002016/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002017///
2018/// This is not, because 'x' does not immediately follow the declspec (though
2019/// ')' happens to be valid anyway).
2020/// int (x)
2021///
2022static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2023 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2024 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002025 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002026}
2027
Chris Lattner20a0c612009-04-14 21:34:55 +00002028
2029/// ParseImplicitInt - This method is called when we have an non-typename
2030/// identifier in a declspec (which normally terminates the decl spec) when
2031/// the declspec has no type specifier. In this case, the declspec is either
2032/// malformed or is "implicit int" (in K&R and C89).
2033///
2034/// This method handles diagnosing this prettily and returns false if the
2035/// declspec is done being processed. If it recovers and thinks there may be
2036/// other pieces of declspec after it, it returns true.
2037///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002038bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002039 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002040 AccessSpecifier AS, DeclSpecContext DSC,
2041 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002042 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002043
Chris Lattner20a0c612009-04-14 21:34:55 +00002044 SourceLocation Loc = Tok.getLocation();
2045 // If we see an identifier that is not a type name, we normally would
2046 // parse it as the identifer being declared. However, when a typename
2047 // is typo'd or the definition is not included, this will incorrectly
2048 // parse the typename as the identifier name and fall over misparsing
2049 // later parts of the diagnostic.
2050 //
2051 // As such, we try to do some look-ahead in cases where this would
2052 // otherwise be an "implicit-int" case to see if this is invalid. For
2053 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2054 // an identifier with implicit int, we'd get a parse error because the
2055 // next token is obviously invalid for a type. Parse these as a case
2056 // with an invalid type specifier.
2057 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002058
Chris Lattner20a0c612009-04-14 21:34:55 +00002059 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002060 // error, do lookahead to try to do better recovery. This never applies
2061 // within a type specifier. Outside of C++, we allow this even if the
2062 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002063 // implicit int as an extension in C99 and C11.
Richard Smith649c7b062014-01-08 00:56:48 +00002064 if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002065 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002066 // If this token is valid for implicit int, e.g. "static x = 4", then
2067 // we just avoid eating the identifier, so it will be parsed as the
2068 // identifier in the declarator.
2069 return false;
2070 }
Mike Stump11289f42009-09-09 15:08:12 +00002071
Richard Smitha952ebb2012-05-15 21:01:51 +00002072 if (getLangOpts().CPlusPlus &&
2073 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2074 // Don't require a type specifier if we have the 'auto' storage class
2075 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002076 if (SS)
2077 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002078 return false;
2079 }
2080
Chris Lattner20a0c612009-04-14 21:34:55 +00002081 // Otherwise, if we don't consume this token, we are going to emit an
2082 // error anyway. Try to recover from various common problems. Check
2083 // to see if this was a reference to a tag name without a tag specified.
2084 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002085 //
2086 // C++ doesn't need this, and isTagName doesn't take SS.
2087 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002088 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002089 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002090
Douglas Gregor0be31a22010-07-02 17:43:08 +00002091 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002092 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002093 case DeclSpec::TST_enum:
2094 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2095 case DeclSpec::TST_union:
2096 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2097 case DeclSpec::TST_struct:
2098 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002099 case DeclSpec::TST_interface:
2100 TagName="__interface"; FixitTagName = "__interface ";
2101 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002102 case DeclSpec::TST_class:
2103 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002104 }
Mike Stump11289f42009-09-09 15:08:12 +00002105
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002106 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002107 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2108 LookupResult R(Actions, TokenName, SourceLocation(),
2109 Sema::LookupOrdinaryName);
2110
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002111 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002112 << TokenName << TagName << getLangOpts().CPlusPlus
2113 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2114
2115 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2116 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2117 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002118 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002119 << TokenName << TagName;
2120 }
Mike Stump11289f42009-09-09 15:08:12 +00002121
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002122 // Parse this as a tag as if the missing tag were present.
2123 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002124 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002125 else
Richard Smithc5b05522012-03-12 07:56:15 +00002126 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002127 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002128 return true;
2129 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002130 }
Mike Stump11289f42009-09-09 15:08:12 +00002131
Richard Smithfe904f02012-05-15 21:29:55 +00002132 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002133 // being declared (with a missing type).
Richard Smith649c7b062014-01-08 00:56:48 +00002134 if (!isTypeSpecifier(DSC) &&
Richard Smithfe904f02012-05-15 21:29:55 +00002135 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002136 // Look ahead to the next token to try to figure out what this declaration
2137 // was supposed to be.
2138 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002139 case tok::l_paren: {
2140 // static x(4); // 'x' is not a type
2141 // x(int n); // 'x' is not a type
2142 // x (*p)[]; // 'x' is a type
2143 //
2144 // Since we're in an error case (or the rare 'implicit int in C++' MS
2145 // extension), we can afford to perform a tentative parse to determine
2146 // which case we're in.
2147 TentativeParsingAction PA(*this);
2148 ConsumeToken();
2149 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2150 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002151
2152 if (TPR != TPResult::False()) {
2153 // The identifier is followed by a parenthesized declarator.
2154 // It's supposed to be a type.
2155 break;
2156 }
2157
2158 // If we're in a context where we could be declaring a constructor,
2159 // check whether this is a constructor declaration with a bogus name.
2160 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2161 IdentifierInfo *II = Tok.getIdentifierInfo();
2162 if (Actions.isCurrentClassNameTypo(II, SS)) {
2163 Diag(Loc, diag::err_constructor_bad_name)
2164 << Tok.getIdentifierInfo() << II
2165 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2166 Tok.setIdentifierInfo(II);
2167 }
2168 }
2169 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002170 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002171 case tok::comma:
2172 case tok::equal:
2173 case tok::kw_asm:
2174 case tok::l_brace:
2175 case tok::l_square:
2176 case tok::semi:
2177 // This looks like a variable or function declaration. The type is
2178 // probably missing. We're done parsing decl-specifiers.
2179 if (SS)
2180 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2181 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002182
2183 default:
2184 // This is probably supposed to be a type. This includes cases like:
2185 // int f(itn);
2186 // struct S { unsinged : 4; };
2187 break;
2188 }
2189 }
2190
Chad Rosierc1183952012-06-26 22:30:43 +00002191 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002192 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002193 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002194 IdentifierInfo *II = Tok.getIdentifierInfo();
Kaelyn Uhrain67b44c92014-02-13 20:14:07 +00002195 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T,
2196 getLangOpts().CPlusPlus &&
2197 NextToken().is(tok::less))) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002198 // The action emitted a diagnostic, so we don't have to.
2199 if (T) {
2200 // The action has suggested that the type T could be used. Set that as
2201 // the type in the declaration specifiers, consume the would-be type
2202 // name token, and we're done.
2203 const char *PrevSpec;
2204 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002205 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
2206 Actions.getASTContext().getPrintingPolicy());
Douglas Gregor15e56022009-10-13 23:27:22 +00002207 DS.SetRangeEnd(Tok.getLocation());
2208 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002209 // There may be other declaration specifiers after this.
2210 return true;
2211 } else if (II != Tok.getIdentifierInfo()) {
2212 // If no type was suggested, the correction is to a keyword
2213 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002214 // There may be other declaration specifiers after this.
2215 return true;
2216 }
Chad Rosierc1183952012-06-26 22:30:43 +00002217
Douglas Gregor15e56022009-10-13 23:27:22 +00002218 // Fall through; the action had no suggestion for us.
2219 } else {
2220 // The action did not emit a diagnostic, so emit one now.
2221 SourceRange R;
2222 if (SS) R = SS->getRange();
2223 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2224 }
Mike Stump11289f42009-09-09 15:08:12 +00002225
Douglas Gregor15e56022009-10-13 23:27:22 +00002226 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002227 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002228 DS.SetRangeEnd(Tok.getLocation());
2229 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002230
Chris Lattner20a0c612009-04-14 21:34:55 +00002231 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2232 // avoid rippling error messages on subsequent uses of the same type,
2233 // could be useful if #include was forgotten.
2234 return false;
2235}
2236
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002237/// \brief Determine the declaration specifier context from the declarator
2238/// context.
2239///
2240/// \param Context the declarator context, which is one of the
2241/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002242Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002243Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2244 if (Context == Declarator::MemberContext)
2245 return DSC_class;
2246 if (Context == Declarator::FileContext)
2247 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002248 if (Context == Declarator::TrailingReturnContext)
2249 return DSC_trailing;
Richard Smith649c7b062014-01-08 00:56:48 +00002250 if (Context == Declarator::AliasDeclContext ||
2251 Context == Declarator::AliasTemplateContext)
2252 return DSC_alias_declaration;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002253 return DSC_normal;
2254}
2255
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002256/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2257///
2258/// FIXME: Simply returns an alignof() expression if the argument is a
2259/// type. Ideally, the type should be propagated directly into Sema.
2260///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002261/// [C11] type-id
2262/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002263/// [C++0x] type-id ...[opt]
2264/// [C++0x] assignment-expression ...[opt]
2265ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2266 SourceLocation &EllipsisLoc) {
2267 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002268 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002269 SourceLocation TypeLoc = Tok.getLocation();
2270 ParsedType Ty = ParseTypeName().get();
2271 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002272 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2273 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002274 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002275 ER = ParseConstantExpression();
2276
Alp Toker8fbec672013-12-17 23:29:36 +00002277 if (getLangOpts().CPlusPlus11)
2278 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002279
2280 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002281}
2282
2283/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2284/// attribute to Attrs.
2285///
2286/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002287/// [C11] '_Alignas' '(' type-id ')'
2288/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002289/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2290/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002291void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002292 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002293 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2294 "Not an alignment-specifier!");
2295
Richard Smithd11c7a12013-01-29 01:48:07 +00002296 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2297 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002298
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002299 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002300 if (T.expectAndConsume())
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002301 return;
2302
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002303 SourceLocation EllipsisLoc;
2304 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002305 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002306 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002307 return;
2308 }
2309
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002310 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002311 if (EndLoc)
2312 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002313
Aaron Ballman00e99962013-08-31 01:11:41 +00002314 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002315 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002316 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2317 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002318}
2319
Richard Smith404dfb42013-11-19 22:47:36 +00002320/// Determine whether we're looking at something that might be a declarator
2321/// in a simple-declaration. If it can't possibly be a declarator, maybe
2322/// diagnose a missing semicolon after a prior tag definition in the decl
2323/// specifier.
2324///
2325/// \return \c true if an error occurred and this can't be any kind of
2326/// declaration.
2327bool
2328Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2329 DeclSpecContext DSContext,
2330 LateParsedAttrList *LateAttrs) {
2331 assert(DS.hasTagDefinition() && "shouldn't call this");
2332
2333 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002334
2335 if (getLangOpts().CPlusPlus &&
2336 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2337 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
2338 TryAnnotateCXXScopeToken(EnteringContext)) {
2339 SkipMalformedDecl();
2340 return true;
2341 }
2342
Richard Smith698875a2013-11-20 23:40:57 +00002343 bool HasScope = Tok.is(tok::annot_cxxscope);
2344 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2345 Token AfterScope = HasScope ? NextToken() : Tok;
2346
Richard Smith404dfb42013-11-19 22:47:36 +00002347 // Determine whether the following tokens could possibly be a
2348 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002349 bool MightBeDeclarator = true;
2350 if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) {
2351 // A declarator-id can't start with 'typename'.
2352 MightBeDeclarator = false;
2353 } else if (AfterScope.is(tok::annot_template_id)) {
2354 // If we have a type expressed as a template-id, this cannot be a
2355 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2356 TemplateIdAnnotation *Annot =
2357 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2358 if (Annot->Kind == TNK_Type_template)
2359 MightBeDeclarator = false;
2360 } else if (AfterScope.is(tok::identifier)) {
2361 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2362
Richard Smith404dfb42013-11-19 22:47:36 +00002363 // These tokens cannot come after the declarator-id in a
2364 // simple-declaration, and are likely to come after a type-specifier.
Richard Smith698875a2013-11-20 23:40:57 +00002365 if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) ||
2366 Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) ||
2367 Next.is(tok::coloncolon)) {
2368 // Missing a semicolon.
2369 MightBeDeclarator = false;
2370 } else if (HasScope) {
2371 // If the declarator-id has a scope specifier, it must redeclare a
2372 // previously-declared entity. If that's a type (and this is not a
2373 // typedef), that's an error.
2374 CXXScopeSpec SS;
2375 Actions.RestoreNestedNameSpecifierAnnotation(
2376 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2377 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2378 Sema::NameClassification Classification = Actions.ClassifyName(
2379 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2380 /*IsAddressOfOperand*/false);
2381 switch (Classification.getKind()) {
2382 case Sema::NC_Error:
2383 SkipMalformedDecl();
2384 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002385
Richard Smith698875a2013-11-20 23:40:57 +00002386 case Sema::NC_Keyword:
2387 case Sema::NC_NestedNameSpecifier:
2388 llvm_unreachable("typo correction and nested name specifiers not "
2389 "possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002390
Richard Smith698875a2013-11-20 23:40:57 +00002391 case Sema::NC_Type:
2392 case Sema::NC_TypeTemplate:
2393 // Not a previously-declared non-type entity.
2394 MightBeDeclarator = false;
2395 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002396
Richard Smith698875a2013-11-20 23:40:57 +00002397 case Sema::NC_Unknown:
2398 case Sema::NC_Expression:
2399 case Sema::NC_VarTemplate:
2400 case Sema::NC_FunctionTemplate:
2401 // Might be a redeclaration of a prior entity.
2402 break;
2403 }
Richard Smith404dfb42013-11-19 22:47:36 +00002404 }
Richard Smith404dfb42013-11-19 22:47:36 +00002405 }
2406
Richard Smith698875a2013-11-20 23:40:57 +00002407 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002408 return false;
2409
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002410 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Richard Smith404dfb42013-11-19 22:47:36 +00002411 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
Alp Toker383d2c42014-01-01 03:08:43 +00002412 diag::err_expected_after)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002413 << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi;
Richard Smith404dfb42013-11-19 22:47:36 +00002414
2415 // Try to recover from the typo, by dropping the tag definition and parsing
2416 // the problematic tokens as a type.
2417 //
2418 // FIXME: Split the DeclSpec into pieces for the standalone
2419 // declaration and pieces for the following declaration, instead
2420 // of assuming that all the other pieces attach to new declaration,
2421 // and call ParsedFreeStandingDeclSpec as appropriate.
2422 DS.ClearTypeSpecType();
2423 ParsedTemplateInfo NotATemplate;
2424 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2425 return false;
2426}
2427
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002428/// ParseDeclarationSpecifiers
2429/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002430/// storage-class-specifier declaration-specifiers[opt]
2431/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002432/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002433/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002434/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002435/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002436///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002437/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002438/// 'typedef'
2439/// 'extern'
2440/// 'static'
2441/// 'auto'
2442/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002443/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002444/// [C++11] 'thread_local'
2445/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002446/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002447/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002448/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002449/// [C++] 'virtual'
2450/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002451/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002452/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002453/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002454
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002455///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002456void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002457 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002458 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002459 DeclSpecContext DSContext,
2460 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002461 if (DS.getSourceRange().isInvalid()) {
2462 DS.SetRangeStart(Tok.getLocation());
2463 DS.SetRangeEnd(Tok.getLocation());
2464 }
Chad Rosierc1183952012-06-26 22:30:43 +00002465
Douglas Gregordf593fb2011-11-07 17:33:42 +00002466 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002467 bool AttrsLastTime = false;
2468 ParsedAttributesWithRange attrs(AttrFactory);
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002469 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002470 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002471 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002472 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002473 unsigned DiagID = 0;
2474
Chris Lattner4d8f8732006-11-28 05:05:08 +00002475 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002476
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002477 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002478 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002479 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002480 if (!AttrsLastTime)
2481 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002482 else {
2483 // Reject C++11 attributes that appertain to decl specifiers as
2484 // we don't support any C++11 attributes that appertain to decl
2485 // specifiers. This also conforms to what g++ 4.8 is doing.
2486 ProhibitCXX11Attributes(attrs);
2487
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002488 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002489 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002490
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002491 // If this is not a declaration specifier token, we're done reading decl
2492 // specifiers. First verify that DeclSpec's are consistent.
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002493 DS.Finish(Diags, PP, Policy);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002494 return;
Mike Stump11289f42009-09-09 15:08:12 +00002495
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002496 case tok::l_square:
2497 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002498 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002499 goto DoneWithDeclSpec;
2500
2501 ProhibitAttributes(attrs);
2502 // FIXME: It would be good to recover by accepting the attributes,
2503 // but attempting to do that now would cause serious
2504 // madness in terms of diagnostics.
2505 attrs.clear();
2506 attrs.Range = SourceRange();
2507
2508 ParseCXX11Attributes(attrs);
2509 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002510 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002511
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002512 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002513 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002514 if (DS.hasTypeSpecifier()) {
2515 bool AllowNonIdentifiers
2516 = (getCurScope()->getFlags() & (Scope::ControlScope |
2517 Scope::BlockScope |
2518 Scope::TemplateParamScope |
2519 Scope::FunctionPrototypeScope |
2520 Scope::AtCatchScope)) == 0;
2521 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002522 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002523 (DSContext == DSC_class && DS.isFriendSpecified());
2524
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002525 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002526 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002527 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002528 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002529 }
2530
Douglas Gregor80039242011-02-15 20:33:25 +00002531 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2532 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2533 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002534 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002535 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002536 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002537 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002538 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002539 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002540
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002541 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002542 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002543 }
2544
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002545 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002546 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002547 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002548 if (!DS.hasTypeSpecifier())
2549 DS.SetTypeSpecError();
2550 goto DoneWithDeclSpec;
2551 }
John McCall8bc2a702010-03-01 18:20:46 +00002552 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2553 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002554 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002555
2556 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002557 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002558 goto DoneWithDeclSpec;
2559
John McCall9dab4e62009-12-12 11:40:51 +00002560 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002561 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2562 Tok.getAnnotationRange(),
2563 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002564
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002565 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002566 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002567 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002568 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002569 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002570 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002571
2572 // C++ [class.qual]p2:
2573 // In a lookup in which the constructor is an acceptable lookup
2574 // result and the nested-name-specifier nominates a class C:
2575 //
2576 // - if the name specified after the
2577 // nested-name-specifier, when looked up in C, is the
2578 // injected-class-name of C (Clause 9), or
2579 //
2580 // - if the name specified after the nested-name-specifier
2581 // is the same as the identifier or the
2582 // simple-template-id's template-name in the last
2583 // component of the nested-name-specifier,
2584 //
2585 // the name is instead considered to name the constructor of
2586 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002587 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002588 // Thus, if the template-name is actually the constructor
2589 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002590 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002591 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002592 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002593 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002594 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Richard Smith446161b2014-03-03 21:12:53 +00002595 if (isConstructorDeclarator(/*Unqualified*/false)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002596 // The user meant this to be an out-of-line constructor
2597 // definition, but template arguments are not allowed
2598 // there. Just allow this as a constructor; we'll
2599 // complain about it later.
2600 goto DoneWithDeclSpec;
2601 }
2602
2603 // The user meant this to name a type, but it actually names
2604 // a constructor with some extraneous template
2605 // arguments. Complain, then parse it as a type as the user
2606 // intended.
2607 Diag(TemplateId->TemplateNameLoc,
2608 diag::err_out_of_line_template_id_names_constructor)
2609 << TemplateId->Name;
2610 }
2611
John McCall9dab4e62009-12-12 11:40:51 +00002612 DS.getTypeSpecScope() = SS;
2613 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002614 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002615 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002616 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002617 continue;
2618 }
2619
Douglas Gregorc5790df2009-09-28 07:26:33 +00002620 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002621 DS.getTypeSpecScope() = SS;
2622 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002623 if (Tok.getAnnotationValue()) {
2624 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002625 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002626 Tok.getAnnotationEndLoc(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002627 PrevSpec, DiagID, T, Policy);
Richard Smithda837032012-09-14 18:27:01 +00002628 if (isInvalid)
2629 break;
John McCallba7bf592010-08-24 05:47:05 +00002630 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002631 else
2632 DS.SetTypeSpecError();
2633 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2634 ConsumeToken(); // The typename
2635 }
2636
Douglas Gregor167fa622009-03-25 15:40:00 +00002637 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002638 goto DoneWithDeclSpec;
2639
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002640 // If we're in a context where the identifier could be a class name,
2641 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002642 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002643 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002644 &SS)) {
Richard Smith446161b2014-03-03 21:12:53 +00002645 if (isConstructorDeclarator(/*Unqualified*/false))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002646 goto DoneWithDeclSpec;
2647
2648 // As noted in C++ [class.qual]p2 (cited above), when the name
2649 // of the class is qualified in a context where it could name
2650 // a constructor, its a constructor name. However, we've
2651 // looked at the declarator, and the user probably meant this
2652 // to be a type. Complain that it isn't supposed to be treated
2653 // as a type, then proceed to parse it as a type.
2654 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2655 << Next.getIdentifierInfo();
2656 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002657
John McCallba7bf592010-08-24 05:47:05 +00002658 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2659 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002660 getCurScope(), &SS,
2661 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002662 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002663 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002664
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002665 // If the referenced identifier is not a type, then this declspec is
2666 // erroneous: We already checked about that it has no type specifier, and
2667 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002668 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002669 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002670 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002671 ParsedAttributesWithRange Attrs(AttrFactory);
2672 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2673 if (!Attrs.empty()) {
2674 AttrsLastTime = true;
2675 attrs.takeAllFrom(Attrs);
2676 }
2677 continue;
2678 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002679 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002680 }
Mike Stump11289f42009-09-09 15:08:12 +00002681
John McCall9dab4e62009-12-12 11:40:51 +00002682 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002683 ConsumeToken(); // The C++ scope.
2684
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002685 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002686 DiagID, TypeRep, Policy);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002687 if (isInvalid)
2688 break;
Mike Stump11289f42009-09-09 15:08:12 +00002689
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002690 DS.SetRangeEnd(Tok.getLocation());
2691 ConsumeToken(); // The typename.
2692
2693 continue;
2694 }
Mike Stump11289f42009-09-09 15:08:12 +00002695
Chris Lattnere387d9e2009-01-21 19:48:37 +00002696 case tok::annot_typename: {
Richard Smith404dfb42013-11-19 22:47:36 +00002697 // If we've previously seen a tag definition, we were almost surely
2698 // missing a semicolon after it.
2699 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
2700 goto DoneWithDeclSpec;
2701
John McCallba7bf592010-08-24 05:47:05 +00002702 if (Tok.getAnnotationValue()) {
2703 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002704 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002705 DiagID, T, Policy);
John McCallba7bf592010-08-24 05:47:05 +00002706 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002707 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002708
Chris Lattner005fc1b2010-04-05 18:18:31 +00002709 if (isInvalid)
2710 break;
2711
Chris Lattnere387d9e2009-01-21 19:48:37 +00002712 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2713 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002714
Chris Lattnere387d9e2009-01-21 19:48:37 +00002715 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2716 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002717 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002718 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002719 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002720
Chris Lattnere387d9e2009-01-21 19:48:37 +00002721 continue;
2722 }
Mike Stump11289f42009-09-09 15:08:12 +00002723
Douglas Gregor06873092011-04-28 15:48:45 +00002724 case tok::kw___is_signed:
2725 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2726 // typically treats it as a trait. If we see __is_signed as it appears
2727 // in libstdc++, e.g.,
2728 //
2729 // static const bool __is_signed;
2730 //
2731 // then treat __is_signed as an identifier rather than as a keyword.
2732 if (DS.getTypeSpecType() == TST_bool &&
2733 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
Alp Toker47642d22013-12-03 06:13:01 +00002734 DS.getStorageClassSpec() == DeclSpec::SCS_static)
2735 TryKeywordIdentFallback(true);
Douglas Gregor06873092011-04-28 15:48:45 +00002736
2737 // We're done with the declaration-specifiers.
2738 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002739
Chris Lattner16fac4f2008-07-26 01:18:38 +00002740 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002741 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002742 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002743 // In C++, check to see if this is a scope specifier like foo::bar::, if
2744 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002745 if (getLangOpts().CPlusPlus) {
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002746 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002747 if (!DS.hasTypeSpecifier())
2748 DS.SetTypeSpecError();
2749 goto DoneWithDeclSpec;
2750 }
2751 if (!Tok.is(tok::identifier))
2752 continue;
2753 }
Mike Stump11289f42009-09-09 15:08:12 +00002754
Chris Lattner16fac4f2008-07-26 01:18:38 +00002755 // This identifier can only be a typedef name if we haven't already seen
2756 // a type-specifier. Without this check we misparse:
2757 // typedef int X; struct Y { short X; }; as 'short int'.
2758 if (DS.hasTypeSpecifier())
2759 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002760
John Thompson22334602010-02-05 00:12:22 +00002761 // Check for need to substitute AltiVec keyword tokens.
2762 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2763 break;
2764
Richard Smith3092a3b2012-05-09 18:56:43 +00002765 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2766 // allow the use of a typedef name as a type specifier.
2767 if (DS.isTypeAltiVecVector())
2768 goto DoneWithDeclSpec;
2769
John McCallba7bf592010-08-24 05:47:05 +00002770 ParsedType TypeRep =
2771 Actions.getTypeName(*Tok.getIdentifierInfo(),
2772 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002773
Chris Lattner6cc055a2009-04-12 20:42:31 +00002774 // If this is not a typedef name, don't parse it as part of the declspec,
2775 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002776 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002777 ParsedAttributesWithRange Attrs(AttrFactory);
2778 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2779 if (!Attrs.empty()) {
2780 AttrsLastTime = true;
2781 attrs.takeAllFrom(Attrs);
2782 }
2783 continue;
2784 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002785 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002786 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002787
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002788 // If we're in a context where the identifier could be a class name,
2789 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002790 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002791 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Richard Smith446161b2014-03-03 21:12:53 +00002792 isConstructorDeclarator(/*Unqualified*/true))
Douglas Gregor61956c42008-10-31 09:07:45 +00002793 goto DoneWithDeclSpec;
2794
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002795 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002796 DiagID, TypeRep, Policy);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002797 if (isInvalid)
2798 break;
Mike Stump11289f42009-09-09 15:08:12 +00002799
Chris Lattner16fac4f2008-07-26 01:18:38 +00002800 DS.SetRangeEnd(Tok.getLocation());
2801 ConsumeToken(); // The identifier
2802
2803 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2804 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002805 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002806 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002807 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002808
Steve Naroffcd5e7822008-09-22 10:28:57 +00002809 // Need to support trailing type qualifiers (e.g. "id<p> const").
2810 // If a type specifier follows, it will be diagnosed elsewhere.
2811 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002812 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002813
2814 // type-name
2815 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002816 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002817 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002818 // This template-id does not refer to a type name, so we're
2819 // done with the type-specifiers.
2820 goto DoneWithDeclSpec;
2821 }
2822
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002823 // If we're in a context where the template-id could be a
2824 // constructor name or specialization, check whether this is a
2825 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002826 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002827 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Richard Smith446161b2014-03-03 21:12:53 +00002828 isConstructorDeclarator(TemplateId->SS.isEmpty()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002829 goto DoneWithDeclSpec;
2830
Douglas Gregor7f741122009-02-25 19:37:18 +00002831 // Turn the template-id annotation token into a type annotation
2832 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002833 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002834 continue;
2835 }
2836
Chris Lattnere37e2332006-08-15 04:50:22 +00002837 // GNU attributes support.
2838 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002839 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002840 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002841
2842 // Microsoft declspec support.
2843 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002844 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002845 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002846
Steve Naroff44ac7772008-12-25 14:16:32 +00002847 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002848 case tok::kw___forceinline: {
Serge Pavlov750db652013-11-13 06:57:53 +00002849 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002850 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002851 SourceLocation AttrNameLoc = Tok.getLocation();
Aaron Ballman00e99962013-08-31 01:11:41 +00002852 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
Aaron Ballman3fe6ed52014-01-13 21:40:16 +00002853 AttributeList::AS_Keyword);
Richard Smithda837032012-09-14 18:27:01 +00002854 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002855 }
Eli Friedman53339e02009-06-08 23:27:34 +00002856
Aaron Ballman317a77f2013-05-22 23:25:32 +00002857 case tok::kw___sptr:
2858 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002859 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002860 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002861 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002862 case tok::kw___cdecl:
2863 case tok::kw___stdcall:
2864 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002865 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002866 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002867 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002868 continue;
2869
Dawn Perchik335e16b2010-09-03 01:29:35 +00002870 // Borland single token adornments.
2871 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002872 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002873 continue;
2874
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002875 // OpenCL single token adornments.
2876 case tok::kw___kernel:
2877 ParseOpenCLAttributes(DS.getAttributes());
2878 continue;
2879
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002880 // storage-class-specifier
2881 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002882 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002883 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002884 break;
2885 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002886 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002887 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002888 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002889 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002890 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002891 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002892 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002893 Loc, PrevSpec, DiagID, Policy);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002894 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002895 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002896 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002897 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002898 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002899 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002900 break;
2901 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002902 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002903 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002904 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002905 PrevSpec, DiagID, Policy);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002906 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002907 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002908 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002909 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002910 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002911 DiagID, Policy);
Richard Smith58c74332011-09-04 19:54:14 +00002912 } else
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);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002915 break;
2916 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002917 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002918 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002919 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002920 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002921 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002922 PrevSpec, DiagID, Policy);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002923 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002924 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002925 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2926 PrevSpec, DiagID);
2927 break;
2928 case tok::kw_thread_local:
2929 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2930 PrevSpec, DiagID);
2931 break;
2932 case tok::kw__Thread_local:
2933 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2934 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002935 break;
Mike Stump11289f42009-09-09 15:08:12 +00002936
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002937 // function-specifier
2938 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00002939 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002940 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002941 case tok::kw_virtual:
Serge Pavlov750db652013-11-13 06:57:53 +00002942 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002943 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002944 case tok::kw_explicit:
Serge Pavlov750db652013-11-13 06:57:53 +00002945 isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002946 break;
Richard Smith0015f092013-01-17 22:16:11 +00002947 case tok::kw__Noreturn:
2948 if (!getLangOpts().C11)
2949 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlov750db652013-11-13 06:57:53 +00002950 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00002951 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002952
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002953 // alignment-specifier
2954 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002955 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002956 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002957 ParseAlignmentSpecifier(DS.getAttributes());
2958 continue;
2959
Anders Carlssoncd8db412009-05-06 04:46:28 +00002960 // friend
2961 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002962 if (DSContext == DSC_class)
2963 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2964 else {
2965 PrevSpec = ""; // not actually used by the diagnostic
2966 DiagID = diag::err_friend_invalid_in_context;
2967 isInvalid = true;
2968 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002969 break;
Mike Stump11289f42009-09-09 15:08:12 +00002970
Douglas Gregor26701a42011-09-09 02:06:17 +00002971 // Modules
2972 case tok::kw___module_private__:
2973 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2974 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002975
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002976 // constexpr
2977 case tok::kw_constexpr:
2978 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2979 break;
2980
Chris Lattnere387d9e2009-01-21 19:48:37 +00002981 // type-specifier
2982 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002983 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002984 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002985 break;
2986 case tok::kw_long:
2987 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002988 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002989 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002990 else
John McCall49bfce42009-08-03 20:12:06 +00002991 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002992 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002993 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002994 case tok::kw___int64:
2995 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002996 DiagID, Policy);
Francois Pichet84133e42011-04-28 01:59:37 +00002997 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002998 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002999 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3000 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003001 break;
3002 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003003 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3004 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003005 break;
3006 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00003007 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3008 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003009 break;
3010 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00003011 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3012 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003013 break;
3014 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003015 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003016 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003017 break;
3018 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003019 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003020 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003021 break;
3022 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003023 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003024 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003025 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003026 case tok::kw___int128:
3027 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003028 DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00003029 break;
3030 case tok::kw_half:
3031 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003032 DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00003033 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003034 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003035 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003036 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003037 break;
3038 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003039 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003040 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003041 break;
3042 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003043 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003044 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003045 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003046 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003047 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003048 DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003049 break;
3050 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003051 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003052 DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003053 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003054 case tok::kw_bool:
3055 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003056 if (Tok.is(tok::kw_bool) &&
3057 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3058 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3059 PrevSpec = ""; // Not used by the diagnostic.
3060 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003061 // For better error recovery.
3062 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003063 isInvalid = true;
3064 } else {
3065 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003066 DiagID, Policy);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003067 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003068 break;
3069 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003070 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003071 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003072 break;
3073 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003074 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003075 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003076 break;
3077 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003078 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003079 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003080 break;
John Thompson22334602010-02-05 00:12:22 +00003081 case tok::kw___vector:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003082 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
John Thompson22334602010-02-05 00:12:22 +00003083 break;
3084 case tok::kw___pixel:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003085 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
John Thompson22334602010-02-05 00:12:22 +00003086 break;
John McCall39439732011-04-09 22:50:59 +00003087 case tok::kw___unknown_anytype:
3088 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003089 PrevSpec, DiagID, Policy);
John McCall39439732011-04-09 22:50:59 +00003090 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003091
3092 // class-specifier:
3093 case tok::kw_class:
3094 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003095 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003096 case tok::kw_union: {
3097 tok::TokenKind Kind = Tok.getKind();
3098 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003099
3100 // These are attributes following class specifiers.
3101 // To produce better diagnostic, we parse them when
3102 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003103 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003104 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003105 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003106
3107 // If there are attributes following class specifier,
3108 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003109 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003110 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003111 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003112 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003113 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003114 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003115
3116 // enum-specifier:
3117 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003118 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003119 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003120 continue;
3121
3122 // cv-qualifier:
3123 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003124 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003125 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003126 break;
3127 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003128 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003129 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003130 break;
3131 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003132 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003133 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003134 break;
3135
Douglas Gregor333489b2009-03-27 23:10:48 +00003136 // C++ typename-specifier:
3137 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003138 if (TryAnnotateTypeOrScopeToken()) {
3139 DS.SetTypeSpecError();
3140 goto DoneWithDeclSpec;
3141 }
3142 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003143 continue;
3144 break;
3145
Chris Lattnere387d9e2009-01-21 19:48:37 +00003146 // GNU typeof support.
3147 case tok::kw_typeof:
3148 ParseTypeofSpecifier(DS);
3149 continue;
3150
David Blaikie15a430a2011-12-04 05:04:18 +00003151 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003152 ParseDecltypeSpecifier(DS);
3153 continue;
3154
Alexis Hunt4a257072011-05-19 05:37:45 +00003155 case tok::kw___underlying_type:
3156 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003157 continue;
3158
3159 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003160 // C11 6.7.2.4/4:
3161 // If the _Atomic keyword is immediately followed by a left parenthesis,
3162 // it is interpreted as a type specifier (with a type name), not as a
3163 // type qualifier.
3164 if (NextToken().is(tok::l_paren)) {
3165 ParseAtomicSpecifier(DS);
3166 continue;
3167 }
3168 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3169 getLangOpts());
3170 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003171
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003172 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003173 case tok::kw___private:
3174 case tok::kw___global:
3175 case tok::kw___local:
3176 case tok::kw___constant:
3177 case tok::kw___read_only:
3178 case tok::kw___write_only:
3179 case tok::kw___read_write:
Aaron Ballman05d76ea2014-01-14 01:29:54 +00003180 ParseOpenCLQualifiers(DS.getAttributes());
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003181 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003182
Steve Naroffcfdf6162008-06-05 00:02:44 +00003183 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003184 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003185 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3186 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003187 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003188 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003189
Douglas Gregor3a001f42010-11-19 17:10:50 +00003190 if (!ParseObjCProtocolQualifiers(DS))
3191 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3192 << FixItHint::CreateInsertion(Loc, "id")
3193 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003194
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003195 // Need to support trailing type qualifiers (e.g. "id<p> const").
3196 // If a type specifier follows, it will be diagnosed elsewhere.
3197 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003198 }
John McCall49bfce42009-08-03 20:12:06 +00003199 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003200 if (isInvalid) {
3201 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003202 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003203
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003204 if (DiagID == diag::ext_duplicate_declspec)
3205 Diag(Tok, DiagID)
3206 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3207 else
3208 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003209 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003210
Chris Lattner2e232092008-03-13 06:29:04 +00003211 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003212 if (DiagID != diag::err_bool_redeclaration)
3213 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003214
3215 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003216 }
3217}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003218
Chris Lattner70ae4912007-10-29 04:42:53 +00003219/// ParseStructDeclaration - Parse a struct declaration without the terminating
3220/// semicolon.
3221///
Chris Lattner90a26b02007-01-23 04:38:16 +00003222/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003223/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003224/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003225/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003226/// struct-declarator-list:
3227/// struct-declarator
3228/// struct-declarator-list ',' struct-declarator
3229/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3230/// struct-declarator:
3231/// declarator
3232/// [GNU] declarator attributes[opt]
3233/// declarator[opt] ':' constant-expression
3234/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3235///
Chris Lattnera12405b2008-04-10 06:46:29 +00003236void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003237ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003238
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003239 if (Tok.is(tok::kw___extension__)) {
3240 // __extension__ silences extension warnings in the subexpression.
3241 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003242 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003243 return ParseStructDeclaration(DS, Fields);
3244 }
Mike Stump11289f42009-09-09 15:08:12 +00003245
Steve Naroff97170802007-08-20 22:28:22 +00003246 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003247 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003248
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003249 // If there are no declarators, this is a free-standing declaration
3250 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003251 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003252 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3253 DS);
3254 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003255 return;
3256 }
3257
3258 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003259 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003260 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003261 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003262 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003263 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003264
Bill Wendling44426052012-12-20 19:22:21 +00003265 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003266 if (!FirstDeclarator)
3267 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003268
Steve Naroff97170802007-08-20 22:28:22 +00003269 /// struct-declarator: declarator
3270 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003271 if (Tok.isNot(tok::colon)) {
3272 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3273 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003274 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003275 }
Mike Stump11289f42009-09-09 15:08:12 +00003276
Alp Toker8fbec672013-12-17 23:29:36 +00003277 if (TryConsumeToken(tok::colon)) {
John McCalldadc5752010-08-24 06:29:42 +00003278 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003279 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003280 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00003281 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003282 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003283 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003284
Steve Naroff97170802007-08-20 22:28:22 +00003285 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003286 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003287
John McCallcfefb6d2009-11-03 02:38:08 +00003288 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003289 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003290
Steve Naroff97170802007-08-20 22:28:22 +00003291 // If we don't have a comma, it is either the end of the list (a ';')
3292 // or an error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00003293 if (!TryConsumeToken(tok::comma, CommaLoc))
Chris Lattner70ae4912007-10-29 04:42:53 +00003294 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003295
John McCallcfefb6d2009-11-03 02:38:08 +00003296 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003297 }
Steve Naroff97170802007-08-20 22:28:22 +00003298}
3299
3300/// ParseStructUnionBody
3301/// struct-contents:
3302/// struct-declaration-list
3303/// [EXT] empty
3304/// [GNU] "struct-declaration-list" without terminatoring ';'
3305/// struct-declaration-list:
3306/// struct-declaration
3307/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003308/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003309///
Chris Lattner1300fb92007-01-23 23:42:53 +00003310void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003311 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003312 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3313 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003314 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003315
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003316 BalancedDelimiterTracker T(*this, tok::l_brace);
3317 if (T.consumeOpen())
3318 return;
Mike Stump11289f42009-09-09 15:08:12 +00003319
Douglas Gregor658b9552009-01-09 22:42:13 +00003320 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003321 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003322
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003323 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003324
Chris Lattner7b9ace62007-01-23 20:11:08 +00003325 // While we still have something to read, read the declarations in the struct.
Richard Smith34f30512013-11-23 04:06:09 +00003326 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003327 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003328
Chris Lattner736ed5d2007-06-09 05:59:07 +00003329 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003330 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003331 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003332 continue;
3333 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003334
Andy Gibbsc804e082013-04-03 09:46:04 +00003335 // Parse _Static_assert declaration.
3336 if (Tok.is(tok::kw__Static_assert)) {
3337 SourceLocation DeclEnd;
3338 ParseStaticAssertDeclaration(DeclEnd);
3339 continue;
3340 }
3341
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003342 if (Tok.is(tok::annot_pragma_pack)) {
3343 HandlePragmaPack();
3344 continue;
3345 }
3346
3347 if (Tok.is(tok::annot_pragma_align)) {
3348 HandlePragmaAlign();
3349 continue;
3350 }
3351
John McCallcfefb6d2009-11-03 02:38:08 +00003352 if (!Tok.is(tok::at)) {
3353 struct CFieldCallback : FieldCallback {
3354 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003355 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003356 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003357
John McCall48871652010-08-21 09:40:31 +00003358 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003359 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003360 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3361
Eli Friedman934dbbf2012-08-08 23:53:27 +00003362 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003363 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003364 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003365 FD.D.getDeclSpec().getSourceRange().getBegin(),
3366 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003367 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003368 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003369 }
John McCallcfefb6d2009-11-03 02:38:08 +00003370 } Callback(*this, TagDecl, FieldDecls);
3371
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003372 // Parse all the comma separated declarators.
3373 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003374 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003375 } else { // Handle @defs
3376 ConsumeToken();
3377 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3378 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003379 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003380 continue;
3381 }
3382 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003383 ExpectAndConsume(tok::l_paren);
Chris Lattner535b8302008-06-21 19:39:06 +00003384 if (!Tok.is(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003385 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003386 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003387 continue;
3388 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003389 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003390 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003391 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003392 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3393 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003394 ExpectAndConsume(tok::r_paren);
Mike Stump11289f42009-09-09 15:08:12 +00003395 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003396
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003397 if (TryConsumeToken(tok::semi))
3398 continue;
3399
3400 if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003401 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003402 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003403 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003404
3405 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3406 // Skip to end of block or statement to avoid ext-warning on extra ';'.
3407 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
3408 // If we stopped at a ';', eat it.
3409 TryConsumeToken(tok::semi);
Chris Lattner90a26b02007-01-23 04:38:16 +00003410 }
Mike Stump11289f42009-09-09 15:08:12 +00003411
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003412 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003413
John McCall084e83d2011-03-24 11:26:52 +00003414 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003415 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003416 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003417
Douglas Gregor0be31a22010-07-02 17:43:08 +00003418 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003419 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003420 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003421 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003422 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003423 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3424 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003425}
3426
Chris Lattner3b561a32006-08-13 00:12:11 +00003427/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003428/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003429/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003430///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003431/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3432/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003433/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3434/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003435/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003436/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003437///
Richard Smith7d137e32012-03-23 03:33:32 +00003438/// [C++11] enum-head '{' enumerator-list[opt] '}'
3439/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003440///
Richard Smith7d137e32012-03-23 03:33:32 +00003441/// enum-head: [C++11]
3442/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3443/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3444/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003445///
Richard Smith7d137e32012-03-23 03:33:32 +00003446/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003447/// 'enum'
3448/// 'enum' 'class'
3449/// 'enum' 'struct'
3450///
Richard Smith7d137e32012-03-23 03:33:32 +00003451/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003452/// ':' type-specifier-seq
3453///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003454/// [C++] elaborated-type-specifier:
3455/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3456///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003457void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003458 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003459 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003460 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003461 if (Tok.is(tok::code_completion)) {
3462 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003463 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003464 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003465 }
John McCallcb432fa2011-07-06 05:58:41 +00003466
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003467 // If attributes exist after tag, parse them.
3468 ParsedAttributesWithRange attrs(AttrFactory);
3469 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003470 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003471
3472 // If declspecs exist after tag, parse them.
3473 while (Tok.is(tok::kw___declspec))
3474 ParseMicrosoftDeclSpec(attrs);
3475
Richard Smith0f8ee222012-01-10 01:33:14 +00003476 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003477 bool IsScopedUsingClassTag = false;
3478
John McCallbeae29a2012-06-23 22:30:04 +00003479 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003480 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3481 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3482 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003483 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003484 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003485
Bill Wendling44426052012-12-20 19:22:21 +00003486 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003487 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003488 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003489
3490 // They are allowed afterwards, though.
3491 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003492 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003493 while (Tok.is(tok::kw___declspec))
3494 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003495 }
Richard Smith7d137e32012-03-23 03:33:32 +00003496
John McCall6347b682012-05-07 06:16:58 +00003497 // C++11 [temp.explicit]p12:
3498 // The usual access controls do not apply to names used to specify
3499 // explicit instantiations.
3500 // We extend this to also cover explicit specializations. Note that
3501 // we don't suppress if this turns out to be an elaborated type
3502 // specifier.
3503 bool shouldDelayDiagsInTag =
3504 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3505 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3506 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003507
Richard Smithbfdb1082012-03-12 08:56:40 +00003508 // Enum definitions should not be parsed in a trailing-return-type.
3509 bool AllowDeclaration = DSC != DSC_trailing;
3510
3511 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003512 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003513 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003514
Abramo Bagnarad7548482010-05-19 21:37:53 +00003515 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003516 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003517 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3518 // if a fixed underlying type is allowed.
3519 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003520
3521 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003522 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003523 return;
3524
3525 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003526 Diag(Tok, diag::err_expected) << tok::identifier;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003527 if (Tok.isNot(tok::l_brace)) {
3528 // Has no name and is not a definition.
3529 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003530 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003531 return;
3532 }
3533 }
3534 }
Mike Stump11289f42009-09-09 15:08:12 +00003535
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003536 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003537 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003538 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Alp Tokerec543272013-12-24 09:48:30 +00003539 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
Mike Stump11289f42009-09-09 15:08:12 +00003540
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003541 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003542 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00003543 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003544 }
Mike Stump11289f42009-09-09 15:08:12 +00003545
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003546 // If an identifier is present, consume and remember it.
3547 IdentifierInfo *Name = 0;
3548 SourceLocation NameLoc;
3549 if (Tok.is(tok::identifier)) {
3550 Name = Tok.getIdentifierInfo();
3551 NameLoc = ConsumeToken();
3552 }
Mike Stump11289f42009-09-09 15:08:12 +00003553
Richard Smith0f8ee222012-01-10 01:33:14 +00003554 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003555 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3556 // declaration of a scoped enumeration.
3557 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003558 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003559 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003560 }
3561
John McCall6347b682012-05-07 06:16:58 +00003562 // Okay, end the suppression area. We'll decide whether to emit the
3563 // diagnostics in a second.
3564 if (shouldDelayDiagsInTag)
3565 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003566
Douglas Gregor0bf31402010-10-08 23:50:27 +00003567 TypeResult BaseType;
3568
Douglas Gregord1f69f62010-12-01 17:42:47 +00003569 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003570 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003571 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003572 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003573 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003574 // If we're in class scope, this can either be an enum declaration with
3575 // an underlying type, or a declaration of a bitfield member. We try to
3576 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003577 // (integer literal, sizeof); if it's still ambiguous, we then consider
3578 // anything that's a simple-type-specifier followed by '(' as an
3579 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003580 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003581 EnterExpressionEvaluationContext Unevaluated(Actions,
3582 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003583 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003584 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003585 // bit-field. This is the common case.
3586 if (TPR == TPResult::True())
3587 PossibleBitfield = true;
3588 // If the next token starts a type-specifier-seq, it may be either a
3589 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003590 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003591 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003592 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003593 GetLookAheadToken(2).getKind() == tok::semi) {
3594 // Consume the ':'.
3595 ConsumeToken();
3596 } else {
3597 // We have the start of a type-specifier-seq, so we have to perform
3598 // tentative parsing to determine whether we have an expression or a
3599 // type.
3600 TentativeParsingAction TPA(*this);
3601
3602 // Consume the ':'.
3603 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003604
3605 // If we see a type specifier followed by an open-brace, we have an
3606 // ambiguity between an underlying type and a C++11 braced
3607 // function-style cast. Resolve this by always treating it as an
3608 // underlying type.
3609 // FIXME: The standard is not entirely clear on how to disambiguate in
3610 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003611 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003612 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003613 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003614 // We'll parse this as a bitfield later.
3615 PossibleBitfield = true;
3616 TPA.Revert();
3617 } else {
3618 // We have a type-specifier-seq.
3619 TPA.Commit();
3620 }
3621 }
3622 } else {
3623 // Consume the ':'.
3624 ConsumeToken();
3625 }
3626
3627 if (!PossibleBitfield) {
3628 SourceRange Range;
3629 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003630
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003631 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003632 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003633 } else if (!getLangOpts().ObjC2) {
3634 if (getLangOpts().CPlusPlus)
3635 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3636 else
3637 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3638 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003639 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003640 }
3641
Richard Smith0f8ee222012-01-10 01:33:14 +00003642 // There are four options here. If we have 'friend enum foo;' then this is a
3643 // friend declaration, and cannot have an accompanying definition. If we have
3644 // 'enum foo;', then this is a forward declaration. If we have
3645 // 'enum foo {...' then this is a definition. Otherwise we have something
3646 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003647 //
3648 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3649 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3650 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3651 //
John McCallfaf5fb42010-08-26 23:41:50 +00003652 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003653 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003654 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003655 } else if (Tok.is(tok::l_brace)) {
3656 if (DS.isFriendSpecified()) {
3657 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3658 << SourceRange(DS.getFriendSpecLoc());
3659 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003660 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00003661 TUK = Sema::TUK_Friend;
3662 } else {
3663 TUK = Sema::TUK_Definition;
3664 }
Richard Smith649c7b062014-01-08 00:56:48 +00003665 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00003666 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003667 (Tok.isAtStartOfLine() &&
3668 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003669 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3670 if (Tok.isNot(tok::semi)) {
3671 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00003672 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003673 PP.EnterToken(Tok);
3674 Tok.setKind(tok::semi);
3675 }
John McCall6347b682012-05-07 06:16:58 +00003676 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003677 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003678 }
3679
3680 // If this is an elaborated type specifier, and we delayed
3681 // diagnostics before, just merge them into the current pool.
3682 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3683 diagsFromTag.redelay();
3684 }
Richard Smith7d137e32012-03-23 03:33:32 +00003685
3686 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003687 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003688 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003689 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003690 // Skip the rest of this declarator, up until the comma or semicolon.
3691 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003692 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00003693 return;
3694 }
3695
3696 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3697 // Enumerations can't be explicitly instantiated.
3698 DS.SetTypeSpecError();
3699 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3700 return;
3701 }
3702
3703 assert(TemplateInfo.TemplateParams && "no template parameters");
3704 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3705 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003706 }
Chad Rosierc1183952012-06-26 22:30:43 +00003707
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003708 if (TUK == Sema::TUK_Reference)
3709 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003710
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003711 if (!Name && TUK != Sema::TUK_Definition) {
3712 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003713
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003714 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003715 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003716 return;
3717 }
Richard Smith7d137e32012-03-23 03:33:32 +00003718
Douglas Gregord6ab8742009-05-28 23:31:59 +00003719 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003720 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003721 const char *PrevSpec = 0;
3722 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003723 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003724 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003725 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003726 Owned, IsDependent, ScopedEnumKWLoc,
Richard Smith649c7b062014-01-08 00:56:48 +00003727 IsScopedUsingClassTag, BaseType,
3728 DSC == DSC_type_specifier);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003729
Douglas Gregorba41d012010-04-24 16:38:41 +00003730 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003731 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003732 // dependent tag.
3733 if (!Name) {
3734 DS.SetTypeSpecError();
3735 Diag(Tok, diag::err_expected_type_name_after_typename);
3736 return;
3737 }
Chad Rosierc1183952012-06-26 22:30:43 +00003738
Douglas Gregor0be31a22010-07-02 17:43:08 +00003739 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003740 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003741 NameLoc);
3742 if (Type.isInvalid()) {
3743 DS.SetTypeSpecError();
3744 return;
3745 }
Chad Rosierc1183952012-06-26 22:30:43 +00003746
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003747 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3748 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003749 PrevSpec, DiagID, Type.get(),
3750 Actions.getASTContext().getPrintingPolicy()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003751 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003752
Douglas Gregorba41d012010-04-24 16:38:41 +00003753 return;
3754 }
Mike Stump11289f42009-09-09 15:08:12 +00003755
John McCall48871652010-08-21 09:40:31 +00003756 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003757 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003758 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003759 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003760 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003761 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00003762 }
Chad Rosierc1183952012-06-26 22:30:43 +00003763
Douglas Gregorba41d012010-04-24 16:38:41 +00003764 DS.SetTypeSpecError();
3765 return;
3766 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003767
Richard Smith369b9f92012-06-25 21:37:02 +00003768 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003769 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003770
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003771 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3772 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003773 PrevSpec, DiagID, TagDecl, Owned,
3774 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00003775 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003776}
3777
Chris Lattnerc1915e22007-01-25 07:29:02 +00003778/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3779/// enumerator-list:
3780/// enumerator
3781/// enumerator-list ',' enumerator
3782/// enumerator:
3783/// enumeration-constant
3784/// enumeration-constant '=' constant-expression
3785/// enumeration-constant:
3786/// identifier
3787///
John McCall48871652010-08-21 09:40:31 +00003788void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003789 // Enter the scope of the enum body and start the definition.
3790 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003791 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003792
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003793 BalancedDelimiterTracker T(*this, tok::l_brace);
3794 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003795
Chris Lattner37256fb2007-08-27 17:24:30 +00003796 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003797 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003798 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003799
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003800 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003801
John McCall48871652010-08-21 09:40:31 +00003802 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003803
Chris Lattnerc1915e22007-01-25 07:29:02 +00003804 // Parse the enumerator-list.
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003805 while (Tok.isNot(tok::r_brace)) {
3806 // Parse enumerator. If failed, try skipping till the start of the next
3807 // enumerator definition.
3808 if (Tok.isNot(tok::identifier)) {
3809 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
3810 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
3811 TryConsumeToken(tok::comma))
3812 continue;
3813 break;
3814 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003815 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3816 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003817
John McCall811a0f52010-10-22 23:36:17 +00003818 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003819 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003820 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003821 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003822 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003823
Chris Lattnerc1915e22007-01-25 07:29:02 +00003824 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003825 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003826 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003827
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003828 if (TryConsumeToken(tok::equal, EqualLoc)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003829 AssignedVal = ParseConstantExpression();
3830 if (AssignedVal.isInvalid())
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003831 SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003832 }
Mike Stump11289f42009-09-09 15:08:12 +00003833
Chris Lattnerc1915e22007-01-25 07:29:02 +00003834 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003835 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3836 LastEnumConstDecl,
3837 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003838 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003839 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003840 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003841
Chris Lattner4ef40012007-06-11 01:28:17 +00003842 EnumConstantDecls.push_back(EnumConstDecl);
3843 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003844
Douglas Gregorce66d022010-09-07 14:51:08 +00003845 if (Tok.is(tok::identifier)) {
3846 // We're missing a comma between enumerators.
3847 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003848 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003849 << FixItHint::CreateInsertion(Loc, ", ");
3850 continue;
3851 }
Chad Rosierc1183952012-06-26 22:30:43 +00003852
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003853 // Emumerator definition must be finished, only comma or r_brace are
3854 // allowed here.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003855 SourceLocation CommaLoc;
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003856 if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
3857 if (EqualLoc.isValid())
3858 Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
3859 << tok::comma;
3860 else
3861 Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
3862 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
3863 if (TryConsumeToken(tok::comma, CommaLoc))
3864 continue;
3865 } else {
3866 break;
3867 }
3868 }
Mike Stump11289f42009-09-09 15:08:12 +00003869
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003870 // If comma is followed by r_brace, emit appropriate warning.
3871 if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003872 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003873 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3874 diag::ext_enumerator_list_comma_cxx :
3875 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003876 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003877 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003878 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3879 << FixItHint::CreateRemoval(CommaLoc);
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003880 break;
Richard Smith5d164bc2011-10-15 05:09:34 +00003881 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003882 }
Mike Stump11289f42009-09-09 15:08:12 +00003883
Chris Lattnerc1915e22007-01-25 07:29:02 +00003884 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003885 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003886
Chris Lattnerc1915e22007-01-25 07:29:02 +00003887 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003888 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003889 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003890
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003891 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003892 EnumDecl, EnumConstantDecls,
3893 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003894 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003895
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003896 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003897 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3898 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003899
3900 // The next token must be valid after an enum definition. If not, a ';'
3901 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003902 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3903 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Alp Toker383d2c42014-01-01 03:08:43 +00003904 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003905 // Push this token back into the preprocessor and change our current token
3906 // to ';' so that the rest of the code recovers as though there were an
3907 // ';' after the definition.
3908 PP.EnterToken(Tok);
3909 Tok.setKind(tok::semi);
3910 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003911}
Chris Lattner3b561a32006-08-13 00:12:11 +00003912
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003913/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003914/// start of a type-qualifier-list.
3915bool Parser::isTypeQualifier() const {
3916 switch (Tok.getKind()) {
3917 default: return false;
Alp Tokerde50ff32013-12-17 18:17:46 +00003918 // type-qualifier
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003919 case tok::kw_const:
3920 case tok::kw_volatile:
3921 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003922 case tok::kw___private:
3923 case tok::kw___local:
3924 case tok::kw___global:
3925 case tok::kw___constant:
3926 case tok::kw___read_only:
3927 case tok::kw___read_write:
3928 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003929 return true;
3930 }
3931}
3932
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003933/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3934/// is definitely a type-specifier. Return false if it isn't part of a type
3935/// specifier or if we're not sure.
3936bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3937 switch (Tok.getKind()) {
3938 default: return false;
3939 // type-specifiers
3940 case tok::kw_short:
3941 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003942 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003943 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003944 case tok::kw_signed:
3945 case tok::kw_unsigned:
3946 case tok::kw__Complex:
3947 case tok::kw__Imaginary:
3948 case tok::kw_void:
3949 case tok::kw_char:
3950 case tok::kw_wchar_t:
3951 case tok::kw_char16_t:
3952 case tok::kw_char32_t:
3953 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003954 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003955 case tok::kw_float:
3956 case tok::kw_double:
3957 case tok::kw_bool:
3958 case tok::kw__Bool:
3959 case tok::kw__Decimal32:
3960 case tok::kw__Decimal64:
3961 case tok::kw__Decimal128:
3962 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003963
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003964 // struct-or-union-specifier (C99) or class-specifier (C++)
3965 case tok::kw_class:
3966 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003967 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003968 case tok::kw_union:
3969 // enum-specifier
3970 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003971
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003972 // typedef-name
3973 case tok::annot_typename:
3974 return true;
3975 }
3976}
3977
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003978/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003979/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003980bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003981 switch (Tok.getKind()) {
3982 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003983
Chris Lattner020bab92009-01-04 23:41:41 +00003984 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003985 if (TryAltiVecVectorToken())
3986 return true;
3987 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003988 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003989 // Annotate typenames and C++ scope specifiers. If we get one, just
3990 // recurse to handle whatever we get.
3991 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003992 return true;
3993 if (Tok.is(tok::identifier))
3994 return false;
3995 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003996
Chris Lattner020bab92009-01-04 23:41:41 +00003997 case tok::coloncolon: // ::foo::bar
3998 if (NextToken().is(tok::kw_new) || // ::new
3999 NextToken().is(tok::kw_delete)) // ::delete
4000 return false;
4001
Chris Lattner020bab92009-01-04 23:41:41 +00004002 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004003 return true;
4004 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004005
Chris Lattnere37e2332006-08-15 04:50:22 +00004006 // GNU attributes support.
4007 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004008 // GNU typeof support.
4009 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004010
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004011 // type-specifiers
4012 case tok::kw_short:
4013 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004014 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004015 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004016 case tok::kw_signed:
4017 case tok::kw_unsigned:
4018 case tok::kw__Complex:
4019 case tok::kw__Imaginary:
4020 case tok::kw_void:
4021 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004022 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004023 case tok::kw_char16_t:
4024 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004025 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004026 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004027 case tok::kw_float:
4028 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004029 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004030 case tok::kw__Bool:
4031 case tok::kw__Decimal32:
4032 case tok::kw__Decimal64:
4033 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004034 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004035
Chris Lattner861a2262008-04-13 18:59:07 +00004036 // struct-or-union-specifier (C99) or class-specifier (C++)
4037 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004038 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004039 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004040 case tok::kw_union:
4041 // enum-specifier
4042 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004043
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004044 // type-qualifier
4045 case tok::kw_const:
4046 case tok::kw_volatile:
4047 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004048
John McCallea0a39e2012-11-14 00:49:39 +00004049 // Debugger support.
4050 case tok::kw___unknown_anytype:
4051
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004052 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004053 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004054 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004055
Chris Lattner409bf7d2008-10-20 00:25:30 +00004056 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4057 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004058 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004059
Steve Naroff44ac7772008-12-25 14:16:32 +00004060 case tok::kw___cdecl:
4061 case tok::kw___stdcall:
4062 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004063 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004064 case tok::kw___w64:
4065 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004066 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004067 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004068 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004069
4070 case tok::kw___private:
4071 case tok::kw___local:
4072 case tok::kw___global:
4073 case tok::kw___constant:
4074 case tok::kw___read_only:
4075 case tok::kw___read_write:
4076 case tok::kw___write_only:
4077
Eli Friedman53339e02009-06-08 23:27:34 +00004078 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004079
Richard Smith8e1ac332013-03-28 01:55:44 +00004080 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004081 case tok::kw__Atomic:
4082 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004083 }
4084}
4085
Chris Lattneracd58a32006-08-06 17:24:14 +00004086/// isDeclarationSpecifier() - Return true if the current token is part of a
4087/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004088///
4089/// \param DisambiguatingWithExpression True to indicate that the purpose of
4090/// this check is to disambiguate between an expression and a declaration.
4091bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004092 switch (Tok.getKind()) {
4093 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004094
Chris Lattner020bab92009-01-04 23:41:41 +00004095 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004096 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004097 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004098 return false;
John Thompson22334602010-02-05 00:12:22 +00004099 if (TryAltiVecVectorToken())
4100 return true;
4101 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004102 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004103 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004104 // Annotate typenames and C++ scope specifiers. If we get one, just
4105 // recurse to handle whatever we get.
4106 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004107 return true;
4108 if (Tok.is(tok::identifier))
4109 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004110
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004111 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004112 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004113 // expression is permitted, then this is probably a class message send
4114 // missing the initial '['. In this case, we won't consider this to be
4115 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004116 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004117 isStartOfObjCClassMessageMissingOpenBracket())
4118 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004119
John McCall1f476a12010-02-26 08:45:28 +00004120 return isDeclarationSpecifier();
4121
Chris Lattner020bab92009-01-04 23:41:41 +00004122 case tok::coloncolon: // ::foo::bar
4123 if (NextToken().is(tok::kw_new) || // ::new
4124 NextToken().is(tok::kw_delete)) // ::delete
4125 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004126
Chris Lattner020bab92009-01-04 23:41:41 +00004127 // Annotate typenames and C++ scope specifiers. If we get one, just
4128 // recurse to handle whatever we get.
4129 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004130 return true;
4131 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004132
Chris Lattneracd58a32006-08-06 17:24:14 +00004133 // storage-class-specifier
4134 case tok::kw_typedef:
4135 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004136 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004137 case tok::kw_static:
4138 case tok::kw_auto:
4139 case tok::kw_register:
4140 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004141 case tok::kw_thread_local:
4142 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004143
Douglas Gregor26701a42011-09-09 02:06:17 +00004144 // Modules
4145 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004146
John McCallea0a39e2012-11-14 00:49:39 +00004147 // Debugger support
4148 case tok::kw___unknown_anytype:
4149
Chris Lattneracd58a32006-08-06 17:24:14 +00004150 // type-specifiers
4151 case tok::kw_short:
4152 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004153 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004154 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004155 case tok::kw_signed:
4156 case tok::kw_unsigned:
4157 case tok::kw__Complex:
4158 case tok::kw__Imaginary:
4159 case tok::kw_void:
4160 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004161 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004162 case tok::kw_char16_t:
4163 case tok::kw_char32_t:
4164
Chris Lattneracd58a32006-08-06 17:24:14 +00004165 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004166 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004167 case tok::kw_float:
4168 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004169 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004170 case tok::kw__Bool:
4171 case tok::kw__Decimal32:
4172 case tok::kw__Decimal64:
4173 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004174 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004175
Chris Lattner861a2262008-04-13 18:59:07 +00004176 // struct-or-union-specifier (C99) or class-specifier (C++)
4177 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004178 case tok::kw_struct:
4179 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004180 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004181 // enum-specifier
4182 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004183
Chris Lattneracd58a32006-08-06 17:24:14 +00004184 // type-qualifier
4185 case tok::kw_const:
4186 case tok::kw_volatile:
4187 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004188
Chris Lattneracd58a32006-08-06 17:24:14 +00004189 // function-specifier
4190 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004191 case tok::kw_virtual:
4192 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004193 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004194
Richard Smith1dba27c2013-01-29 09:02:09 +00004195 // alignment-specifier
4196 case tok::kw__Alignas:
4197
Richard Smithd16fe122012-10-25 00:00:53 +00004198 // friend keyword.
4199 case tok::kw_friend:
4200
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004201 // static_assert-declaration
4202 case tok::kw__Static_assert:
4203
Chris Lattner599e47e2007-08-09 17:01:07 +00004204 // GNU typeof support.
4205 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004206
Chris Lattner599e47e2007-08-09 17:01:07 +00004207 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004208 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004209
Richard Smithd16fe122012-10-25 00:00:53 +00004210 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004211 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004212 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004213
Richard Smith8e1ac332013-03-28 01:55:44 +00004214 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004215 case tok::kw__Atomic:
4216 return true;
4217
Chris Lattner8b2ec162008-07-26 03:38:44 +00004218 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4219 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004220 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004221
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004222 // typedef-name
4223 case tok::annot_typename:
4224 return !DisambiguatingWithExpression ||
4225 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004226
Steve Narofff192fab2009-01-06 19:34:12 +00004227 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004228 case tok::kw___cdecl:
4229 case tok::kw___stdcall:
4230 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004231 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004232 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004233 case tok::kw___sptr:
4234 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004235 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004236 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004237 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004238 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004239 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004240
4241 case tok::kw___private:
4242 case tok::kw___local:
4243 case tok::kw___global:
4244 case tok::kw___constant:
4245 case tok::kw___read_only:
4246 case tok::kw___read_write:
4247 case tok::kw___write_only:
4248
Eli Friedman53339e02009-06-08 23:27:34 +00004249 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004250 }
4251}
4252
Richard Smith446161b2014-03-03 21:12:53 +00004253bool Parser::isConstructorDeclarator(bool IsUnqualified) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004254 TentativeParsingAction TPA(*this);
4255
4256 // Parse the C++ scope specifier.
4257 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004258 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004259 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004260 TPA.Revert();
4261 return false;
4262 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004263
4264 // Parse the constructor name.
4265 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4266 // We already know that we have a constructor name; just consume
4267 // the token.
4268 ConsumeToken();
4269 } else {
4270 TPA.Revert();
4271 return false;
4272 }
4273
Richard Smith43f340f2012-03-27 23:05:05 +00004274 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004275 if (Tok.isNot(tok::l_paren)) {
4276 TPA.Revert();
4277 return false;
4278 }
4279 ConsumeParen();
4280
Richard Smith43f340f2012-03-27 23:05:05 +00004281 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4282 // that we have a constructor.
4283 if (Tok.is(tok::r_paren) ||
4284 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004285 TPA.Revert();
4286 return true;
4287 }
4288
Richard Smithf2163662013-09-06 00:12:20 +00004289 // A C++11 attribute here signals that we have a constructor, and is an
4290 // attribute on the first constructor parameter.
4291 if (getLangOpts().CPlusPlus11 &&
4292 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4293 /*OuterMightBeMessageSend*/ true)) {
4294 TPA.Revert();
4295 return true;
4296 }
4297
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004298 // If we need to, enter the specified scope.
4299 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004300 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004301 DeclScopeObj.EnterDeclaratorScope();
4302
Francois Pichet79f3a872011-01-31 04:54:32 +00004303 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004304 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004305 MaybeParseMicrosoftAttributes(Attrs);
4306
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004307 // Check whether the next token(s) are part of a declaration
4308 // specifier, in which case we have the start of a parameter and,
4309 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004310 bool IsConstructor = false;
4311 if (isDeclarationSpecifier())
4312 IsConstructor = true;
4313 else if (Tok.is(tok::identifier) ||
4314 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4315 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4316 // This might be a parenthesized member name, but is more likely to
4317 // be a constructor declaration with an invalid argument type. Keep
4318 // looking.
4319 if (Tok.is(tok::annot_cxxscope))
4320 ConsumeToken();
4321 ConsumeToken();
4322
4323 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004324 // which must have one of the following syntactic forms (see the
4325 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004326 switch (Tok.getKind()) {
4327 case tok::l_paren:
4328 // C(X ( int));
4329 case tok::l_square:
4330 // C(X [ 5]);
4331 // C(X [ [attribute]]);
4332 case tok::coloncolon:
4333 // C(X :: Y);
4334 // C(X :: *p);
Richard Smithefd009d2012-03-27 00:56:56 +00004335 // Assume this isn't a constructor, rather than assuming it's a
4336 // constructor with an unnamed parameter of an ill-formed type.
4337 break;
4338
Richard Smith446161b2014-03-03 21:12:53 +00004339 case tok::r_paren:
4340 // C(X )
4341 if (NextToken().is(tok::colon) || NextToken().is(tok::kw_try)) {
4342 // Assume these were meant to be constructors:
4343 // C(X) : (the name of a bit-field cannot be parenthesized).
4344 // C(X) try (this is otherwise ill-formed).
4345 IsConstructor = true;
4346 }
4347 if (NextToken().is(tok::semi) || NextToken().is(tok::l_brace)) {
4348 // If we have a constructor name within the class definition,
4349 // assume these were meant to be constructors:
4350 // C(X) {
4351 // C(X) ;
4352 // ... because otherwise we would be declaring a non-static data
4353 // member that is ill-formed because it's of the same type as its
4354 // surrounding class.
4355 //
4356 // FIXME: We can actually do this whether or not the name is qualified,
4357 // because if it is qualified in this context it must be being used as
4358 // a constructor name. However, we do not implement that rule correctly
4359 // currently, so we're somewhat conservative here.
4360 IsConstructor = IsUnqualified;
4361 }
4362 break;
4363
Richard Smithefd009d2012-03-27 00:56:56 +00004364 default:
4365 IsConstructor = true;
4366 break;
4367 }
4368 }
4369
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004370 TPA.Revert();
4371 return IsConstructor;
4372}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004373
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004374/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004375/// type-qualifier-list: [C99 6.7.5]
4376/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004377/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004378/// [ only if VendorAttributesAllowed=true ]
4379/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004380/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004381/// [ only if VendorAttributesAllowed=true ]
4382/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004383/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004384/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004385///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004386void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4387 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004388 bool CXX11AttributesAllowed,
Alp Toker62c5b572013-11-26 01:30:10 +00004389 bool AtomicAllowed,
4390 bool IdentifierRequired) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004391 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004392 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004393 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004394 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004395 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004396 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004397
4398 SourceLocation EndLoc;
4399
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004400 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004401 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004402 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004403 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004404 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004405
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004406 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004407 case tok::code_completion:
4408 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004409 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004410
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004411 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004412 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004413 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004414 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004415 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004416 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004417 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004418 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004419 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004420 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004421 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004422 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004423 case tok::kw__Atomic:
4424 if (!AtomicAllowed)
4425 goto DoneWithTypeQuals;
4426 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4427 getLangOpts());
4428 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004429
4430 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004431 case tok::kw___private:
4432 case tok::kw___global:
4433 case tok::kw___local:
4434 case tok::kw___constant:
4435 case tok::kw___read_only:
4436 case tok::kw___write_only:
4437 case tok::kw___read_write:
Aaron Ballman05d76ea2014-01-14 01:29:54 +00004438 ParseOpenCLQualifiers(DS.getAttributes());
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004439 break;
4440
Aaron Ballman317a77f2013-05-22 23:25:32 +00004441 case tok::kw___uptr:
Alp Toker62c5b572013-11-26 01:30:10 +00004442 // GNU libc headers in C mode use '__uptr' as an identifer which conflicts
4443 // with the MS modifier keyword.
4444 if (VendorAttributesAllowed && !getLangOpts().CPlusPlus &&
Alp Toker47642d22013-12-03 06:13:01 +00004445 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
4446 if (TryKeywordIdentFallback(false))
4447 continue;
Alp Toker62c5b572013-11-26 01:30:10 +00004448 }
4449 case tok::kw___sptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004450 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004451 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004452 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004453 case tok::kw___cdecl:
4454 case tok::kw___stdcall:
4455 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004456 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004457 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004458 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004459 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004460 continue;
4461 }
4462 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004463 case tok::kw___pascal:
4464 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004465 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004466 continue;
4467 }
4468 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004469 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004470 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004471 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004472 continue; // do *not* consume the next token!
4473 }
4474 // otherwise, FALL THROUGH!
4475 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004476 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004477 // If this is not a type-qualifier token, we're done reading type
4478 // qualifiers. First verify that DeclSpec's are consistent.
Erik Verbruggen888d52a2014-01-15 09:15:43 +00004479 DS.Finish(Diags, PP, Actions.getASTContext().getPrintingPolicy());
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004480 if (EndLoc.isValid())
4481 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004482 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004483 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004484
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004485 // If the specifier combination wasn't legal, issue a diagnostic.
4486 if (isInvalid) {
4487 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004488 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004489 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004490 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004491 }
4492}
4493
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004494
4495/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4496///
4497void Parser::ParseDeclarator(Declarator &D) {
4498 /// This implements the 'declarator' production in the C grammar, then checks
4499 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004500 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004501}
4502
Richard Smith0efa75c2012-03-29 01:16:42 +00004503static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4504 if (Kind == tok::star || Kind == tok::caret)
4505 return true;
4506
4507 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4508 if (!Lang.CPlusPlus)
4509 return false;
4510
4511 return Kind == tok::amp || Kind == tok::ampamp;
4512}
4513
Sebastian Redlbd150f42008-11-21 19:14:01 +00004514/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4515/// is parsed by the function passed to it. Pass null, and the direct-declarator
4516/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004517/// ptr-operator production.
4518///
Richard Smith09f76ee2011-10-19 21:33:05 +00004519/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004520/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4521/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004522///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004523/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4524/// [C] pointer[opt] direct-declarator
4525/// [C++] direct-declarator
4526/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004527///
4528/// pointer: [C99 6.7.5]
4529/// '*' type-qualifier-list[opt]
4530/// '*' type-qualifier-list[opt] pointer
4531///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004532/// ptr-operator:
4533/// '*' cv-qualifier-seq[opt]
4534/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004535/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004536/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004537/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004538/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004539void Parser::ParseDeclaratorInternal(Declarator &D,
4540 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004541 if (Diags.hasAllExtensionsSilenced())
4542 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004543
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004544 // C++ member pointers start with a '::' or a nested-name.
4545 // Member pointers get special handling, since there's no place for the
4546 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004547 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004548 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4549 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004550 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4551 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004552 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004553 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004554
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004555 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004556 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004557 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004558 if (D.mayHaveIdentifier())
4559 D.getCXXScopeSpec() = SS;
4560 else
4561 AnnotateScopeToken(SS, true);
4562
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004563 if (DirectDeclParser)
4564 (this->*DirectDeclParser)(D);
4565 return;
4566 }
4567
4568 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004569 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004570 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004571 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004572 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004573
4574 // Recurse to parse whatever is left.
4575 ParseDeclaratorInternal(D, DirectDeclParser);
4576
4577 // Sema will have to catch (syntactically invalid) pointers into global
4578 // scope. It has to catch pointers into namespace scope anyway.
4579 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004580 Loc),
4581 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004582 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004583 return;
4584 }
4585 }
4586
4587 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004588 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004589 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004590 if (DirectDeclParser)
4591 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004592 return;
4593 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004594
Sebastian Redled0f3b02009-03-15 22:02:01 +00004595 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4596 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004597 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004598 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004599
Chris Lattner9eac9312009-03-27 04:18:06 +00004600 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004601 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004602 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004603
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004604 // FIXME: GNU attributes are not allowed here in a new-type-id.
Alp Toker62c5b572013-11-26 01:30:10 +00004605 ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004606 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004607
Bill Wendling3708c182007-05-27 10:15:43 +00004608 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004609 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004610 if (Kind == tok::star)
4611 // Remember that we parsed a pointer type, and remember the type-quals.
4612 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004613 DS.getConstSpecLoc(),
4614 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004615 DS.getRestrictSpecLoc()),
4616 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004617 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004618 else
4619 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004620 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004621 Loc),
4622 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004623 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004624 } else {
4625 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004626 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004627
Sebastian Redl3b27be62009-03-23 00:00:23 +00004628 // Complain about rvalue references in C++03, but then go on and build
4629 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004630 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004631 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004632 diag::warn_cxx98_compat_rvalue_reference :
4633 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004634
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004635 // GNU-style and C++11 attributes are allowed here, as is restrict.
4636 ParseTypeQualifierListOpt(DS);
4637 D.ExtendWithDeclSpec(DS);
4638
Bill Wendling93efb222007-06-02 23:28:54 +00004639 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4640 // cv-qualifiers are introduced through the use of a typedef or of a
4641 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004642 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4643 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4644 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004645 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004646 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4647 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004648 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004649 // 'restrict' is permitted as an extension.
4650 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4651 Diag(DS.getAtomicSpecLoc(),
4652 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004653 }
Bill Wendling3708c182007-05-27 10:15:43 +00004654
4655 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004656 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004657
Douglas Gregor66583c52008-11-03 15:51:28 +00004658 if (D.getNumTypeObjects() > 0) {
4659 // C++ [dcl.ref]p4: There shall be no references to references.
4660 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4661 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004662 if (const IdentifierInfo *II = D.getIdentifier())
4663 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4664 << II;
4665 else
4666 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4667 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004668
Sebastian Redlbd150f42008-11-21 19:14:01 +00004669 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004670 // can go ahead and build the (technically ill-formed)
4671 // declarator: reference collapsing will take care of it.
4672 }
4673 }
4674
Richard Smith8e1ac332013-03-28 01:55:44 +00004675 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004676 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004677 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004678 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004679 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004680 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004681}
4682
Richard Smith0efa75c2012-03-29 01:16:42 +00004683static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4684 SourceLocation EllipsisLoc) {
4685 if (EllipsisLoc.isValid()) {
4686 FixItHint Insertion;
4687 if (!D.getEllipsisLoc().isValid()) {
4688 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4689 D.setEllipsisLoc(EllipsisLoc);
4690 }
4691 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4692 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4693 }
4694}
4695
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004696/// ParseDirectDeclarator
4697/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004698/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004699/// '(' declarator ')'
4700/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004701/// [C90] direct-declarator '[' constant-expression[opt] ']'
4702/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4703/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4704/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4705/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004706/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4707/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004708/// direct-declarator '(' parameter-type-list ')'
4709/// direct-declarator '(' identifier-list[opt] ')'
4710/// [GNU] direct-declarator '(' parameter-forward-declarations
4711/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004712/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4713/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004714/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4715/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4716/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004717/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004718/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004719///
4720/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004721/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004722/// '::'[opt] nested-name-specifier[opt] type-name
4723///
4724/// id-expression: [C++ 5.1]
4725/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004726/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004727///
4728/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004729/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004730/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004731/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004732/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004733/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004734///
Richard Smith1453e312012-03-27 01:42:32 +00004735/// Note, any additional constructs added here may need corresponding changes
4736/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004737void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004738 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004739
David Blaikiebbafb8a2012-03-11 07:00:24 +00004740 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004741 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004742 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004743 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4744 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004745 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004746 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004747 }
4748
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004749 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004750 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004751 // Change the declaration context for name lookup, until this function
4752 // is exited (and the declarator has been parsed).
4753 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004754 }
4755
Douglas Gregor27b4c162010-12-23 22:44:42 +00004756 // C++0x [dcl.fct]p14:
4757 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004758 // of a parameter-declaration-clause without a preceding comma. In
4759 // this case, the ellipsis is parsed as part of the
4760 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004761 // parameter pack that has not been expanded; otherwise, it is parsed
4762 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004763 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004764 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004765 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004766 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004767 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004768 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004769 !Actions.containsUnexpandedParameterPacks(D))) {
4770 SourceLocation EllipsisLoc = ConsumeToken();
4771 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4772 // The ellipsis was put in the wrong place. Recover, and explain to
4773 // the user what they should have done.
4774 ParseDeclarator(D);
4775 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4776 return;
4777 } else
4778 D.setEllipsisLoc(EllipsisLoc);
4779
4780 // The ellipsis can't be followed by a parenthesized declarator. We
4781 // check for that in ParseParenDeclarator, after we have disambiguated
4782 // the l_paren token.
4783 }
4784
Douglas Gregor7861a802009-11-03 01:35:08 +00004785 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4786 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4787 // We found something that indicates the start of an unqualified-id.
4788 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004789 bool AllowConstructorName;
4790 if (D.getDeclSpec().hasTypeSpecifier())
4791 AllowConstructorName = false;
4792 else if (D.getCXXScopeSpec().isSet())
4793 AllowConstructorName =
4794 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004795 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004796 else
4797 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4798
Abramo Bagnara7945c982012-01-27 09:46:47 +00004799 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004800 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4801 /*EnteringContext=*/true,
4802 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004803 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004804 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004805 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004806 D.getName()) ||
4807 // Once we're past the identifier, if the scope was bad, mark the
4808 // whole declarator bad.
4809 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004810 D.SetIdentifier(0, Tok.getLocation());
4811 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004812 } else {
4813 // Parsed the unqualified-id; update range information and move along.
4814 if (D.getSourceRange().getBegin().isInvalid())
4815 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4816 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004817 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004818 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004819 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004820 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004821 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004822 "There's a C++-specific check for tok::identifier above");
4823 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4824 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4825 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004826 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004827 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004828 // A virt-specifier isn't treated as an identifier if it appears after a
4829 // trailing-return-type.
4830 if (D.getContext() != Declarator::TrailingReturnContext ||
4831 !isCXX11VirtSpecifier(Tok)) {
4832 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4833 << FixItHint::CreateRemoval(Tok.getLocation());
4834 D.SetIdentifier(0, Tok.getLocation());
4835 ConsumeToken();
4836 goto PastIdentifier;
4837 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004838 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004839
Douglas Gregor7861a802009-11-03 01:35:08 +00004840 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004841 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004842 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004843 // Example: 'char (*X)' or 'int (*XX)(void)'
4844 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004845
4846 // If the declarator was parenthesized, we entered the declarator
4847 // scope when parsing the parenthesized declarator, then exited
4848 // the scope already. Re-enter the scope, if we need to.
4849 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004850 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004851 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004852 if (!D.isInvalidType() &&
4853 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004854 // Change the declaration context for name lookup, until this function
4855 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004856 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004857 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004858 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004859 // This could be something simple like "int" (in which case the declarator
4860 // portion is empty), if an abstract-declarator is allowed.
4861 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004862
4863 // The grammar for abstract-pack-declarator does not allow grouping parens.
4864 // FIXME: Revisit this once core issue 1488 is resolved.
4865 if (D.hasEllipsis() && D.hasGroupingParens())
4866 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4867 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004868 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004869 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004870 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004871 if (D.getContext() == Declarator::MemberContext)
4872 Diag(Tok, diag::err_expected_member_name_or_semi)
4873 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004874 else if (getLangOpts().CPlusPlus) {
4875 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4876 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004877 else {
4878 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4879 if (Tok.isAtStartOfLine() && Loc.isValid())
4880 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4881 << getLangOpts().CPlusPlus;
4882 else
4883 Diag(Tok, diag::err_expected_unqualified_id)
4884 << getLangOpts().CPlusPlus;
4885 }
Richard Trieu9c672672013-01-26 02:31:38 +00004886 } else
Alp Tokerec543272013-12-24 09:48:30 +00004887 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_paren;
Chris Lattnereec40f92006-08-06 21:55:29 +00004888 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004889 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004890 }
Mike Stump11289f42009-09-09 15:08:12 +00004891
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004892 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004893 assert(D.isPastIdentifier() &&
4894 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004895
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004896 // Don't parse attributes unless we have parsed an unparenthesized name.
4897 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004898 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004899
Chris Lattneracd58a32006-08-06 17:24:14 +00004900 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004901 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004902 // Enter function-declaration scope, limiting any declarators to the
4903 // function prototype scope, including parameter declarators.
4904 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004905 Scope::FunctionPrototypeScope|Scope::DeclScope|
4906 (D.isFunctionDeclaratorAFunctionDeclaration()
4907 ? Scope::FunctionDeclarationScope : 0));
4908
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004909 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4910 // In such a case, check if we actually have a function declarator; if it
4911 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004912 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004913 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4914 // The name of the declarator, if any, is tentatively declared within
4915 // a possible direct initializer.
4916 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4917 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4918 TentativelyDeclaredIdentifiers.pop_back();
4919 if (!IsFunctionDecl)
4920 break;
4921 }
John McCall084e83d2011-03-24 11:26:52 +00004922 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004923 BalancedDelimiterTracker T(*this, tok::l_paren);
4924 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004925 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004926 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004927 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004928 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004929 } else {
4930 break;
4931 }
4932 }
Chad Rosierc1183952012-06-26 22:30:43 +00004933}
Chris Lattneracd58a32006-08-06 17:24:14 +00004934
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004935/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4936/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004937/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004938/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4939///
4940/// direct-declarator:
4941/// '(' declarator ')'
4942/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004943/// direct-declarator '(' parameter-type-list ')'
4944/// direct-declarator '(' identifier-list[opt] ')'
4945/// [GNU] direct-declarator '(' parameter-forward-declarations
4946/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004947///
4948void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004949 BalancedDelimiterTracker T(*this, tok::l_paren);
4950 T.consumeOpen();
4951
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004952 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004953
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004954 // Eat any attributes before we look at whether this is a grouping or function
4955 // declarator paren. If this is a grouping paren, the attribute applies to
4956 // the type being built up, for example:
4957 // int (__attribute__(()) *x)(long y)
4958 // If this ends up not being a grouping paren, the attribute applies to the
4959 // first argument, for example:
4960 // int (__attribute__(()) int x)
4961 // In either case, we need to eat any attributes to be able to determine what
4962 // sort of paren this is.
4963 //
John McCall084e83d2011-03-24 11:26:52 +00004964 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004965 bool RequiresArg = false;
4966 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004967 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004968
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004969 // We require that the argument list (if this is a non-grouping paren) be
4970 // present even if the attribute list was empty.
4971 RequiresArg = true;
4972 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004973
Steve Naroff44ac7772008-12-25 14:16:32 +00004974 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004975 ParseMicrosoftTypeAttributes(attrs);
4976
Dawn Perchik335e16b2010-09-03 01:29:35 +00004977 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004978 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004979 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004980
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004981 // If we haven't past the identifier yet (or where the identifier would be
4982 // stored, if this is an abstract declarator), then this is probably just
4983 // grouping parens. However, if this could be an abstract-declarator, then
4984 // this could also be the start of function arguments (consider 'void()').
4985 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004986
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004987 if (!D.mayOmitIdentifier()) {
4988 // If this can't be an abstract-declarator, this *must* be a grouping
4989 // paren, because we haven't seen the identifier yet.
4990 isGrouping = true;
4991 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004992 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4993 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004994 isDeclarationSpecifier() || // 'int(int)' is a function.
4995 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004996 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4997 // considered to be a type, not a K&R identifier-list.
4998 isGrouping = false;
4999 } else {
5000 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
5001 isGrouping = true;
5002 }
Mike Stump11289f42009-09-09 15:08:12 +00005003
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005004 // If this is a grouping paren, handle:
5005 // direct-declarator: '(' declarator ')'
5006 // direct-declarator: '(' attributes declarator ')'
5007 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005008 SourceLocation EllipsisLoc = D.getEllipsisLoc();
5009 D.setEllipsisLoc(SourceLocation());
5010
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005011 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005012 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00005013 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005014 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005015 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00005016 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005017 T.getCloseLocation()),
5018 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005019
5020 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00005021
5022 // An ellipsis cannot be placed outside parentheses.
5023 if (EllipsisLoc.isValid())
5024 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
5025
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005026 return;
5027 }
Mike Stump11289f42009-09-09 15:08:12 +00005028
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005029 // Okay, if this wasn't a grouping paren, it must be the start of a function
5030 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005031 // identifier (and remember where it would have been), then call into
5032 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005033 D.SetIdentifier(0, Tok.getLocation());
5034
David Blaikie15a430a2011-12-04 05:04:18 +00005035 // Enter function-declaration scope, limiting any declarators to the
5036 // function prototype scope, including parameter declarators.
5037 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005038 Scope::FunctionPrototypeScope | Scope::DeclScope |
5039 (D.isFunctionDeclaratorAFunctionDeclaration()
5040 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00005041 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00005042 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005043}
5044
5045/// ParseFunctionDeclarator - We are after the identifier and have parsed the
5046/// declarator D up to a paren, which indicates that we are parsing function
5047/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00005048///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005049/// If FirstArgAttrs is non-null, then the caller parsed those arguments
5050/// immediately after the open paren - they should be considered to be the
5051/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005052///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005053/// If RequiresArg is true, then the first argument of the function is required
5054/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005055///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005056/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5057/// (C++11) ref-qualifier[opt], exception-specification[opt],
5058/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5059///
5060/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00005061/// dynamic-exception-specification
5062/// noexcept-specification
5063///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005064void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005065 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005066 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00005067 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005068 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00005069 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00005070 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00005071 // lparen is already consumed!
5072 assert(D.isPastIdentifier() && "Should not call before identifier!");
5073
5074 // This should be true when the function has typed arguments.
5075 // Otherwise, it is treated as a K&R-style function.
5076 bool HasProto = false;
5077 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005078 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005079 // Remember where we see an ellipsis, if any.
5080 SourceLocation EllipsisLoc;
5081
5082 DeclSpec DS(AttrFactory);
5083 bool RefQualifierIsLValueRef = true;
5084 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00005085 SourceLocation ConstQualifierLoc;
5086 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005087 ExceptionSpecificationType ESpecType = EST_None;
5088 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005089 SmallVector<ParsedType, 2> DynamicExceptions;
5090 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005091 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005092 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00005093 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005094
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005095 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5096 EndLoc is the end location for the function declarator.
5097 They differ for trailing return types. */
5098 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005099 SourceLocation LParenLoc, RParenLoc;
5100 LParenLoc = Tracker.getOpenLocation();
5101 StartLoc = LParenLoc;
5102
Douglas Gregor9e66af42011-07-05 16:44:18 +00005103 if (isFunctionDeclaratorIdentifierList()) {
5104 if (RequiresArg)
5105 Diag(Tok, diag::err_argument_required_after_attribute);
5106
5107 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5108
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005109 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005110 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005111 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005112 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005113 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005114 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005115 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5116 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005117 else if (RequiresArg)
5118 Diag(Tok, diag::err_argument_required_after_attribute);
5119
David Blaikiebbafb8a2012-03-11 07:00:24 +00005120 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005121
5122 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005123 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005124 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005125 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005126 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005127
David Blaikiebbafb8a2012-03-11 07:00:24 +00005128 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005129 // FIXME: Accept these components in any order, and produce fixits to
5130 // correct the order if the user gets it wrong. Ideally we should deal
5131 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005132
5133 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005134 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5135 /*CXX11AttributesAllowed*/ false,
5136 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005137 if (!DS.getSourceRange().getEnd().isInvalid()) {
5138 EndLoc = DS.getSourceRange().getEnd();
5139 ConstQualifierLoc = DS.getConstSpecLoc();
5140 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5141 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005142
5143 // Parse ref-qualifier[opt].
5144 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005145 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005146 diag::warn_cxx98_compat_ref_qualifier :
5147 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005148
Douglas Gregor9e66af42011-07-05 16:44:18 +00005149 RefQualifierIsLValueRef = Tok.is(tok::amp);
5150 RefQualifierLoc = ConsumeToken();
5151 EndLoc = RefQualifierLoc;
5152 }
5153
Douglas Gregor3024f072012-04-16 07:05:22 +00005154 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005155 // If a declaration declares a member function or member function
5156 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005157 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005158 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005159 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005160 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005161 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005162 getLangOpts().CPlusPlus11 &&
Richard Smith990a6922014-01-17 21:01:18 +00005163 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005164 (D.getContext() == Declarator::MemberContext
5165 ? !D.getDeclSpec().isFriendSpecified()
5166 : D.getContext() == Declarator::FileContext &&
5167 D.getCXXScopeSpec().isValid() &&
5168 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005169 Sema::CXXThisScopeRAII ThisScope(Actions,
5170 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005171 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005172 (D.getDeclSpec().isConstexprSpecified() &&
5173 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005174 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005175 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005176
Douglas Gregor9e66af42011-07-05 16:44:18 +00005177 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005178 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005179 DynamicExceptions,
5180 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005181 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005182 if (ESpecType != EST_None)
5183 EndLoc = ESpecRange.getEnd();
5184
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005185 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5186 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005187 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005188
Douglas Gregor9e66af42011-07-05 16:44:18 +00005189 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005190 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005191 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005192 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005193 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5194 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005195 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005196 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005197 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005198 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005199 }
5200 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005201 }
5202
5203 // Remember that we parsed a function type, and remember the attributes.
5204 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005205 IsAmbiguous,
5206 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005207 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005208 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005209 DS.getTypeQualifiers(),
5210 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005211 RefQualifierLoc, ConstQualifierLoc,
5212 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005213 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005214 ESpecType, ESpecRange.getBegin(),
5215 DynamicExceptions.data(),
5216 DynamicExceptionRanges.data(),
5217 DynamicExceptions.size(),
5218 NoexceptExpr.isUsable() ?
5219 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005220 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005221 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005222 FnAttrs, EndLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005223}
5224
5225/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5226/// identifier list form for a K&R-style function: void foo(a,b,c)
5227///
5228/// Note that identifier-lists are only allowed for normal declarators, not for
5229/// abstract-declarators.
5230bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005231 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005232 && Tok.is(tok::identifier)
5233 && !TryAltiVecVectorToken()
5234 // K&R identifier lists can't have typedefs as identifiers, per C99
5235 // 6.7.5.3p11.
5236 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5237 // Identifier lists follow a really simple grammar: the identifiers can
5238 // be followed *only* by a ", identifier" or ")". However, K&R
5239 // identifier lists are really rare in the brave new modern world, and
5240 // it is very common for someone to typo a type in a non-K&R style
5241 // list. If we are presented with something like: "void foo(intptr x,
5242 // float y)", we don't want to start parsing the function declarator as
5243 // though it is a K&R style declarator just because intptr is an
5244 // invalid type.
5245 //
5246 // To handle this, we check to see if the token after the first
5247 // identifier is a "," or ")". Only then do we parse it as an
5248 // identifier list.
5249 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5250}
5251
5252/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5253/// we found a K&R-style identifier list instead of a typed parameter list.
5254///
5255/// After returning, ParamInfo will hold the parsed parameters.
5256///
5257/// identifier-list: [C99 6.7.5]
5258/// identifier
5259/// identifier-list ',' identifier
5260///
5261void Parser::ParseFunctionDeclaratorIdentifierList(
5262 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005263 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005264 // If there was no identifier specified for the declarator, either we are in
5265 // an abstract-declarator, or we are in a parameter declarator which was found
5266 // to be abstract. In abstract-declarators, identifier lists are not valid:
5267 // diagnose this.
5268 if (!D.getIdentifier())
5269 Diag(Tok, diag::ext_ident_list_in_param);
5270
5271 // Maintain an efficient lookup of params we have seen so far.
5272 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5273
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005274 do {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005275 // If this isn't an identifier, report the error and skip until ')'.
5276 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00005277 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00005278 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005279 // Forget we parsed anything.
5280 ParamInfo.clear();
5281 return;
5282 }
5283
5284 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5285
5286 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5287 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5288 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5289
5290 // Verify that the argument identifier has not already been mentioned.
5291 if (!ParamsSoFar.insert(ParmII)) {
5292 Diag(Tok, diag::err_param_redefinition) << ParmII;
5293 } else {
5294 // Remember this identifier in ParamInfo.
5295 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5296 Tok.getLocation(),
5297 0));
5298 }
5299
5300 // Eat the identifier.
5301 ConsumeToken();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005302 // The list continues if we see a comma.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005303 } while (TryConsumeToken(tok::comma));
Douglas Gregor9e66af42011-07-05 16:44:18 +00005304}
5305
5306/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5307/// after the opening parenthesis. This function will not parse a K&R-style
5308/// identifier list.
5309///
Richard Smith2620cd92012-04-11 04:01:28 +00005310/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5311/// caller parsed those arguments immediately after the open paren - they should
5312/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005313///
5314/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5315/// be the location of the ellipsis, if any was parsed.
5316///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005317/// parameter-type-list: [C99 6.7.5]
5318/// parameter-list
5319/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005320/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005321///
5322/// parameter-list: [C99 6.7.5]
5323/// parameter-declaration
5324/// parameter-list ',' parameter-declaration
5325///
5326/// parameter-declaration: [C99 6.7.5]
5327/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005328/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005329/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005330/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005331/// declaration-specifiers abstract-declarator[opt]
5332/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005333/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005334/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005335/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005336///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005337void Parser::ParseParameterDeclarationClause(
5338 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005339 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005340 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005341 SourceLocation &EllipsisLoc) {
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005342 do {
5343 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5344 // before deciding this was a parameter-declaration-clause.
5345 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Chris Lattner371ed4e2008-04-06 06:57:35 +00005346 break;
Mike Stump11289f42009-09-09 15:08:12 +00005347
Chris Lattner371ed4e2008-04-06 06:57:35 +00005348 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005349 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005350 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005351
Richard Smith2620cd92012-04-11 04:01:28 +00005352 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005353 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005354
John McCall53fa7142010-12-24 02:08:15 +00005355 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005356 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005357
5358 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005359
5360 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005361 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005362 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005363 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5364 // too much hassle.
5365 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005366
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005367 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005368
Faisal Vali2b391ab2013-09-26 19:54:12 +00005369
5370 // Parse the declarator. This is "PrototypeContext" or
5371 // "LambdaExprParameterContext", because we must accept either
5372 // 'declarator' or 'abstract-declarator' here.
5373 Declarator ParmDeclarator(DS,
5374 D.getContext() == Declarator::LambdaExprContext ?
5375 Declarator::LambdaExprParameterContext :
5376 Declarator::PrototypeContext);
5377 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005378
5379 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005380 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005381
Chris Lattner371ed4e2008-04-06 06:57:35 +00005382 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005383 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005384
Douglas Gregor4d87df52008-12-16 21:30:33 +00005385 // DefArgToks is used when the parsing of default arguments needs
5386 // to be delayed.
5387 CachedTokens *DefArgToks = 0;
5388
Chris Lattner371ed4e2008-04-06 06:57:35 +00005389 // If no parameter was specified, verify that *something* was specified,
5390 // otherwise we have a missing type and identifier.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005391 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5392 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005393 // Completely missing, emit error.
5394 Diag(DSStart, diag::err_missing_param);
5395 } else {
5396 // Otherwise, we have something. Add it and let semantic analysis try
5397 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005398
Chris Lattner371ed4e2008-04-06 06:57:35 +00005399 // Inform the actions module about the parameter declarator, so it gets
5400 // added to the current scope.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005401 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5402 ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005403 // Parse the default argument, if any. We parse the default
5404 // arguments in all dialects; the semantic analysis in
5405 // ActOnParamDefaultArgument will reject the default argument in
5406 // C.
5407 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005408 SourceLocation EqualLoc = Tok.getLocation();
5409
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005410 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005411 if (D.getContext() == Declarator::MemberContext) {
5412 // If we're inside a class definition, cache the tokens
5413 // corresponding to the default argument. We'll actually parse
5414 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005415 // FIXME: Can we use a smart pointer for Toks?
5416 DefArgToks = new CachedTokens;
5417
Richard Smith1fff95c2013-09-12 23:28:08 +00005418 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005419 delete DefArgToks;
5420 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005421 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005422 } else {
5423 // Mark the end of the default argument so that we know when to
5424 // stop when we parse it later on.
5425 Token DefArgEnd;
5426 DefArgEnd.startToken();
5427 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5428 DefArgEnd.setLocation(Tok.getLocation());
5429 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005430 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005431 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005432 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005433 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005434 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005435 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005436
Chad Rosierc1183952012-06-26 22:30:43 +00005437 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005438 // used.
5439 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005440 Sema::PotentiallyEvaluatedIfUsed,
5441 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005442
Sebastian Redldb63af22012-03-14 15:54:00 +00005443 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005444 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005445 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005446 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005447 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005448 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005449 if (DefArgResult.isInvalid()) {
5450 Actions.ActOnParamDefaultArgumentError(Param);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005451 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00005452 } else {
5453 // Inform the actions module about the default argument
5454 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005455 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005456 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005457 }
5458 }
Mike Stump11289f42009-09-09 15:08:12 +00005459
5460 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005461 ParmDeclarator.getIdentifierLoc(),
5462 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005463 }
5464
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005465 if (TryConsumeToken(tok::ellipsis, EllipsisLoc) &&
5466 !getLangOpts().CPlusPlus) {
5467 // We have ellipsis without a preceding ',', which is ill-formed
5468 // in C. Complain and provide the fix.
5469 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
5470 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005471 break;
5472 }
Mike Stump11289f42009-09-09 15:08:12 +00005473
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005474 // If the next token is a comma, consume it and keep reading arguments.
5475 } while (TryConsumeToken(tok::comma));
Chris Lattner6c940e62008-04-06 06:34:08 +00005476}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005477
Chris Lattnere8074e62006-08-06 18:30:15 +00005478/// [C90] direct-declarator '[' constant-expression[opt] ']'
5479/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5480/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5481/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5482/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005483/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5484/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005485void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005486 if (CheckProhibitedCXX11Attribute())
5487 return;
5488
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005489 BalancedDelimiterTracker T(*this, tok::l_square);
5490 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005491
Chris Lattner84a11622008-12-18 07:27:21 +00005492 // C array syntax has many features, but by-far the most common is [] and [4].
5493 // This code does a fast path to handle some of the most obvious cases.
5494 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005495 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005496 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005497 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005498
Chris Lattner84a11622008-12-18 07:27:21 +00005499 // Remember that we parsed the empty array type.
John McCall084e83d2011-03-24 11:26:52 +00005500 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005501 T.getOpenLocation(),
5502 T.getCloseLocation()),
5503 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005504 return;
5505 } else if (Tok.getKind() == tok::numeric_constant &&
5506 GetLookAheadToken(1).is(tok::r_square)) {
5507 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005508 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005509 ConsumeToken();
5510
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005511 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005512 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005513 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005514
Chris Lattner84a11622008-12-18 07:27:21 +00005515 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005516 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005517 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005518 T.getOpenLocation(),
5519 T.getCloseLocation()),
5520 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005521 return;
5522 }
Mike Stump11289f42009-09-09 15:08:12 +00005523
Chris Lattnere8074e62006-08-06 18:30:15 +00005524 // If valid, this location is the position where we read the 'static' keyword.
5525 SourceLocation StaticLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005526 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005527
Chris Lattnere8074e62006-08-06 18:30:15 +00005528 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005529 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005530 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005531 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005532
Chris Lattnere8074e62006-08-06 18:30:15 +00005533 // If we haven't already read 'static', check to see if there is one after the
5534 // type-qualifier-list.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005535 if (!StaticLoc.isValid())
5536 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005537
Chris Lattnere8074e62006-08-06 18:30:15 +00005538 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005539 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005540 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005541
Chris Lattner521ff2b2008-04-06 05:26:30 +00005542 // Handle the case where we have '[*]' as the array size. However, a leading
5543 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005544 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005545 // infrequent, use of lookahead is not costly here.
5546 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005547 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005548
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005549 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005550 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005551 StaticLoc = SourceLocation(); // Drop the static.
5552 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005553 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005554 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005555 // Note, in C89, this production uses the constant-expr production instead
5556 // of assignment-expr. The only difference is that assignment-expr allows
5557 // things like '=' and '*='. Sema rejects these in C89 mode because they
5558 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005559
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005560 // Parse the constant-expression or assignment-expression now (depending
5561 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005562 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005563 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005564 } else {
5565 EnterExpressionEvaluationContext Unevaluated(Actions,
5566 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005567 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005568 }
Chris Lattner62591722006-08-12 18:40:58 +00005569 }
Mike Stump11289f42009-09-09 15:08:12 +00005570
Chris Lattner62591722006-08-12 18:40:58 +00005571 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005572 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005573 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005574 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00005575 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00005576 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005577 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005578
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005579 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005580
John McCall084e83d2011-03-24 11:26:52 +00005581 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005582 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005583
Chris Lattner84a11622008-12-18 07:27:21 +00005584 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005585 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005586 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005587 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005588 T.getOpenLocation(),
5589 T.getCloseLocation()),
5590 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005591}
5592
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005593/// [GNU] typeof-specifier:
5594/// typeof ( expressions )
5595/// typeof ( type-name )
5596/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005597///
5598void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005599 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005600 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005601 SourceLocation StartLoc = ConsumeToken();
5602
John McCalle8595032010-01-13 20:03:27 +00005603 const bool hasParens = Tok.is(tok::l_paren);
5604
Eli Friedman15681d62012-09-26 04:34:21 +00005605 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5606 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005607
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005608 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005609 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005610 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005611 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5612 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005613 if (hasParens)
5614 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005615
5616 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005617 // FIXME: Not accurate, the range gets one token more than it should.
5618 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005619 else
5620 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005621
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005622 if (isCastExpr) {
5623 if (!CastTy) {
5624 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005625 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005626 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005627
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005628 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005629 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005630 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5631 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005632 DiagID, CastTy,
5633 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00005634 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005635 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005636 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005637
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005638 // If we get here, the operand to the typeof was an expresion.
5639 if (Operand.isInvalid()) {
5640 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005641 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005642 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005643
Eli Friedmane0afc982012-01-21 01:01:51 +00005644 // We might need to transform the operand if it is potentially evaluated.
5645 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5646 if (Operand.isInvalid()) {
5647 DS.SetTypeSpecError();
5648 return;
5649 }
5650
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005651 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005652 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005653 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5654 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005655 DiagID, Operand.get(),
5656 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00005657 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005658}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005659
Benjamin Kramere56f3932011-12-23 17:00:35 +00005660/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005661/// _Atomic ( type-name )
5662///
5663void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005664 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5665 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005666
5667 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005668 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005669 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005670 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005671
5672 TypeResult Result = ParseTypeName();
5673 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00005674 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00005675 return;
5676 }
5677
5678 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005679 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005680
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005681 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005682 return;
5683
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005684 DS.setTypeofParensRange(T.getRange());
5685 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005686
5687 const char *PrevSpec = 0;
5688 unsigned DiagID;
5689 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005690 DiagID, Result.release(),
5691 Actions.getASTContext().getPrintingPolicy()))
Eli Friedman0dfb8892011-10-06 23:00:33 +00005692 Diag(StartLoc, DiagID) << PrevSpec;
5693}
5694
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005695
5696/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5697/// from TryAltiVecVectorToken.
5698bool Parser::TryAltiVecVectorTokenOutOfLine() {
5699 Token Next = NextToken();
5700 switch (Next.getKind()) {
5701 default: return false;
5702 case tok::kw_short:
5703 case tok::kw_long:
5704 case tok::kw_signed:
5705 case tok::kw_unsigned:
5706 case tok::kw_void:
5707 case tok::kw_char:
5708 case tok::kw_int:
5709 case tok::kw_float:
5710 case tok::kw_double:
5711 case tok::kw_bool:
5712 case tok::kw___pixel:
5713 Tok.setKind(tok::kw___vector);
5714 return true;
5715 case tok::identifier:
5716 if (Next.getIdentifierInfo() == Ident_pixel) {
5717 Tok.setKind(tok::kw___vector);
5718 return true;
5719 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005720 if (Next.getIdentifierInfo() == Ident_bool) {
5721 Tok.setKind(tok::kw___vector);
5722 return true;
5723 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005724 return false;
5725 }
5726}
5727
5728bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5729 const char *&PrevSpec, unsigned &DiagID,
5730 bool &isInvalid) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005731 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005732 if (Tok.getIdentifierInfo() == Ident_vector) {
5733 Token Next = NextToken();
5734 switch (Next.getKind()) {
5735 case tok::kw_short:
5736 case tok::kw_long:
5737 case tok::kw_signed:
5738 case tok::kw_unsigned:
5739 case tok::kw_void:
5740 case tok::kw_char:
5741 case tok::kw_int:
5742 case tok::kw_float:
5743 case tok::kw_double:
5744 case tok::kw_bool:
5745 case tok::kw___pixel:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005746 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005747 return true;
5748 case tok::identifier:
5749 if (Next.getIdentifierInfo() == Ident_pixel) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005750 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005751 return true;
5752 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005753 if (Next.getIdentifierInfo() == Ident_bool) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005754 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
Bill Schmidt99a084b2013-07-03 20:54:09 +00005755 return true;
5756 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005757 break;
5758 default:
5759 break;
5760 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005761 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005762 DS.isTypeAltiVecVector()) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005763 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005764 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005765 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5766 DS.isTypeAltiVecVector()) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005767 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
Bill Schmidt99a084b2013-07-03 20:54:09 +00005768 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005769 }
5770 return false;
5771}