blob: 9835e249b1f062387962692ff669581ab9caae14 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Larisse Voufo725de3e2013-06-21 00:08:46 +000016#include "clang/AST/DeclTemplate.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000017#include "clang/Basic/AddressSpaces.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000018#include "clang/Basic/CharInfo.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000019#include "clang/Basic/OpenCL.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000021#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Sema/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000025#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000026#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000027#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// C99 6.7: Declarations.
32//===----------------------------------------------------------------------===//
33
Chris Lattnerf5fbd792006-08-10 23:56:11 +000034/// ParseTypeName
35/// type-name: [C99 6.7.6]
36/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000037///
38/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000039TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000040 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000041 AccessSpecifier AS,
Richard Smith54ecd982013-02-20 19:22:51 +000042 Decl **OwnedType,
43 ParsedAttributes *Attrs) {
Richard Smith62dad822012-03-15 01:02:11 +000044 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000045 if (DSC == DSC_normal)
46 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000047
Chris Lattnerf5fbd792006-08-10 23:56:11 +000048 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000049 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +000050 if (Attrs)
51 DS.addAttributes(Attrs->getList());
Richard Smithbfdb1082012-03-12 08:56:40 +000052 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000053 if (OwnedType)
54 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000055
Chris Lattnerf5fbd792006-08-10 23:56:11 +000056 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000057 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000058 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000059 if (Range)
60 *Range = DeclaratorInfo.getSourceRange();
61
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000062 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000063 return true;
64
Douglas Gregor0be31a22010-07-02 17:43:08 +000065 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000066}
67
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000068
69/// isAttributeLateParsed - Return true if the attribute has arguments that
70/// require late parsing.
71static bool isAttributeLateParsed(const IdentifierInfo &II) {
72 return llvm::StringSwitch<bool>(II.getName())
73#include "clang/Parse/AttrLateParsed.inc"
74 .Default(false);
75}
76
Alexis Hunt96d5c762009-11-21 08:43:09 +000077/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000078///
79/// [GNU] attributes:
80/// attribute
81/// attributes attribute
82///
83/// [GNU] attribute:
84/// '__attribute__' '(' '(' attribute-list ')' ')'
85///
86/// [GNU] attribute-list:
87/// attrib
88/// attribute_list ',' attrib
89///
90/// [GNU] attrib:
91/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000092/// attrib-name
93/// attrib-name '(' identifier ')'
94/// attrib-name '(' identifier ',' nonempty-expr-list ')'
95/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000096///
Steve Naroff0f2fe172007-06-01 17:11:19 +000097/// [GNU] attrib-name:
98/// identifier
99/// typespec
100/// typequal
101/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +0000102///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000103/// Whether an attribute takes an 'identifier' is determined by the
104/// attrib-name. GCC's behavior here is not worth imitating:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000106/// * In C mode, if the attribute argument list starts with an identifier
107/// followed by a ',' or an ')', and the identifier doesn't resolve to
108/// a type, it is parsed as an identifier. If the attribute actually
109/// wanted an expression, it's out of luck (but it turns out that no
110/// attributes work that way, because C constant expressions are very
111/// limited).
112/// * In C++ mode, if the attribute argument list starts with an identifier,
113/// and the attribute *wants* an identifier, it is parsed as an identifier.
114/// At block scope, any additional tokens between the identifier and the
115/// ',' or ')' are ignored, otherwise they produce a parse error.
Richard Smithb12bf692011-10-17 21:20:17 +0000116///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000117/// We follow the C++ model, but don't allow junk after the identifier.
John McCall53fa7142010-12-24 02:08:15 +0000118void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000119 SourceLocation *endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000120 LateParsedAttrList *LateAttrs,
121 Declarator *D) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000122 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000123
Chris Lattner76c72282007-10-09 17:33:22 +0000124 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000125 ConsumeToken();
126 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
127 "attribute")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000128 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000129 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000130 }
131 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000132 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000133 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000134 }
135 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Alp Toker094e5212014-01-05 03:27:11 +0000136 while (true) {
137 // Allow empty/non-empty attributes. ((__vector_size__(16),,,,))
138 if (TryConsumeToken(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000139 continue;
Alp Toker094e5212014-01-05 03:27:11 +0000140
141 // Expect an identifier or declaration specifier (const, int, etc.)
142 if (Tok.isNot(tok::identifier) && !isDeclarationSpecifier())
143 break;
144
Steve Naroff0f2fe172007-06-01 17:11:19 +0000145 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
146 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000147
Alp Toker094e5212014-01-05 03:27:11 +0000148 if (Tok.isNot(tok::l_paren)) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000149 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
150 AttributeList::AS_GNU);
Alp Toker094e5212014-01-05 03:27:11 +0000151 continue;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000152 }
Alp Toker094e5212014-01-05 03:27:11 +0000153
154 // Handle "parameterized" attributes
155 if (!LateAttrs || !isAttributeLateParsed(*AttrName)) {
156 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, 0,
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000157 SourceLocation(), AttributeList::AS_GNU, D);
Alp Toker094e5212014-01-05 03:27:11 +0000158 continue;
159 }
160
161 // Handle attributes with arguments that require late parsing.
162 LateParsedAttribute *LA =
163 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
164 LateAttrs->push_back(LA);
165
166 // Attributes in a class are parsed at the end of the class, along
167 // with other late-parsed declarations.
168 if (!ClassStack.empty() && !LateAttrs->parseSoon())
169 getCurrentClass().LateParsedDeclarations.push_back(LA);
170
171 // consume everything up to and including the matching right parens
172 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
173
174 Token Eof;
175 Eof.startToken();
176 Eof.setLocation(Tok.getLocation());
177 LA->Toks.push_back(Eof);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000178 }
Alp Toker094e5212014-01-05 03:27:11 +0000179
Alp Toker383d2c42014-01-01 03:08:43 +0000180 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000181 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000182 SourceLocation Loc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000183 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000184 SkipUntil(tok::r_paren, StopAtSemi);
John McCall53fa7142010-12-24 02:08:15 +0000185 if (endLoc)
186 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000187 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000188}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000189
Aaron Ballman4768b312013-11-04 12:55:56 +0000190/// \brief Normalizes an attribute name by dropping prefixed and suffixed __.
191static StringRef normalizeAttrName(StringRef Name) {
Richard Smith66e71682013-10-24 01:07:54 +0000192 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
193 Name = Name.drop_front(2).drop_back(2);
Aaron Ballman4768b312013-11-04 12:55:56 +0000194 return Name;
195}
196
197/// \brief Determine whether the given attribute has an identifier argument.
198static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
199 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Richard Smith66e71682013-10-24 01:07:54 +0000200#include "clang/Parse/AttrIdentifierArg.inc"
Douglas Gregord2472d42013-05-02 23:25:32 +0000201 .Default(false);
202}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000203
Aaron Ballman4768b312013-11-04 12:55:56 +0000204/// \brief Determine whether the given attribute parses a type argument.
205static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
206 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
207#include "clang/Parse/AttrTypeArg.inc"
208 .Default(false);
209}
210
Aaron Ballman15b27b92014-01-09 19:39:35 +0000211/// \brief Determine whether the given attribute requires parsing its arguments
212/// in an unevaluated context or not.
213static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) {
214 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
215#include "clang/Parse/AttrArgContext.inc"
216 .Default(false);
217}
218
Richard Smithfeefaf52013-09-03 18:01:40 +0000219IdentifierLoc *Parser::ParseIdentifierLoc() {
220 assert(Tok.is(tok::identifier) && "expected an identifier");
221 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
222 Tok.getLocation(),
223 Tok.getIdentifierInfo());
224 ConsumeToken();
225 return IL;
226}
227
Richard Smithb1f9a282013-10-31 01:56:18 +0000228void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
229 SourceLocation AttrNameLoc,
230 ParsedAttributes &Attrs,
231 SourceLocation *EndLoc) {
232 BalancedDelimiterTracker Parens(*this, tok::l_paren);
233 Parens.consumeOpen();
234
235 TypeResult T;
236 if (Tok.isNot(tok::r_paren))
237 T = ParseTypeName();
238
239 if (Parens.consumeClose())
240 return;
241
242 if (T.isInvalid())
243 return;
244
245 if (T.isUsable())
246 Attrs.addNewTypeAttr(&AttrName,
247 SourceRange(AttrNameLoc, Parens.getCloseLocation()), 0,
248 AttrNameLoc, T.get(), AttributeList::AS_GNU);
249 else
250 Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
251 0, AttrNameLoc, 0, 0, AttributeList::AS_GNU);
252}
253
Michael Han23214e52012-10-03 01:56:22 +0000254/// Parse the arguments to a parameterized GNU attribute or
255/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000256void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
257 SourceLocation AttrNameLoc,
258 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000259 SourceLocation *EndLoc,
260 IdentifierInfo *ScopeName,
261 SourceLocation ScopeLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000262 AttributeList::Syntax Syntax,
263 Declarator *D) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000264
265 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
266
Richard Smith66e71682013-10-24 01:07:54 +0000267 AttributeList::Kind AttrKind =
Richard Smithb1f9a282013-10-31 01:56:18 +0000268 AttributeList::getKind(AttrName, ScopeName, Syntax);
Richard Smith66e71682013-10-24 01:07:54 +0000269
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000270 // Availability attributes have their own grammar.
Richard Smithb1f9a282013-10-31 01:56:18 +0000271 // FIXME: All these cases fail to pass in the syntax and scope, and might be
272 // written as C++11 gnu:: attributes.
Richard Smith66e71682013-10-24 01:07:54 +0000273 if (AttrKind == AttributeList::AT_Availability) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000274 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
275 return;
276 }
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000277
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000278 if (AttrKind == AttributeList::AT_ObjCBridgeRelated) {
279 ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
280 return;
281 }
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000282
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000283 // Type safety attributes have their own grammar.
Richard Smith66e71682013-10-24 01:07:54 +0000284 if (AttrKind == AttributeList::AT_TypeTagForDatatype) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000285 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
286 return;
287 }
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000288
Aaron Ballman4768b312013-11-04 12:55:56 +0000289 // Some attributes expect solely a type parameter.
290 if (attributeIsTypeArgAttr(*AttrName)) {
Richard Smithb1f9a282013-10-31 01:56:18 +0000291 ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc);
292 return;
293 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000294
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000295 // These may refer to the function arguments, but need to be parsed early to
296 // participate in determining whether it's a redeclaration.
297 llvm::OwningPtr<ParseScope> PrototypeScope;
298 if (AttrName->isStr("enable_if") && D && D->isFunctionDeclarator()) {
299 DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo();
300 PrototypeScope.reset(new ParseScope(this, Scope::FunctionPrototypeScope |
301 Scope::FunctionDeclarationScope |
302 Scope::DeclScope));
303 for (unsigned i = 0; i != FTI.NumArgs; ++i) {
304 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
305 Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param);
306 }
307 }
308
Richard Smith66e71682013-10-24 01:07:54 +0000309 // Ignore the left paren location for now.
310 ConsumeParen();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000311
Aaron Ballman00e99962013-08-31 01:11:41 +0000312 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000313
Richard Smithb1f9a282013-10-31 01:56:18 +0000314 if (Tok.is(tok::identifier)) {
Richard Smith66e71682013-10-24 01:07:54 +0000315 // If this attribute wants an 'identifier' argument, make it so.
Richard Smithb1f9a282013-10-31 01:56:18 +0000316 bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName);
Richard Smith66e71682013-10-24 01:07:54 +0000317
318 // If we don't know how to parse this attribute, but this is the only
319 // token in this argument, assume it's meant to be an identifier.
Aaron Ballman66037472013-12-04 15:32:26 +0000320 if (AttrKind == AttributeList::UnknownAttribute ||
321 AttrKind == AttributeList::IgnoredAttribute) {
Richard Smith66e71682013-10-24 01:07:54 +0000322 const Token &Next = NextToken();
Richard Smithb1f9a282013-10-31 01:56:18 +0000323 IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma);
Richard Smith66e71682013-10-24 01:07:54 +0000324 }
Richard Smithb12bf692011-10-17 21:20:17 +0000325
Richard Smithb1f9a282013-10-31 01:56:18 +0000326 if (IsIdentifierArg)
327 ArgExprs.push_back(ParseIdentifierLoc());
Richard Smithb12bf692011-10-17 21:20:17 +0000328 }
329
Richard Smithb1f9a282013-10-31 01:56:18 +0000330 if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
Richard Smithb12bf692011-10-17 21:20:17 +0000331 // Eat the comma.
Aaron Ballman00e99962013-08-31 01:11:41 +0000332 if (!ArgExprs.empty())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000333 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000334
Richard Smithb12bf692011-10-17 21:20:17 +0000335 // Parse the non-empty comma-separated list of expressions.
Alp Toker8fbec672013-12-17 23:29:36 +0000336 do {
Aaron Ballman7c1fcf82014-01-09 20:12:12 +0000337 OwningPtr<EnterExpressionEvaluationContext> Unevaluated;
338 if (attributeParsedArgsUnevaluated(*AttrName))
339 Unevaluated.reset(new EnterExpressionEvaluationContext(Actions,
340 Sema::Unevaluated));
341
Richard Smithb12bf692011-10-17 21:20:17 +0000342 ExprResult ArgExpr(ParseAssignmentExpression());
343 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000344 SkipUntil(tok::r_paren, StopAtSemi);
Richard Smithb12bf692011-10-17 21:20:17 +0000345 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000346 }
Richard Smithb12bf692011-10-17 21:20:17 +0000347 ArgExprs.push_back(ArgExpr.release());
Alp Toker8fbec672013-12-17 23:29:36 +0000348 // Eat the comma, move to the next argument
349 } while (TryConsumeToken(tok::comma));
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000350 }
Richard Smithb12bf692011-10-17 21:20:17 +0000351
352 SourceLocation RParen = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000353 if (!ExpectAndConsume(tok::r_paren)) {
Michael Han360d2252012-10-04 16:42:52 +0000354 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithb1f9a282013-10-31 01:56:18 +0000355 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
356 ArgExprs.data(), ArgExprs.size(), Syntax);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000357 }
Aaron Ballman7c1fcf82014-01-09 20:12:12 +0000358
359 if (EndLoc)
360 *EndLoc = RParen;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000361}
362
Chad Rosierc1183952012-06-26 22:30:43 +0000363/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000364/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000365void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000366 SourceLocation AttrNameLoc,
367 ParsedAttributes &Attrs)
368{
369 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000370 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000371 AttrName->getNameStart(), tok::r_paren))
372 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000373
Aaron Ballman478faed2012-06-19 22:09:27 +0000374 ExprResult ArgExpr(ParseConstantExpression());
375 if (ArgExpr.isInvalid()) {
376 T.skipToEnd();
377 return;
378 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000379 ArgsUnion ExprList = ArgExpr.take();
380 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
381 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000382
383 T.consumeClose();
384}
385
Chad Rosierc1183952012-06-26 22:30:43 +0000386/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000387/// arguments.
388bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
389 return llvm::StringSwitch<bool>(Ident->getName())
390 .Case("dllimport", true)
391 .Case("dllexport", true)
392 .Case("noreturn", true)
393 .Case("nothrow", true)
394 .Case("noinline", true)
395 .Case("naked", true)
396 .Case("appdomain", true)
397 .Case("process", true)
398 .Case("jitintrinsic", true)
399 .Case("noalias", true)
400 .Case("restrict", true)
401 .Case("novtable", true)
402 .Case("selectany", true)
403 .Case("thread", true)
Aaron Ballman444eb6e2013-05-04 16:58:37 +0000404 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000405 .Default(false);
406}
407
Chad Rosierc1183952012-06-26 22:30:43 +0000408/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000409/// parameters). Will return false if we properly handled the declspec, or
410/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000411void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000412 SourceLocation Loc,
413 ParsedAttributes &Attrs) {
414 // Try to handle the easy case first -- these declspecs all take a single
415 // parameter as their argument.
416 if (llvm::StringSwitch<bool>(Ident->getName())
417 .Case("uuid", true)
418 .Case("align", true)
419 .Case("allocate", true)
420 .Default(false)) {
421 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
422 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000423 // The deprecated declspec has an optional single argument, so we will
424 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000425 // not.
426 if (Tok.getKind() == tok::l_paren)
427 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
428 else
Aaron Ballman00e99962013-08-31 01:11:41 +0000429 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000430 } else if (Ident->getName() == "property") {
431 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000432 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000433 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000434 if (Tok.isNot(tok::l_paren)) {
435 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
436 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000437 return;
John McCall5e77d762013-04-16 07:28:30 +0000438 }
439 BalancedDelimiterTracker T(*this, tok::l_paren);
440 T.expectAndConsume(diag::err_expected_lparen_after,
441 Ident->getNameStart(), tok::r_paren);
442
443 enum AccessorKind {
444 AK_Invalid = -1,
445 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
446 };
447 IdentifierInfo *AccessorNames[] = { 0, 0 };
448 bool HasInvalidAccessor = false;
449
450 // Parse the accessor specifications.
451 while (true) {
452 // Stop if this doesn't look like an accessor spec.
453 if (!Tok.is(tok::identifier)) {
454 // If the user wrote a completely empty list, use a special diagnostic.
455 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
456 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
457 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
458 break;
459 }
460
461 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
462 break;
463 }
464
465 AccessorKind Kind;
466 SourceLocation KindLoc = Tok.getLocation();
467 StringRef KindStr = Tok.getIdentifierInfo()->getName();
468 if (KindStr == "get") {
469 Kind = AK_Get;
470 } else if (KindStr == "put") {
471 Kind = AK_Put;
472
473 // Recover from the common mistake of using 'set' instead of 'put'.
474 } else if (KindStr == "set") {
475 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
476 << FixItHint::CreateReplacement(KindLoc, "put");
477 Kind = AK_Put;
478
479 // Handle the mistake of forgetting the accessor kind by skipping
480 // this accessor.
481 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
482 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
483 ConsumeToken();
484 HasInvalidAccessor = true;
485 goto next_property_accessor;
486
487 // Otherwise, complain about the unknown accessor kind.
488 } else {
489 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
490 HasInvalidAccessor = true;
491 Kind = AK_Invalid;
492
493 // Try to keep parsing unless it doesn't look like an accessor spec.
494 if (!NextToken().is(tok::equal)) break;
495 }
496
497 // Consume the identifier.
498 ConsumeToken();
499
500 // Consume the '='.
Alp Toker8fbec672013-12-17 23:29:36 +0000501 if (!TryConsumeToken(tok::equal)) {
John McCall5e77d762013-04-16 07:28:30 +0000502 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
503 << KindStr;
504 break;
505 }
506
507 // Expect the method name.
508 if (!Tok.is(tok::identifier)) {
509 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
510 break;
511 }
512
513 if (Kind == AK_Invalid) {
514 // Just drop invalid accessors.
515 } else if (AccessorNames[Kind] != NULL) {
516 // Complain about the repeated accessor, ignore it, and keep parsing.
517 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
518 } else {
519 AccessorNames[Kind] = Tok.getIdentifierInfo();
520 }
521 ConsumeToken();
522
523 next_property_accessor:
524 // Keep processing accessors until we run out.
Alp Toker094e5212014-01-05 03:27:11 +0000525 if (TryConsumeToken(tok::comma))
John McCall5e77d762013-04-16 07:28:30 +0000526 continue;
527
528 // If we run into the ')', stop without consuming it.
Alp Toker094e5212014-01-05 03:27:11 +0000529 if (Tok.is(tok::r_paren))
John McCall5e77d762013-04-16 07:28:30 +0000530 break;
Alp Toker094e5212014-01-05 03:27:11 +0000531
532 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
533 break;
John McCall5e77d762013-04-16 07:28:30 +0000534 }
535
536 // Only add the property attribute if it was well-formed.
537 if (!HasInvalidAccessor) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000538 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000539 AccessorNames[AK_Get], AccessorNames[AK_Put],
540 AttributeList::AS_Declspec);
541 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000542 T.skipToEnd();
543 } else {
544 // We don't recognize this as a valid declspec, but instead of creating the
545 // attribute and allowing sema to warn about it, we will warn here instead.
546 // This is because some attributes have multiple spellings, but we need to
547 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000548 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000549 // both locations.
550 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
551
552 // If there's an open paren, we should eat the open and close parens under
553 // the assumption that this unknown declspec has parameters.
554 BalancedDelimiterTracker T(*this, tok::l_paren);
555 if (!T.consumeOpen())
556 T.skipToEnd();
557 }
558}
559
Eli Friedman06de2b52009-06-08 07:21:15 +0000560/// [MS] decl-specifier:
561/// __declspec ( extended-decl-modifier-seq )
562///
563/// [MS] extended-decl-modifier-seq:
564/// extended-decl-modifier[opt]
565/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000566void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000567 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000568
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000569 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000570 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000571 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000572 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000573 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000574
Chad Rosierc1183952012-06-26 22:30:43 +0000575 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000576 // you can specify multiple attributes per declspec.
577 while (Tok.getKind() != tok::r_paren) {
578 // We expect either a well-known identifier or a generic string. Anything
579 // else is a malformed declspec.
580 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000581 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000582 Tok.getKind() != tok::kw_restrict) {
583 Diag(Tok, diag::err_ms_declspec_type);
584 T.skipToEnd();
585 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000586 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000587
588 IdentifierInfo *AttrName;
589 SourceLocation AttrNameLoc;
590 if (IsString) {
591 SmallString<8> StrBuffer;
592 bool Invalid = false;
593 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
594 if (Invalid) {
595 T.skipToEnd();
596 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000597 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000598 AttrName = PP.getIdentifierInfo(Str);
599 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000600 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000601 AttrName = Tok.getIdentifierInfo();
602 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000603 }
Chad Rosierc1183952012-06-26 22:30:43 +0000604
Aaron Ballman478faed2012-06-19 22:09:27 +0000605 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000606 // If we have a generic string, we will allow it because there is no
607 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000608 // (for instance, SAL declspecs in older versions of MSVC).
609 //
Chad Rosierc1183952012-06-26 22:30:43 +0000610 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000611 // arguments and can be turned into an attribute directly.
Aaron Ballman00e99962013-08-31 01:11:41 +0000612 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
613 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000614 else
615 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000616 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000617 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000618}
619
John McCall53fa7142010-12-24 02:08:15 +0000620void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000621 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000622 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000623 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000624 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballman317a77f2013-05-22 23:25:32 +0000625 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
626 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000627 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
628 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000629 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
630 AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000631 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000632}
633
John McCall53fa7142010-12-24 02:08:15 +0000634void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000635 // Treat these like attributes
636 while (Tok.is(tok::kw___pascal)) {
637 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
638 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000639 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
640 AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000641 }
John McCall53fa7142010-12-24 02:08:15 +0000642}
643
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000644void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
645 // Treat these like attributes
646 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000647 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000648 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000649 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
650 AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000651 }
652}
653
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000654void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000655 // FIXME: The mapping from attribute spelling to semantics should be
656 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000657 SourceLocation Loc = Tok.getLocation();
658 switch(Tok.getKind()) {
659 // OpenCL qualifiers:
660 case tok::kw___private:
John McCall084e83d2011-03-24 11:26:52 +0000661 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000662 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000663 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000664 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000665
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000666 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000667 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000668 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000669 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000670 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000671
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000672 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000673 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000674 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000675 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000676 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000677
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000678 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000679 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000680 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000681 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000682 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000683
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000684 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000685 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000686 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000687 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000688 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000689
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000690 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000691 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000692 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000693 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000694 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000695
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000696 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000697 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000698 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000699 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000700 break;
701 default: break;
702 }
703}
704
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000705/// \brief Parse a version number.
706///
707/// version:
708/// simple-integer
709/// simple-integer ',' simple-integer
710/// simple-integer ',' simple-integer ',' simple-integer
711VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
712 Range = Tok.getLocation();
713
714 if (!Tok.is(tok::numeric_constant)) {
715 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000716 SkipUntil(tok::comma, tok::r_paren,
717 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000718 return VersionTuple();
719 }
720
721 // Parse the major (and possibly minor and subminor) versions, which
722 // are stored in the numeric constant. We utilize a quirk of the
723 // lexer, which is that it handles something like 1.2.3 as a single
724 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000725 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000726 Buffer.resize(Tok.getLength()+1);
727 const char *ThisTokBegin = &Buffer[0];
728
729 // Get the spelling of the token, which eliminates trigraphs, etc.
730 bool Invalid = false;
731 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
732 if (Invalid)
733 return VersionTuple();
734
735 // Parse the major version.
736 unsigned AfterMajor = 0;
737 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000738 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000739 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
740 ++AfterMajor;
741 }
742
743 if (AfterMajor == 0) {
744 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000745 SkipUntil(tok::comma, tok::r_paren,
746 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000747 return VersionTuple();
748 }
749
750 if (AfterMajor == ActualLength) {
751 ConsumeToken();
752
753 // We only had a single version component.
754 if (Major == 0) {
755 Diag(Tok, diag::err_zero_version);
756 return VersionTuple();
757 }
758
759 return VersionTuple(Major);
760 }
761
762 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
763 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000764 SkipUntil(tok::comma, tok::r_paren,
765 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000766 return VersionTuple();
767 }
768
769 // Parse the minor version.
770 unsigned AfterMinor = AfterMajor + 1;
771 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000772 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000773 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
774 ++AfterMinor;
775 }
776
777 if (AfterMinor == ActualLength) {
778 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000779
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000780 // We had major.minor.
781 if (Major == 0 && Minor == 0) {
782 Diag(Tok, diag::err_zero_version);
783 return VersionTuple();
784 }
785
Chad Rosierc1183952012-06-26 22:30:43 +0000786 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000787 }
788
789 // If what follows is not a '.', we have a problem.
790 if (ThisTokBegin[AfterMinor] != '.') {
791 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000792 SkipUntil(tok::comma, tok::r_paren,
793 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Chad Rosierc1183952012-06-26 22:30:43 +0000794 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000795 }
796
797 // Parse the subminor version.
798 unsigned AfterSubminor = AfterMinor + 1;
799 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000800 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000801 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
802 ++AfterSubminor;
803 }
804
805 if (AfterSubminor != ActualLength) {
806 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000807 SkipUntil(tok::comma, tok::r_paren,
808 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000809 return VersionTuple();
810 }
811 ConsumeToken();
812 return VersionTuple(Major, Minor, Subminor);
813}
814
815/// \brief Parse the contents of the "availability" attribute.
816///
817/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000818/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000819///
820/// platform:
821/// identifier
822///
823/// version-arg-list:
824/// version-arg
825/// version-arg ',' version-arg-list
826///
827/// version-arg:
828/// 'introduced' '=' version
829/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000830/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000831/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000832/// opt-message:
833/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000834void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
835 SourceLocation AvailabilityLoc,
836 ParsedAttributes &attrs,
837 SourceLocation *endLoc) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000838 enum { Introduced, Deprecated, Obsoleted, Unknown };
839 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000840 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000841
842 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000843 BalancedDelimiterTracker T(*this, tok::l_paren);
844 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000845 Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000846 return;
847 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000848
849 // Parse the platform name,
850 if (Tok.isNot(tok::identifier)) {
851 Diag(Tok, diag::err_availability_expected_platform);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000852 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000853 return;
854 }
Richard Smithfeefaf52013-09-03 18:01:40 +0000855 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000856
857 // Parse the ',' following the platform name.
Alp Toker383d2c42014-01-01 03:08:43 +0000858 if (ExpectAndConsume(tok::comma)) {
859 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000860 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000861 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000862
863 // If we haven't grabbed the pointers for the identifiers
864 // "introduced", "deprecated", and "obsoleted", do so now.
865 if (!Ident_introduced) {
866 Ident_introduced = PP.getIdentifierInfo("introduced");
867 Ident_deprecated = PP.getIdentifierInfo("deprecated");
868 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000869 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000870 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000871 }
872
873 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000874 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000875 do {
876 if (Tok.isNot(tok::identifier)) {
877 Diag(Tok, diag::err_availability_expected_change);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000878 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000879 return;
880 }
881 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
882 SourceLocation KeywordLoc = ConsumeToken();
883
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000884 if (Keyword == Ident_unavailable) {
885 if (UnavailableLoc.isValid()) {
886 Diag(KeywordLoc, diag::err_availability_redundant)
887 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000888 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000889 UnavailableLoc = KeywordLoc;
Alp Toker97650562014-01-10 11:19:30 +0000890 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000891 }
892
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000893 if (Tok.isNot(tok::equal)) {
Alp Tokerec543272013-12-24 09:48:30 +0000894 Diag(Tok, diag::err_expected_after) << Keyword << tok::equal;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000895 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000896 return;
897 }
898 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000899 if (Keyword == Ident_message) {
Benjamin Kramera9dfa922013-09-13 17:31:48 +0000900 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000901 Diag(Tok, diag::err_expected_string_literal)
902 << /*Source='availability attribute'*/2;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000903 SkipUntil(tok::r_paren, StopAtSemi);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000904 return;
905 }
906 MessageExpr = ParseStringLiteralExpression();
907 break;
908 }
Chad Rosierc1183952012-06-26 22:30:43 +0000909
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000910 SourceRange VersionRange;
911 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000912
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000913 if (Version.empty()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000914 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000915 return;
916 }
917
918 unsigned Index;
919 if (Keyword == Ident_introduced)
920 Index = Introduced;
921 else if (Keyword == Ident_deprecated)
922 Index = Deprecated;
923 else if (Keyword == Ident_obsoleted)
924 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000925 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000926 Index = Unknown;
927
928 if (Index < Unknown) {
929 if (!Changes[Index].KeywordLoc.isInvalid()) {
930 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000931 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000932 << SourceRange(Changes[Index].KeywordLoc,
933 Changes[Index].VersionRange.getEnd());
934 }
935
936 Changes[Index].KeywordLoc = KeywordLoc;
937 Changes[Index].Version = Version;
938 Changes[Index].VersionRange = VersionRange;
939 } else {
940 Diag(KeywordLoc, diag::err_availability_unknown_change)
941 << Keyword << VersionRange;
942 }
943
Alp Toker97650562014-01-10 11:19:30 +0000944 } while (TryConsumeToken(tok::comma));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000945
946 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000947 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000948 return;
949
950 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000951 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000952
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000953 // The 'unavailable' availability cannot be combined with any other
954 // availability changes. Make sure that hasn't happened.
955 if (UnavailableLoc.isValid()) {
956 bool Complained = false;
957 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
958 if (Changes[Index].KeywordLoc.isValid()) {
959 if (!Complained) {
960 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
961 << SourceRange(Changes[Index].KeywordLoc,
962 Changes[Index].VersionRange.getEnd());
963 Complained = true;
964 }
965
966 // Clear out the availability.
967 Changes[Index] = AvailabilityChange();
968 }
969 }
970 }
971
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000972 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000973 attrs.addNew(&Availability,
974 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000975 0, AvailabilityLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +0000976 Platform,
John McCall084e83d2011-03-24 11:26:52 +0000977 Changes[Introduced],
978 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000979 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000980 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000981 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000982}
983
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000984/// \brief Parse the contents of the "objc_bridge_related" attribute.
985/// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')'
986/// related_class:
987/// Identifier
988///
989/// opt-class_method:
990/// Identifier: | <empty>
991///
992/// opt-instance_method:
993/// Identifier | <empty>
994///
995void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated,
996 SourceLocation ObjCBridgeRelatedLoc,
997 ParsedAttributes &attrs,
998 SourceLocation *endLoc) {
999 // Opening '('.
1000 BalancedDelimiterTracker T(*this, tok::l_paren);
1001 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00001002 Diag(Tok, diag::err_expected) << tok::l_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001003 return;
1004 }
1005
1006 // Parse the related class name.
1007 if (Tok.isNot(tok::identifier)) {
1008 Diag(Tok, diag::err_objcbridge_related_expected_related_class);
1009 SkipUntil(tok::r_paren, StopAtSemi);
1010 return;
1011 }
1012 IdentifierLoc *RelatedClass = ParseIdentifierLoc();
Alp Toker97650562014-01-10 11:19:30 +00001013 if (ExpectAndConsume(tok::comma)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001014 SkipUntil(tok::r_paren, StopAtSemi);
1015 return;
1016 }
Alp Toker8fbec672013-12-17 23:29:36 +00001017
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001018 // Parse optional class method name.
1019 IdentifierLoc *ClassMethod = 0;
1020 if (Tok.is(tok::identifier)) {
1021 ClassMethod = ParseIdentifierLoc();
Alp Toker8fbec672013-12-17 23:29:36 +00001022 if (!TryConsumeToken(tok::colon)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001023 Diag(Tok, diag::err_objcbridge_related_selector_name);
1024 SkipUntil(tok::r_paren, StopAtSemi);
1025 return;
1026 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001027 }
Alp Toker8fbec672013-12-17 23:29:36 +00001028 if (!TryConsumeToken(tok::comma)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001029 if (Tok.is(tok::colon))
1030 Diag(Tok, diag::err_objcbridge_related_selector_name);
1031 else
Alp Tokerec543272013-12-24 09:48:30 +00001032 Diag(Tok, diag::err_expected) << tok::comma;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001033 SkipUntil(tok::r_paren, StopAtSemi);
1034 return;
1035 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001036
1037 // Parse optional instance method name.
1038 IdentifierLoc *InstanceMethod = 0;
1039 if (Tok.is(tok::identifier))
1040 InstanceMethod = ParseIdentifierLoc();
1041 else if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001042 Diag(Tok, diag::err_expected) << tok::r_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001043 SkipUntil(tok::r_paren, StopAtSemi);
1044 return;
1045 }
1046
1047 // Closing ')'.
1048 if (T.consumeClose())
1049 return;
1050
1051 if (endLoc)
1052 *endLoc = T.getCloseLocation();
1053
1054 // Record this attribute
1055 attrs.addNew(&ObjCBridgeRelated,
1056 SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
1057 0, ObjCBridgeRelatedLoc,
1058 RelatedClass,
1059 ClassMethod,
1060 InstanceMethod,
1061 AttributeList::AS_GNU);
1062
1063}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001064
Bill Wendling44426052012-12-20 19:22:21 +00001065// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001066// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
1067
1068void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
1069
1070void Parser::LateParsedClass::ParseLexedAttributes() {
1071 Self->ParseLexedAttributes(*Class);
1072}
1073
1074void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001075 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001076}
1077
1078/// Wrapper class which calls ParseLexedAttribute, after setting up the
1079/// scope appropriately.
1080void Parser::ParseLexedAttributes(ParsingClass &Class) {
1081 // Deal with templates
1082 // FIXME: Test cases to make sure this does the right thing for templates.
1083 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
1084 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
1085 HasTemplateScope);
1086 if (HasTemplateScope)
1087 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
1088
Douglas Gregor3024f072012-04-16 07:05:22 +00001089 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001090 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +00001091 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001092 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1093 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1094
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001095 // Enter the scope of nested classes
1096 if (!AlreadyHasClassScope)
1097 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1098 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001099 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001100 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1101 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1102 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001103 }
Chad Rosierc1183952012-06-26 22:30:43 +00001104
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001105 if (!AlreadyHasClassScope)
1106 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1107 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001108}
1109
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001110
1111/// \brief Parse all attributes in LAs, and attach them to Decl D.
1112void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1113 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001114 assert(LAs.parseSoon() &&
1115 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001116 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001117 if (D)
1118 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001119 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001120 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001121 }
1122 LAs.clear();
1123}
1124
1125
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001126/// \brief Finish parsing an attribute for which parsing was delayed.
1127/// This will be called at the end of parsing a class declaration
1128/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001129/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001130/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001131void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1132 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001133 // Save the current token position.
1134 SourceLocation OrigLoc = Tok.getLocation();
1135
1136 // Append the current token at the end of the new token stream so that it
1137 // doesn't get lost.
1138 LA.Toks.push_back(Tok);
1139 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1140 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001141 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001142
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001143 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +00001144 // FIXME: Do not warn on C++11 attributes, once we start supporting
1145 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001146 Diag(Tok, diag::warn_attribute_on_function_definition)
Aaron Ballman6d80b3c2014-01-02 18:10:17 +00001147 << &LA.AttrName;
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001148 }
1149
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001150 ParsedAttributes Attrs(AttrFactory);
1151 SourceLocation endLoc;
1152
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001153 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001154 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001155 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1156 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001157
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001158 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001159 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1160 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001161
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001162 if (LA.Decls.size() == 1) {
1163 // If the Decl is templatized, add template parameters to scope.
1164 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1165 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1166 if (HasTemplateScope)
1167 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001168
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001169 // If the Decl is on a function, add function parameters to the scope.
1170 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1171 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1172 if (HasFunScope)
1173 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001174
Michael Han23214e52012-10-03 01:56:22 +00001175 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001176 0, SourceLocation(), AttributeList::AS_GNU, 0);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001177
1178 if (HasFunScope) {
1179 Actions.ActOnExitFunctionContext();
1180 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1181 }
1182 if (HasTemplateScope) {
1183 TempScope.Exit();
1184 }
1185 } else {
1186 // If there are multiple decls, then the decl cannot be within the
1187 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001188 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001189 0, SourceLocation(), AttributeList::AS_GNU, 0);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001190 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001191 } else {
1192 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001193 }
1194
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001195 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1196 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1197 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001198
1199 if (Tok.getLocation() != OrigLoc) {
1200 // Due to a parsing error, we either went over the cached tokens or
1201 // there are still cached tokens left, so we skip the leftover tokens.
1202 // Since this is an uncommon situation that should be avoided, use the
1203 // expensive isBeforeInTranslationUnit call.
1204 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1205 OrigLoc))
1206 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001207 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001208 }
1209}
1210
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001211/// \brief Wrapper around a case statement checking if AttrName is
1212/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001213bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001214 return llvm::StringSwitch<bool>(AttrName)
1215 .Case("guarded_by", true)
1216 .Case("guarded_var", true)
1217 .Case("pt_guarded_by", true)
1218 .Case("pt_guarded_var", true)
1219 .Case("lockable", true)
1220 .Case("scoped_lockable", true)
1221 .Case("no_thread_safety_analysis", true)
1222 .Case("acquired_after", true)
1223 .Case("acquired_before", true)
1224 .Case("exclusive_lock_function", true)
1225 .Case("shared_lock_function", true)
1226 .Case("exclusive_trylock_function", true)
1227 .Case("shared_trylock_function", true)
1228 .Case("unlock_function", true)
1229 .Case("lock_returned", true)
1230 .Case("locks_excluded", true)
1231 .Case("exclusive_locks_required", true)
1232 .Case("shared_locks_required", true)
1233 .Default(false);
1234}
1235
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001236void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1237 SourceLocation AttrNameLoc,
1238 ParsedAttributes &Attrs,
1239 SourceLocation *EndLoc) {
1240 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1241
1242 BalancedDelimiterTracker T(*this, tok::l_paren);
1243 T.consumeOpen();
1244
1245 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001246 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001247 T.skipToEnd();
1248 return;
1249 }
Richard Smithfeefaf52013-09-03 18:01:40 +00001250 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001251
Alp Toker094e5212014-01-05 03:27:11 +00001252 if (ExpectAndConsume(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001253 T.skipToEnd();
1254 return;
1255 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001256
1257 SourceRange MatchingCTypeRange;
1258 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1259 if (MatchingCType.isInvalid()) {
1260 T.skipToEnd();
1261 return;
1262 }
1263
1264 bool LayoutCompatible = false;
1265 bool MustBeNull = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001266 while (TryConsumeToken(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001267 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001268 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001269 T.skipToEnd();
1270 return;
1271 }
1272 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1273 if (Flag->isStr("layout_compatible"))
1274 LayoutCompatible = true;
1275 else if (Flag->isStr("must_be_null"))
1276 MustBeNull = true;
1277 else {
1278 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1279 T.skipToEnd();
1280 return;
1281 }
1282 ConsumeToken(); // consume flag
1283 }
1284
1285 if (!T.consumeClose()) {
1286 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +00001287 ArgumentKind, MatchingCType.release(),
1288 LayoutCompatible, MustBeNull,
1289 AttributeList::AS_GNU);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001290 }
1291
1292 if (EndLoc)
1293 *EndLoc = T.getCloseLocation();
1294}
1295
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001296/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1297/// of a C++11 attribute-specifier in a location where an attribute is not
1298/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1299/// situation.
1300///
1301/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1302/// this doesn't appear to actually be an attribute-specifier, and the caller
1303/// should try to parse it.
1304bool Parser::DiagnoseProhibitedCXX11Attribute() {
1305 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1306
1307 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1308 case CAK_NotAttributeSpecifier:
1309 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1310 return false;
1311
1312 case CAK_InvalidAttributeSpecifier:
1313 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1314 return false;
1315
1316 case CAK_AttributeSpecifier:
1317 // Parse and discard the attributes.
1318 SourceLocation BeginLoc = ConsumeBracket();
1319 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001320 SkipUntil(tok::r_square);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001321 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1322 SourceLocation EndLoc = ConsumeBracket();
1323 Diag(BeginLoc, diag::err_attributes_not_allowed)
1324 << SourceRange(BeginLoc, EndLoc);
1325 return true;
1326 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001327 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001328}
1329
Richard Smith98155ad2013-02-20 01:17:14 +00001330/// \brief We have found the opening square brackets of a C++11
1331/// attribute-specifier in a location where an attribute is not permitted, but
1332/// we know where the attributes ought to be written. Parse them anyway, and
1333/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001334void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1335 SourceLocation CorrectLocation) {
1336 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1337 Tok.is(tok::kw_alignas));
1338
1339 // Consume the attributes.
1340 SourceLocation Loc = Tok.getLocation();
1341 ParseCXX11Attributes(Attrs);
1342 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1343
1344 Diag(Loc, diag::err_attributes_not_allowed)
1345 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1346 << FixItHint::CreateRemoval(AttrRange);
1347}
1348
John McCall53fa7142010-12-24 02:08:15 +00001349void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1350 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1351 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001352}
1353
Michael Han64536a62012-11-06 19:34:54 +00001354void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1355 AttributeList *AttrList = attrs.getList();
1356 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001357 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001358 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001359 << AttrList->getName();
1360 AttrList->setInvalid();
1361 }
1362 AttrList = AttrList->getNext();
1363 }
1364}
1365
Chris Lattner53361ac2006-08-10 05:19:57 +00001366/// ParseDeclaration - Parse a full 'declaration', which consists of
1367/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001368/// 'Context' should be a Declarator::TheContext value. This returns the
1369/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001370///
1371/// declaration: [C99 6.7]
1372/// block-declaration ->
1373/// simple-declaration
1374/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001375/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001376/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001377/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001378/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001379/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001380/// others... [FIXME]
1381///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001382Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1383 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001384 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001385 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001386 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001387 // Must temporarily exit the objective-c container scope for
1388 // parsing c none objective-c decls.
1389 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001390
John McCall48871652010-08-21 09:40:31 +00001391 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001392 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001393 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001394 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001395 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001396 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001397 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001398 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001399 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001400 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001401 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001402 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001403 SourceLocation InlineLoc = ConsumeToken();
1404 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1405 break;
1406 }
Chad Rosierc1183952012-06-26 22:30:43 +00001407 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001408 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001409 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001410 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001411 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001412 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001413 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001414 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001415 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001416 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001417 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001418 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001419 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001420 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001421 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001422 default:
John McCall53fa7142010-12-24 02:08:15 +00001423 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001424 }
Chad Rosierc1183952012-06-26 22:30:43 +00001425
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001426 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001427 // single decl, convert it now. Alias declarations can also declare a type;
1428 // include that too if it is present.
1429 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001430}
1431
1432/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1433/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001434/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1435/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001436///[C90/C++]init-declarator-list ';' [TODO]
1437/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001438///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001439/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001440/// attribute-specifier-seq[opt] type-specifier-seq declarator
1441///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001442/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001443/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001444///
1445/// If FRI is non-null, we might be parsing a for-range-declaration instead
1446/// of a simple-declaration. If we find that we are, we also parse the
1447/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001448Parser::DeclGroupPtrTy
1449Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1450 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001451 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001452 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001453 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001454 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001455
Richard Smith404dfb42013-11-19 22:47:36 +00001456 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
1457 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
1458
1459 // If we had a free-standing type definition with a missing semicolon, we
1460 // may get this far before the problem becomes obvious.
1461 if (DS.hasTagDefinition() &&
1462 DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
1463 return DeclGroupPtrTy();
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001464
Chris Lattner0e894622006-08-13 19:58:17 +00001465 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1466 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001467 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001468 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001469 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001470 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001471 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001472 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001473 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001474 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001475 }
Chad Rosierc1183952012-06-26 22:30:43 +00001476
Richard Smith2386c8b2013-02-22 09:06:26 +00001477 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001478 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001479}
Mike Stump11289f42009-09-09 15:08:12 +00001480
Richard Smith09f76ee2011-10-19 21:33:05 +00001481/// Returns true if this might be the start of a declarator, or a common typo
1482/// for a declarator.
1483bool Parser::MightBeDeclarator(unsigned Context) {
1484 switch (Tok.getKind()) {
1485 case tok::annot_cxxscope:
1486 case tok::annot_template_id:
1487 case tok::caret:
1488 case tok::code_completion:
1489 case tok::coloncolon:
1490 case tok::ellipsis:
1491 case tok::kw___attribute:
1492 case tok::kw_operator:
1493 case tok::l_paren:
1494 case tok::star:
1495 return true;
1496
1497 case tok::amp:
1498 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001499 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001500
Richard Smithc8a79032012-01-09 22:31:44 +00001501 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001502 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001503 NextToken().is(tok::l_square);
1504
1505 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001506 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001507
Richard Smith09f76ee2011-10-19 21:33:05 +00001508 case tok::identifier:
1509 switch (NextToken().getKind()) {
1510 case tok::code_completion:
1511 case tok::coloncolon:
1512 case tok::comma:
1513 case tok::equal:
1514 case tok::equalequal: // Might be a typo for '='.
1515 case tok::kw_alignas:
1516 case tok::kw_asm:
1517 case tok::kw___attribute:
1518 case tok::l_brace:
1519 case tok::l_paren:
1520 case tok::l_square:
1521 case tok::less:
1522 case tok::r_brace:
1523 case tok::r_paren:
1524 case tok::r_square:
1525 case tok::semi:
1526 return true;
1527
1528 case tok::colon:
1529 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001530 // and in block scope it's probably a label. Inside a class definition,
1531 // this is a bit-field.
1532 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001533 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001534
1535 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001536 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001537
1538 default:
1539 return false;
1540 }
1541
1542 default:
1543 return false;
1544 }
1545}
1546
Richard Smithb8caac82012-04-11 20:59:20 +00001547/// Skip until we reach something which seems like a sensible place to pick
1548/// up parsing after a malformed declaration. This will sometimes stop sooner
1549/// than SkipUntil(tok::r_brace) would, but will never stop later.
1550void Parser::SkipMalformedDecl() {
1551 while (true) {
1552 switch (Tok.getKind()) {
1553 case tok::l_brace:
1554 // Skip until matching }, then stop. We've probably skipped over
1555 // a malformed class or function definition or similar.
1556 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001557 SkipUntil(tok::r_brace);
Richard Smithb8caac82012-04-11 20:59:20 +00001558 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1559 // This declaration isn't over yet. Keep skipping.
1560 continue;
1561 }
Alp Toker8fbec672013-12-17 23:29:36 +00001562 TryConsumeToken(tok::semi);
Richard Smithb8caac82012-04-11 20:59:20 +00001563 return;
1564
1565 case tok::l_square:
1566 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001567 SkipUntil(tok::r_square);
Richard Smithb8caac82012-04-11 20:59:20 +00001568 continue;
1569
1570 case tok::l_paren:
1571 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001572 SkipUntil(tok::r_paren);
Richard Smithb8caac82012-04-11 20:59:20 +00001573 continue;
1574
1575 case tok::r_brace:
1576 return;
1577
1578 case tok::semi:
1579 ConsumeToken();
1580 return;
1581
1582 case tok::kw_inline:
1583 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001584 // a good place to pick back up parsing, except in an Objective-C
1585 // @interface context.
1586 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1587 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001588 return;
1589 break;
1590
1591 case tok::kw_namespace:
1592 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001593 // place to pick back up parsing, except in an Objective-C
1594 // @interface context.
1595 if (Tok.isAtStartOfLine() &&
1596 (!ParsingInObjCContainer || CurParsedObjCImpl))
1597 return;
1598 break;
1599
1600 case tok::at:
1601 // @end is very much like } in Objective-C contexts.
1602 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1603 ParsingInObjCContainer)
1604 return;
1605 break;
1606
1607 case tok::minus:
1608 case tok::plus:
1609 // - and + probably start new method declarations in Objective-C contexts.
1610 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001611 return;
1612 break;
1613
1614 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001615 case tok::annot_module_begin:
1616 case tok::annot_module_end:
1617 case tok::annot_module_include:
Richard Smithb8caac82012-04-11 20:59:20 +00001618 return;
1619
1620 default:
1621 break;
1622 }
1623
1624 ConsumeAnyToken();
1625 }
1626}
1627
John McCalld5a36322009-11-03 19:26:08 +00001628/// ParseDeclGroup - Having concluded that this is either a function
1629/// definition or a group of object declarations, actually parse the
1630/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001631Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1632 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001633 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001634 SourceLocation *DeclEnd,
1635 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001636 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001637 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001638 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001639
John McCalld5a36322009-11-03 19:26:08 +00001640 // Bail out if the first declarator didn't seem well-formed.
1641 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001642 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001643 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001644 }
Mike Stump11289f42009-09-09 15:08:12 +00001645
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001646 // Save late-parsed attributes for now; they need to be parsed in the
1647 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001648 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1649 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001650 if (D.isFunctionDeclarator())
1651 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1652
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001653 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001654 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001655 // Look at the next token to make sure that this isn't a function
1656 // declaration. We have to check this because __attribute__ might be the
1657 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001658 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001659
Douglas Gregor012efe22013-04-16 16:01:32 +00001660 if (AllowFunctionDefinitions) {
1661 if (isStartOfFunctionDefinition(D)) {
1662 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1663 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001664
Douglas Gregor012efe22013-04-16 16:01:32 +00001665 // Recover by treating the 'typedef' as spurious.
1666 DS.ClearStorageClassSpecs();
1667 }
1668
1669 Decl *TheDecl =
1670 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1671 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001672 }
1673
Douglas Gregor012efe22013-04-16 16:01:32 +00001674 if (isDeclarationSpecifier()) {
1675 // If there is an invalid declaration specifier right after the function
1676 // prototype, then we must be in a missing semicolon case where this isn't
1677 // actually a body. Just fall through into the code that handles it as a
1678 // prototype, and let the top-level code handle the erroneous declspec
1679 // where it would otherwise expect a comma or semicolon.
1680 } else {
1681 Diag(Tok, diag::err_expected_fn_body);
1682 SkipUntil(tok::semi);
1683 return DeclGroupPtrTy();
1684 }
John McCalld5a36322009-11-03 19:26:08 +00001685 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001686 if (Tok.is(tok::l_brace)) {
1687 Diag(Tok, diag::err_function_definition_not_allowed);
Serge Pavlov1de51512013-12-09 05:25:47 +00001688 SkipMalformedDecl();
1689 return DeclGroupPtrTy();
Douglas Gregor012efe22013-04-16 16:01:32 +00001690 }
John McCalld5a36322009-11-03 19:26:08 +00001691 }
1692 }
1693
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001694 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001695 return DeclGroupPtrTy();
1696
1697 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1698 // must parse and analyze the for-range-initializer before the declaration is
1699 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001700 //
1701 // Handle the Objective-C for-in loop variable similarly, although we
1702 // don't need to parse the container in advance.
1703 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1704 bool IsForRangeLoop = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001705 if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001706 IsForRangeLoop = true;
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001707 if (Tok.is(tok::l_brace))
1708 FRI->RangeExpr = ParseBraceInitializer();
1709 else
1710 FRI->RangeExpr = ParseExpression();
1711 }
1712
Richard Smith02e85f32011-04-14 22:09:26 +00001713 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001714 if (IsForRangeLoop)
1715 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001716 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001717 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001718 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001719 }
1720
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001721 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001722 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001723 if (LateParsedAttrs.size() > 0)
1724 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001725 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001726 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001727 DeclsInGroup.push_back(FirstDecl);
1728
Richard Smith09f76ee2011-10-19 21:33:05 +00001729 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001730
John McCalld5a36322009-11-03 19:26:08 +00001731 // If we don't have a comma, it is either the end of the list (a ';') or an
1732 // error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00001733 SourceLocation CommaLoc;
1734 while (TryConsumeToken(tok::comma, CommaLoc)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001735 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1736 // This comma was followed by a line-break and something which can't be
1737 // the start of a declarator. The comma was probably a typo for a
1738 // semicolon.
1739 Diag(CommaLoc, diag::err_expected_semi_declaration)
1740 << FixItHint::CreateReplacement(CommaLoc, ";");
1741 ExpectSemi = false;
1742 break;
1743 }
John McCalld5a36322009-11-03 19:26:08 +00001744
1745 // Parse the next declarator.
1746 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001747 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001748
1749 // Accept attributes in an init-declarator. In the first declarator in a
1750 // declaration, these would be part of the declspec. In subsequent
1751 // declarators, they become part of the declarator itself, so that they
1752 // don't apply to declarators after *this* one. Examples:
1753 // short __attribute__((common)) var; -> declspec
1754 // short var __attribute__((common)); -> declarator
1755 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001756 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001757
1758 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001759 if (!D.isInvalidType()) {
1760 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1761 D.complete(ThisDecl);
1762 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001763 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001764 }
John McCalld5a36322009-11-03 19:26:08 +00001765 }
1766
1767 if (DeclEnd)
1768 *DeclEnd = Tok.getLocation();
1769
Richard Smith09f76ee2011-10-19 21:33:05 +00001770 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001771 ExpectAndConsumeSemi(Context == Declarator::FileContext
1772 ? diag::err_invalid_token_after_toplevel_declarator
1773 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001774 // Okay, there was no semicolon and one was expected. If we see a
1775 // declaration specifier, just assume it was missing and continue parsing.
1776 // Otherwise things are very confused and we skip to recover.
1777 if (!isDeclarationSpecifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001778 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Alp Toker8fbec672013-12-17 23:29:36 +00001779 TryConsumeToken(tok::semi);
Chris Lattner13901342010-07-11 22:42:07 +00001780 }
John McCalld5a36322009-11-03 19:26:08 +00001781 }
1782
Rafael Espindolaab417692013-07-09 12:05:01 +00001783 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001784}
1785
Richard Smith02e85f32011-04-14 22:09:26 +00001786/// Parse an optional simple-asm-expr and attributes, and attach them to a
1787/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001788bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001789 // If a simple-asm-expr is present, parse it.
1790 if (Tok.is(tok::kw_asm)) {
1791 SourceLocation Loc;
1792 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1793 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001794 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smith02e85f32011-04-14 22:09:26 +00001795 return true;
1796 }
1797
1798 D.setAsmLabel(AsmLabel.release());
1799 D.SetRangeEnd(Loc);
1800 }
1801
1802 MaybeParseGNUAttributes(D);
1803 return false;
1804}
1805
Douglas Gregor23996282009-05-12 21:31:51 +00001806/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1807/// declarator'. This method parses the remainder of the declaration
1808/// (including any attributes or initializer, among other things) and
1809/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001810///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001811/// init-declarator: [C99 6.7]
1812/// declarator
1813/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001814/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1815/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001816/// [C++] declarator initializer[opt]
1817///
1818/// [C++] initializer:
1819/// [C++] '=' initializer-clause
1820/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001821/// [C++0x] '=' 'default' [TODO]
1822/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001823/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001824///
1825/// According to the standard grammar, =default and =delete are function
1826/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001827///
John McCall48871652010-08-21 09:40:31 +00001828Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001829 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001830 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001831 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001832
Richard Smith02e85f32011-04-14 22:09:26 +00001833 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1834}
Mike Stump11289f42009-09-09 15:08:12 +00001835
Richard Smith02e85f32011-04-14 22:09:26 +00001836Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1837 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001838 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001839 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001840 switch (TemplateInfo.Kind) {
1841 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001842 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001843 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001844
Douglas Gregor450f00842009-09-25 18:43:00 +00001845 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001846 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001847 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001848 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001849 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00001850 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001851 // Re-direct this decl to refer to the templated decl so that we can
1852 // initialize it.
1853 ThisDecl = VT->getTemplatedDecl();
1854 break;
1855 }
1856 case ParsedTemplateInfo::ExplicitInstantiation: {
1857 if (Tok.is(tok::semi)) {
1858 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1859 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1860 if (ThisRes.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001861 SkipUntil(tok::semi, StopBeforeMatch);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001862 return 0;
1863 }
1864 ThisDecl = ThisRes.get();
1865 } else {
1866 // FIXME: This check should be for a variable template instantiation only.
1867
1868 // Check that this is a valid instantiation
1869 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1870 // If the declarator-id is not a template-id, issue a diagnostic and
1871 // recover by ignoring the 'template' keyword.
1872 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1873 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1874 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1875 } else {
1876 SourceLocation LAngleLoc =
1877 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1878 Diag(D.getIdentifierLoc(),
1879 diag::err_explicit_instantiation_with_definition)
1880 << SourceRange(TemplateInfo.TemplateLoc)
1881 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1882
1883 // Recover as if it were an explicit specialization.
1884 TemplateParameterLists FakedParamLists;
1885 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1886 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1887 LAngleLoc));
1888
1889 ThisDecl =
1890 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1891 }
1892 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001893 break;
1894 }
1895 }
Mike Stump11289f42009-09-09 15:08:12 +00001896
Richard Smith74aeef52013-04-26 16:15:35 +00001897 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001898
Douglas Gregor23996282009-05-12 21:31:51 +00001899 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001900 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001901 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001902 ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001903
Anders Carlsson991285e2010-09-24 21:25:25 +00001904 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001905 if (D.isFunctionDeclarator())
1906 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1907 << 1 /* delete */;
1908 else
1909 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001910 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001911 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001912 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1913 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001914 else
1915 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001916 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001917 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001918 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001919 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001920 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001921
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001922 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001923 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001924 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001925 cutOffParsing();
1926 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001927 }
Chad Rosierc1183952012-06-26 22:30:43 +00001928
John McCalldadc5752010-08-24 06:29:42 +00001929 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001930
David Blaikiebbafb8a2012-03-11 07:00:24 +00001931 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001932 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001933 ExitScope();
1934 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001935
Douglas Gregor23996282009-05-12 21:31:51 +00001936 if (Init.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001937 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor604c3022010-03-01 18:27:54 +00001938 Actions.ActOnInitializerError(ThisDecl);
1939 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001940 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1941 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001942 }
1943 } else if (Tok.is(tok::l_paren)) {
1944 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001945 BalancedDelimiterTracker T(*this, tok::l_paren);
1946 T.consumeOpen();
1947
Benjamin Kramerf0623432012-08-23 22:51:59 +00001948 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001949 CommaLocsTy CommaLocs;
1950
David Blaikiebbafb8a2012-03-11 07:00:24 +00001951 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001952 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001953 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001954 }
1955
Douglas Gregor23996282009-05-12 21:31:51 +00001956 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001957 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001958 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor613bf102009-12-22 17:47:17 +00001959
David Blaikiebbafb8a2012-03-11 07:00:24 +00001960 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001961 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001962 ExitScope();
1963 }
Douglas Gregor23996282009-05-12 21:31:51 +00001964 } else {
1965 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001966 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001967
1968 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1969 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001970
David Blaikiebbafb8a2012-03-11 07:00:24 +00001971 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001972 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001973 ExitScope();
1974 }
1975
Sebastian Redla9351792012-02-11 23:51:47 +00001976 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1977 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001978 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001979 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1980 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001981 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001982 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001983 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001984 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001985 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1986
Sebastian Redl3da34892011-06-05 12:23:16 +00001987 if (D.getCXXScopeSpec().isSet()) {
1988 EnterScope(0);
1989 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1990 }
1991
1992 ExprResult Init(ParseBraceInitializer());
1993
1994 if (D.getCXXScopeSpec().isSet()) {
1995 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1996 ExitScope();
1997 }
1998
1999 if (Init.isInvalid()) {
2000 Actions.ActOnInitializerError(ThisDecl);
2001 } else
2002 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
2003 /*DirectInit=*/true, TypeContainsAuto);
2004
Douglas Gregor23996282009-05-12 21:31:51 +00002005 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00002006 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00002007 }
2008
Richard Smithb2bc2e62011-02-21 20:05:19 +00002009 Actions.FinalizeDeclaration(ThisDecl);
2010
Douglas Gregor23996282009-05-12 21:31:51 +00002011 return ThisDecl;
2012}
2013
Chris Lattner1890ac82006-08-13 01:16:23 +00002014/// ParseSpecifierQualifierList
2015/// specifier-qualifier-list:
2016/// type-specifier specifier-qualifier-list[opt]
2017/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002018/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00002019///
Richard Smithc5b05522012-03-12 07:56:15 +00002020void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
2021 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002022 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
2023 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002024 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00002025 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00002026
Chris Lattner1890ac82006-08-13 01:16:23 +00002027 // Validate declspec for type-name.
2028 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith649c7b062014-01-08 00:56:48 +00002029 if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00002030 Diag(Tok, diag::err_expected_type);
2031 DS.SetTypeSpecError();
2032 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
2033 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002034 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00002035 if (!DS.hasTypeSpecifier())
2036 DS.SetTypeSpecError();
2037 }
Mike Stump11289f42009-09-09 15:08:12 +00002038
Chris Lattner1b22eed2006-11-28 05:12:07 +00002039 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002040 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00002041 if (DS.getStorageClassSpecLoc().isValid())
2042 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2043 else
Richard Smithb4a9e862013-04-12 22:46:28 +00002044 Diag(DS.getThreadStorageClassSpecLoc(),
2045 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00002046 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002047 }
Mike Stump11289f42009-09-09 15:08:12 +00002048
Chris Lattner1b22eed2006-11-28 05:12:07 +00002049 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002050 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002051 if (DS.isInlineSpecified())
2052 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2053 if (DS.isVirtualSpecified())
2054 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2055 if (DS.isExplicitSpecified())
2056 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002057 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002058 }
Richard Smithc5b05522012-03-12 07:56:15 +00002059
2060 // Issue diagnostic and remove constexpr specfier if present.
2061 if (DS.isConstexprSpecified()) {
2062 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2063 DS.ClearConstexprSpec();
2064 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002065}
Chris Lattner53361ac2006-08-10 05:19:57 +00002066
Chris Lattner6cc055a2009-04-12 20:42:31 +00002067/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2068/// specified token is valid after the identifier in a declarator which
2069/// immediately follows the declspec. For example, these things are valid:
2070///
2071/// int x [ 4]; // direct-declarator
2072/// int x ( int y); // direct-declarator
2073/// int(int x ) // direct-declarator
2074/// int x ; // simple-declaration
2075/// int x = 17; // init-declarator-list
2076/// int x , y; // init-declarator-list
2077/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002078/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002079/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002080///
2081/// This is not, because 'x' does not immediately follow the declspec (though
2082/// ')' happens to be valid anyway).
2083/// int (x)
2084///
2085static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2086 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2087 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002088 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002089}
2090
Chris Lattner20a0c612009-04-14 21:34:55 +00002091
2092/// ParseImplicitInt - This method is called when we have an non-typename
2093/// identifier in a declspec (which normally terminates the decl spec) when
2094/// the declspec has no type specifier. In this case, the declspec is either
2095/// malformed or is "implicit int" (in K&R and C89).
2096///
2097/// This method handles diagnosing this prettily and returns false if the
2098/// declspec is done being processed. If it recovers and thinks there may be
2099/// other pieces of declspec after it, it returns true.
2100///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002101bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002102 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002103 AccessSpecifier AS, DeclSpecContext DSC,
2104 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002105 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002106
Chris Lattner20a0c612009-04-14 21:34:55 +00002107 SourceLocation Loc = Tok.getLocation();
2108 // If we see an identifier that is not a type name, we normally would
2109 // parse it as the identifer being declared. However, when a typename
2110 // is typo'd or the definition is not included, this will incorrectly
2111 // parse the typename as the identifier name and fall over misparsing
2112 // later parts of the diagnostic.
2113 //
2114 // As such, we try to do some look-ahead in cases where this would
2115 // otherwise be an "implicit-int" case to see if this is invalid. For
2116 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2117 // an identifier with implicit int, we'd get a parse error because the
2118 // next token is obviously invalid for a type. Parse these as a case
2119 // with an invalid type specifier.
2120 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002121
Chris Lattner20a0c612009-04-14 21:34:55 +00002122 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002123 // error, do lookahead to try to do better recovery. This never applies
2124 // within a type specifier. Outside of C++, we allow this even if the
2125 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002126 // implicit int as an extension in C99 and C11.
Richard Smith649c7b062014-01-08 00:56:48 +00002127 if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002128 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002129 // If this token is valid for implicit int, e.g. "static x = 4", then
2130 // we just avoid eating the identifier, so it will be parsed as the
2131 // identifier in the declarator.
2132 return false;
2133 }
Mike Stump11289f42009-09-09 15:08:12 +00002134
Richard Smitha952ebb2012-05-15 21:01:51 +00002135 if (getLangOpts().CPlusPlus &&
2136 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2137 // Don't require a type specifier if we have the 'auto' storage class
2138 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002139 if (SS)
2140 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002141 return false;
2142 }
2143
Chris Lattner20a0c612009-04-14 21:34:55 +00002144 // Otherwise, if we don't consume this token, we are going to emit an
2145 // error anyway. Try to recover from various common problems. Check
2146 // to see if this was a reference to a tag name without a tag specified.
2147 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002148 //
2149 // C++ doesn't need this, and isTagName doesn't take SS.
2150 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002151 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002152 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002153
Douglas Gregor0be31a22010-07-02 17:43:08 +00002154 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002155 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002156 case DeclSpec::TST_enum:
2157 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2158 case DeclSpec::TST_union:
2159 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2160 case DeclSpec::TST_struct:
2161 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002162 case DeclSpec::TST_interface:
2163 TagName="__interface"; FixitTagName = "__interface ";
2164 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002165 case DeclSpec::TST_class:
2166 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002167 }
Mike Stump11289f42009-09-09 15:08:12 +00002168
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002169 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002170 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2171 LookupResult R(Actions, TokenName, SourceLocation(),
2172 Sema::LookupOrdinaryName);
2173
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002174 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002175 << TokenName << TagName << getLangOpts().CPlusPlus
2176 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2177
2178 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2179 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2180 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002181 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002182 << TokenName << TagName;
2183 }
Mike Stump11289f42009-09-09 15:08:12 +00002184
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002185 // Parse this as a tag as if the missing tag were present.
2186 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002187 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002188 else
Richard Smithc5b05522012-03-12 07:56:15 +00002189 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002190 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002191 return true;
2192 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002193 }
Mike Stump11289f42009-09-09 15:08:12 +00002194
Richard Smithfe904f02012-05-15 21:29:55 +00002195 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002196 // being declared (with a missing type).
Richard Smith649c7b062014-01-08 00:56:48 +00002197 if (!isTypeSpecifier(DSC) &&
Richard Smithfe904f02012-05-15 21:29:55 +00002198 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002199 // Look ahead to the next token to try to figure out what this declaration
2200 // was supposed to be.
2201 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002202 case tok::l_paren: {
2203 // static x(4); // 'x' is not a type
2204 // x(int n); // 'x' is not a type
2205 // x (*p)[]; // 'x' is a type
2206 //
2207 // Since we're in an error case (or the rare 'implicit int in C++' MS
2208 // extension), we can afford to perform a tentative parse to determine
2209 // which case we're in.
2210 TentativeParsingAction PA(*this);
2211 ConsumeToken();
2212 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2213 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002214
2215 if (TPR != TPResult::False()) {
2216 // The identifier is followed by a parenthesized declarator.
2217 // It's supposed to be a type.
2218 break;
2219 }
2220
2221 // If we're in a context where we could be declaring a constructor,
2222 // check whether this is a constructor declaration with a bogus name.
2223 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2224 IdentifierInfo *II = Tok.getIdentifierInfo();
2225 if (Actions.isCurrentClassNameTypo(II, SS)) {
2226 Diag(Loc, diag::err_constructor_bad_name)
2227 << Tok.getIdentifierInfo() << II
2228 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2229 Tok.setIdentifierInfo(II);
2230 }
2231 }
2232 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002233 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002234 case tok::comma:
2235 case tok::equal:
2236 case tok::kw_asm:
2237 case tok::l_brace:
2238 case tok::l_square:
2239 case tok::semi:
2240 // This looks like a variable or function declaration. The type is
2241 // probably missing. We're done parsing decl-specifiers.
2242 if (SS)
2243 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2244 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002245
2246 default:
2247 // This is probably supposed to be a type. This includes cases like:
2248 // int f(itn);
2249 // struct S { unsinged : 4; };
2250 break;
2251 }
2252 }
2253
Chad Rosierc1183952012-06-26 22:30:43 +00002254 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002255 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002256 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002257 IdentifierInfo *II = Tok.getIdentifierInfo();
2258 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002259 // The action emitted a diagnostic, so we don't have to.
2260 if (T) {
2261 // The action has suggested that the type T could be used. Set that as
2262 // the type in the declaration specifiers, consume the would-be type
2263 // name token, and we're done.
2264 const char *PrevSpec;
2265 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002266 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002267 DS.SetRangeEnd(Tok.getLocation());
2268 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002269 // There may be other declaration specifiers after this.
2270 return true;
2271 } else if (II != Tok.getIdentifierInfo()) {
2272 // If no type was suggested, the correction is to a keyword
2273 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002274 // There may be other declaration specifiers after this.
2275 return true;
2276 }
Chad Rosierc1183952012-06-26 22:30:43 +00002277
Douglas Gregor15e56022009-10-13 23:27:22 +00002278 // Fall through; the action had no suggestion for us.
2279 } else {
2280 // The action did not emit a diagnostic, so emit one now.
2281 SourceRange R;
2282 if (SS) R = SS->getRange();
2283 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2284 }
Mike Stump11289f42009-09-09 15:08:12 +00002285
Douglas Gregor15e56022009-10-13 23:27:22 +00002286 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002287 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002288 DS.SetRangeEnd(Tok.getLocation());
2289 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002290
Chris Lattner20a0c612009-04-14 21:34:55 +00002291 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2292 // avoid rippling error messages on subsequent uses of the same type,
2293 // could be useful if #include was forgotten.
2294 return false;
2295}
2296
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002297/// \brief Determine the declaration specifier context from the declarator
2298/// context.
2299///
2300/// \param Context the declarator context, which is one of the
2301/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002302Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002303Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2304 if (Context == Declarator::MemberContext)
2305 return DSC_class;
2306 if (Context == Declarator::FileContext)
2307 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002308 if (Context == Declarator::TrailingReturnContext)
2309 return DSC_trailing;
Richard Smith649c7b062014-01-08 00:56:48 +00002310 if (Context == Declarator::AliasDeclContext ||
2311 Context == Declarator::AliasTemplateContext)
2312 return DSC_alias_declaration;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002313 return DSC_normal;
2314}
2315
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002316/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2317///
2318/// FIXME: Simply returns an alignof() expression if the argument is a
2319/// type. Ideally, the type should be propagated directly into Sema.
2320///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002321/// [C11] type-id
2322/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002323/// [C++0x] type-id ...[opt]
2324/// [C++0x] assignment-expression ...[opt]
2325ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2326 SourceLocation &EllipsisLoc) {
2327 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002328 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002329 SourceLocation TypeLoc = Tok.getLocation();
2330 ParsedType Ty = ParseTypeName().get();
2331 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002332 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2333 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002334 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002335 ER = ParseConstantExpression();
2336
Alp Toker8fbec672013-12-17 23:29:36 +00002337 if (getLangOpts().CPlusPlus11)
2338 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002339
2340 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002341}
2342
2343/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2344/// attribute to Attrs.
2345///
2346/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002347/// [C11] '_Alignas' '(' type-id ')'
2348/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002349/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2350/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002351void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002352 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002353 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2354 "Not an alignment-specifier!");
2355
Richard Smithd11c7a12013-01-29 01:48:07 +00002356 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2357 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002358
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002359 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002360 if (T.expectAndConsume())
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002361 return;
2362
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002363 SourceLocation EllipsisLoc;
2364 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002365 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002366 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002367 return;
2368 }
2369
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002370 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002371 if (EndLoc)
2372 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002373
Aaron Ballman00e99962013-08-31 01:11:41 +00002374 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002375 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002376 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2377 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002378}
2379
Richard Smith404dfb42013-11-19 22:47:36 +00002380/// Determine whether we're looking at something that might be a declarator
2381/// in a simple-declaration. If it can't possibly be a declarator, maybe
2382/// diagnose a missing semicolon after a prior tag definition in the decl
2383/// specifier.
2384///
2385/// \return \c true if an error occurred and this can't be any kind of
2386/// declaration.
2387bool
2388Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2389 DeclSpecContext DSContext,
2390 LateParsedAttrList *LateAttrs) {
2391 assert(DS.hasTagDefinition() && "shouldn't call this");
2392
2393 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002394
2395 if (getLangOpts().CPlusPlus &&
2396 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2397 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
2398 TryAnnotateCXXScopeToken(EnteringContext)) {
2399 SkipMalformedDecl();
2400 return true;
2401 }
2402
Richard Smith698875a2013-11-20 23:40:57 +00002403 bool HasScope = Tok.is(tok::annot_cxxscope);
2404 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2405 Token AfterScope = HasScope ? NextToken() : Tok;
2406
Richard Smith404dfb42013-11-19 22:47:36 +00002407 // Determine whether the following tokens could possibly be a
2408 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002409 bool MightBeDeclarator = true;
2410 if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) {
2411 // A declarator-id can't start with 'typename'.
2412 MightBeDeclarator = false;
2413 } else if (AfterScope.is(tok::annot_template_id)) {
2414 // If we have a type expressed as a template-id, this cannot be a
2415 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2416 TemplateIdAnnotation *Annot =
2417 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2418 if (Annot->Kind == TNK_Type_template)
2419 MightBeDeclarator = false;
2420 } else if (AfterScope.is(tok::identifier)) {
2421 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2422
Richard Smith404dfb42013-11-19 22:47:36 +00002423 // These tokens cannot come after the declarator-id in a
2424 // simple-declaration, and are likely to come after a type-specifier.
Richard Smith698875a2013-11-20 23:40:57 +00002425 if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) ||
2426 Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) ||
2427 Next.is(tok::coloncolon)) {
2428 // Missing a semicolon.
2429 MightBeDeclarator = false;
2430 } else if (HasScope) {
2431 // If the declarator-id has a scope specifier, it must redeclare a
2432 // previously-declared entity. If that's a type (and this is not a
2433 // typedef), that's an error.
2434 CXXScopeSpec SS;
2435 Actions.RestoreNestedNameSpecifierAnnotation(
2436 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2437 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2438 Sema::NameClassification Classification = Actions.ClassifyName(
2439 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2440 /*IsAddressOfOperand*/false);
2441 switch (Classification.getKind()) {
2442 case Sema::NC_Error:
2443 SkipMalformedDecl();
2444 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002445
Richard Smith698875a2013-11-20 23:40:57 +00002446 case Sema::NC_Keyword:
2447 case Sema::NC_NestedNameSpecifier:
2448 llvm_unreachable("typo correction and nested name specifiers not "
2449 "possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002450
Richard Smith698875a2013-11-20 23:40:57 +00002451 case Sema::NC_Type:
2452 case Sema::NC_TypeTemplate:
2453 // Not a previously-declared non-type entity.
2454 MightBeDeclarator = false;
2455 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002456
Richard Smith698875a2013-11-20 23:40:57 +00002457 case Sema::NC_Unknown:
2458 case Sema::NC_Expression:
2459 case Sema::NC_VarTemplate:
2460 case Sema::NC_FunctionTemplate:
2461 // Might be a redeclaration of a prior entity.
2462 break;
2463 }
Richard Smith404dfb42013-11-19 22:47:36 +00002464 }
Richard Smith404dfb42013-11-19 22:47:36 +00002465 }
2466
Richard Smith698875a2013-11-20 23:40:57 +00002467 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002468 return false;
2469
2470 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
Alp Toker383d2c42014-01-01 03:08:43 +00002471 diag::err_expected_after)
2472 << DeclSpec::getSpecifierName(DS.getTypeSpecType()) << tok::semi;
Richard Smith404dfb42013-11-19 22:47:36 +00002473
2474 // Try to recover from the typo, by dropping the tag definition and parsing
2475 // the problematic tokens as a type.
2476 //
2477 // FIXME: Split the DeclSpec into pieces for the standalone
2478 // declaration and pieces for the following declaration, instead
2479 // of assuming that all the other pieces attach to new declaration,
2480 // and call ParsedFreeStandingDeclSpec as appropriate.
2481 DS.ClearTypeSpecType();
2482 ParsedTemplateInfo NotATemplate;
2483 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2484 return false;
2485}
2486
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002487/// ParseDeclarationSpecifiers
2488/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002489/// storage-class-specifier declaration-specifiers[opt]
2490/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002491/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002492/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002493/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002494/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002495///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002496/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002497/// 'typedef'
2498/// 'extern'
2499/// 'static'
2500/// 'auto'
2501/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002502/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002503/// [C++11] 'thread_local'
2504/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002505/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002506/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002507/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002508/// [C++] 'virtual'
2509/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002510/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002511/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002512/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002513
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002514///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002515void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002516 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002517 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002518 DeclSpecContext DSContext,
2519 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002520 if (DS.getSourceRange().isInvalid()) {
2521 DS.SetRangeStart(Tok.getLocation());
2522 DS.SetRangeEnd(Tok.getLocation());
2523 }
Chad Rosierc1183952012-06-26 22:30:43 +00002524
Douglas Gregordf593fb2011-11-07 17:33:42 +00002525 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002526 bool AttrsLastTime = false;
2527 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002528 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002529 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002530 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002531 unsigned DiagID = 0;
2532
Chris Lattner4d8f8732006-11-28 05:05:08 +00002533 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002534
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002535 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002536 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002537 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002538 if (!AttrsLastTime)
2539 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002540 else {
2541 // Reject C++11 attributes that appertain to decl specifiers as
2542 // we don't support any C++11 attributes that appertain to decl
2543 // specifiers. This also conforms to what g++ 4.8 is doing.
2544 ProhibitCXX11Attributes(attrs);
2545
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002546 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002547 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002548
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002549 // If this is not a declaration specifier token, we're done reading decl
2550 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002551 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002552 return;
Mike Stump11289f42009-09-09 15:08:12 +00002553
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002554 case tok::l_square:
2555 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002556 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002557 goto DoneWithDeclSpec;
2558
2559 ProhibitAttributes(attrs);
2560 // FIXME: It would be good to recover by accepting the attributes,
2561 // but attempting to do that now would cause serious
2562 // madness in terms of diagnostics.
2563 attrs.clear();
2564 attrs.Range = SourceRange();
2565
2566 ParseCXX11Attributes(attrs);
2567 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002568 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002569
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002570 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002571 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002572 if (DS.hasTypeSpecifier()) {
2573 bool AllowNonIdentifiers
2574 = (getCurScope()->getFlags() & (Scope::ControlScope |
2575 Scope::BlockScope |
2576 Scope::TemplateParamScope |
2577 Scope::FunctionPrototypeScope |
2578 Scope::AtCatchScope)) == 0;
2579 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002580 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002581 (DSContext == DSC_class && DS.isFriendSpecified());
2582
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002583 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002584 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002585 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002586 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002587 }
2588
Douglas Gregor80039242011-02-15 20:33:25 +00002589 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2590 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2591 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002592 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002593 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002594 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002595 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002596 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002597 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002598
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002599 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002600 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002601 }
2602
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002603 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002604 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002605 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002606 if (!DS.hasTypeSpecifier())
2607 DS.SetTypeSpecError();
2608 goto DoneWithDeclSpec;
2609 }
John McCall8bc2a702010-03-01 18:20:46 +00002610 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2611 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002612 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002613
2614 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002615 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002616 goto DoneWithDeclSpec;
2617
John McCall9dab4e62009-12-12 11:40:51 +00002618 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002619 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2620 Tok.getAnnotationRange(),
2621 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002622
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002623 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002624 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002625 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002626 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002627 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002628 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002629
2630 // C++ [class.qual]p2:
2631 // In a lookup in which the constructor is an acceptable lookup
2632 // result and the nested-name-specifier nominates a class C:
2633 //
2634 // - if the name specified after the
2635 // nested-name-specifier, when looked up in C, is the
2636 // injected-class-name of C (Clause 9), or
2637 //
2638 // - if the name specified after the nested-name-specifier
2639 // is the same as the identifier or the
2640 // simple-template-id's template-name in the last
2641 // component of the nested-name-specifier,
2642 //
2643 // the name is instead considered to name the constructor of
2644 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002645 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002646 // Thus, if the template-name is actually the constructor
2647 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002648 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002649 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002650 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002651 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002652 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002653 if (isConstructorDeclarator()) {
2654 // The user meant this to be an out-of-line constructor
2655 // definition, but template arguments are not allowed
2656 // there. Just allow this as a constructor; we'll
2657 // complain about it later.
2658 goto DoneWithDeclSpec;
2659 }
2660
2661 // The user meant this to name a type, but it actually names
2662 // a constructor with some extraneous template
2663 // arguments. Complain, then parse it as a type as the user
2664 // intended.
2665 Diag(TemplateId->TemplateNameLoc,
2666 diag::err_out_of_line_template_id_names_constructor)
2667 << TemplateId->Name;
2668 }
2669
John McCall9dab4e62009-12-12 11:40:51 +00002670 DS.getTypeSpecScope() = SS;
2671 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002672 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002673 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002674 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002675 continue;
2676 }
2677
Douglas Gregorc5790df2009-09-28 07:26:33 +00002678 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002679 DS.getTypeSpecScope() = SS;
2680 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002681 if (Tok.getAnnotationValue()) {
2682 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002683 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002684 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002685 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002686 if (isInvalid)
2687 break;
John McCallba7bf592010-08-24 05:47:05 +00002688 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002689 else
2690 DS.SetTypeSpecError();
2691 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2692 ConsumeToken(); // The typename
2693 }
2694
Douglas Gregor167fa622009-03-25 15:40:00 +00002695 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002696 goto DoneWithDeclSpec;
2697
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002698 // If we're in a context where the identifier could be a class name,
2699 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002700 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002701 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002702 &SS)) {
2703 if (isConstructorDeclarator())
2704 goto DoneWithDeclSpec;
2705
2706 // As noted in C++ [class.qual]p2 (cited above), when the name
2707 // of the class is qualified in a context where it could name
2708 // a constructor, its a constructor name. However, we've
2709 // looked at the declarator, and the user probably meant this
2710 // to be a type. Complain that it isn't supposed to be treated
2711 // as a type, then proceed to parse it as a type.
2712 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2713 << Next.getIdentifierInfo();
2714 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002715
John McCallba7bf592010-08-24 05:47:05 +00002716 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2717 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002718 getCurScope(), &SS,
2719 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002720 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002721 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002722
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002723 // If the referenced identifier is not a type, then this declspec is
2724 // erroneous: We already checked about that it has no type specifier, and
2725 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002726 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002727 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002728 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002729 ParsedAttributesWithRange Attrs(AttrFactory);
2730 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2731 if (!Attrs.empty()) {
2732 AttrsLastTime = true;
2733 attrs.takeAllFrom(Attrs);
2734 }
2735 continue;
2736 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002737 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002738 }
Mike Stump11289f42009-09-09 15:08:12 +00002739
John McCall9dab4e62009-12-12 11:40:51 +00002740 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002741 ConsumeToken(); // The C++ scope.
2742
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002743 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002744 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002745 if (isInvalid)
2746 break;
Mike Stump11289f42009-09-09 15:08:12 +00002747
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002748 DS.SetRangeEnd(Tok.getLocation());
2749 ConsumeToken(); // The typename.
2750
2751 continue;
2752 }
Mike Stump11289f42009-09-09 15:08:12 +00002753
Chris Lattnere387d9e2009-01-21 19:48:37 +00002754 case tok::annot_typename: {
Richard Smith404dfb42013-11-19 22:47:36 +00002755 // If we've previously seen a tag definition, we were almost surely
2756 // missing a semicolon after it.
2757 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
2758 goto DoneWithDeclSpec;
2759
John McCallba7bf592010-08-24 05:47:05 +00002760 if (Tok.getAnnotationValue()) {
2761 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002762 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002763 DiagID, T);
2764 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002765 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002766
Chris Lattner005fc1b2010-04-05 18:18:31 +00002767 if (isInvalid)
2768 break;
2769
Chris Lattnere387d9e2009-01-21 19:48:37 +00002770 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2771 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002772
Chris Lattnere387d9e2009-01-21 19:48:37 +00002773 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2774 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002775 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002776 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002777 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002778
Chris Lattnere387d9e2009-01-21 19:48:37 +00002779 continue;
2780 }
Mike Stump11289f42009-09-09 15:08:12 +00002781
Douglas Gregor06873092011-04-28 15:48:45 +00002782 case tok::kw___is_signed:
2783 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2784 // typically treats it as a trait. If we see __is_signed as it appears
2785 // in libstdc++, e.g.,
2786 //
2787 // static const bool __is_signed;
2788 //
2789 // then treat __is_signed as an identifier rather than as a keyword.
2790 if (DS.getTypeSpecType() == TST_bool &&
2791 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
Alp Toker47642d22013-12-03 06:13:01 +00002792 DS.getStorageClassSpec() == DeclSpec::SCS_static)
2793 TryKeywordIdentFallback(true);
Douglas Gregor06873092011-04-28 15:48:45 +00002794
2795 // We're done with the declaration-specifiers.
2796 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002797
Chris Lattner16fac4f2008-07-26 01:18:38 +00002798 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002799 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002800 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002801 // In C++, check to see if this is a scope specifier like foo::bar::, if
2802 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002803 if (getLangOpts().CPlusPlus) {
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002804 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002805 if (!DS.hasTypeSpecifier())
2806 DS.SetTypeSpecError();
2807 goto DoneWithDeclSpec;
2808 }
2809 if (!Tok.is(tok::identifier))
2810 continue;
2811 }
Mike Stump11289f42009-09-09 15:08:12 +00002812
Chris Lattner16fac4f2008-07-26 01:18:38 +00002813 // This identifier can only be a typedef name if we haven't already seen
2814 // a type-specifier. Without this check we misparse:
2815 // typedef int X; struct Y { short X; }; as 'short int'.
2816 if (DS.hasTypeSpecifier())
2817 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002818
John Thompson22334602010-02-05 00:12:22 +00002819 // Check for need to substitute AltiVec keyword tokens.
2820 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2821 break;
2822
Richard Smith3092a3b2012-05-09 18:56:43 +00002823 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2824 // allow the use of a typedef name as a type specifier.
2825 if (DS.isTypeAltiVecVector())
2826 goto DoneWithDeclSpec;
2827
John McCallba7bf592010-08-24 05:47:05 +00002828 ParsedType TypeRep =
2829 Actions.getTypeName(*Tok.getIdentifierInfo(),
2830 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002831
Chris Lattner6cc055a2009-04-12 20:42:31 +00002832 // If this is not a typedef name, don't parse it as part of the declspec,
2833 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002834 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002835 ParsedAttributesWithRange Attrs(AttrFactory);
2836 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2837 if (!Attrs.empty()) {
2838 AttrsLastTime = true;
2839 attrs.takeAllFrom(Attrs);
2840 }
2841 continue;
2842 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002843 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002844 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002845
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002846 // If we're in a context where the identifier could be a class name,
2847 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002848 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002849 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002850 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002851 goto DoneWithDeclSpec;
2852
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002853 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002854 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002855 if (isInvalid)
2856 break;
Mike Stump11289f42009-09-09 15:08:12 +00002857
Chris Lattner16fac4f2008-07-26 01:18:38 +00002858 DS.SetRangeEnd(Tok.getLocation());
2859 ConsumeToken(); // The identifier
2860
2861 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2862 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002863 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002864 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002865 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002866
Steve Naroffcd5e7822008-09-22 10:28:57 +00002867 // Need to support trailing type qualifiers (e.g. "id<p> const").
2868 // If a type specifier follows, it will be diagnosed elsewhere.
2869 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002870 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002871
2872 // type-name
2873 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002874 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002875 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002876 // This template-id does not refer to a type name, so we're
2877 // done with the type-specifiers.
2878 goto DoneWithDeclSpec;
2879 }
2880
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002881 // If we're in a context where the template-id could be a
2882 // constructor name or specialization, check whether this is a
2883 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002884 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002885 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002886 isConstructorDeclarator())
2887 goto DoneWithDeclSpec;
2888
Douglas Gregor7f741122009-02-25 19:37:18 +00002889 // Turn the template-id annotation token into a type annotation
2890 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002891 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002892 continue;
2893 }
2894
Chris Lattnere37e2332006-08-15 04:50:22 +00002895 // GNU attributes support.
2896 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002897 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002898 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002899
2900 // Microsoft declspec support.
2901 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002902 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002903 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002904
Steve Naroff44ac7772008-12-25 14:16:32 +00002905 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002906 case tok::kw___forceinline: {
Serge Pavlov750db652013-11-13 06:57:53 +00002907 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002908 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002909 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002910 // FIXME: This does not work correctly if it is set to be a declspec
2911 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman00e99962013-08-31 01:11:41 +00002912 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2913 AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002914 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002915 }
Eli Friedman53339e02009-06-08 23:27:34 +00002916
Aaron Ballman317a77f2013-05-22 23:25:32 +00002917 case tok::kw___sptr:
2918 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002919 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002920 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002921 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002922 case tok::kw___cdecl:
2923 case tok::kw___stdcall:
2924 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002925 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002926 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002927 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002928 continue;
2929
Dawn Perchik335e16b2010-09-03 01:29:35 +00002930 // Borland single token adornments.
2931 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002932 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002933 continue;
2934
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002935 // OpenCL single token adornments.
2936 case tok::kw___kernel:
2937 ParseOpenCLAttributes(DS.getAttributes());
2938 continue;
2939
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002940 // storage-class-specifier
2941 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002942 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2943 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002944 break;
2945 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002946 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002947 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002948 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2949 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002950 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002951 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002952 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2953 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002954 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002955 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002956 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002957 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002958 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2959 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002960 break;
2961 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002962 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002963 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002964 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2965 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002966 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002967 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002968 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002969 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002970 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2971 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002972 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002973 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2974 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002975 break;
2976 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002977 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2978 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002979 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002980 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002981 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2982 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002983 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002984 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002985 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2986 PrevSpec, DiagID);
2987 break;
2988 case tok::kw_thread_local:
2989 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2990 PrevSpec, DiagID);
2991 break;
2992 case tok::kw__Thread_local:
2993 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2994 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002995 break;
Mike Stump11289f42009-09-09 15:08:12 +00002996
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002997 // function-specifier
2998 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00002999 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003000 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00003001 case tok::kw_virtual:
Serge Pavlov750db652013-11-13 06:57:53 +00003002 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00003003 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00003004 case tok::kw_explicit:
Serge Pavlov750db652013-11-13 06:57:53 +00003005 isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00003006 break;
Richard Smith0015f092013-01-17 22:16:11 +00003007 case tok::kw__Noreturn:
3008 if (!getLangOpts().C11)
3009 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlov750db652013-11-13 06:57:53 +00003010 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00003011 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003012
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003013 // alignment-specifier
3014 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003015 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00003016 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003017 ParseAlignmentSpecifier(DS.getAttributes());
3018 continue;
3019
Anders Carlssoncd8db412009-05-06 04:46:28 +00003020 // friend
3021 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00003022 if (DSContext == DSC_class)
3023 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
3024 else {
3025 PrevSpec = ""; // not actually used by the diagnostic
3026 DiagID = diag::err_friend_invalid_in_context;
3027 isInvalid = true;
3028 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00003029 break;
Mike Stump11289f42009-09-09 15:08:12 +00003030
Douglas Gregor26701a42011-09-09 02:06:17 +00003031 // Modules
3032 case tok::kw___module_private__:
3033 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
3034 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003035
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003036 // constexpr
3037 case tok::kw_constexpr:
3038 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
3039 break;
3040
Chris Lattnere387d9e2009-01-21 19:48:37 +00003041 // type-specifier
3042 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00003043 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
3044 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003045 break;
3046 case tok::kw_long:
3047 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00003048 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
3049 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003050 else
John McCall49bfce42009-08-03 20:12:06 +00003051 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3052 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003053 break;
Francois Pichet84133e42011-04-28 01:59:37 +00003054 case tok::kw___int64:
3055 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3056 DiagID);
3057 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003058 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00003059 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3060 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003061 break;
3062 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003063 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3064 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003065 break;
3066 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00003067 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3068 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003069 break;
3070 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00003071 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3072 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003073 break;
3074 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003075 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
3076 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003077 break;
3078 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003079 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
3080 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003081 break;
3082 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003083 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
3084 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003085 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003086 case tok::kw___int128:
3087 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
3088 DiagID);
3089 break;
3090 case tok::kw_half:
3091 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
3092 DiagID);
3093 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003094 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003095 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
3096 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003097 break;
3098 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003099 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
3100 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003101 break;
3102 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003103 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
3104 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003105 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003106 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003107 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
3108 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003109 break;
3110 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003111 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
3112 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003113 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003114 case tok::kw_bool:
3115 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003116 if (Tok.is(tok::kw_bool) &&
3117 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3118 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3119 PrevSpec = ""; // Not used by the diagnostic.
3120 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003121 // For better error recovery.
3122 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003123 isInvalid = true;
3124 } else {
3125 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
3126 DiagID);
3127 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003128 break;
3129 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003130 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
3131 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003132 break;
3133 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003134 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
3135 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003136 break;
3137 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003138 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
3139 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003140 break;
John Thompson22334602010-02-05 00:12:22 +00003141 case tok::kw___vector:
3142 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3143 break;
3144 case tok::kw___pixel:
3145 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3146 break;
John McCall39439732011-04-09 22:50:59 +00003147 case tok::kw___unknown_anytype:
3148 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3149 PrevSpec, DiagID);
3150 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003151
3152 // class-specifier:
3153 case tok::kw_class:
3154 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003155 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003156 case tok::kw_union: {
3157 tok::TokenKind Kind = Tok.getKind();
3158 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003159
3160 // These are attributes following class specifiers.
3161 // To produce better diagnostic, we parse them when
3162 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003163 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003164 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003165 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003166
3167 // If there are attributes following class specifier,
3168 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003169 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003170 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003171 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003172 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003173 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003174 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003175
3176 // enum-specifier:
3177 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003178 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003179 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003180 continue;
3181
3182 // cv-qualifier:
3183 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003184 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003185 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003186 break;
3187 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003188 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003189 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003190 break;
3191 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003192 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003193 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003194 break;
3195
Douglas Gregor333489b2009-03-27 23:10:48 +00003196 // C++ typename-specifier:
3197 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003198 if (TryAnnotateTypeOrScopeToken()) {
3199 DS.SetTypeSpecError();
3200 goto DoneWithDeclSpec;
3201 }
3202 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003203 continue;
3204 break;
3205
Chris Lattnere387d9e2009-01-21 19:48:37 +00003206 // GNU typeof support.
3207 case tok::kw_typeof:
3208 ParseTypeofSpecifier(DS);
3209 continue;
3210
David Blaikie15a430a2011-12-04 05:04:18 +00003211 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003212 ParseDecltypeSpecifier(DS);
3213 continue;
3214
Alexis Hunt4a257072011-05-19 05:37:45 +00003215 case tok::kw___underlying_type:
3216 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003217 continue;
3218
3219 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003220 // C11 6.7.2.4/4:
3221 // If the _Atomic keyword is immediately followed by a left parenthesis,
3222 // it is interpreted as a type specifier (with a type name), not as a
3223 // type qualifier.
3224 if (NextToken().is(tok::l_paren)) {
3225 ParseAtomicSpecifier(DS);
3226 continue;
3227 }
3228 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3229 getLangOpts());
3230 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003231
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003232 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003233 case tok::kw___private:
3234 case tok::kw___global:
3235 case tok::kw___local:
3236 case tok::kw___constant:
3237 case tok::kw___read_only:
3238 case tok::kw___write_only:
3239 case tok::kw___read_write:
3240 ParseOpenCLQualifiers(DS);
3241 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003242
Steve Naroffcfdf6162008-06-05 00:02:44 +00003243 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003244 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003245 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3246 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003247 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003248 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003249
Douglas Gregor3a001f42010-11-19 17:10:50 +00003250 if (!ParseObjCProtocolQualifiers(DS))
3251 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3252 << FixItHint::CreateInsertion(Loc, "id")
3253 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003254
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003255 // Need to support trailing type qualifiers (e.g. "id<p> const").
3256 // If a type specifier follows, it will be diagnosed elsewhere.
3257 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003258 }
John McCall49bfce42009-08-03 20:12:06 +00003259 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003260 if (isInvalid) {
3261 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003262 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003263
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003264 if (DiagID == diag::ext_duplicate_declspec)
3265 Diag(Tok, DiagID)
3266 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3267 else
3268 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003269 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003270
Chris Lattner2e232092008-03-13 06:29:04 +00003271 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003272 if (DiagID != diag::err_bool_redeclaration)
3273 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003274
3275 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003276 }
3277}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003278
Chris Lattner70ae4912007-10-29 04:42:53 +00003279/// ParseStructDeclaration - Parse a struct declaration without the terminating
3280/// semicolon.
3281///
Chris Lattner90a26b02007-01-23 04:38:16 +00003282/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003283/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003284/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003285/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003286/// struct-declarator-list:
3287/// struct-declarator
3288/// struct-declarator-list ',' struct-declarator
3289/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3290/// struct-declarator:
3291/// declarator
3292/// [GNU] declarator attributes[opt]
3293/// declarator[opt] ':' constant-expression
3294/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3295///
Chris Lattnera12405b2008-04-10 06:46:29 +00003296void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003297ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003298
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003299 if (Tok.is(tok::kw___extension__)) {
3300 // __extension__ silences extension warnings in the subexpression.
3301 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003302 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003303 return ParseStructDeclaration(DS, Fields);
3304 }
Mike Stump11289f42009-09-09 15:08:12 +00003305
Steve Naroff97170802007-08-20 22:28:22 +00003306 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003307 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003308
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003309 // If there are no declarators, this is a free-standing declaration
3310 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003311 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003312 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3313 DS);
3314 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003315 return;
3316 }
3317
3318 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003319 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003320 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003321 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003322 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003323 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003324
Bill Wendling44426052012-12-20 19:22:21 +00003325 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003326 if (!FirstDeclarator)
3327 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003328
Steve Naroff97170802007-08-20 22:28:22 +00003329 /// struct-declarator: declarator
3330 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003331 if (Tok.isNot(tok::colon)) {
3332 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3333 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003334 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003335 }
Mike Stump11289f42009-09-09 15:08:12 +00003336
Alp Toker8fbec672013-12-17 23:29:36 +00003337 if (TryConsumeToken(tok::colon)) {
John McCalldadc5752010-08-24 06:29:42 +00003338 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003339 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003340 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00003341 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003342 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003343 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003344
Steve Naroff97170802007-08-20 22:28:22 +00003345 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003346 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003347
John McCallcfefb6d2009-11-03 02:38:08 +00003348 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003349 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003350
Steve Naroff97170802007-08-20 22:28:22 +00003351 // If we don't have a comma, it is either the end of the list (a ';')
3352 // or an error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00003353 if (!TryConsumeToken(tok::comma, CommaLoc))
Chris Lattner70ae4912007-10-29 04:42:53 +00003354 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003355
John McCallcfefb6d2009-11-03 02:38:08 +00003356 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003357 }
Steve Naroff97170802007-08-20 22:28:22 +00003358}
3359
3360/// ParseStructUnionBody
3361/// struct-contents:
3362/// struct-declaration-list
3363/// [EXT] empty
3364/// [GNU] "struct-declaration-list" without terminatoring ';'
3365/// struct-declaration-list:
3366/// struct-declaration
3367/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003368/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003369///
Chris Lattner1300fb92007-01-23 23:42:53 +00003370void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003371 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003372 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3373 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003374 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003375
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003376 BalancedDelimiterTracker T(*this, tok::l_brace);
3377 if (T.consumeOpen())
3378 return;
Mike Stump11289f42009-09-09 15:08:12 +00003379
Douglas Gregor658b9552009-01-09 22:42:13 +00003380 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003381 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003382
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003383 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003384
Chris Lattner7b9ace62007-01-23 20:11:08 +00003385 // While we still have something to read, read the declarations in the struct.
Richard Smith34f30512013-11-23 04:06:09 +00003386 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003387 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003388
Chris Lattner736ed5d2007-06-09 05:59:07 +00003389 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003390 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003391 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003392 continue;
3393 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003394
Andy Gibbsc804e082013-04-03 09:46:04 +00003395 // Parse _Static_assert declaration.
3396 if (Tok.is(tok::kw__Static_assert)) {
3397 SourceLocation DeclEnd;
3398 ParseStaticAssertDeclaration(DeclEnd);
3399 continue;
3400 }
3401
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003402 if (Tok.is(tok::annot_pragma_pack)) {
3403 HandlePragmaPack();
3404 continue;
3405 }
3406
3407 if (Tok.is(tok::annot_pragma_align)) {
3408 HandlePragmaAlign();
3409 continue;
3410 }
3411
John McCallcfefb6d2009-11-03 02:38:08 +00003412 if (!Tok.is(tok::at)) {
3413 struct CFieldCallback : FieldCallback {
3414 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003415 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003416 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003417
John McCall48871652010-08-21 09:40:31 +00003418 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003419 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003420 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3421
Eli Friedman934dbbf2012-08-08 23:53:27 +00003422 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003423 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003424 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003425 FD.D.getDeclSpec().getSourceRange().getBegin(),
3426 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003427 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003428 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003429 }
John McCallcfefb6d2009-11-03 02:38:08 +00003430 } Callback(*this, TagDecl, FieldDecls);
3431
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003432 // Parse all the comma separated declarators.
3433 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003434 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003435 } else { // Handle @defs
3436 ConsumeToken();
3437 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3438 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003439 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003440 continue;
3441 }
3442 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003443 ExpectAndConsume(tok::l_paren);
Chris Lattner535b8302008-06-21 19:39:06 +00003444 if (!Tok.is(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003445 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003446 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003447 continue;
3448 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003449 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003450 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003451 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003452 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3453 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003454 ExpectAndConsume(tok::r_paren);
Mike Stump11289f42009-09-09 15:08:12 +00003455 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003456
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003457 if (TryConsumeToken(tok::semi))
3458 continue;
3459
3460 if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003461 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003462 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003463 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003464
3465 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3466 // Skip to end of block or statement to avoid ext-warning on extra ';'.
3467 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
3468 // If we stopped at a ';', eat it.
3469 TryConsumeToken(tok::semi);
Chris Lattner90a26b02007-01-23 04:38:16 +00003470 }
Mike Stump11289f42009-09-09 15:08:12 +00003471
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003472 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003473
John McCall084e83d2011-03-24 11:26:52 +00003474 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003475 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003476 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003477
Douglas Gregor0be31a22010-07-02 17:43:08 +00003478 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003479 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003480 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003481 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003482 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003483 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3484 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003485}
3486
Chris Lattner3b561a32006-08-13 00:12:11 +00003487/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003488/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003489/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003490///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003491/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3492/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003493/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3494/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003495/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003496/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003497///
Richard Smith7d137e32012-03-23 03:33:32 +00003498/// [C++11] enum-head '{' enumerator-list[opt] '}'
3499/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003500///
Richard Smith7d137e32012-03-23 03:33:32 +00003501/// enum-head: [C++11]
3502/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3503/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3504/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003505///
Richard Smith7d137e32012-03-23 03:33:32 +00003506/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003507/// 'enum'
3508/// 'enum' 'class'
3509/// 'enum' 'struct'
3510///
Richard Smith7d137e32012-03-23 03:33:32 +00003511/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003512/// ':' type-specifier-seq
3513///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003514/// [C++] elaborated-type-specifier:
3515/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3516///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003517void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003518 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003519 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003520 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003521 if (Tok.is(tok::code_completion)) {
3522 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003523 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003524 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003525 }
John McCallcb432fa2011-07-06 05:58:41 +00003526
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003527 // If attributes exist after tag, parse them.
3528 ParsedAttributesWithRange attrs(AttrFactory);
3529 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003530 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003531
3532 // If declspecs exist after tag, parse them.
3533 while (Tok.is(tok::kw___declspec))
3534 ParseMicrosoftDeclSpec(attrs);
3535
Richard Smith0f8ee222012-01-10 01:33:14 +00003536 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003537 bool IsScopedUsingClassTag = false;
3538
John McCallbeae29a2012-06-23 22:30:04 +00003539 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003540 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3541 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3542 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003543 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003544 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003545
Bill Wendling44426052012-12-20 19:22:21 +00003546 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003547 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003548 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003549
3550 // They are allowed afterwards, though.
3551 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003552 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003553 while (Tok.is(tok::kw___declspec))
3554 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003555 }
Richard Smith7d137e32012-03-23 03:33:32 +00003556
John McCall6347b682012-05-07 06:16:58 +00003557 // C++11 [temp.explicit]p12:
3558 // The usual access controls do not apply to names used to specify
3559 // explicit instantiations.
3560 // We extend this to also cover explicit specializations. Note that
3561 // we don't suppress if this turns out to be an elaborated type
3562 // specifier.
3563 bool shouldDelayDiagsInTag =
3564 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3565 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3566 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003567
Richard Smithbfdb1082012-03-12 08:56:40 +00003568 // Enum definitions should not be parsed in a trailing-return-type.
3569 bool AllowDeclaration = DSC != DSC_trailing;
3570
3571 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003572 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003573 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003574
Abramo Bagnarad7548482010-05-19 21:37:53 +00003575 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003576 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003577 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3578 // if a fixed underlying type is allowed.
3579 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003580
3581 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003582 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003583 return;
3584
3585 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003586 Diag(Tok, diag::err_expected) << tok::identifier;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003587 if (Tok.isNot(tok::l_brace)) {
3588 // Has no name and is not a definition.
3589 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003590 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003591 return;
3592 }
3593 }
3594 }
Mike Stump11289f42009-09-09 15:08:12 +00003595
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003596 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003597 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003598 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Alp Tokerec543272013-12-24 09:48:30 +00003599 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
Mike Stump11289f42009-09-09 15:08:12 +00003600
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003601 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003602 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00003603 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003604 }
Mike Stump11289f42009-09-09 15:08:12 +00003605
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003606 // If an identifier is present, consume and remember it.
3607 IdentifierInfo *Name = 0;
3608 SourceLocation NameLoc;
3609 if (Tok.is(tok::identifier)) {
3610 Name = Tok.getIdentifierInfo();
3611 NameLoc = ConsumeToken();
3612 }
Mike Stump11289f42009-09-09 15:08:12 +00003613
Richard Smith0f8ee222012-01-10 01:33:14 +00003614 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003615 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3616 // declaration of a scoped enumeration.
3617 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003618 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003619 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003620 }
3621
John McCall6347b682012-05-07 06:16:58 +00003622 // Okay, end the suppression area. We'll decide whether to emit the
3623 // diagnostics in a second.
3624 if (shouldDelayDiagsInTag)
3625 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003626
Douglas Gregor0bf31402010-10-08 23:50:27 +00003627 TypeResult BaseType;
3628
Douglas Gregord1f69f62010-12-01 17:42:47 +00003629 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003630 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003631 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003632 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003633 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003634 // If we're in class scope, this can either be an enum declaration with
3635 // an underlying type, or a declaration of a bitfield member. We try to
3636 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003637 // (integer literal, sizeof); if it's still ambiguous, we then consider
3638 // anything that's a simple-type-specifier followed by '(' as an
3639 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003640 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003641 EnterExpressionEvaluationContext Unevaluated(Actions,
3642 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003643 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003644 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003645 // bit-field. This is the common case.
3646 if (TPR == TPResult::True())
3647 PossibleBitfield = true;
3648 // If the next token starts a type-specifier-seq, it may be either a
3649 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003650 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003651 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003652 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003653 GetLookAheadToken(2).getKind() == tok::semi) {
3654 // Consume the ':'.
3655 ConsumeToken();
3656 } else {
3657 // We have the start of a type-specifier-seq, so we have to perform
3658 // tentative parsing to determine whether we have an expression or a
3659 // type.
3660 TentativeParsingAction TPA(*this);
3661
3662 // Consume the ':'.
3663 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003664
3665 // If we see a type specifier followed by an open-brace, we have an
3666 // ambiguity between an underlying type and a C++11 braced
3667 // function-style cast. Resolve this by always treating it as an
3668 // underlying type.
3669 // FIXME: The standard is not entirely clear on how to disambiguate in
3670 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003671 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003672 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003673 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003674 // We'll parse this as a bitfield later.
3675 PossibleBitfield = true;
3676 TPA.Revert();
3677 } else {
3678 // We have a type-specifier-seq.
3679 TPA.Commit();
3680 }
3681 }
3682 } else {
3683 // Consume the ':'.
3684 ConsumeToken();
3685 }
3686
3687 if (!PossibleBitfield) {
3688 SourceRange Range;
3689 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003690
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003691 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003692 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003693 } else if (!getLangOpts().ObjC2) {
3694 if (getLangOpts().CPlusPlus)
3695 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3696 else
3697 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3698 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003699 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003700 }
3701
Richard Smith0f8ee222012-01-10 01:33:14 +00003702 // There are four options here. If we have 'friend enum foo;' then this is a
3703 // friend declaration, and cannot have an accompanying definition. If we have
3704 // 'enum foo;', then this is a forward declaration. If we have
3705 // 'enum foo {...' then this is a definition. Otherwise we have something
3706 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003707 //
3708 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3709 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3710 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3711 //
John McCallfaf5fb42010-08-26 23:41:50 +00003712 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003713 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003714 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003715 } else if (Tok.is(tok::l_brace)) {
3716 if (DS.isFriendSpecified()) {
3717 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3718 << SourceRange(DS.getFriendSpecLoc());
3719 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003720 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00003721 TUK = Sema::TUK_Friend;
3722 } else {
3723 TUK = Sema::TUK_Definition;
3724 }
Richard Smith649c7b062014-01-08 00:56:48 +00003725 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00003726 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003727 (Tok.isAtStartOfLine() &&
3728 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003729 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3730 if (Tok.isNot(tok::semi)) {
3731 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00003732 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003733 PP.EnterToken(Tok);
3734 Tok.setKind(tok::semi);
3735 }
John McCall6347b682012-05-07 06:16:58 +00003736 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003737 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003738 }
3739
3740 // If this is an elaborated type specifier, and we delayed
3741 // diagnostics before, just merge them into the current pool.
3742 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3743 diagsFromTag.redelay();
3744 }
Richard Smith7d137e32012-03-23 03:33:32 +00003745
3746 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003747 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003748 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003749 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003750 // Skip the rest of this declarator, up until the comma or semicolon.
3751 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003752 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00003753 return;
3754 }
3755
3756 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3757 // Enumerations can't be explicitly instantiated.
3758 DS.SetTypeSpecError();
3759 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3760 return;
3761 }
3762
3763 assert(TemplateInfo.TemplateParams && "no template parameters");
3764 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3765 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003766 }
Chad Rosierc1183952012-06-26 22:30:43 +00003767
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003768 if (TUK == Sema::TUK_Reference)
3769 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003770
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003771 if (!Name && TUK != Sema::TUK_Definition) {
3772 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003773
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003774 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003775 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003776 return;
3777 }
Richard Smith7d137e32012-03-23 03:33:32 +00003778
Douglas Gregord6ab8742009-05-28 23:31:59 +00003779 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003780 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003781 const char *PrevSpec = 0;
3782 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003783 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003784 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003785 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003786 Owned, IsDependent, ScopedEnumKWLoc,
Richard Smith649c7b062014-01-08 00:56:48 +00003787 IsScopedUsingClassTag, BaseType,
3788 DSC == DSC_type_specifier);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003789
Douglas Gregorba41d012010-04-24 16:38:41 +00003790 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003791 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003792 // dependent tag.
3793 if (!Name) {
3794 DS.SetTypeSpecError();
3795 Diag(Tok, diag::err_expected_type_name_after_typename);
3796 return;
3797 }
Chad Rosierc1183952012-06-26 22:30:43 +00003798
Douglas Gregor0be31a22010-07-02 17:43:08 +00003799 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003800 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003801 NameLoc);
3802 if (Type.isInvalid()) {
3803 DS.SetTypeSpecError();
3804 return;
3805 }
Chad Rosierc1183952012-06-26 22:30:43 +00003806
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003807 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3808 NameLoc.isValid() ? NameLoc : StartLoc,
3809 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003810 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003811
Douglas Gregorba41d012010-04-24 16:38:41 +00003812 return;
3813 }
Mike Stump11289f42009-09-09 15:08:12 +00003814
John McCall48871652010-08-21 09:40:31 +00003815 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003816 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003817 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003818 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003819 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003820 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00003821 }
Chad Rosierc1183952012-06-26 22:30:43 +00003822
Douglas Gregorba41d012010-04-24 16:38:41 +00003823 DS.SetTypeSpecError();
3824 return;
3825 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003826
Richard Smith369b9f92012-06-25 21:37:02 +00003827 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003828 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003829
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003830 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3831 NameLoc.isValid() ? NameLoc : StartLoc,
3832 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003833 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003834}
3835
Chris Lattnerc1915e22007-01-25 07:29:02 +00003836/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3837/// enumerator-list:
3838/// enumerator
3839/// enumerator-list ',' enumerator
3840/// enumerator:
3841/// enumeration-constant
3842/// enumeration-constant '=' constant-expression
3843/// enumeration-constant:
3844/// identifier
3845///
John McCall48871652010-08-21 09:40:31 +00003846void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003847 // Enter the scope of the enum body and start the definition.
3848 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003849 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003850
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003851 BalancedDelimiterTracker T(*this, tok::l_brace);
3852 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003853
Chris Lattner37256fb2007-08-27 17:24:30 +00003854 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003855 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003856 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003857
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003858 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003859
John McCall48871652010-08-21 09:40:31 +00003860 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003861
Chris Lattnerc1915e22007-01-25 07:29:02 +00003862 // Parse the enumerator-list.
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003863 while (Tok.isNot(tok::r_brace)) {
3864 // Parse enumerator. If failed, try skipping till the start of the next
3865 // enumerator definition.
3866 if (Tok.isNot(tok::identifier)) {
3867 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
3868 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
3869 TryConsumeToken(tok::comma))
3870 continue;
3871 break;
3872 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003873 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3874 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003875
John McCall811a0f52010-10-22 23:36:17 +00003876 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003877 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003878 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003879 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003880 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003881
Chris Lattnerc1915e22007-01-25 07:29:02 +00003882 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003883 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003884 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003885
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003886 if (TryConsumeToken(tok::equal, EqualLoc)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003887 AssignedVal = ParseConstantExpression();
3888 if (AssignedVal.isInvalid())
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003889 SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003890 }
Mike Stump11289f42009-09-09 15:08:12 +00003891
Chris Lattnerc1915e22007-01-25 07:29:02 +00003892 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003893 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3894 LastEnumConstDecl,
3895 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003896 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003897 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003898 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003899
Chris Lattner4ef40012007-06-11 01:28:17 +00003900 EnumConstantDecls.push_back(EnumConstDecl);
3901 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003902
Douglas Gregorce66d022010-09-07 14:51:08 +00003903 if (Tok.is(tok::identifier)) {
3904 // We're missing a comma between enumerators.
3905 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003906 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003907 << FixItHint::CreateInsertion(Loc, ", ");
3908 continue;
3909 }
Chad Rosierc1183952012-06-26 22:30:43 +00003910
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003911 // Emumerator definition must be finished, only comma or r_brace are
3912 // allowed here.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003913 SourceLocation CommaLoc;
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003914 if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
3915 if (EqualLoc.isValid())
3916 Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
3917 << tok::comma;
3918 else
3919 Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
3920 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
3921 if (TryConsumeToken(tok::comma, CommaLoc))
3922 continue;
3923 } else {
3924 break;
3925 }
3926 }
Mike Stump11289f42009-09-09 15:08:12 +00003927
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003928 // If comma is followed by r_brace, emit appropriate warning.
3929 if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003930 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003931 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3932 diag::ext_enumerator_list_comma_cxx :
3933 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003934 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003935 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003936 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3937 << FixItHint::CreateRemoval(CommaLoc);
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003938 break;
Richard Smith5d164bc2011-10-15 05:09:34 +00003939 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003940 }
Mike Stump11289f42009-09-09 15:08:12 +00003941
Chris Lattnerc1915e22007-01-25 07:29:02 +00003942 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003943 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003944
Chris Lattnerc1915e22007-01-25 07:29:02 +00003945 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003946 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003947 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003948
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003949 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003950 EnumDecl, EnumConstantDecls,
3951 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003952 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003953
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003954 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003955 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3956 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003957
3958 // The next token must be valid after an enum definition. If not, a ';'
3959 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003960 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3961 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Alp Toker383d2c42014-01-01 03:08:43 +00003962 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003963 // Push this token back into the preprocessor and change our current token
3964 // to ';' so that the rest of the code recovers as though there were an
3965 // ';' after the definition.
3966 PP.EnterToken(Tok);
3967 Tok.setKind(tok::semi);
3968 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003969}
Chris Lattner3b561a32006-08-13 00:12:11 +00003970
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003971/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003972/// start of a type-qualifier-list.
3973bool Parser::isTypeQualifier() const {
3974 switch (Tok.getKind()) {
3975 default: return false;
Alp Tokerde50ff32013-12-17 18:17:46 +00003976 // type-qualifier
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003977 case tok::kw_const:
3978 case tok::kw_volatile:
3979 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003980 case tok::kw___private:
3981 case tok::kw___local:
3982 case tok::kw___global:
3983 case tok::kw___constant:
3984 case tok::kw___read_only:
3985 case tok::kw___read_write:
3986 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003987 return true;
3988 }
3989}
3990
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003991/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3992/// is definitely a type-specifier. Return false if it isn't part of a type
3993/// specifier or if we're not sure.
3994bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3995 switch (Tok.getKind()) {
3996 default: return false;
3997 // type-specifiers
3998 case tok::kw_short:
3999 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004000 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004001 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004002 case tok::kw_signed:
4003 case tok::kw_unsigned:
4004 case tok::kw__Complex:
4005 case tok::kw__Imaginary:
4006 case tok::kw_void:
4007 case tok::kw_char:
4008 case tok::kw_wchar_t:
4009 case tok::kw_char16_t:
4010 case tok::kw_char32_t:
4011 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004012 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004013 case tok::kw_float:
4014 case tok::kw_double:
4015 case tok::kw_bool:
4016 case tok::kw__Bool:
4017 case tok::kw__Decimal32:
4018 case tok::kw__Decimal64:
4019 case tok::kw__Decimal128:
4020 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00004021
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004022 // struct-or-union-specifier (C99) or class-specifier (C++)
4023 case tok::kw_class:
4024 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004025 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004026 case tok::kw_union:
4027 // enum-specifier
4028 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00004029
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004030 // typedef-name
4031 case tok::annot_typename:
4032 return true;
4033 }
4034}
4035
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004036/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004037/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004038bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004039 switch (Tok.getKind()) {
4040 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004041
Chris Lattner020bab92009-01-04 23:41:41 +00004042 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00004043 if (TryAltiVecVectorToken())
4044 return true;
4045 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00004046 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004047 // Annotate typenames and C++ scope specifiers. If we get one, just
4048 // recurse to handle whatever we get.
4049 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004050 return true;
4051 if (Tok.is(tok::identifier))
4052 return false;
4053 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00004054
Chris Lattner020bab92009-01-04 23:41:41 +00004055 case tok::coloncolon: // ::foo::bar
4056 if (NextToken().is(tok::kw_new) || // ::new
4057 NextToken().is(tok::kw_delete)) // ::delete
4058 return false;
4059
Chris Lattner020bab92009-01-04 23:41:41 +00004060 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004061 return true;
4062 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004063
Chris Lattnere37e2332006-08-15 04:50:22 +00004064 // GNU attributes support.
4065 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004066 // GNU typeof support.
4067 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004068
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004069 // type-specifiers
4070 case tok::kw_short:
4071 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004072 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004073 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004074 case tok::kw_signed:
4075 case tok::kw_unsigned:
4076 case tok::kw__Complex:
4077 case tok::kw__Imaginary:
4078 case tok::kw_void:
4079 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004080 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004081 case tok::kw_char16_t:
4082 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004083 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004084 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004085 case tok::kw_float:
4086 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004087 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004088 case tok::kw__Bool:
4089 case tok::kw__Decimal32:
4090 case tok::kw__Decimal64:
4091 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004092 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004093
Chris Lattner861a2262008-04-13 18:59:07 +00004094 // struct-or-union-specifier (C99) or class-specifier (C++)
4095 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004096 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004097 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004098 case tok::kw_union:
4099 // enum-specifier
4100 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004101
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004102 // type-qualifier
4103 case tok::kw_const:
4104 case tok::kw_volatile:
4105 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004106
John McCallea0a39e2012-11-14 00:49:39 +00004107 // Debugger support.
4108 case tok::kw___unknown_anytype:
4109
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004110 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004111 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004112 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004113
Chris Lattner409bf7d2008-10-20 00:25:30 +00004114 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4115 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004116 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004117
Steve Naroff44ac7772008-12-25 14:16:32 +00004118 case tok::kw___cdecl:
4119 case tok::kw___stdcall:
4120 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004121 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004122 case tok::kw___w64:
4123 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004124 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004125 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004126 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004127
4128 case tok::kw___private:
4129 case tok::kw___local:
4130 case tok::kw___global:
4131 case tok::kw___constant:
4132 case tok::kw___read_only:
4133 case tok::kw___read_write:
4134 case tok::kw___write_only:
4135
Eli Friedman53339e02009-06-08 23:27:34 +00004136 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004137
Richard Smith8e1ac332013-03-28 01:55:44 +00004138 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004139 case tok::kw__Atomic:
4140 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004141 }
4142}
4143
Chris Lattneracd58a32006-08-06 17:24:14 +00004144/// isDeclarationSpecifier() - Return true if the current token is part of a
4145/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004146///
4147/// \param DisambiguatingWithExpression True to indicate that the purpose of
4148/// this check is to disambiguate between an expression and a declaration.
4149bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004150 switch (Tok.getKind()) {
4151 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004152
Chris Lattner020bab92009-01-04 23:41:41 +00004153 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004154 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004155 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004156 return false;
John Thompson22334602010-02-05 00:12:22 +00004157 if (TryAltiVecVectorToken())
4158 return true;
4159 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004160 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004161 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004162 // Annotate typenames and C++ scope specifiers. If we get one, just
4163 // recurse to handle whatever we get.
4164 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004165 return true;
4166 if (Tok.is(tok::identifier))
4167 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004168
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004169 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004170 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004171 // expression is permitted, then this is probably a class message send
4172 // missing the initial '['. In this case, we won't consider this to be
4173 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004174 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004175 isStartOfObjCClassMessageMissingOpenBracket())
4176 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004177
John McCall1f476a12010-02-26 08:45:28 +00004178 return isDeclarationSpecifier();
4179
Chris Lattner020bab92009-01-04 23:41:41 +00004180 case tok::coloncolon: // ::foo::bar
4181 if (NextToken().is(tok::kw_new) || // ::new
4182 NextToken().is(tok::kw_delete)) // ::delete
4183 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004184
Chris Lattner020bab92009-01-04 23:41:41 +00004185 // Annotate typenames and C++ scope specifiers. If we get one, just
4186 // recurse to handle whatever we get.
4187 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004188 return true;
4189 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004190
Chris Lattneracd58a32006-08-06 17:24:14 +00004191 // storage-class-specifier
4192 case tok::kw_typedef:
4193 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004194 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004195 case tok::kw_static:
4196 case tok::kw_auto:
4197 case tok::kw_register:
4198 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004199 case tok::kw_thread_local:
4200 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004201
Douglas Gregor26701a42011-09-09 02:06:17 +00004202 // Modules
4203 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004204
John McCallea0a39e2012-11-14 00:49:39 +00004205 // Debugger support
4206 case tok::kw___unknown_anytype:
4207
Chris Lattneracd58a32006-08-06 17:24:14 +00004208 // type-specifiers
4209 case tok::kw_short:
4210 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004211 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004212 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004213 case tok::kw_signed:
4214 case tok::kw_unsigned:
4215 case tok::kw__Complex:
4216 case tok::kw__Imaginary:
4217 case tok::kw_void:
4218 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004219 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004220 case tok::kw_char16_t:
4221 case tok::kw_char32_t:
4222
Chris Lattneracd58a32006-08-06 17:24:14 +00004223 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004224 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004225 case tok::kw_float:
4226 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004227 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004228 case tok::kw__Bool:
4229 case tok::kw__Decimal32:
4230 case tok::kw__Decimal64:
4231 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004232 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004233
Chris Lattner861a2262008-04-13 18:59:07 +00004234 // struct-or-union-specifier (C99) or class-specifier (C++)
4235 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004236 case tok::kw_struct:
4237 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004238 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004239 // enum-specifier
4240 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004241
Chris Lattneracd58a32006-08-06 17:24:14 +00004242 // type-qualifier
4243 case tok::kw_const:
4244 case tok::kw_volatile:
4245 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004246
Chris Lattneracd58a32006-08-06 17:24:14 +00004247 // function-specifier
4248 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004249 case tok::kw_virtual:
4250 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004251 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004252
Richard Smith1dba27c2013-01-29 09:02:09 +00004253 // alignment-specifier
4254 case tok::kw__Alignas:
4255
Richard Smithd16fe122012-10-25 00:00:53 +00004256 // friend keyword.
4257 case tok::kw_friend:
4258
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004259 // static_assert-declaration
4260 case tok::kw__Static_assert:
4261
Chris Lattner599e47e2007-08-09 17:01:07 +00004262 // GNU typeof support.
4263 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004264
Chris Lattner599e47e2007-08-09 17:01:07 +00004265 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004266 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004267
Richard Smithd16fe122012-10-25 00:00:53 +00004268 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004269 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004270 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004271
Richard Smith8e1ac332013-03-28 01:55:44 +00004272 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004273 case tok::kw__Atomic:
4274 return true;
4275
Chris Lattner8b2ec162008-07-26 03:38:44 +00004276 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4277 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004278 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004279
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004280 // typedef-name
4281 case tok::annot_typename:
4282 return !DisambiguatingWithExpression ||
4283 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004284
Steve Narofff192fab2009-01-06 19:34:12 +00004285 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004286 case tok::kw___cdecl:
4287 case tok::kw___stdcall:
4288 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004289 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004290 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004291 case tok::kw___sptr:
4292 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004293 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004294 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004295 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004296 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004297 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004298
4299 case tok::kw___private:
4300 case tok::kw___local:
4301 case tok::kw___global:
4302 case tok::kw___constant:
4303 case tok::kw___read_only:
4304 case tok::kw___read_write:
4305 case tok::kw___write_only:
4306
Eli Friedman53339e02009-06-08 23:27:34 +00004307 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004308 }
4309}
4310
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004311bool Parser::isConstructorDeclarator() {
4312 TentativeParsingAction TPA(*this);
4313
4314 // Parse the C++ scope specifier.
4315 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004316 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004317 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004318 TPA.Revert();
4319 return false;
4320 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004321
4322 // Parse the constructor name.
4323 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4324 // We already know that we have a constructor name; just consume
4325 // the token.
4326 ConsumeToken();
4327 } else {
4328 TPA.Revert();
4329 return false;
4330 }
4331
Richard Smith43f340f2012-03-27 23:05:05 +00004332 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004333 if (Tok.isNot(tok::l_paren)) {
4334 TPA.Revert();
4335 return false;
4336 }
4337 ConsumeParen();
4338
Richard Smith43f340f2012-03-27 23:05:05 +00004339 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4340 // that we have a constructor.
4341 if (Tok.is(tok::r_paren) ||
4342 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004343 TPA.Revert();
4344 return true;
4345 }
4346
Richard Smithf2163662013-09-06 00:12:20 +00004347 // A C++11 attribute here signals that we have a constructor, and is an
4348 // attribute on the first constructor parameter.
4349 if (getLangOpts().CPlusPlus11 &&
4350 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4351 /*OuterMightBeMessageSend*/ true)) {
4352 TPA.Revert();
4353 return true;
4354 }
4355
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004356 // If we need to, enter the specified scope.
4357 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004358 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004359 DeclScopeObj.EnterDeclaratorScope();
4360
Francois Pichet79f3a872011-01-31 04:54:32 +00004361 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004362 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004363 MaybeParseMicrosoftAttributes(Attrs);
4364
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004365 // Check whether the next token(s) are part of a declaration
4366 // specifier, in which case we have the start of a parameter and,
4367 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004368 bool IsConstructor = false;
4369 if (isDeclarationSpecifier())
4370 IsConstructor = true;
4371 else if (Tok.is(tok::identifier) ||
4372 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4373 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4374 // This might be a parenthesized member name, but is more likely to
4375 // be a constructor declaration with an invalid argument type. Keep
4376 // looking.
4377 if (Tok.is(tok::annot_cxxscope))
4378 ConsumeToken();
4379 ConsumeToken();
4380
4381 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004382 // which must have one of the following syntactic forms (see the
4383 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004384 switch (Tok.getKind()) {
4385 case tok::l_paren:
4386 // C(X ( int));
4387 case tok::l_square:
4388 // C(X [ 5]);
4389 // C(X [ [attribute]]);
4390 case tok::coloncolon:
4391 // C(X :: Y);
4392 // C(X :: *p);
4393 case tok::r_paren:
4394 // C(X )
4395 // Assume this isn't a constructor, rather than assuming it's a
4396 // constructor with an unnamed parameter of an ill-formed type.
4397 break;
4398
4399 default:
4400 IsConstructor = true;
4401 break;
4402 }
4403 }
4404
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004405 TPA.Revert();
4406 return IsConstructor;
4407}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004408
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004409/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004410/// type-qualifier-list: [C99 6.7.5]
4411/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004412/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004413/// [ only if VendorAttributesAllowed=true ]
4414/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004415/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004416/// [ only if VendorAttributesAllowed=true ]
4417/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004418/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004419/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004420///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004421void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4422 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004423 bool CXX11AttributesAllowed,
Alp Toker62c5b572013-11-26 01:30:10 +00004424 bool AtomicAllowed,
4425 bool IdentifierRequired) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004426 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004427 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004428 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004429 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004430 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004431 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004432
4433 SourceLocation EndLoc;
4434
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004435 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004436 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004437 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004438 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004439 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004440
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004441 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004442 case tok::code_completion:
4443 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004444 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004445
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004446 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004447 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004448 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004449 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004450 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004451 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004452 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004453 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004454 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004455 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004456 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004457 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004458 case tok::kw__Atomic:
4459 if (!AtomicAllowed)
4460 goto DoneWithTypeQuals;
4461 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4462 getLangOpts());
4463 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004464
4465 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004466 case tok::kw___private:
4467 case tok::kw___global:
4468 case tok::kw___local:
4469 case tok::kw___constant:
4470 case tok::kw___read_only:
4471 case tok::kw___write_only:
4472 case tok::kw___read_write:
4473 ParseOpenCLQualifiers(DS);
4474 break;
4475
Aaron Ballman317a77f2013-05-22 23:25:32 +00004476 case tok::kw___uptr:
Alp Toker62c5b572013-11-26 01:30:10 +00004477 // GNU libc headers in C mode use '__uptr' as an identifer which conflicts
4478 // with the MS modifier keyword.
4479 if (VendorAttributesAllowed && !getLangOpts().CPlusPlus &&
Alp Toker47642d22013-12-03 06:13:01 +00004480 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
4481 if (TryKeywordIdentFallback(false))
4482 continue;
Alp Toker62c5b572013-11-26 01:30:10 +00004483 }
4484 case tok::kw___sptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004485 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004486 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004487 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004488 case tok::kw___cdecl:
4489 case tok::kw___stdcall:
4490 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004491 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004492 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004493 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004494 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004495 continue;
4496 }
4497 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004498 case tok::kw___pascal:
4499 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004500 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004501 continue;
4502 }
4503 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004504 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004505 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004506 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004507 continue; // do *not* consume the next token!
4508 }
4509 // otherwise, FALL THROUGH!
4510 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004511 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004512 // If this is not a type-qualifier token, we're done reading type
4513 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004514 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004515 if (EndLoc.isValid())
4516 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004517 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004518 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004519
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004520 // If the specifier combination wasn't legal, issue a diagnostic.
4521 if (isInvalid) {
4522 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004523 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004524 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004525 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004526 }
4527}
4528
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004529
4530/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4531///
4532void Parser::ParseDeclarator(Declarator &D) {
4533 /// This implements the 'declarator' production in the C grammar, then checks
4534 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004535 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004536}
4537
Richard Smith0efa75c2012-03-29 01:16:42 +00004538static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4539 if (Kind == tok::star || Kind == tok::caret)
4540 return true;
4541
4542 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4543 if (!Lang.CPlusPlus)
4544 return false;
4545
4546 return Kind == tok::amp || Kind == tok::ampamp;
4547}
4548
Sebastian Redlbd150f42008-11-21 19:14:01 +00004549/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4550/// is parsed by the function passed to it. Pass null, and the direct-declarator
4551/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004552/// ptr-operator production.
4553///
Richard Smith09f76ee2011-10-19 21:33:05 +00004554/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004555/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4556/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004557///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004558/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4559/// [C] pointer[opt] direct-declarator
4560/// [C++] direct-declarator
4561/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004562///
4563/// pointer: [C99 6.7.5]
4564/// '*' type-qualifier-list[opt]
4565/// '*' type-qualifier-list[opt] pointer
4566///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004567/// ptr-operator:
4568/// '*' cv-qualifier-seq[opt]
4569/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004570/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004571/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004572/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004573/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004574void Parser::ParseDeclaratorInternal(Declarator &D,
4575 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004576 if (Diags.hasAllExtensionsSilenced())
4577 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004578
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004579 // C++ member pointers start with a '::' or a nested-name.
4580 // Member pointers get special handling, since there's no place for the
4581 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004582 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004583 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4584 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004585 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4586 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004587 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004588 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004589
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004590 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004591 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004592 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004593 if (D.mayHaveIdentifier())
4594 D.getCXXScopeSpec() = SS;
4595 else
4596 AnnotateScopeToken(SS, true);
4597
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004598 if (DirectDeclParser)
4599 (this->*DirectDeclParser)(D);
4600 return;
4601 }
4602
4603 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004604 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004605 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004606 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004607 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004608
4609 // Recurse to parse whatever is left.
4610 ParseDeclaratorInternal(D, DirectDeclParser);
4611
4612 // Sema will have to catch (syntactically invalid) pointers into global
4613 // scope. It has to catch pointers into namespace scope anyway.
4614 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004615 Loc),
4616 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004617 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004618 return;
4619 }
4620 }
4621
4622 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004623 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004624 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004625 if (DirectDeclParser)
4626 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004627 return;
4628 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004629
Sebastian Redled0f3b02009-03-15 22:02:01 +00004630 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4631 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004632 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004633 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004634
Chris Lattner9eac9312009-03-27 04:18:06 +00004635 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004636 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004637 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004638
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004639 // FIXME: GNU attributes are not allowed here in a new-type-id.
Alp Toker62c5b572013-11-26 01:30:10 +00004640 ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004641 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004642
Bill Wendling3708c182007-05-27 10:15:43 +00004643 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004644 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004645 if (Kind == tok::star)
4646 // Remember that we parsed a pointer type, and remember the type-quals.
4647 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004648 DS.getConstSpecLoc(),
4649 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004650 DS.getRestrictSpecLoc()),
4651 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004652 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004653 else
4654 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004655 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004656 Loc),
4657 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004658 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004659 } else {
4660 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004661 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004662
Sebastian Redl3b27be62009-03-23 00:00:23 +00004663 // Complain about rvalue references in C++03, but then go on and build
4664 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004665 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004666 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004667 diag::warn_cxx98_compat_rvalue_reference :
4668 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004669
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004670 // GNU-style and C++11 attributes are allowed here, as is restrict.
4671 ParseTypeQualifierListOpt(DS);
4672 D.ExtendWithDeclSpec(DS);
4673
Bill Wendling93efb222007-06-02 23:28:54 +00004674 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4675 // cv-qualifiers are introduced through the use of a typedef or of a
4676 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004677 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4678 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4679 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004680 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004681 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4682 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004683 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004684 // 'restrict' is permitted as an extension.
4685 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4686 Diag(DS.getAtomicSpecLoc(),
4687 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004688 }
Bill Wendling3708c182007-05-27 10:15:43 +00004689
4690 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004691 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004692
Douglas Gregor66583c52008-11-03 15:51:28 +00004693 if (D.getNumTypeObjects() > 0) {
4694 // C++ [dcl.ref]p4: There shall be no references to references.
4695 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4696 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004697 if (const IdentifierInfo *II = D.getIdentifier())
4698 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4699 << II;
4700 else
4701 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4702 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004703
Sebastian Redlbd150f42008-11-21 19:14:01 +00004704 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004705 // can go ahead and build the (technically ill-formed)
4706 // declarator: reference collapsing will take care of it.
4707 }
4708 }
4709
Richard Smith8e1ac332013-03-28 01:55:44 +00004710 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004711 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004712 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004713 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004714 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004715 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004716}
4717
Richard Smith0efa75c2012-03-29 01:16:42 +00004718static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4719 SourceLocation EllipsisLoc) {
4720 if (EllipsisLoc.isValid()) {
4721 FixItHint Insertion;
4722 if (!D.getEllipsisLoc().isValid()) {
4723 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4724 D.setEllipsisLoc(EllipsisLoc);
4725 }
4726 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4727 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4728 }
4729}
4730
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004731/// ParseDirectDeclarator
4732/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004733/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004734/// '(' declarator ')'
4735/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004736/// [C90] direct-declarator '[' constant-expression[opt] ']'
4737/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4738/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4739/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4740/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004741/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4742/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004743/// direct-declarator '(' parameter-type-list ')'
4744/// direct-declarator '(' identifier-list[opt] ')'
4745/// [GNU] direct-declarator '(' parameter-forward-declarations
4746/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004747/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4748/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004749/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4750/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4751/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004752/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004753/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004754///
4755/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004756/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004757/// '::'[opt] nested-name-specifier[opt] type-name
4758///
4759/// id-expression: [C++ 5.1]
4760/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004761/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004762///
4763/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004764/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004765/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004766/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004767/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004768/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004769///
Richard Smith1453e312012-03-27 01:42:32 +00004770/// Note, any additional constructs added here may need corresponding changes
4771/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004772void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004773 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004774
David Blaikiebbafb8a2012-03-11 07:00:24 +00004775 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004776 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004777 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004778 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4779 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004780 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004781 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004782 }
4783
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004784 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004785 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004786 // Change the declaration context for name lookup, until this function
4787 // is exited (and the declarator has been parsed).
4788 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004789 }
4790
Douglas Gregor27b4c162010-12-23 22:44:42 +00004791 // C++0x [dcl.fct]p14:
4792 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004793 // of a parameter-declaration-clause without a preceding comma. In
4794 // this case, the ellipsis is parsed as part of the
4795 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004796 // parameter pack that has not been expanded; otherwise, it is parsed
4797 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004798 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004799 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004800 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004801 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004802 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004803 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004804 !Actions.containsUnexpandedParameterPacks(D))) {
4805 SourceLocation EllipsisLoc = ConsumeToken();
4806 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4807 // The ellipsis was put in the wrong place. Recover, and explain to
4808 // the user what they should have done.
4809 ParseDeclarator(D);
4810 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4811 return;
4812 } else
4813 D.setEllipsisLoc(EllipsisLoc);
4814
4815 // The ellipsis can't be followed by a parenthesized declarator. We
4816 // check for that in ParseParenDeclarator, after we have disambiguated
4817 // the l_paren token.
4818 }
4819
Douglas Gregor7861a802009-11-03 01:35:08 +00004820 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4821 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4822 // We found something that indicates the start of an unqualified-id.
4823 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004824 bool AllowConstructorName;
4825 if (D.getDeclSpec().hasTypeSpecifier())
4826 AllowConstructorName = false;
4827 else if (D.getCXXScopeSpec().isSet())
4828 AllowConstructorName =
4829 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004830 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004831 else
4832 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4833
Abramo Bagnara7945c982012-01-27 09:46:47 +00004834 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004835 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4836 /*EnteringContext=*/true,
4837 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004838 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004839 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004840 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004841 D.getName()) ||
4842 // Once we're past the identifier, if the scope was bad, mark the
4843 // whole declarator bad.
4844 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004845 D.SetIdentifier(0, Tok.getLocation());
4846 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004847 } else {
4848 // Parsed the unqualified-id; update range information and move along.
4849 if (D.getSourceRange().getBegin().isInvalid())
4850 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4851 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004852 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004853 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004854 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004855 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004856 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004857 "There's a C++-specific check for tok::identifier above");
4858 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4859 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4860 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004861 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004862 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004863 // A virt-specifier isn't treated as an identifier if it appears after a
4864 // trailing-return-type.
4865 if (D.getContext() != Declarator::TrailingReturnContext ||
4866 !isCXX11VirtSpecifier(Tok)) {
4867 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4868 << FixItHint::CreateRemoval(Tok.getLocation());
4869 D.SetIdentifier(0, Tok.getLocation());
4870 ConsumeToken();
4871 goto PastIdentifier;
4872 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004873 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004874
Douglas Gregor7861a802009-11-03 01:35:08 +00004875 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004876 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004877 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004878 // Example: 'char (*X)' or 'int (*XX)(void)'
4879 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004880
4881 // If the declarator was parenthesized, we entered the declarator
4882 // scope when parsing the parenthesized declarator, then exited
4883 // the scope already. Re-enter the scope, if we need to.
4884 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004885 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004886 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004887 if (!D.isInvalidType() &&
4888 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004889 // Change the declaration context for name lookup, until this function
4890 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004891 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004892 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004893 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004894 // This could be something simple like "int" (in which case the declarator
4895 // portion is empty), if an abstract-declarator is allowed.
4896 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004897
4898 // The grammar for abstract-pack-declarator does not allow grouping parens.
4899 // FIXME: Revisit this once core issue 1488 is resolved.
4900 if (D.hasEllipsis() && D.hasGroupingParens())
4901 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4902 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004903 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004904 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004905 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004906 if (D.getContext() == Declarator::MemberContext)
4907 Diag(Tok, diag::err_expected_member_name_or_semi)
4908 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004909 else if (getLangOpts().CPlusPlus) {
4910 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4911 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004912 else {
4913 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4914 if (Tok.isAtStartOfLine() && Loc.isValid())
4915 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4916 << getLangOpts().CPlusPlus;
4917 else
4918 Diag(Tok, diag::err_expected_unqualified_id)
4919 << getLangOpts().CPlusPlus;
4920 }
Richard Trieu9c672672013-01-26 02:31:38 +00004921 } else
Alp Tokerec543272013-12-24 09:48:30 +00004922 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_paren;
Chris Lattnereec40f92006-08-06 21:55:29 +00004923 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004924 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004925 }
Mike Stump11289f42009-09-09 15:08:12 +00004926
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004927 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004928 assert(D.isPastIdentifier() &&
4929 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004930
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004931 // Don't parse attributes unless we have parsed an unparenthesized name.
4932 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004933 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004934
Chris Lattneracd58a32006-08-06 17:24:14 +00004935 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004936 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004937 // Enter function-declaration scope, limiting any declarators to the
4938 // function prototype scope, including parameter declarators.
4939 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004940 Scope::FunctionPrototypeScope|Scope::DeclScope|
4941 (D.isFunctionDeclaratorAFunctionDeclaration()
4942 ? Scope::FunctionDeclarationScope : 0));
4943
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004944 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4945 // In such a case, check if we actually have a function declarator; if it
4946 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004947 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004948 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4949 // The name of the declarator, if any, is tentatively declared within
4950 // a possible direct initializer.
4951 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4952 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4953 TentativelyDeclaredIdentifiers.pop_back();
4954 if (!IsFunctionDecl)
4955 break;
4956 }
John McCall084e83d2011-03-24 11:26:52 +00004957 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004958 BalancedDelimiterTracker T(*this, tok::l_paren);
4959 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004960 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004961 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004962 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004963 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004964 } else {
4965 break;
4966 }
4967 }
Chad Rosierc1183952012-06-26 22:30:43 +00004968}
Chris Lattneracd58a32006-08-06 17:24:14 +00004969
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004970/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4971/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004972/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004973/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4974///
4975/// direct-declarator:
4976/// '(' declarator ')'
4977/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004978/// direct-declarator '(' parameter-type-list ')'
4979/// direct-declarator '(' identifier-list[opt] ')'
4980/// [GNU] direct-declarator '(' parameter-forward-declarations
4981/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004982///
4983void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004984 BalancedDelimiterTracker T(*this, tok::l_paren);
4985 T.consumeOpen();
4986
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004987 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004988
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004989 // Eat any attributes before we look at whether this is a grouping or function
4990 // declarator paren. If this is a grouping paren, the attribute applies to
4991 // the type being built up, for example:
4992 // int (__attribute__(()) *x)(long y)
4993 // If this ends up not being a grouping paren, the attribute applies to the
4994 // first argument, for example:
4995 // int (__attribute__(()) int x)
4996 // In either case, we need to eat any attributes to be able to determine what
4997 // sort of paren this is.
4998 //
John McCall084e83d2011-03-24 11:26:52 +00004999 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005000 bool RequiresArg = false;
5001 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00005002 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005003
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005004 // We require that the argument list (if this is a non-grouping paren) be
5005 // present even if the attribute list was empty.
5006 RequiresArg = true;
5007 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00005008
Steve Naroff44ac7772008-12-25 14:16:32 +00005009 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00005010 ParseMicrosoftTypeAttributes(attrs);
5011
Dawn Perchik335e16b2010-09-03 01:29:35 +00005012 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00005013 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00005014 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005015
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005016 // If we haven't past the identifier yet (or where the identifier would be
5017 // stored, if this is an abstract declarator), then this is probably just
5018 // grouping parens. However, if this could be an abstract-declarator, then
5019 // this could also be the start of function arguments (consider 'void()').
5020 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00005021
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005022 if (!D.mayOmitIdentifier()) {
5023 // If this can't be an abstract-declarator, this *must* be a grouping
5024 // paren, because we haven't seen the identifier yet.
5025 isGrouping = true;
5026 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00005027 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
5028 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00005029 isDeclarationSpecifier() || // 'int(int)' is a function.
5030 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005031 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
5032 // considered to be a type, not a K&R identifier-list.
5033 isGrouping = false;
5034 } else {
5035 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
5036 isGrouping = true;
5037 }
Mike Stump11289f42009-09-09 15:08:12 +00005038
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005039 // If this is a grouping paren, handle:
5040 // direct-declarator: '(' declarator ')'
5041 // direct-declarator: '(' attributes declarator ')'
5042 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005043 SourceLocation EllipsisLoc = D.getEllipsisLoc();
5044 D.setEllipsisLoc(SourceLocation());
5045
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005046 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005047 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00005048 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005049 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005050 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00005051 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005052 T.getCloseLocation()),
5053 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005054
5055 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00005056
5057 // An ellipsis cannot be placed outside parentheses.
5058 if (EllipsisLoc.isValid())
5059 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
5060
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005061 return;
5062 }
Mike Stump11289f42009-09-09 15:08:12 +00005063
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005064 // Okay, if this wasn't a grouping paren, it must be the start of a function
5065 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005066 // identifier (and remember where it would have been), then call into
5067 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005068 D.SetIdentifier(0, Tok.getLocation());
5069
David Blaikie15a430a2011-12-04 05:04:18 +00005070 // Enter function-declaration scope, limiting any declarators to the
5071 // function prototype scope, including parameter declarators.
5072 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005073 Scope::FunctionPrototypeScope | Scope::DeclScope |
5074 (D.isFunctionDeclaratorAFunctionDeclaration()
5075 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00005076 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00005077 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005078}
5079
5080/// ParseFunctionDeclarator - We are after the identifier and have parsed the
5081/// declarator D up to a paren, which indicates that we are parsing function
5082/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00005083///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005084/// If FirstArgAttrs is non-null, then the caller parsed those arguments
5085/// immediately after the open paren - they should be considered to be the
5086/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005087///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005088/// If RequiresArg is true, then the first argument of the function is required
5089/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005090///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005091/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5092/// (C++11) ref-qualifier[opt], exception-specification[opt],
5093/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5094///
5095/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00005096/// dynamic-exception-specification
5097/// noexcept-specification
5098///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005099void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005100 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005101 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00005102 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005103 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00005104 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00005105 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00005106 // lparen is already consumed!
5107 assert(D.isPastIdentifier() && "Should not call before identifier!");
5108
5109 // This should be true when the function has typed arguments.
5110 // Otherwise, it is treated as a K&R-style function.
5111 bool HasProto = false;
5112 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005113 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005114 // Remember where we see an ellipsis, if any.
5115 SourceLocation EllipsisLoc;
5116
5117 DeclSpec DS(AttrFactory);
5118 bool RefQualifierIsLValueRef = true;
5119 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00005120 SourceLocation ConstQualifierLoc;
5121 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005122 ExceptionSpecificationType ESpecType = EST_None;
5123 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005124 SmallVector<ParsedType, 2> DynamicExceptions;
5125 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005126 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005127 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00005128 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005129
James Molloy6f8780b2012-02-29 10:24:19 +00005130 Actions.ActOnStartFunctionDeclarator();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005131 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5132 EndLoc is the end location for the function declarator.
5133 They differ for trailing return types. */
5134 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005135 SourceLocation LParenLoc, RParenLoc;
5136 LParenLoc = Tracker.getOpenLocation();
5137 StartLoc = LParenLoc;
5138
Douglas Gregor9e66af42011-07-05 16:44:18 +00005139 if (isFunctionDeclaratorIdentifierList()) {
5140 if (RequiresArg)
5141 Diag(Tok, diag::err_argument_required_after_attribute);
5142
5143 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5144
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005145 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005146 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005147 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005148 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005149 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005150 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005151 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5152 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005153 else if (RequiresArg)
5154 Diag(Tok, diag::err_argument_required_after_attribute);
5155
David Blaikiebbafb8a2012-03-11 07:00:24 +00005156 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005157
5158 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005159 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005160 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005161 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005162 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005163
David Blaikiebbafb8a2012-03-11 07:00:24 +00005164 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005165 // FIXME: Accept these components in any order, and produce fixits to
5166 // correct the order if the user gets it wrong. Ideally we should deal
5167 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005168
5169 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005170 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5171 /*CXX11AttributesAllowed*/ false,
5172 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005173 if (!DS.getSourceRange().getEnd().isInvalid()) {
5174 EndLoc = DS.getSourceRange().getEnd();
5175 ConstQualifierLoc = DS.getConstSpecLoc();
5176 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5177 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005178
5179 // Parse ref-qualifier[opt].
5180 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005181 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005182 diag::warn_cxx98_compat_ref_qualifier :
5183 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005184
Douglas Gregor9e66af42011-07-05 16:44:18 +00005185 RefQualifierIsLValueRef = Tok.is(tok::amp);
5186 RefQualifierLoc = ConsumeToken();
5187 EndLoc = RefQualifierLoc;
5188 }
5189
Douglas Gregor3024f072012-04-16 07:05:22 +00005190 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005191 // If a declaration declares a member function or member function
5192 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005193 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005194 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005195 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005196 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005197 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005198 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005199 (D.getContext() == Declarator::MemberContext
5200 ? !D.getDeclSpec().isFriendSpecified()
5201 : D.getContext() == Declarator::FileContext &&
5202 D.getCXXScopeSpec().isValid() &&
5203 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005204 Sema::CXXThisScopeRAII ThisScope(Actions,
5205 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005206 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005207 (D.getDeclSpec().isConstexprSpecified() &&
5208 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005209 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005210 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005211
Douglas Gregor9e66af42011-07-05 16:44:18 +00005212 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005213 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005214 DynamicExceptions,
5215 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005216 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005217 if (ESpecType != EST_None)
5218 EndLoc = ESpecRange.getEnd();
5219
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005220 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5221 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005222 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005223
Douglas Gregor9e66af42011-07-05 16:44:18 +00005224 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005225 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005226 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005227 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005228 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5229 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005230 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005231 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005232 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005233 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005234 }
5235 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005236 }
5237
5238 // Remember that we parsed a function type, and remember the attributes.
5239 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005240 IsAmbiguous,
5241 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005242 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005243 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005244 DS.getTypeQualifiers(),
5245 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005246 RefQualifierLoc, ConstQualifierLoc,
5247 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005248 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005249 ESpecType, ESpecRange.getBegin(),
5250 DynamicExceptions.data(),
5251 DynamicExceptionRanges.data(),
5252 DynamicExceptions.size(),
5253 NoexceptExpr.isUsable() ?
5254 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005255 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005256 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005257 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005258
5259 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005260}
5261
5262/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5263/// identifier list form for a K&R-style function: void foo(a,b,c)
5264///
5265/// Note that identifier-lists are only allowed for normal declarators, not for
5266/// abstract-declarators.
5267bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005268 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005269 && Tok.is(tok::identifier)
5270 && !TryAltiVecVectorToken()
5271 // K&R identifier lists can't have typedefs as identifiers, per C99
5272 // 6.7.5.3p11.
5273 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5274 // Identifier lists follow a really simple grammar: the identifiers can
5275 // be followed *only* by a ", identifier" or ")". However, K&R
5276 // identifier lists are really rare in the brave new modern world, and
5277 // it is very common for someone to typo a type in a non-K&R style
5278 // list. If we are presented with something like: "void foo(intptr x,
5279 // float y)", we don't want to start parsing the function declarator as
5280 // though it is a K&R style declarator just because intptr is an
5281 // invalid type.
5282 //
5283 // To handle this, we check to see if the token after the first
5284 // identifier is a "," or ")". Only then do we parse it as an
5285 // identifier list.
5286 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5287}
5288
5289/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5290/// we found a K&R-style identifier list instead of a typed parameter list.
5291///
5292/// After returning, ParamInfo will hold the parsed parameters.
5293///
5294/// identifier-list: [C99 6.7.5]
5295/// identifier
5296/// identifier-list ',' identifier
5297///
5298void Parser::ParseFunctionDeclaratorIdentifierList(
5299 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005300 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005301 // If there was no identifier specified for the declarator, either we are in
5302 // an abstract-declarator, or we are in a parameter declarator which was found
5303 // to be abstract. In abstract-declarators, identifier lists are not valid:
5304 // diagnose this.
5305 if (!D.getIdentifier())
5306 Diag(Tok, diag::ext_ident_list_in_param);
5307
5308 // Maintain an efficient lookup of params we have seen so far.
5309 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5310
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005311 do {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005312 // If this isn't an identifier, report the error and skip until ')'.
5313 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00005314 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00005315 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005316 // Forget we parsed anything.
5317 ParamInfo.clear();
5318 return;
5319 }
5320
5321 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5322
5323 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5324 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5325 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5326
5327 // Verify that the argument identifier has not already been mentioned.
5328 if (!ParamsSoFar.insert(ParmII)) {
5329 Diag(Tok, diag::err_param_redefinition) << ParmII;
5330 } else {
5331 // Remember this identifier in ParamInfo.
5332 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5333 Tok.getLocation(),
5334 0));
5335 }
5336
5337 // Eat the identifier.
5338 ConsumeToken();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005339 // The list continues if we see a comma.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005340 } while (TryConsumeToken(tok::comma));
Douglas Gregor9e66af42011-07-05 16:44:18 +00005341}
5342
5343/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5344/// after the opening parenthesis. This function will not parse a K&R-style
5345/// identifier list.
5346///
Richard Smith2620cd92012-04-11 04:01:28 +00005347/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5348/// caller parsed those arguments immediately after the open paren - they should
5349/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005350///
5351/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5352/// be the location of the ellipsis, if any was parsed.
5353///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005354/// parameter-type-list: [C99 6.7.5]
5355/// parameter-list
5356/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005357/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005358///
5359/// parameter-list: [C99 6.7.5]
5360/// parameter-declaration
5361/// parameter-list ',' parameter-declaration
5362///
5363/// parameter-declaration: [C99 6.7.5]
5364/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005365/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005366/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005367/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005368/// declaration-specifiers abstract-declarator[opt]
5369/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005370/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005371/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005372/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005373///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005374void Parser::ParseParameterDeclarationClause(
5375 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005376 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005377 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005378 SourceLocation &EllipsisLoc) {
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005379 do {
5380 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5381 // before deciding this was a parameter-declaration-clause.
5382 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Chris Lattner371ed4e2008-04-06 06:57:35 +00005383 break;
Mike Stump11289f42009-09-09 15:08:12 +00005384
Chris Lattner371ed4e2008-04-06 06:57:35 +00005385 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005386 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005387 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005388
Richard Smith2620cd92012-04-11 04:01:28 +00005389 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005390 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005391
John McCall53fa7142010-12-24 02:08:15 +00005392 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005393 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005394
5395 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005396
5397 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005398 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005399 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005400 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5401 // too much hassle.
5402 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005403
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005404 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005405
Faisal Vali2b391ab2013-09-26 19:54:12 +00005406
5407 // Parse the declarator. This is "PrototypeContext" or
5408 // "LambdaExprParameterContext", because we must accept either
5409 // 'declarator' or 'abstract-declarator' here.
5410 Declarator ParmDeclarator(DS,
5411 D.getContext() == Declarator::LambdaExprContext ?
5412 Declarator::LambdaExprParameterContext :
5413 Declarator::PrototypeContext);
5414 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005415
5416 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005417 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005418
Chris Lattner371ed4e2008-04-06 06:57:35 +00005419 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005420 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005421
Douglas Gregor4d87df52008-12-16 21:30:33 +00005422 // DefArgToks is used when the parsing of default arguments needs
5423 // to be delayed.
5424 CachedTokens *DefArgToks = 0;
5425
Chris Lattner371ed4e2008-04-06 06:57:35 +00005426 // If no parameter was specified, verify that *something* was specified,
5427 // otherwise we have a missing type and identifier.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005428 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5429 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005430 // Completely missing, emit error.
5431 Diag(DSStart, diag::err_missing_param);
5432 } else {
5433 // Otherwise, we have something. Add it and let semantic analysis try
5434 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005435
Chris Lattner371ed4e2008-04-06 06:57:35 +00005436 // Inform the actions module about the parameter declarator, so it gets
5437 // added to the current scope.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005438 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5439 ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005440 // Parse the default argument, if any. We parse the default
5441 // arguments in all dialects; the semantic analysis in
5442 // ActOnParamDefaultArgument will reject the default argument in
5443 // C.
5444 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005445 SourceLocation EqualLoc = Tok.getLocation();
5446
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005447 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005448 if (D.getContext() == Declarator::MemberContext) {
5449 // If we're inside a class definition, cache the tokens
5450 // corresponding to the default argument. We'll actually parse
5451 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005452 // FIXME: Can we use a smart pointer for Toks?
5453 DefArgToks = new CachedTokens;
5454
Richard Smith1fff95c2013-09-12 23:28:08 +00005455 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005456 delete DefArgToks;
5457 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005458 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005459 } else {
5460 // Mark the end of the default argument so that we know when to
5461 // stop when we parse it later on.
5462 Token DefArgEnd;
5463 DefArgEnd.startToken();
5464 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5465 DefArgEnd.setLocation(Tok.getLocation());
5466 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005467 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005468 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005469 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005470 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005471 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005472 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005473
Chad Rosierc1183952012-06-26 22:30:43 +00005474 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005475 // used.
5476 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005477 Sema::PotentiallyEvaluatedIfUsed,
5478 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005479
Sebastian Redldb63af22012-03-14 15:54:00 +00005480 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005481 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005482 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005483 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005484 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005485 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005486 if (DefArgResult.isInvalid()) {
5487 Actions.ActOnParamDefaultArgumentError(Param);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005488 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00005489 } else {
5490 // Inform the actions module about the default argument
5491 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005492 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005493 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005494 }
5495 }
Mike Stump11289f42009-09-09 15:08:12 +00005496
5497 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005498 ParmDeclarator.getIdentifierLoc(),
5499 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005500 }
5501
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005502 if (TryConsumeToken(tok::ellipsis, EllipsisLoc) &&
5503 !getLangOpts().CPlusPlus) {
5504 // We have ellipsis without a preceding ',', which is ill-formed
5505 // in C. Complain and provide the fix.
5506 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
5507 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005508 break;
5509 }
Mike Stump11289f42009-09-09 15:08:12 +00005510
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005511 // If the next token is a comma, consume it and keep reading arguments.
5512 } while (TryConsumeToken(tok::comma));
Chris Lattner6c940e62008-04-06 06:34:08 +00005513}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005514
Chris Lattnere8074e62006-08-06 18:30:15 +00005515/// [C90] direct-declarator '[' constant-expression[opt] ']'
5516/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5517/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5518/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5519/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005520/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5521/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005522void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005523 if (CheckProhibitedCXX11Attribute())
5524 return;
5525
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005526 BalancedDelimiterTracker T(*this, tok::l_square);
5527 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005528
Chris Lattner84a11622008-12-18 07:27:21 +00005529 // C array syntax has many features, but by-far the most common is [] and [4].
5530 // This code does a fast path to handle some of the most obvious cases.
5531 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005532 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005533 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005534 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005535
Chris Lattner84a11622008-12-18 07:27:21 +00005536 // Remember that we parsed the empty array type.
John McCall084e83d2011-03-24 11:26:52 +00005537 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005538 T.getOpenLocation(),
5539 T.getCloseLocation()),
5540 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005541 return;
5542 } else if (Tok.getKind() == tok::numeric_constant &&
5543 GetLookAheadToken(1).is(tok::r_square)) {
5544 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005545 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005546 ConsumeToken();
5547
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005548 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005549 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005550 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005551
Chris Lattner84a11622008-12-18 07:27:21 +00005552 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005553 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005554 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005555 T.getOpenLocation(),
5556 T.getCloseLocation()),
5557 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005558 return;
5559 }
Mike Stump11289f42009-09-09 15:08:12 +00005560
Chris Lattnere8074e62006-08-06 18:30:15 +00005561 // If valid, this location is the position where we read the 'static' keyword.
5562 SourceLocation StaticLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005563 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005564
Chris Lattnere8074e62006-08-06 18:30:15 +00005565 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005566 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005567 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005568 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005569
Chris Lattnere8074e62006-08-06 18:30:15 +00005570 // If we haven't already read 'static', check to see if there is one after the
5571 // type-qualifier-list.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005572 if (!StaticLoc.isValid())
5573 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005574
Chris Lattnere8074e62006-08-06 18:30:15 +00005575 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005576 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005577 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005578
Chris Lattner521ff2b2008-04-06 05:26:30 +00005579 // Handle the case where we have '[*]' as the array size. However, a leading
5580 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005581 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005582 // infrequent, use of lookahead is not costly here.
5583 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005584 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005585
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005586 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005587 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005588 StaticLoc = SourceLocation(); // Drop the static.
5589 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005590 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005591 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005592 // Note, in C89, this production uses the constant-expr production instead
5593 // of assignment-expr. The only difference is that assignment-expr allows
5594 // things like '=' and '*='. Sema rejects these in C89 mode because they
5595 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005596
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005597 // Parse the constant-expression or assignment-expression now (depending
5598 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005599 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005600 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005601 } else {
5602 EnterExpressionEvaluationContext Unevaluated(Actions,
5603 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005604 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005605 }
Chris Lattner62591722006-08-12 18:40:58 +00005606 }
Mike Stump11289f42009-09-09 15:08:12 +00005607
Chris Lattner62591722006-08-12 18:40:58 +00005608 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005609 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005610 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005611 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00005612 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00005613 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005614 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005615
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005616 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005617
John McCall084e83d2011-03-24 11:26:52 +00005618 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005619 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005620
Chris Lattner84a11622008-12-18 07:27:21 +00005621 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005622 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005623 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005624 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005625 T.getOpenLocation(),
5626 T.getCloseLocation()),
5627 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005628}
5629
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005630/// [GNU] typeof-specifier:
5631/// typeof ( expressions )
5632/// typeof ( type-name )
5633/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005634///
5635void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005636 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005637 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005638 SourceLocation StartLoc = ConsumeToken();
5639
John McCalle8595032010-01-13 20:03:27 +00005640 const bool hasParens = Tok.is(tok::l_paren);
5641
Eli Friedman15681d62012-09-26 04:34:21 +00005642 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5643 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005644
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005645 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005646 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005647 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005648 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5649 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005650 if (hasParens)
5651 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005652
5653 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005654 // FIXME: Not accurate, the range gets one token more than it should.
5655 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005656 else
5657 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005658
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005659 if (isCastExpr) {
5660 if (!CastTy) {
5661 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005662 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005663 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005664
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005665 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005666 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005667 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5668 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005669 DiagID, CastTy))
5670 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005671 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005672 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005673
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005674 // If we get here, the operand to the typeof was an expresion.
5675 if (Operand.isInvalid()) {
5676 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005677 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005678 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005679
Eli Friedmane0afc982012-01-21 01:01:51 +00005680 // We might need to transform the operand if it is potentially evaluated.
5681 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5682 if (Operand.isInvalid()) {
5683 DS.SetTypeSpecError();
5684 return;
5685 }
5686
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005687 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005688 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005689 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5690 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005691 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005692 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005693}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005694
Benjamin Kramere56f3932011-12-23 17:00:35 +00005695/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005696/// _Atomic ( type-name )
5697///
5698void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005699 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5700 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005701
5702 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005703 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005704 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005705 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005706
5707 TypeResult Result = ParseTypeName();
5708 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00005709 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00005710 return;
5711 }
5712
5713 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005714 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005715
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005716 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005717 return;
5718
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005719 DS.setTypeofParensRange(T.getRange());
5720 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005721
5722 const char *PrevSpec = 0;
5723 unsigned DiagID;
5724 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5725 DiagID, Result.release()))
5726 Diag(StartLoc, DiagID) << PrevSpec;
5727}
5728
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005729
5730/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5731/// from TryAltiVecVectorToken.
5732bool Parser::TryAltiVecVectorTokenOutOfLine() {
5733 Token Next = NextToken();
5734 switch (Next.getKind()) {
5735 default: return false;
5736 case tok::kw_short:
5737 case tok::kw_long:
5738 case tok::kw_signed:
5739 case tok::kw_unsigned:
5740 case tok::kw_void:
5741 case tok::kw_char:
5742 case tok::kw_int:
5743 case tok::kw_float:
5744 case tok::kw_double:
5745 case tok::kw_bool:
5746 case tok::kw___pixel:
5747 Tok.setKind(tok::kw___vector);
5748 return true;
5749 case tok::identifier:
5750 if (Next.getIdentifierInfo() == Ident_pixel) {
5751 Tok.setKind(tok::kw___vector);
5752 return true;
5753 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005754 if (Next.getIdentifierInfo() == Ident_bool) {
5755 Tok.setKind(tok::kw___vector);
5756 return true;
5757 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005758 return false;
5759 }
5760}
5761
5762bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5763 const char *&PrevSpec, unsigned &DiagID,
5764 bool &isInvalid) {
5765 if (Tok.getIdentifierInfo() == Ident_vector) {
5766 Token Next = NextToken();
5767 switch (Next.getKind()) {
5768 case tok::kw_short:
5769 case tok::kw_long:
5770 case tok::kw_signed:
5771 case tok::kw_unsigned:
5772 case tok::kw_void:
5773 case tok::kw_char:
5774 case tok::kw_int:
5775 case tok::kw_float:
5776 case tok::kw_double:
5777 case tok::kw_bool:
5778 case tok::kw___pixel:
5779 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5780 return true;
5781 case tok::identifier:
5782 if (Next.getIdentifierInfo() == Ident_pixel) {
5783 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5784 return true;
5785 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005786 if (Next.getIdentifierInfo() == Ident_bool) {
5787 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5788 return true;
5789 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005790 break;
5791 default:
5792 break;
5793 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005794 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005795 DS.isTypeAltiVecVector()) {
5796 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5797 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005798 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5799 DS.isTypeAltiVecVector()) {
5800 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5801 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005802 }
5803 return false;
5804}