blob: 0950ab09b76f60b27d00528ec448f126f51bcf8b [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"
Erik Verbruggen888d52a2014-01-15 09:15:43 +000017#include "clang/AST/ASTContext.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000018#include "clang/Basic/AddressSpaces.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000019#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000021#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Sema/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000025#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000026#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000027#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// C99 6.7: Declarations.
32//===----------------------------------------------------------------------===//
33
Chris Lattnerf5fbd792006-08-10 23:56:11 +000034/// ParseTypeName
35/// type-name: [C99 6.7.6]
36/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000037///
38/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000039TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000040 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000041 AccessSpecifier AS,
Richard Smith54ecd982013-02-20 19:22:51 +000042 Decl **OwnedType,
43 ParsedAttributes *Attrs) {
Richard Smith62dad822012-03-15 01:02:11 +000044 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000045 if (DSC == DSC_normal)
46 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000047
Chris Lattnerf5fbd792006-08-10 23:56:11 +000048 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000049 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +000050 if (Attrs)
51 DS.addAttributes(Attrs->getList());
Richard Smithbfdb1082012-03-12 08:56:40 +000052 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000053 if (OwnedType)
54 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000055
Chris Lattnerf5fbd792006-08-10 23:56:11 +000056 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000057 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000058 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000059 if (Range)
60 *Range = DeclaratorInfo.getSourceRange();
61
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000062 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000063 return true;
64
Douglas Gregor0be31a22010-07-02 17:43:08 +000065 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000066}
67
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000068
69/// isAttributeLateParsed - Return true if the attribute has arguments that
70/// require late parsing.
71static bool isAttributeLateParsed(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +000072#define CLANG_ATTR_LATE_PARSED_LIST
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000073 return llvm::StringSwitch<bool>(II.getName())
Aaron Ballman35db2b32014-01-29 22:13:45 +000074#include "clang/Parse/AttrParserStringSwitches.inc"
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000075 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +000076#undef CLANG_ATTR_LATE_PARSED_LIST
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000077}
78
Alexis Hunt96d5c762009-11-21 08:43:09 +000079/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000080///
81/// [GNU] attributes:
82/// attribute
83/// attributes attribute
84///
85/// [GNU] attribute:
86/// '__attribute__' '(' '(' attribute-list ')' ')'
87///
88/// [GNU] attribute-list:
89/// attrib
90/// attribute_list ',' attrib
91///
92/// [GNU] attrib:
93/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000094/// attrib-name
95/// attrib-name '(' identifier ')'
96/// attrib-name '(' identifier ',' nonempty-expr-list ')'
97/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000098///
Steve Naroff0f2fe172007-06-01 17:11:19 +000099/// [GNU] attrib-name:
100/// identifier
101/// typespec
102/// typequal
103/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +0000104///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000105/// Whether an attribute takes an 'identifier' is determined by the
106/// attrib-name. GCC's behavior here is not worth imitating:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000107///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000108/// * In C mode, if the attribute argument list starts with an identifier
109/// followed by a ',' or an ')', and the identifier doesn't resolve to
110/// a type, it is parsed as an identifier. If the attribute actually
111/// wanted an expression, it's out of luck (but it turns out that no
112/// attributes work that way, because C constant expressions are very
113/// limited).
114/// * In C++ mode, if the attribute argument list starts with an identifier,
115/// and the attribute *wants* an identifier, it is parsed as an identifier.
116/// At block scope, any additional tokens between the identifier and the
117/// ',' or ')' are ignored, otherwise they produce a parse error.
Richard Smithb12bf692011-10-17 21:20:17 +0000118///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000119/// We follow the C++ model, but don't allow junk after the identifier.
John McCall53fa7142010-12-24 02:08:15 +0000120void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000121 SourceLocation *endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000122 LateParsedAttrList *LateAttrs,
123 Declarator *D) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000124 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000125
Chris Lattner76c72282007-10-09 17:33:22 +0000126 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000127 ConsumeToken();
128 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
129 "attribute")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000130 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000131 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000132 }
133 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000134 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000135 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000136 }
137 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Alp Toker094e5212014-01-05 03:27:11 +0000138 while (true) {
139 // Allow empty/non-empty attributes. ((__vector_size__(16),,,,))
140 if (TryConsumeToken(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000141 continue;
Alp Toker094e5212014-01-05 03:27:11 +0000142
143 // Expect an identifier or declaration specifier (const, int, etc.)
144 if (Tok.isNot(tok::identifier) && !isDeclarationSpecifier())
145 break;
146
Steve Naroff0f2fe172007-06-01 17:11:19 +0000147 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
148 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000149
Alp Toker094e5212014-01-05 03:27:11 +0000150 if (Tok.isNot(tok::l_paren)) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000151 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
152 AttributeList::AS_GNU);
Alp Toker094e5212014-01-05 03:27:11 +0000153 continue;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000154 }
Alp Toker094e5212014-01-05 03:27:11 +0000155
156 // Handle "parameterized" attributes
157 if (!LateAttrs || !isAttributeLateParsed(*AttrName)) {
158 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, 0,
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000159 SourceLocation(), AttributeList::AS_GNU, D);
Alp Toker094e5212014-01-05 03:27:11 +0000160 continue;
161 }
162
163 // Handle attributes with arguments that require late parsing.
164 LateParsedAttribute *LA =
165 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
166 LateAttrs->push_back(LA);
167
168 // Attributes in a class are parsed at the end of the class, along
169 // with other late-parsed declarations.
170 if (!ClassStack.empty() && !LateAttrs->parseSoon())
171 getCurrentClass().LateParsedDeclarations.push_back(LA);
172
173 // consume everything up to and including the matching right parens
174 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
175
176 Token Eof;
177 Eof.startToken();
178 Eof.setLocation(Tok.getLocation());
179 LA->Toks.push_back(Eof);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000180 }
Alp Toker094e5212014-01-05 03:27:11 +0000181
Alp Toker383d2c42014-01-01 03:08:43 +0000182 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000183 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000184 SourceLocation Loc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000185 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000186 SkipUntil(tok::r_paren, StopAtSemi);
John McCall53fa7142010-12-24 02:08:15 +0000187 if (endLoc)
188 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000189 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000190}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000191
Aaron Ballman4768b312013-11-04 12:55:56 +0000192/// \brief Normalizes an attribute name by dropping prefixed and suffixed __.
193static StringRef normalizeAttrName(StringRef Name) {
Richard Smith66e71682013-10-24 01:07:54 +0000194 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
195 Name = Name.drop_front(2).drop_back(2);
Aaron Ballman4768b312013-11-04 12:55:56 +0000196 return Name;
197}
198
199/// \brief Determine whether the given attribute has an identifier argument.
200static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +0000201#define CLANG_ATTR_IDENTIFIER_ARG_LIST
Aaron Ballman4768b312013-11-04 12:55:56 +0000202 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +0000203#include "clang/Parse/AttrParserStringSwitches.inc"
Douglas Gregord2472d42013-05-02 23:25:32 +0000204 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +0000205#undef CLANG_ATTR_IDENTIFIER_ARG_LIST
Douglas Gregord2472d42013-05-02 23:25:32 +0000206}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000207
Aaron Ballman4768b312013-11-04 12:55:56 +0000208/// \brief Determine whether the given attribute parses a type argument.
209static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +0000210#define CLANG_ATTR_TYPE_ARG_LIST
Aaron Ballman4768b312013-11-04 12:55:56 +0000211 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +0000212#include "clang/Parse/AttrParserStringSwitches.inc"
Aaron Ballman4768b312013-11-04 12:55:56 +0000213 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +0000214#undef CLANG_ATTR_TYPE_ARG_LIST
Aaron Ballman4768b312013-11-04 12:55:56 +0000215}
216
Aaron Ballman15b27b92014-01-09 19:39:35 +0000217/// \brief Determine whether the given attribute requires parsing its arguments
218/// in an unevaluated context or not.
219static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +0000220#define CLANG_ATTR_ARG_CONTEXT_LIST
Aaron Ballman15b27b92014-01-09 19:39:35 +0000221 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +0000222#include "clang/Parse/AttrParserStringSwitches.inc"
Aaron Ballman15b27b92014-01-09 19:39:35 +0000223 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +0000224#undef CLANG_ATTR_ARG_CONTEXT_LIST
Aaron Ballman15b27b92014-01-09 19:39:35 +0000225}
226
Richard Smithfeefaf52013-09-03 18:01:40 +0000227IdentifierLoc *Parser::ParseIdentifierLoc() {
228 assert(Tok.is(tok::identifier) && "expected an identifier");
229 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
230 Tok.getLocation(),
231 Tok.getIdentifierInfo());
232 ConsumeToken();
233 return IL;
234}
235
Richard Smithb1f9a282013-10-31 01:56:18 +0000236void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
237 SourceLocation AttrNameLoc,
238 ParsedAttributes &Attrs,
239 SourceLocation *EndLoc) {
240 BalancedDelimiterTracker Parens(*this, tok::l_paren);
241 Parens.consumeOpen();
242
243 TypeResult T;
244 if (Tok.isNot(tok::r_paren))
245 T = ParseTypeName();
246
247 if (Parens.consumeClose())
248 return;
249
250 if (T.isInvalid())
251 return;
252
253 if (T.isUsable())
254 Attrs.addNewTypeAttr(&AttrName,
255 SourceRange(AttrNameLoc, Parens.getCloseLocation()), 0,
256 AttrNameLoc, T.get(), AttributeList::AS_GNU);
257 else
258 Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
259 0, AttrNameLoc, 0, 0, AttributeList::AS_GNU);
260}
261
Michael Han23214e52012-10-03 01:56:22 +0000262/// Parse the arguments to a parameterized GNU attribute or
263/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000264void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
265 SourceLocation AttrNameLoc,
266 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000267 SourceLocation *EndLoc,
268 IdentifierInfo *ScopeName,
269 SourceLocation ScopeLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000270 AttributeList::Syntax Syntax,
271 Declarator *D) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000272
273 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
274
Richard Smith66e71682013-10-24 01:07:54 +0000275 AttributeList::Kind AttrKind =
Richard Smithb1f9a282013-10-31 01:56:18 +0000276 AttributeList::getKind(AttrName, ScopeName, Syntax);
Richard Smith66e71682013-10-24 01:07:54 +0000277
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000278 // Availability attributes have their own grammar.
Richard Smithb1f9a282013-10-31 01:56:18 +0000279 // FIXME: All these cases fail to pass in the syntax and scope, and might be
280 // written as C++11 gnu:: attributes.
Richard Smith66e71682013-10-24 01:07:54 +0000281 if (AttrKind == AttributeList::AT_Availability) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000282 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
283 return;
284 }
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000285
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000286 if (AttrKind == AttributeList::AT_ObjCBridgeRelated) {
287 ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
288 return;
289 }
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000290
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000291 // Type safety attributes have their own grammar.
Richard Smith66e71682013-10-24 01:07:54 +0000292 if (AttrKind == AttributeList::AT_TypeTagForDatatype) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000293 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
294 return;
295 }
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000296
Aaron Ballman4768b312013-11-04 12:55:56 +0000297 // Some attributes expect solely a type parameter.
298 if (attributeIsTypeArgAttr(*AttrName)) {
Richard Smithb1f9a282013-10-31 01:56:18 +0000299 ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc);
300 return;
301 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000302
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000303 // These may refer to the function arguments, but need to be parsed early to
304 // participate in determining whether it's a redeclaration.
305 llvm::OwningPtr<ParseScope> PrototypeScope;
306 if (AttrName->isStr("enable_if") && D && D->isFunctionDeclarator()) {
307 DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo();
308 PrototypeScope.reset(new ParseScope(this, Scope::FunctionPrototypeScope |
309 Scope::FunctionDeclarationScope |
310 Scope::DeclScope));
311 for (unsigned i = 0; i != FTI.NumArgs; ++i) {
312 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
313 Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param);
314 }
315 }
316
Richard Smith66e71682013-10-24 01:07:54 +0000317 // Ignore the left paren location for now.
318 ConsumeParen();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000319
Aaron Ballman00e99962013-08-31 01:11:41 +0000320 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000321
Richard Smithb1f9a282013-10-31 01:56:18 +0000322 if (Tok.is(tok::identifier)) {
Richard Smith66e71682013-10-24 01:07:54 +0000323 // If this attribute wants an 'identifier' argument, make it so.
Richard Smithb1f9a282013-10-31 01:56:18 +0000324 bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName);
Richard Smith66e71682013-10-24 01:07:54 +0000325
326 // If we don't know how to parse this attribute, but this is the only
327 // token in this argument, assume it's meant to be an identifier.
Aaron Ballman66037472013-12-04 15:32:26 +0000328 if (AttrKind == AttributeList::UnknownAttribute ||
329 AttrKind == AttributeList::IgnoredAttribute) {
Richard Smith66e71682013-10-24 01:07:54 +0000330 const Token &Next = NextToken();
Richard Smithb1f9a282013-10-31 01:56:18 +0000331 IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma);
Richard Smith66e71682013-10-24 01:07:54 +0000332 }
Richard Smithb12bf692011-10-17 21:20:17 +0000333
Richard Smithb1f9a282013-10-31 01:56:18 +0000334 if (IsIdentifierArg)
335 ArgExprs.push_back(ParseIdentifierLoc());
Richard Smithb12bf692011-10-17 21:20:17 +0000336 }
337
Richard Smithb1f9a282013-10-31 01:56:18 +0000338 if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
Richard Smithb12bf692011-10-17 21:20:17 +0000339 // Eat the comma.
Aaron Ballman00e99962013-08-31 01:11:41 +0000340 if (!ArgExprs.empty())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000341 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000342
Richard Smithb12bf692011-10-17 21:20:17 +0000343 // Parse the non-empty comma-separated list of expressions.
Alp Toker8fbec672013-12-17 23:29:36 +0000344 do {
Aaron Ballman7c1fcf82014-01-09 20:12:12 +0000345 OwningPtr<EnterExpressionEvaluationContext> Unevaluated;
346 if (attributeParsedArgsUnevaluated(*AttrName))
347 Unevaluated.reset(new EnterExpressionEvaluationContext(Actions,
348 Sema::Unevaluated));
349
Richard Smithb12bf692011-10-17 21:20:17 +0000350 ExprResult ArgExpr(ParseAssignmentExpression());
351 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000352 SkipUntil(tok::r_paren, StopAtSemi);
Richard Smithb12bf692011-10-17 21:20:17 +0000353 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000354 }
Richard Smithb12bf692011-10-17 21:20:17 +0000355 ArgExprs.push_back(ArgExpr.release());
Alp Toker8fbec672013-12-17 23:29:36 +0000356 // Eat the comma, move to the next argument
357 } while (TryConsumeToken(tok::comma));
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000358 }
Richard Smithb12bf692011-10-17 21:20:17 +0000359
360 SourceLocation RParen = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000361 if (!ExpectAndConsume(tok::r_paren)) {
Michael Han360d2252012-10-04 16:42:52 +0000362 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithb1f9a282013-10-31 01:56:18 +0000363 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
364 ArgExprs.data(), ArgExprs.size(), Syntax);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000365 }
Aaron Ballman7c1fcf82014-01-09 20:12:12 +0000366
367 if (EndLoc)
368 *EndLoc = RParen;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000369}
370
Chad Rosierc1183952012-06-26 22:30:43 +0000371/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000372/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000373void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000374 SourceLocation AttrNameLoc,
375 ParsedAttributes &Attrs)
376{
377 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000378 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000379 AttrName->getNameStart(), tok::r_paren))
380 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000381
Aaron Ballman478faed2012-06-19 22:09:27 +0000382 ExprResult ArgExpr(ParseConstantExpression());
383 if (ArgExpr.isInvalid()) {
384 T.skipToEnd();
385 return;
386 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000387 ArgsUnion ExprList = ArgExpr.take();
388 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
389 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000390
391 T.consumeClose();
392}
393
Chad Rosierc1183952012-06-26 22:30:43 +0000394/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000395/// arguments.
396bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
397 return llvm::StringSwitch<bool>(Ident->getName())
398 .Case("dllimport", true)
399 .Case("dllexport", true)
400 .Case("noreturn", true)
401 .Case("nothrow", true)
402 .Case("noinline", true)
403 .Case("naked", true)
404 .Case("appdomain", true)
405 .Case("process", true)
406 .Case("jitintrinsic", true)
407 .Case("noalias", true)
408 .Case("restrict", true)
409 .Case("novtable", true)
410 .Case("selectany", true)
411 .Case("thread", true)
Aaron Ballman444eb6e2013-05-04 16:58:37 +0000412 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000413 .Default(false);
414}
415
Chad Rosierc1183952012-06-26 22:30:43 +0000416/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000417/// parameters). Will return false if we properly handled the declspec, or
418/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000419void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000420 SourceLocation Loc,
421 ParsedAttributes &Attrs) {
422 // Try to handle the easy case first -- these declspecs all take a single
423 // parameter as their argument.
424 if (llvm::StringSwitch<bool>(Ident->getName())
425 .Case("uuid", true)
426 .Case("align", true)
427 .Case("allocate", true)
428 .Default(false)) {
429 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
430 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000431 // The deprecated declspec has an optional single argument, so we will
432 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000433 // not.
434 if (Tok.getKind() == tok::l_paren)
435 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
436 else
Aaron Ballman00e99962013-08-31 01:11:41 +0000437 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000438 } else if (Ident->getName() == "property") {
439 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000440 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000441 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000442 if (Tok.isNot(tok::l_paren)) {
443 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
444 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000445 return;
John McCall5e77d762013-04-16 07:28:30 +0000446 }
447 BalancedDelimiterTracker T(*this, tok::l_paren);
448 T.expectAndConsume(diag::err_expected_lparen_after,
449 Ident->getNameStart(), tok::r_paren);
450
451 enum AccessorKind {
452 AK_Invalid = -1,
453 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
454 };
455 IdentifierInfo *AccessorNames[] = { 0, 0 };
456 bool HasInvalidAccessor = false;
457
458 // Parse the accessor specifications.
459 while (true) {
460 // Stop if this doesn't look like an accessor spec.
461 if (!Tok.is(tok::identifier)) {
462 // If the user wrote a completely empty list, use a special diagnostic.
463 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
464 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
465 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
466 break;
467 }
468
469 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
470 break;
471 }
472
473 AccessorKind Kind;
474 SourceLocation KindLoc = Tok.getLocation();
475 StringRef KindStr = Tok.getIdentifierInfo()->getName();
476 if (KindStr == "get") {
477 Kind = AK_Get;
478 } else if (KindStr == "put") {
479 Kind = AK_Put;
480
481 // Recover from the common mistake of using 'set' instead of 'put'.
482 } else if (KindStr == "set") {
483 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
484 << FixItHint::CreateReplacement(KindLoc, "put");
485 Kind = AK_Put;
486
487 // Handle the mistake of forgetting the accessor kind by skipping
488 // this accessor.
489 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
490 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
491 ConsumeToken();
492 HasInvalidAccessor = true;
493 goto next_property_accessor;
494
495 // Otherwise, complain about the unknown accessor kind.
496 } else {
497 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
498 HasInvalidAccessor = true;
499 Kind = AK_Invalid;
500
501 // Try to keep parsing unless it doesn't look like an accessor spec.
502 if (!NextToken().is(tok::equal)) break;
503 }
504
505 // Consume the identifier.
506 ConsumeToken();
507
508 // Consume the '='.
Alp Toker8fbec672013-12-17 23:29:36 +0000509 if (!TryConsumeToken(tok::equal)) {
John McCall5e77d762013-04-16 07:28:30 +0000510 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
511 << KindStr;
512 break;
513 }
514
515 // Expect the method name.
516 if (!Tok.is(tok::identifier)) {
517 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
518 break;
519 }
520
521 if (Kind == AK_Invalid) {
522 // Just drop invalid accessors.
523 } else if (AccessorNames[Kind] != NULL) {
524 // Complain about the repeated accessor, ignore it, and keep parsing.
525 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
526 } else {
527 AccessorNames[Kind] = Tok.getIdentifierInfo();
528 }
529 ConsumeToken();
530
531 next_property_accessor:
532 // Keep processing accessors until we run out.
Alp Toker094e5212014-01-05 03:27:11 +0000533 if (TryConsumeToken(tok::comma))
John McCall5e77d762013-04-16 07:28:30 +0000534 continue;
535
536 // If we run into the ')', stop without consuming it.
Alp Toker094e5212014-01-05 03:27:11 +0000537 if (Tok.is(tok::r_paren))
John McCall5e77d762013-04-16 07:28:30 +0000538 break;
Alp Toker094e5212014-01-05 03:27:11 +0000539
540 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
541 break;
John McCall5e77d762013-04-16 07:28:30 +0000542 }
543
544 // Only add the property attribute if it was well-formed.
545 if (!HasInvalidAccessor) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000546 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000547 AccessorNames[AK_Get], AccessorNames[AK_Put],
548 AttributeList::AS_Declspec);
549 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000550 T.skipToEnd();
551 } else {
552 // We don't recognize this as a valid declspec, but instead of creating the
553 // attribute and allowing sema to warn about it, we will warn here instead.
554 // This is because some attributes have multiple spellings, but we need to
555 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000556 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000557 // both locations.
558 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
559
560 // If there's an open paren, we should eat the open and close parens under
561 // the assumption that this unknown declspec has parameters.
562 BalancedDelimiterTracker T(*this, tok::l_paren);
563 if (!T.consumeOpen())
564 T.skipToEnd();
565 }
566}
567
Eli Friedman06de2b52009-06-08 07:21:15 +0000568/// [MS] decl-specifier:
569/// __declspec ( extended-decl-modifier-seq )
570///
571/// [MS] extended-decl-modifier-seq:
572/// extended-decl-modifier[opt]
573/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000574void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000575 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000576
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000577 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000578 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000579 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000580 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000581 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000582
Chad Rosierc1183952012-06-26 22:30:43 +0000583 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000584 // you can specify multiple attributes per declspec.
585 while (Tok.getKind() != tok::r_paren) {
586 // We expect either a well-known identifier or a generic string. Anything
587 // else is a malformed declspec.
588 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000589 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000590 Tok.getKind() != tok::kw_restrict) {
591 Diag(Tok, diag::err_ms_declspec_type);
592 T.skipToEnd();
593 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000594 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000595
596 IdentifierInfo *AttrName;
597 SourceLocation AttrNameLoc;
598 if (IsString) {
599 SmallString<8> StrBuffer;
600 bool Invalid = false;
601 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
602 if (Invalid) {
603 T.skipToEnd();
604 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000605 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000606 AttrName = PP.getIdentifierInfo(Str);
607 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000608 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000609 AttrName = Tok.getIdentifierInfo();
610 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000611 }
Chad Rosierc1183952012-06-26 22:30:43 +0000612
Aaron Ballman478faed2012-06-19 22:09:27 +0000613 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000614 // If we have a generic string, we will allow it because there is no
615 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000616 // (for instance, SAL declspecs in older versions of MSVC).
617 //
Chad Rosierc1183952012-06-26 22:30:43 +0000618 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000619 // arguments and can be turned into an attribute directly.
Aaron Ballman00e99962013-08-31 01:11:41 +0000620 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
621 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000622 else
623 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000624 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000625 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000626}
627
John McCall53fa7142010-12-24 02:08:15 +0000628void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000629 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000630 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000631 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000632 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballman317a77f2013-05-22 23:25:32 +0000633 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
634 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000635 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
636 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000637 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
638 AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000639 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000640}
641
John McCall53fa7142010-12-24 02:08:15 +0000642void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000643 // Treat these like attributes
644 while (Tok.is(tok::kw___pascal)) {
645 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
646 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000647 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
648 AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000649 }
John McCall53fa7142010-12-24 02:08:15 +0000650}
651
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000652void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
653 // Treat these like attributes
654 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000655 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000656 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000657 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
658 AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000659 }
660}
661
Aaron Ballman05d76ea2014-01-14 01:29:54 +0000662void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) {
663 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
664 SourceLocation AttrNameLoc = Tok.getLocation();
Aaron Ballman26891332014-01-14 17:41:53 +0000665 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
666 AttributeList::AS_Keyword);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000667}
668
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000669/// \brief Parse a version number.
670///
671/// version:
672/// simple-integer
673/// simple-integer ',' simple-integer
674/// simple-integer ',' simple-integer ',' simple-integer
675VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
676 Range = Tok.getLocation();
677
678 if (!Tok.is(tok::numeric_constant)) {
679 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000680 SkipUntil(tok::comma, tok::r_paren,
681 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000682 return VersionTuple();
683 }
684
685 // Parse the major (and possibly minor and subminor) versions, which
686 // are stored in the numeric constant. We utilize a quirk of the
687 // lexer, which is that it handles something like 1.2.3 as a single
688 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000689 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000690 Buffer.resize(Tok.getLength()+1);
691 const char *ThisTokBegin = &Buffer[0];
692
693 // Get the spelling of the token, which eliminates trigraphs, etc.
694 bool Invalid = false;
695 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
696 if (Invalid)
697 return VersionTuple();
698
699 // Parse the major version.
700 unsigned AfterMajor = 0;
701 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000702 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000703 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
704 ++AfterMajor;
705 }
706
707 if (AfterMajor == 0) {
708 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000709 SkipUntil(tok::comma, tok::r_paren,
710 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000711 return VersionTuple();
712 }
713
714 if (AfterMajor == ActualLength) {
715 ConsumeToken();
716
717 // We only had a single version component.
718 if (Major == 0) {
719 Diag(Tok, diag::err_zero_version);
720 return VersionTuple();
721 }
722
723 return VersionTuple(Major);
724 }
725
726 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
727 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000728 SkipUntil(tok::comma, tok::r_paren,
729 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000730 return VersionTuple();
731 }
732
733 // Parse the minor version.
734 unsigned AfterMinor = AfterMajor + 1;
735 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000736 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000737 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
738 ++AfterMinor;
739 }
740
741 if (AfterMinor == ActualLength) {
742 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000743
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000744 // We had major.minor.
745 if (Major == 0 && Minor == 0) {
746 Diag(Tok, diag::err_zero_version);
747 return VersionTuple();
748 }
749
Chad Rosierc1183952012-06-26 22:30:43 +0000750 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000751 }
752
753 // If what follows is not a '.', we have a problem.
754 if (ThisTokBegin[AfterMinor] != '.') {
755 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000756 SkipUntil(tok::comma, tok::r_paren,
757 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Chad Rosierc1183952012-06-26 22:30:43 +0000758 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000759 }
760
761 // Parse the subminor version.
762 unsigned AfterSubminor = AfterMinor + 1;
763 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000764 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000765 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
766 ++AfterSubminor;
767 }
768
769 if (AfterSubminor != ActualLength) {
770 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000771 SkipUntil(tok::comma, tok::r_paren,
772 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000773 return VersionTuple();
774 }
775 ConsumeToken();
776 return VersionTuple(Major, Minor, Subminor);
777}
778
779/// \brief Parse the contents of the "availability" attribute.
780///
781/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000782/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000783///
784/// platform:
785/// identifier
786///
787/// version-arg-list:
788/// version-arg
789/// version-arg ',' version-arg-list
790///
791/// version-arg:
792/// 'introduced' '=' version
793/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000794/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000795/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000796/// opt-message:
797/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000798void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
799 SourceLocation AvailabilityLoc,
800 ParsedAttributes &attrs,
801 SourceLocation *endLoc) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000802 enum { Introduced, Deprecated, Obsoleted, Unknown };
803 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000804 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000805
806 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000807 BalancedDelimiterTracker T(*this, tok::l_paren);
808 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000809 Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000810 return;
811 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000812
813 // Parse the platform name,
814 if (Tok.isNot(tok::identifier)) {
815 Diag(Tok, diag::err_availability_expected_platform);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000816 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000817 return;
818 }
Richard Smithfeefaf52013-09-03 18:01:40 +0000819 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000820
821 // Parse the ',' following the platform name.
Alp Toker383d2c42014-01-01 03:08:43 +0000822 if (ExpectAndConsume(tok::comma)) {
823 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000824 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000825 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000826
827 // If we haven't grabbed the pointers for the identifiers
828 // "introduced", "deprecated", and "obsoleted", do so now.
829 if (!Ident_introduced) {
830 Ident_introduced = PP.getIdentifierInfo("introduced");
831 Ident_deprecated = PP.getIdentifierInfo("deprecated");
832 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000833 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000834 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000835 }
836
837 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000838 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000839 do {
840 if (Tok.isNot(tok::identifier)) {
841 Diag(Tok, diag::err_availability_expected_change);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000842 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000843 return;
844 }
845 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
846 SourceLocation KeywordLoc = ConsumeToken();
847
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000848 if (Keyword == Ident_unavailable) {
849 if (UnavailableLoc.isValid()) {
850 Diag(KeywordLoc, diag::err_availability_redundant)
851 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000852 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000853 UnavailableLoc = KeywordLoc;
Alp Toker97650562014-01-10 11:19:30 +0000854 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000855 }
856
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000857 if (Tok.isNot(tok::equal)) {
Alp Tokerec543272013-12-24 09:48:30 +0000858 Diag(Tok, diag::err_expected_after) << Keyword << tok::equal;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000859 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000860 return;
861 }
862 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000863 if (Keyword == Ident_message) {
Benjamin Kramera9dfa922013-09-13 17:31:48 +0000864 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000865 Diag(Tok, diag::err_expected_string_literal)
866 << /*Source='availability attribute'*/2;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000867 SkipUntil(tok::r_paren, StopAtSemi);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000868 return;
869 }
870 MessageExpr = ParseStringLiteralExpression();
871 break;
872 }
Chad Rosierc1183952012-06-26 22:30:43 +0000873
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000874 SourceRange VersionRange;
875 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000876
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000877 if (Version.empty()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000878 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000879 return;
880 }
881
882 unsigned Index;
883 if (Keyword == Ident_introduced)
884 Index = Introduced;
885 else if (Keyword == Ident_deprecated)
886 Index = Deprecated;
887 else if (Keyword == Ident_obsoleted)
888 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000889 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000890 Index = Unknown;
891
892 if (Index < Unknown) {
893 if (!Changes[Index].KeywordLoc.isInvalid()) {
894 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000895 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000896 << SourceRange(Changes[Index].KeywordLoc,
897 Changes[Index].VersionRange.getEnd());
898 }
899
900 Changes[Index].KeywordLoc = KeywordLoc;
901 Changes[Index].Version = Version;
902 Changes[Index].VersionRange = VersionRange;
903 } else {
904 Diag(KeywordLoc, diag::err_availability_unknown_change)
905 << Keyword << VersionRange;
906 }
907
Alp Toker97650562014-01-10 11:19:30 +0000908 } while (TryConsumeToken(tok::comma));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000909
910 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000911 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000912 return;
913
914 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000915 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000916
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000917 // The 'unavailable' availability cannot be combined with any other
918 // availability changes. Make sure that hasn't happened.
919 if (UnavailableLoc.isValid()) {
920 bool Complained = false;
921 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
922 if (Changes[Index].KeywordLoc.isValid()) {
923 if (!Complained) {
924 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
925 << SourceRange(Changes[Index].KeywordLoc,
926 Changes[Index].VersionRange.getEnd());
927 Complained = true;
928 }
929
930 // Clear out the availability.
931 Changes[Index] = AvailabilityChange();
932 }
933 }
934 }
935
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000936 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000937 attrs.addNew(&Availability,
938 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000939 0, AvailabilityLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +0000940 Platform,
John McCall084e83d2011-03-24 11:26:52 +0000941 Changes[Introduced],
942 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000943 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000944 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000945 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000946}
947
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000948/// \brief Parse the contents of the "objc_bridge_related" attribute.
949/// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')'
950/// related_class:
951/// Identifier
952///
953/// opt-class_method:
954/// Identifier: | <empty>
955///
956/// opt-instance_method:
957/// Identifier | <empty>
958///
959void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated,
960 SourceLocation ObjCBridgeRelatedLoc,
961 ParsedAttributes &attrs,
962 SourceLocation *endLoc) {
963 // Opening '('.
964 BalancedDelimiterTracker T(*this, tok::l_paren);
965 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000966 Diag(Tok, diag::err_expected) << tok::l_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000967 return;
968 }
969
970 // Parse the related class name.
971 if (Tok.isNot(tok::identifier)) {
972 Diag(Tok, diag::err_objcbridge_related_expected_related_class);
973 SkipUntil(tok::r_paren, StopAtSemi);
974 return;
975 }
976 IdentifierLoc *RelatedClass = ParseIdentifierLoc();
Alp Toker97650562014-01-10 11:19:30 +0000977 if (ExpectAndConsume(tok::comma)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000978 SkipUntil(tok::r_paren, StopAtSemi);
979 return;
980 }
Alp Toker8fbec672013-12-17 23:29:36 +0000981
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000982 // Parse optional class method name.
983 IdentifierLoc *ClassMethod = 0;
984 if (Tok.is(tok::identifier)) {
985 ClassMethod = ParseIdentifierLoc();
Alp Toker8fbec672013-12-17 23:29:36 +0000986 if (!TryConsumeToken(tok::colon)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000987 Diag(Tok, diag::err_objcbridge_related_selector_name);
988 SkipUntil(tok::r_paren, StopAtSemi);
989 return;
990 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000991 }
Alp Toker8fbec672013-12-17 23:29:36 +0000992 if (!TryConsumeToken(tok::comma)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000993 if (Tok.is(tok::colon))
994 Diag(Tok, diag::err_objcbridge_related_selector_name);
995 else
Alp Tokerec543272013-12-24 09:48:30 +0000996 Diag(Tok, diag::err_expected) << tok::comma;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000997 SkipUntil(tok::r_paren, StopAtSemi);
998 return;
999 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001000
1001 // Parse optional instance method name.
1002 IdentifierLoc *InstanceMethod = 0;
1003 if (Tok.is(tok::identifier))
1004 InstanceMethod = ParseIdentifierLoc();
1005 else if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001006 Diag(Tok, diag::err_expected) << tok::r_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001007 SkipUntil(tok::r_paren, StopAtSemi);
1008 return;
1009 }
1010
1011 // Closing ')'.
1012 if (T.consumeClose())
1013 return;
1014
1015 if (endLoc)
1016 *endLoc = T.getCloseLocation();
1017
1018 // Record this attribute
1019 attrs.addNew(&ObjCBridgeRelated,
1020 SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
1021 0, ObjCBridgeRelatedLoc,
1022 RelatedClass,
1023 ClassMethod,
1024 InstanceMethod,
1025 AttributeList::AS_GNU);
1026
1027}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001028
Bill Wendling44426052012-12-20 19:22:21 +00001029// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001030// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
1031
1032void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
1033
1034void Parser::LateParsedClass::ParseLexedAttributes() {
1035 Self->ParseLexedAttributes(*Class);
1036}
1037
1038void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001039 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001040}
1041
1042/// Wrapper class which calls ParseLexedAttribute, after setting up the
1043/// scope appropriately.
1044void Parser::ParseLexedAttributes(ParsingClass &Class) {
1045 // Deal with templates
1046 // FIXME: Test cases to make sure this does the right thing for templates.
1047 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
1048 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
1049 HasTemplateScope);
1050 if (HasTemplateScope)
1051 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
1052
Douglas Gregor3024f072012-04-16 07:05:22 +00001053 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001054 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +00001055 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001056 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1057 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1058
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001059 // Enter the scope of nested classes
1060 if (!AlreadyHasClassScope)
1061 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1062 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001063 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001064 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1065 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1066 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001067 }
Chad Rosierc1183952012-06-26 22:30:43 +00001068
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001069 if (!AlreadyHasClassScope)
1070 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1071 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001072}
1073
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001074
1075/// \brief Parse all attributes in LAs, and attach them to Decl D.
1076void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1077 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001078 assert(LAs.parseSoon() &&
1079 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001080 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001081 if (D)
1082 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001083 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001084 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001085 }
1086 LAs.clear();
1087}
1088
1089
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001090/// \brief Finish parsing an attribute for which parsing was delayed.
1091/// This will be called at the end of parsing a class declaration
1092/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001093/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001094/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001095void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1096 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001097 // Save the current token position.
1098 SourceLocation OrigLoc = Tok.getLocation();
1099
1100 // Append the current token at the end of the new token stream so that it
1101 // doesn't get lost.
1102 LA.Toks.push_back(Tok);
1103 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1104 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001105 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001106
1107 ParsedAttributes Attrs(AttrFactory);
1108 SourceLocation endLoc;
1109
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001110 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001111 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001112 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1113 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001114
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001115 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001116 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1117 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001118
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001119 if (LA.Decls.size() == 1) {
1120 // If the Decl is templatized, add template parameters to scope.
1121 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1122 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1123 if (HasTemplateScope)
1124 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001125
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001126 // If the Decl is on a function, add function parameters to the scope.
1127 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1128 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1129 if (HasFunScope)
1130 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001131
Michael Han23214e52012-10-03 01:56:22 +00001132 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001133 0, SourceLocation(), AttributeList::AS_GNU, 0);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001134
1135 if (HasFunScope) {
1136 Actions.ActOnExitFunctionContext();
1137 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1138 }
1139 if (HasTemplateScope) {
1140 TempScope.Exit();
1141 }
1142 } else {
1143 // If there are multiple decls, then the decl cannot be within the
1144 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001145 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001146 0, SourceLocation(), AttributeList::AS_GNU, 0);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001147 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001148 } else {
1149 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001150 }
1151
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00001152 const AttributeList *AL = Attrs.getList();
1153 if (OnDefinition && AL && !AL->isCXX11Attribute() &&
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001154 AL->isKnownToGCC())
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00001155 Diag(Tok, diag::warn_attribute_on_function_definition)
1156 << &LA.AttrName;
1157
1158 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i)
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001159 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001160
1161 if (Tok.getLocation() != OrigLoc) {
1162 // Due to a parsing error, we either went over the cached tokens or
1163 // there are still cached tokens left, so we skip the leftover tokens.
1164 // Since this is an uncommon situation that should be avoided, use the
1165 // expensive isBeforeInTranslationUnit call.
1166 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1167 OrigLoc))
1168 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001169 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001170 }
1171}
1172
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001173void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1174 SourceLocation AttrNameLoc,
1175 ParsedAttributes &Attrs,
1176 SourceLocation *EndLoc) {
1177 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1178
1179 BalancedDelimiterTracker T(*this, tok::l_paren);
1180 T.consumeOpen();
1181
1182 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001183 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001184 T.skipToEnd();
1185 return;
1186 }
Richard Smithfeefaf52013-09-03 18:01:40 +00001187 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001188
Alp Toker094e5212014-01-05 03:27:11 +00001189 if (ExpectAndConsume(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001190 T.skipToEnd();
1191 return;
1192 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001193
1194 SourceRange MatchingCTypeRange;
1195 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1196 if (MatchingCType.isInvalid()) {
1197 T.skipToEnd();
1198 return;
1199 }
1200
1201 bool LayoutCompatible = false;
1202 bool MustBeNull = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001203 while (TryConsumeToken(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001204 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001205 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001206 T.skipToEnd();
1207 return;
1208 }
1209 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1210 if (Flag->isStr("layout_compatible"))
1211 LayoutCompatible = true;
1212 else if (Flag->isStr("must_be_null"))
1213 MustBeNull = true;
1214 else {
1215 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1216 T.skipToEnd();
1217 return;
1218 }
1219 ConsumeToken(); // consume flag
1220 }
1221
1222 if (!T.consumeClose()) {
1223 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +00001224 ArgumentKind, MatchingCType.release(),
1225 LayoutCompatible, MustBeNull,
1226 AttributeList::AS_GNU);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001227 }
1228
1229 if (EndLoc)
1230 *EndLoc = T.getCloseLocation();
1231}
1232
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001233/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1234/// of a C++11 attribute-specifier in a location where an attribute is not
1235/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1236/// situation.
1237///
1238/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1239/// this doesn't appear to actually be an attribute-specifier, and the caller
1240/// should try to parse it.
1241bool Parser::DiagnoseProhibitedCXX11Attribute() {
1242 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1243
1244 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1245 case CAK_NotAttributeSpecifier:
1246 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1247 return false;
1248
1249 case CAK_InvalidAttributeSpecifier:
1250 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1251 return false;
1252
1253 case CAK_AttributeSpecifier:
1254 // Parse and discard the attributes.
1255 SourceLocation BeginLoc = ConsumeBracket();
1256 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001257 SkipUntil(tok::r_square);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001258 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1259 SourceLocation EndLoc = ConsumeBracket();
1260 Diag(BeginLoc, diag::err_attributes_not_allowed)
1261 << SourceRange(BeginLoc, EndLoc);
1262 return true;
1263 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001264 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001265}
1266
Richard Smith98155ad2013-02-20 01:17:14 +00001267/// \brief We have found the opening square brackets of a C++11
1268/// attribute-specifier in a location where an attribute is not permitted, but
1269/// we know where the attributes ought to be written. Parse them anyway, and
1270/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001271void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1272 SourceLocation CorrectLocation) {
1273 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1274 Tok.is(tok::kw_alignas));
1275
1276 // Consume the attributes.
1277 SourceLocation Loc = Tok.getLocation();
1278 ParseCXX11Attributes(Attrs);
1279 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1280
1281 Diag(Loc, diag::err_attributes_not_allowed)
1282 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1283 << FixItHint::CreateRemoval(AttrRange);
1284}
1285
John McCall53fa7142010-12-24 02:08:15 +00001286void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1287 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1288 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001289}
1290
Michael Han64536a62012-11-06 19:34:54 +00001291void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1292 AttributeList *AttrList = attrs.getList();
1293 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001294 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001295 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001296 << AttrList->getName();
1297 AttrList->setInvalid();
1298 }
1299 AttrList = AttrList->getNext();
1300 }
1301}
1302
Chris Lattner53361ac2006-08-10 05:19:57 +00001303/// ParseDeclaration - Parse a full 'declaration', which consists of
1304/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001305/// 'Context' should be a Declarator::TheContext value. This returns the
1306/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001307///
1308/// declaration: [C99 6.7]
1309/// block-declaration ->
1310/// simple-declaration
1311/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001312/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001313/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001314/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001315/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001316/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001317/// others... [FIXME]
1318///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001319Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1320 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001321 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001322 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001323 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001324 // Must temporarily exit the objective-c container scope for
1325 // parsing c none objective-c decls.
1326 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001327
John McCall48871652010-08-21 09:40:31 +00001328 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001329 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001330 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001331 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001332 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001333 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001334 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001335 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001336 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001337 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001338 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001339 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001340 SourceLocation InlineLoc = ConsumeToken();
1341 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1342 break;
1343 }
Chad Rosierc1183952012-06-26 22:30:43 +00001344 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001345 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001346 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001347 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001348 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001349 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001350 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001351 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001352 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001353 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001354 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001355 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001356 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001357 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001358 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001359 default:
John McCall53fa7142010-12-24 02:08:15 +00001360 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001361 }
Chad Rosierc1183952012-06-26 22:30:43 +00001362
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001363 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001364 // single decl, convert it now. Alias declarations can also declare a type;
1365 // include that too if it is present.
1366 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001367}
1368
1369/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1370/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001371/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1372/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001373///[C90/C++]init-declarator-list ';' [TODO]
1374/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001375///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001376/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001377/// attribute-specifier-seq[opt] type-specifier-seq declarator
1378///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001379/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001380/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001381///
1382/// If FRI is non-null, we might be parsing a for-range-declaration instead
1383/// of a simple-declaration. If we find that we are, we also parse the
1384/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001385Parser::DeclGroupPtrTy
1386Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1387 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001388 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001389 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001390 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001391 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001392
Richard Smith404dfb42013-11-19 22:47:36 +00001393 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
1394 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
1395
1396 // If we had a free-standing type definition with a missing semicolon, we
1397 // may get this far before the problem becomes obvious.
1398 if (DS.hasTagDefinition() &&
1399 DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
1400 return DeclGroupPtrTy();
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001401
Chris Lattner0e894622006-08-13 19:58:17 +00001402 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1403 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001404 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001405 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001406 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001407 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001408 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001409 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001410 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001411 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001412 }
Chad Rosierc1183952012-06-26 22:30:43 +00001413
Richard Smith2386c8b2013-02-22 09:06:26 +00001414 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001415 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001416}
Mike Stump11289f42009-09-09 15:08:12 +00001417
Richard Smith09f76ee2011-10-19 21:33:05 +00001418/// Returns true if this might be the start of a declarator, or a common typo
1419/// for a declarator.
1420bool Parser::MightBeDeclarator(unsigned Context) {
1421 switch (Tok.getKind()) {
1422 case tok::annot_cxxscope:
1423 case tok::annot_template_id:
1424 case tok::caret:
1425 case tok::code_completion:
1426 case tok::coloncolon:
1427 case tok::ellipsis:
1428 case tok::kw___attribute:
1429 case tok::kw_operator:
1430 case tok::l_paren:
1431 case tok::star:
1432 return true;
1433
1434 case tok::amp:
1435 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001436 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001437
Richard Smithc8a79032012-01-09 22:31:44 +00001438 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001439 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001440 NextToken().is(tok::l_square);
1441
1442 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001443 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001444
Richard Smith09f76ee2011-10-19 21:33:05 +00001445 case tok::identifier:
1446 switch (NextToken().getKind()) {
1447 case tok::code_completion:
1448 case tok::coloncolon:
1449 case tok::comma:
1450 case tok::equal:
1451 case tok::equalequal: // Might be a typo for '='.
1452 case tok::kw_alignas:
1453 case tok::kw_asm:
1454 case tok::kw___attribute:
1455 case tok::l_brace:
1456 case tok::l_paren:
1457 case tok::l_square:
1458 case tok::less:
1459 case tok::r_brace:
1460 case tok::r_paren:
1461 case tok::r_square:
1462 case tok::semi:
1463 return true;
1464
1465 case tok::colon:
1466 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001467 // and in block scope it's probably a label. Inside a class definition,
1468 // this is a bit-field.
1469 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001470 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001471
1472 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001473 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001474
1475 default:
1476 return false;
1477 }
1478
1479 default:
1480 return false;
1481 }
1482}
1483
Richard Smithb8caac82012-04-11 20:59:20 +00001484/// Skip until we reach something which seems like a sensible place to pick
1485/// up parsing after a malformed declaration. This will sometimes stop sooner
1486/// than SkipUntil(tok::r_brace) would, but will never stop later.
1487void Parser::SkipMalformedDecl() {
1488 while (true) {
1489 switch (Tok.getKind()) {
1490 case tok::l_brace:
1491 // Skip until matching }, then stop. We've probably skipped over
1492 // a malformed class or function definition or similar.
1493 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001494 SkipUntil(tok::r_brace);
Richard Smithb8caac82012-04-11 20:59:20 +00001495 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1496 // This declaration isn't over yet. Keep skipping.
1497 continue;
1498 }
Alp Toker8fbec672013-12-17 23:29:36 +00001499 TryConsumeToken(tok::semi);
Richard Smithb8caac82012-04-11 20:59:20 +00001500 return;
1501
1502 case tok::l_square:
1503 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001504 SkipUntil(tok::r_square);
Richard Smithb8caac82012-04-11 20:59:20 +00001505 continue;
1506
1507 case tok::l_paren:
1508 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001509 SkipUntil(tok::r_paren);
Richard Smithb8caac82012-04-11 20:59:20 +00001510 continue;
1511
1512 case tok::r_brace:
1513 return;
1514
1515 case tok::semi:
1516 ConsumeToken();
1517 return;
1518
1519 case tok::kw_inline:
1520 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001521 // a good place to pick back up parsing, except in an Objective-C
1522 // @interface context.
1523 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1524 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001525 return;
1526 break;
1527
1528 case tok::kw_namespace:
1529 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001530 // place to pick back up parsing, except in an Objective-C
1531 // @interface context.
1532 if (Tok.isAtStartOfLine() &&
1533 (!ParsingInObjCContainer || CurParsedObjCImpl))
1534 return;
1535 break;
1536
1537 case tok::at:
1538 // @end is very much like } in Objective-C contexts.
1539 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1540 ParsingInObjCContainer)
1541 return;
1542 break;
1543
1544 case tok::minus:
1545 case tok::plus:
1546 // - and + probably start new method declarations in Objective-C contexts.
1547 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001548 return;
1549 break;
1550
1551 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001552 case tok::annot_module_begin:
1553 case tok::annot_module_end:
1554 case tok::annot_module_include:
Richard Smithb8caac82012-04-11 20:59:20 +00001555 return;
1556
1557 default:
1558 break;
1559 }
1560
1561 ConsumeAnyToken();
1562 }
1563}
1564
John McCalld5a36322009-11-03 19:26:08 +00001565/// ParseDeclGroup - Having concluded that this is either a function
1566/// definition or a group of object declarations, actually parse the
1567/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001568Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1569 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001570 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001571 SourceLocation *DeclEnd,
1572 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001573 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001574 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001575 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001576
John McCalld5a36322009-11-03 19:26:08 +00001577 // Bail out if the first declarator didn't seem well-formed.
1578 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001579 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001580 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001581 }
Mike Stump11289f42009-09-09 15:08:12 +00001582
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001583 // Save late-parsed attributes for now; they need to be parsed in the
1584 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001585 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1586 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001587 if (D.isFunctionDeclarator())
1588 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1589
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001590 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001591 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001592 // Look at the next token to make sure that this isn't a function
1593 // declaration. We have to check this because __attribute__ might be the
1594 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001595 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001596
Douglas Gregor012efe22013-04-16 16:01:32 +00001597 if (AllowFunctionDefinitions) {
1598 if (isStartOfFunctionDefinition(D)) {
1599 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1600 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001601
Douglas Gregor012efe22013-04-16 16:01:32 +00001602 // Recover by treating the 'typedef' as spurious.
1603 DS.ClearStorageClassSpecs();
1604 }
1605
1606 Decl *TheDecl =
1607 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1608 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001609 }
1610
Douglas Gregor012efe22013-04-16 16:01:32 +00001611 if (isDeclarationSpecifier()) {
1612 // If there is an invalid declaration specifier right after the function
1613 // prototype, then we must be in a missing semicolon case where this isn't
1614 // actually a body. Just fall through into the code that handles it as a
1615 // prototype, and let the top-level code handle the erroneous declspec
1616 // where it would otherwise expect a comma or semicolon.
1617 } else {
1618 Diag(Tok, diag::err_expected_fn_body);
1619 SkipUntil(tok::semi);
1620 return DeclGroupPtrTy();
1621 }
John McCalld5a36322009-11-03 19:26:08 +00001622 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001623 if (Tok.is(tok::l_brace)) {
1624 Diag(Tok, diag::err_function_definition_not_allowed);
Serge Pavlov1de51512013-12-09 05:25:47 +00001625 SkipMalformedDecl();
1626 return DeclGroupPtrTy();
Douglas Gregor012efe22013-04-16 16:01:32 +00001627 }
John McCalld5a36322009-11-03 19:26:08 +00001628 }
1629 }
1630
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001631 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001632 return DeclGroupPtrTy();
1633
1634 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1635 // must parse and analyze the for-range-initializer before the declaration is
1636 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001637 //
1638 // Handle the Objective-C for-in loop variable similarly, although we
1639 // don't need to parse the container in advance.
1640 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1641 bool IsForRangeLoop = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001642 if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001643 IsForRangeLoop = true;
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001644 if (Tok.is(tok::l_brace))
1645 FRI->RangeExpr = ParseBraceInitializer();
1646 else
1647 FRI->RangeExpr = ParseExpression();
1648 }
1649
Richard Smith02e85f32011-04-14 22:09:26 +00001650 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001651 if (IsForRangeLoop)
1652 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001653 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001654 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001655 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001656 }
1657
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001658 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001659 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001660 if (LateParsedAttrs.size() > 0)
1661 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001662 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001663 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001664 DeclsInGroup.push_back(FirstDecl);
1665
Richard Smith09f76ee2011-10-19 21:33:05 +00001666 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001667
John McCalld5a36322009-11-03 19:26:08 +00001668 // If we don't have a comma, it is either the end of the list (a ';') or an
1669 // error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00001670 SourceLocation CommaLoc;
1671 while (TryConsumeToken(tok::comma, CommaLoc)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001672 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1673 // This comma was followed by a line-break and something which can't be
1674 // the start of a declarator. The comma was probably a typo for a
1675 // semicolon.
1676 Diag(CommaLoc, diag::err_expected_semi_declaration)
1677 << FixItHint::CreateReplacement(CommaLoc, ";");
1678 ExpectSemi = false;
1679 break;
1680 }
John McCalld5a36322009-11-03 19:26:08 +00001681
1682 // Parse the next declarator.
1683 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001684 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001685
1686 // Accept attributes in an init-declarator. In the first declarator in a
1687 // declaration, these would be part of the declspec. In subsequent
1688 // declarators, they become part of the declarator itself, so that they
1689 // don't apply to declarators after *this* one. Examples:
1690 // short __attribute__((common)) var; -> declspec
1691 // short var __attribute__((common)); -> declarator
1692 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001693 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001694
1695 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001696 if (!D.isInvalidType()) {
1697 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1698 D.complete(ThisDecl);
1699 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001700 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001701 }
John McCalld5a36322009-11-03 19:26:08 +00001702 }
1703
1704 if (DeclEnd)
1705 *DeclEnd = Tok.getLocation();
1706
Richard Smith09f76ee2011-10-19 21:33:05 +00001707 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001708 ExpectAndConsumeSemi(Context == Declarator::FileContext
1709 ? diag::err_invalid_token_after_toplevel_declarator
1710 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001711 // Okay, there was no semicolon and one was expected. If we see a
1712 // declaration specifier, just assume it was missing and continue parsing.
1713 // Otherwise things are very confused and we skip to recover.
1714 if (!isDeclarationSpecifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001715 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Alp Toker8fbec672013-12-17 23:29:36 +00001716 TryConsumeToken(tok::semi);
Chris Lattner13901342010-07-11 22:42:07 +00001717 }
John McCalld5a36322009-11-03 19:26:08 +00001718 }
1719
Rafael Espindolaab417692013-07-09 12:05:01 +00001720 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001721}
1722
Richard Smith02e85f32011-04-14 22:09:26 +00001723/// Parse an optional simple-asm-expr and attributes, and attach them to a
1724/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001725bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001726 // If a simple-asm-expr is present, parse it.
1727 if (Tok.is(tok::kw_asm)) {
1728 SourceLocation Loc;
1729 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1730 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001731 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smith02e85f32011-04-14 22:09:26 +00001732 return true;
1733 }
1734
1735 D.setAsmLabel(AsmLabel.release());
1736 D.SetRangeEnd(Loc);
1737 }
1738
1739 MaybeParseGNUAttributes(D);
1740 return false;
1741}
1742
Douglas Gregor23996282009-05-12 21:31:51 +00001743/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1744/// declarator'. This method parses the remainder of the declaration
1745/// (including any attributes or initializer, among other things) and
1746/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001747///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001748/// init-declarator: [C99 6.7]
1749/// declarator
1750/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001751/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1752/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001753/// [C++] declarator initializer[opt]
1754///
1755/// [C++] initializer:
1756/// [C++] '=' initializer-clause
1757/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001758/// [C++0x] '=' 'default' [TODO]
1759/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001760/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001761///
1762/// According to the standard grammar, =default and =delete are function
1763/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001764///
John McCall48871652010-08-21 09:40:31 +00001765Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001766 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001767 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001768 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001769
Richard Smith02e85f32011-04-14 22:09:26 +00001770 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1771}
Mike Stump11289f42009-09-09 15:08:12 +00001772
Richard Smith02e85f32011-04-14 22:09:26 +00001773Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1774 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001775 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001776 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001777 switch (TemplateInfo.Kind) {
1778 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001779 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001780 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001781
Douglas Gregor450f00842009-09-25 18:43:00 +00001782 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001783 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001784 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001785 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001786 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00001787 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001788 // Re-direct this decl to refer to the templated decl so that we can
1789 // initialize it.
1790 ThisDecl = VT->getTemplatedDecl();
1791 break;
1792 }
1793 case ParsedTemplateInfo::ExplicitInstantiation: {
1794 if (Tok.is(tok::semi)) {
1795 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1796 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1797 if (ThisRes.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001798 SkipUntil(tok::semi, StopBeforeMatch);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001799 return 0;
1800 }
1801 ThisDecl = ThisRes.get();
1802 } else {
1803 // FIXME: This check should be for a variable template instantiation only.
1804
1805 // Check that this is a valid instantiation
1806 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1807 // If the declarator-id is not a template-id, issue a diagnostic and
1808 // recover by ignoring the 'template' keyword.
1809 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1810 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1811 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1812 } else {
1813 SourceLocation LAngleLoc =
1814 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1815 Diag(D.getIdentifierLoc(),
1816 diag::err_explicit_instantiation_with_definition)
1817 << SourceRange(TemplateInfo.TemplateLoc)
1818 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1819
1820 // Recover as if it were an explicit specialization.
1821 TemplateParameterLists FakedParamLists;
1822 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1823 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1824 LAngleLoc));
1825
1826 ThisDecl =
1827 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1828 }
1829 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001830 break;
1831 }
1832 }
Mike Stump11289f42009-09-09 15:08:12 +00001833
Richard Smith74aeef52013-04-26 16:15:35 +00001834 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001835
Douglas Gregor23996282009-05-12 21:31:51 +00001836 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001837 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001838 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001839 ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001840
Anders Carlsson991285e2010-09-24 21:25:25 +00001841 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001842 if (D.isFunctionDeclarator())
1843 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1844 << 1 /* delete */;
1845 else
1846 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001847 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001848 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001849 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1850 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001851 else
1852 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001853 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001854 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001855 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001856 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001857 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001858
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001859 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001860 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001861 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001862 cutOffParsing();
1863 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001864 }
Chad Rosierc1183952012-06-26 22:30:43 +00001865
John McCalldadc5752010-08-24 06:29:42 +00001866 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001867
David Blaikiebbafb8a2012-03-11 07:00:24 +00001868 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001869 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001870 ExitScope();
1871 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001872
Douglas Gregor23996282009-05-12 21:31:51 +00001873 if (Init.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001874 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor604c3022010-03-01 18:27:54 +00001875 Actions.ActOnInitializerError(ThisDecl);
1876 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001877 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1878 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001879 }
1880 } else if (Tok.is(tok::l_paren)) {
1881 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001882 BalancedDelimiterTracker T(*this, tok::l_paren);
1883 T.consumeOpen();
1884
Benjamin Kramerf0623432012-08-23 22:51:59 +00001885 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001886 CommaLocsTy CommaLocs;
1887
David Blaikiebbafb8a2012-03-11 07:00:24 +00001888 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001889 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001890 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001891 }
1892
Douglas Gregor23996282009-05-12 21:31:51 +00001893 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001894 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001895 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor613bf102009-12-22 17:47:17 +00001896
David Blaikiebbafb8a2012-03-11 07:00:24 +00001897 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001898 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001899 ExitScope();
1900 }
Douglas Gregor23996282009-05-12 21:31:51 +00001901 } else {
1902 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001903 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001904
1905 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1906 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001907
David Blaikiebbafb8a2012-03-11 07:00:24 +00001908 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001909 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001910 ExitScope();
1911 }
1912
Sebastian Redla9351792012-02-11 23:51:47 +00001913 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1914 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001915 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001916 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1917 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001918 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001919 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001920 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001921 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001922 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1923
Sebastian Redl3da34892011-06-05 12:23:16 +00001924 if (D.getCXXScopeSpec().isSet()) {
1925 EnterScope(0);
1926 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1927 }
1928
1929 ExprResult Init(ParseBraceInitializer());
1930
1931 if (D.getCXXScopeSpec().isSet()) {
1932 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1933 ExitScope();
1934 }
1935
1936 if (Init.isInvalid()) {
1937 Actions.ActOnInitializerError(ThisDecl);
1938 } else
1939 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1940 /*DirectInit=*/true, TypeContainsAuto);
1941
Douglas Gregor23996282009-05-12 21:31:51 +00001942 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001943 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001944 }
1945
Richard Smithb2bc2e62011-02-21 20:05:19 +00001946 Actions.FinalizeDeclaration(ThisDecl);
1947
Douglas Gregor23996282009-05-12 21:31:51 +00001948 return ThisDecl;
1949}
1950
Chris Lattner1890ac82006-08-13 01:16:23 +00001951/// ParseSpecifierQualifierList
1952/// specifier-qualifier-list:
1953/// type-specifier specifier-qualifier-list[opt]
1954/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001955/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001956///
Richard Smithc5b05522012-03-12 07:56:15 +00001957void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1958 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001959 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1960 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001961 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001962 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001963
Chris Lattner1890ac82006-08-13 01:16:23 +00001964 // Validate declspec for type-name.
1965 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith649c7b062014-01-08 00:56:48 +00001966 if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001967 Diag(Tok, diag::err_expected_type);
1968 DS.SetTypeSpecError();
1969 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1970 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001971 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001972 if (!DS.hasTypeSpecifier())
1973 DS.SetTypeSpecError();
1974 }
Mike Stump11289f42009-09-09 15:08:12 +00001975
Chris Lattner1b22eed2006-11-28 05:12:07 +00001976 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001977 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001978 if (DS.getStorageClassSpecLoc().isValid())
1979 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1980 else
Richard Smithb4a9e862013-04-12 22:46:28 +00001981 Diag(DS.getThreadStorageClassSpecLoc(),
1982 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001983 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001984 }
Mike Stump11289f42009-09-09 15:08:12 +00001985
Chris Lattner1b22eed2006-11-28 05:12:07 +00001986 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001987 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001988 if (DS.isInlineSpecified())
1989 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1990 if (DS.isVirtualSpecified())
1991 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1992 if (DS.isExplicitSpecified())
1993 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001994 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001995 }
Richard Smithc5b05522012-03-12 07:56:15 +00001996
1997 // Issue diagnostic and remove constexpr specfier if present.
1998 if (DS.isConstexprSpecified()) {
1999 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2000 DS.ClearConstexprSpec();
2001 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002002}
Chris Lattner53361ac2006-08-10 05:19:57 +00002003
Chris Lattner6cc055a2009-04-12 20:42:31 +00002004/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2005/// specified token is valid after the identifier in a declarator which
2006/// immediately follows the declspec. For example, these things are valid:
2007///
2008/// int x [ 4]; // direct-declarator
2009/// int x ( int y); // direct-declarator
2010/// int(int x ) // direct-declarator
2011/// int x ; // simple-declaration
2012/// int x = 17; // init-declarator-list
2013/// int x , y; // init-declarator-list
2014/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002015/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002016/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002017///
2018/// This is not, because 'x' does not immediately follow the declspec (though
2019/// ')' happens to be valid anyway).
2020/// int (x)
2021///
2022static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2023 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2024 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002025 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002026}
2027
Chris Lattner20a0c612009-04-14 21:34:55 +00002028
2029/// ParseImplicitInt - This method is called when we have an non-typename
2030/// identifier in a declspec (which normally terminates the decl spec) when
2031/// the declspec has no type specifier. In this case, the declspec is either
2032/// malformed or is "implicit int" (in K&R and C89).
2033///
2034/// This method handles diagnosing this prettily and returns false if the
2035/// declspec is done being processed. If it recovers and thinks there may be
2036/// other pieces of declspec after it, it returns true.
2037///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002038bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002039 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002040 AccessSpecifier AS, DeclSpecContext DSC,
2041 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002042 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002043
Chris Lattner20a0c612009-04-14 21:34:55 +00002044 SourceLocation Loc = Tok.getLocation();
2045 // If we see an identifier that is not a type name, we normally would
2046 // parse it as the identifer being declared. However, when a typename
2047 // is typo'd or the definition is not included, this will incorrectly
2048 // parse the typename as the identifier name and fall over misparsing
2049 // later parts of the diagnostic.
2050 //
2051 // As such, we try to do some look-ahead in cases where this would
2052 // otherwise be an "implicit-int" case to see if this is invalid. For
2053 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2054 // an identifier with implicit int, we'd get a parse error because the
2055 // next token is obviously invalid for a type. Parse these as a case
2056 // with an invalid type specifier.
2057 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002058
Chris Lattner20a0c612009-04-14 21:34:55 +00002059 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002060 // error, do lookahead to try to do better recovery. This never applies
2061 // within a type specifier. Outside of C++, we allow this even if the
2062 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002063 // implicit int as an extension in C99 and C11.
Richard Smith649c7b062014-01-08 00:56:48 +00002064 if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002065 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002066 // If this token is valid for implicit int, e.g. "static x = 4", then
2067 // we just avoid eating the identifier, so it will be parsed as the
2068 // identifier in the declarator.
2069 return false;
2070 }
Mike Stump11289f42009-09-09 15:08:12 +00002071
Richard Smitha952ebb2012-05-15 21:01:51 +00002072 if (getLangOpts().CPlusPlus &&
2073 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2074 // Don't require a type specifier if we have the 'auto' storage class
2075 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002076 if (SS)
2077 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002078 return false;
2079 }
2080
Chris Lattner20a0c612009-04-14 21:34:55 +00002081 // Otherwise, if we don't consume this token, we are going to emit an
2082 // error anyway. Try to recover from various common problems. Check
2083 // to see if this was a reference to a tag name without a tag specified.
2084 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002085 //
2086 // C++ doesn't need this, and isTagName doesn't take SS.
2087 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002088 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002089 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002090
Douglas Gregor0be31a22010-07-02 17:43:08 +00002091 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002092 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002093 case DeclSpec::TST_enum:
2094 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2095 case DeclSpec::TST_union:
2096 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2097 case DeclSpec::TST_struct:
2098 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002099 case DeclSpec::TST_interface:
2100 TagName="__interface"; FixitTagName = "__interface ";
2101 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002102 case DeclSpec::TST_class:
2103 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002104 }
Mike Stump11289f42009-09-09 15:08:12 +00002105
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002106 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002107 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2108 LookupResult R(Actions, TokenName, SourceLocation(),
2109 Sema::LookupOrdinaryName);
2110
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002111 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002112 << TokenName << TagName << getLangOpts().CPlusPlus
2113 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2114
2115 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2116 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2117 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002118 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002119 << TokenName << TagName;
2120 }
Mike Stump11289f42009-09-09 15:08:12 +00002121
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002122 // Parse this as a tag as if the missing tag were present.
2123 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002124 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002125 else
Richard Smithc5b05522012-03-12 07:56:15 +00002126 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002127 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002128 return true;
2129 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002130 }
Mike Stump11289f42009-09-09 15:08:12 +00002131
Richard Smithfe904f02012-05-15 21:29:55 +00002132 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002133 // being declared (with a missing type).
Richard Smith649c7b062014-01-08 00:56:48 +00002134 if (!isTypeSpecifier(DSC) &&
Richard Smithfe904f02012-05-15 21:29:55 +00002135 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002136 // Look ahead to the next token to try to figure out what this declaration
2137 // was supposed to be.
2138 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002139 case tok::l_paren: {
2140 // static x(4); // 'x' is not a type
2141 // x(int n); // 'x' is not a type
2142 // x (*p)[]; // 'x' is a type
2143 //
2144 // Since we're in an error case (or the rare 'implicit int in C++' MS
2145 // extension), we can afford to perform a tentative parse to determine
2146 // which case we're in.
2147 TentativeParsingAction PA(*this);
2148 ConsumeToken();
2149 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2150 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002151
2152 if (TPR != TPResult::False()) {
2153 // The identifier is followed by a parenthesized declarator.
2154 // It's supposed to be a type.
2155 break;
2156 }
2157
2158 // If we're in a context where we could be declaring a constructor,
2159 // check whether this is a constructor declaration with a bogus name.
2160 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2161 IdentifierInfo *II = Tok.getIdentifierInfo();
2162 if (Actions.isCurrentClassNameTypo(II, SS)) {
2163 Diag(Loc, diag::err_constructor_bad_name)
2164 << Tok.getIdentifierInfo() << II
2165 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2166 Tok.setIdentifierInfo(II);
2167 }
2168 }
2169 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002170 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002171 case tok::comma:
2172 case tok::equal:
2173 case tok::kw_asm:
2174 case tok::l_brace:
2175 case tok::l_square:
2176 case tok::semi:
2177 // This looks like a variable or function declaration. The type is
2178 // probably missing. We're done parsing decl-specifiers.
2179 if (SS)
2180 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2181 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002182
2183 default:
2184 // This is probably supposed to be a type. This includes cases like:
2185 // int f(itn);
2186 // struct S { unsinged : 4; };
2187 break;
2188 }
2189 }
2190
Chad Rosierc1183952012-06-26 22:30:43 +00002191 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002192 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002193 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002194 IdentifierInfo *II = Tok.getIdentifierInfo();
2195 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002196 // The action emitted a diagnostic, so we don't have to.
2197 if (T) {
2198 // The action has suggested that the type T could be used. Set that as
2199 // the type in the declaration specifiers, consume the would-be type
2200 // name token, and we're done.
2201 const char *PrevSpec;
2202 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002203 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
2204 Actions.getASTContext().getPrintingPolicy());
Douglas Gregor15e56022009-10-13 23:27:22 +00002205 DS.SetRangeEnd(Tok.getLocation());
2206 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002207 // There may be other declaration specifiers after this.
2208 return true;
2209 } else if (II != Tok.getIdentifierInfo()) {
2210 // If no type was suggested, the correction is to a keyword
2211 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002212 // There may be other declaration specifiers after this.
2213 return true;
2214 }
Chad Rosierc1183952012-06-26 22:30:43 +00002215
Douglas Gregor15e56022009-10-13 23:27:22 +00002216 // Fall through; the action had no suggestion for us.
2217 } else {
2218 // The action did not emit a diagnostic, so emit one now.
2219 SourceRange R;
2220 if (SS) R = SS->getRange();
2221 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2222 }
Mike Stump11289f42009-09-09 15:08:12 +00002223
Douglas Gregor15e56022009-10-13 23:27:22 +00002224 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002225 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002226 DS.SetRangeEnd(Tok.getLocation());
2227 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002228
Chris Lattner20a0c612009-04-14 21:34:55 +00002229 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2230 // avoid rippling error messages on subsequent uses of the same type,
2231 // could be useful if #include was forgotten.
2232 return false;
2233}
2234
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002235/// \brief Determine the declaration specifier context from the declarator
2236/// context.
2237///
2238/// \param Context the declarator context, which is one of the
2239/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002240Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002241Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2242 if (Context == Declarator::MemberContext)
2243 return DSC_class;
2244 if (Context == Declarator::FileContext)
2245 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002246 if (Context == Declarator::TrailingReturnContext)
2247 return DSC_trailing;
Richard Smith649c7b062014-01-08 00:56:48 +00002248 if (Context == Declarator::AliasDeclContext ||
2249 Context == Declarator::AliasTemplateContext)
2250 return DSC_alias_declaration;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002251 return DSC_normal;
2252}
2253
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002254/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2255///
2256/// FIXME: Simply returns an alignof() expression if the argument is a
2257/// type. Ideally, the type should be propagated directly into Sema.
2258///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002259/// [C11] type-id
2260/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002261/// [C++0x] type-id ...[opt]
2262/// [C++0x] assignment-expression ...[opt]
2263ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2264 SourceLocation &EllipsisLoc) {
2265 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002266 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002267 SourceLocation TypeLoc = Tok.getLocation();
2268 ParsedType Ty = ParseTypeName().get();
2269 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002270 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2271 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002272 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002273 ER = ParseConstantExpression();
2274
Alp Toker8fbec672013-12-17 23:29:36 +00002275 if (getLangOpts().CPlusPlus11)
2276 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002277
2278 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002279}
2280
2281/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2282/// attribute to Attrs.
2283///
2284/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002285/// [C11] '_Alignas' '(' type-id ')'
2286/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002287/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2288/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002289void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002290 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002291 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2292 "Not an alignment-specifier!");
2293
Richard Smithd11c7a12013-01-29 01:48:07 +00002294 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2295 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002296
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002297 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002298 if (T.expectAndConsume())
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002299 return;
2300
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002301 SourceLocation EllipsisLoc;
2302 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002303 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002304 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002305 return;
2306 }
2307
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002308 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002309 if (EndLoc)
2310 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002311
Aaron Ballman00e99962013-08-31 01:11:41 +00002312 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002313 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002314 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2315 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002316}
2317
Richard Smith404dfb42013-11-19 22:47:36 +00002318/// Determine whether we're looking at something that might be a declarator
2319/// in a simple-declaration. If it can't possibly be a declarator, maybe
2320/// diagnose a missing semicolon after a prior tag definition in the decl
2321/// specifier.
2322///
2323/// \return \c true if an error occurred and this can't be any kind of
2324/// declaration.
2325bool
2326Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2327 DeclSpecContext DSContext,
2328 LateParsedAttrList *LateAttrs) {
2329 assert(DS.hasTagDefinition() && "shouldn't call this");
2330
2331 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002332
2333 if (getLangOpts().CPlusPlus &&
2334 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2335 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
2336 TryAnnotateCXXScopeToken(EnteringContext)) {
2337 SkipMalformedDecl();
2338 return true;
2339 }
2340
Richard Smith698875a2013-11-20 23:40:57 +00002341 bool HasScope = Tok.is(tok::annot_cxxscope);
2342 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2343 Token AfterScope = HasScope ? NextToken() : Tok;
2344
Richard Smith404dfb42013-11-19 22:47:36 +00002345 // Determine whether the following tokens could possibly be a
2346 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002347 bool MightBeDeclarator = true;
2348 if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) {
2349 // A declarator-id can't start with 'typename'.
2350 MightBeDeclarator = false;
2351 } else if (AfterScope.is(tok::annot_template_id)) {
2352 // If we have a type expressed as a template-id, this cannot be a
2353 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2354 TemplateIdAnnotation *Annot =
2355 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2356 if (Annot->Kind == TNK_Type_template)
2357 MightBeDeclarator = false;
2358 } else if (AfterScope.is(tok::identifier)) {
2359 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2360
Richard Smith404dfb42013-11-19 22:47:36 +00002361 // These tokens cannot come after the declarator-id in a
2362 // simple-declaration, and are likely to come after a type-specifier.
Richard Smith698875a2013-11-20 23:40:57 +00002363 if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) ||
2364 Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) ||
2365 Next.is(tok::coloncolon)) {
2366 // Missing a semicolon.
2367 MightBeDeclarator = false;
2368 } else if (HasScope) {
2369 // If the declarator-id has a scope specifier, it must redeclare a
2370 // previously-declared entity. If that's a type (and this is not a
2371 // typedef), that's an error.
2372 CXXScopeSpec SS;
2373 Actions.RestoreNestedNameSpecifierAnnotation(
2374 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2375 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2376 Sema::NameClassification Classification = Actions.ClassifyName(
2377 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2378 /*IsAddressOfOperand*/false);
2379 switch (Classification.getKind()) {
2380 case Sema::NC_Error:
2381 SkipMalformedDecl();
2382 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002383
Richard Smith698875a2013-11-20 23:40:57 +00002384 case Sema::NC_Keyword:
2385 case Sema::NC_NestedNameSpecifier:
2386 llvm_unreachable("typo correction and nested name specifiers not "
2387 "possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002388
Richard Smith698875a2013-11-20 23:40:57 +00002389 case Sema::NC_Type:
2390 case Sema::NC_TypeTemplate:
2391 // Not a previously-declared non-type entity.
2392 MightBeDeclarator = false;
2393 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002394
Richard Smith698875a2013-11-20 23:40:57 +00002395 case Sema::NC_Unknown:
2396 case Sema::NC_Expression:
2397 case Sema::NC_VarTemplate:
2398 case Sema::NC_FunctionTemplate:
2399 // Might be a redeclaration of a prior entity.
2400 break;
2401 }
Richard Smith404dfb42013-11-19 22:47:36 +00002402 }
Richard Smith404dfb42013-11-19 22:47:36 +00002403 }
2404
Richard Smith698875a2013-11-20 23:40:57 +00002405 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002406 return false;
2407
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002408 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Richard Smith404dfb42013-11-19 22:47:36 +00002409 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
Alp Toker383d2c42014-01-01 03:08:43 +00002410 diag::err_expected_after)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002411 << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi;
Richard Smith404dfb42013-11-19 22:47:36 +00002412
2413 // Try to recover from the typo, by dropping the tag definition and parsing
2414 // the problematic tokens as a type.
2415 //
2416 // FIXME: Split the DeclSpec into pieces for the standalone
2417 // declaration and pieces for the following declaration, instead
2418 // of assuming that all the other pieces attach to new declaration,
2419 // and call ParsedFreeStandingDeclSpec as appropriate.
2420 DS.ClearTypeSpecType();
2421 ParsedTemplateInfo NotATemplate;
2422 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2423 return false;
2424}
2425
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002426/// ParseDeclarationSpecifiers
2427/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002428/// storage-class-specifier declaration-specifiers[opt]
2429/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002430/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002431/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002432/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002433/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002434///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002435/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002436/// 'typedef'
2437/// 'extern'
2438/// 'static'
2439/// 'auto'
2440/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002441/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002442/// [C++11] 'thread_local'
2443/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002444/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002445/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002446/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002447/// [C++] 'virtual'
2448/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002449/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002450/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002451/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002452
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002453///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002454void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002455 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002456 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002457 DeclSpecContext DSContext,
2458 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002459 if (DS.getSourceRange().isInvalid()) {
2460 DS.SetRangeStart(Tok.getLocation());
2461 DS.SetRangeEnd(Tok.getLocation());
2462 }
Chad Rosierc1183952012-06-26 22:30:43 +00002463
Douglas Gregordf593fb2011-11-07 17:33:42 +00002464 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002465 bool AttrsLastTime = false;
2466 ParsedAttributesWithRange attrs(AttrFactory);
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002467 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002468 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002469 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002470 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002471 unsigned DiagID = 0;
2472
Chris Lattner4d8f8732006-11-28 05:05:08 +00002473 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002474
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002475 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002476 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002477 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002478 if (!AttrsLastTime)
2479 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002480 else {
2481 // Reject C++11 attributes that appertain to decl specifiers as
2482 // we don't support any C++11 attributes that appertain to decl
2483 // specifiers. This also conforms to what g++ 4.8 is doing.
2484 ProhibitCXX11Attributes(attrs);
2485
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002486 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002487 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002488
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002489 // If this is not a declaration specifier token, we're done reading decl
2490 // specifiers. First verify that DeclSpec's are consistent.
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002491 DS.Finish(Diags, PP, Policy);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002492 return;
Mike Stump11289f42009-09-09 15:08:12 +00002493
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002494 case tok::l_square:
2495 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002496 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002497 goto DoneWithDeclSpec;
2498
2499 ProhibitAttributes(attrs);
2500 // FIXME: It would be good to recover by accepting the attributes,
2501 // but attempting to do that now would cause serious
2502 // madness in terms of diagnostics.
2503 attrs.clear();
2504 attrs.Range = SourceRange();
2505
2506 ParseCXX11Attributes(attrs);
2507 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002508 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002509
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002510 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002511 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002512 if (DS.hasTypeSpecifier()) {
2513 bool AllowNonIdentifiers
2514 = (getCurScope()->getFlags() & (Scope::ControlScope |
2515 Scope::BlockScope |
2516 Scope::TemplateParamScope |
2517 Scope::FunctionPrototypeScope |
2518 Scope::AtCatchScope)) == 0;
2519 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002520 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002521 (DSContext == DSC_class && DS.isFriendSpecified());
2522
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002523 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002524 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002525 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002526 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002527 }
2528
Douglas Gregor80039242011-02-15 20:33:25 +00002529 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2530 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2531 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002532 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002533 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002534 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002535 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002536 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002537 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002538
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002539 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002540 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002541 }
2542
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002543 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002544 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002545 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002546 if (!DS.hasTypeSpecifier())
2547 DS.SetTypeSpecError();
2548 goto DoneWithDeclSpec;
2549 }
John McCall8bc2a702010-03-01 18:20:46 +00002550 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2551 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002552 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002553
2554 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002555 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002556 goto DoneWithDeclSpec;
2557
John McCall9dab4e62009-12-12 11:40:51 +00002558 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002559 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2560 Tok.getAnnotationRange(),
2561 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002562
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002563 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002564 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002565 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002566 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002567 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002568 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002569
2570 // C++ [class.qual]p2:
2571 // In a lookup in which the constructor is an acceptable lookup
2572 // result and the nested-name-specifier nominates a class C:
2573 //
2574 // - if the name specified after the
2575 // nested-name-specifier, when looked up in C, is the
2576 // injected-class-name of C (Clause 9), or
2577 //
2578 // - if the name specified after the nested-name-specifier
2579 // is the same as the identifier or the
2580 // simple-template-id's template-name in the last
2581 // component of the nested-name-specifier,
2582 //
2583 // the name is instead considered to name the constructor of
2584 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002585 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002586 // Thus, if the template-name is actually the constructor
2587 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002588 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002589 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002590 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002591 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002592 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002593 if (isConstructorDeclarator()) {
2594 // The user meant this to be an out-of-line constructor
2595 // definition, but template arguments are not allowed
2596 // there. Just allow this as a constructor; we'll
2597 // complain about it later.
2598 goto DoneWithDeclSpec;
2599 }
2600
2601 // The user meant this to name a type, but it actually names
2602 // a constructor with some extraneous template
2603 // arguments. Complain, then parse it as a type as the user
2604 // intended.
2605 Diag(TemplateId->TemplateNameLoc,
2606 diag::err_out_of_line_template_id_names_constructor)
2607 << TemplateId->Name;
2608 }
2609
John McCall9dab4e62009-12-12 11:40:51 +00002610 DS.getTypeSpecScope() = SS;
2611 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002612 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002613 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002614 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002615 continue;
2616 }
2617
Douglas Gregorc5790df2009-09-28 07:26:33 +00002618 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002619 DS.getTypeSpecScope() = SS;
2620 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002621 if (Tok.getAnnotationValue()) {
2622 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002623 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002624 Tok.getAnnotationEndLoc(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002625 PrevSpec, DiagID, T, Policy);
Richard Smithda837032012-09-14 18:27:01 +00002626 if (isInvalid)
2627 break;
John McCallba7bf592010-08-24 05:47:05 +00002628 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002629 else
2630 DS.SetTypeSpecError();
2631 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2632 ConsumeToken(); // The typename
2633 }
2634
Douglas Gregor167fa622009-03-25 15:40:00 +00002635 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002636 goto DoneWithDeclSpec;
2637
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002638 // If we're in a context where the identifier could be a class name,
2639 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002640 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002641 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002642 &SS)) {
2643 if (isConstructorDeclarator())
2644 goto DoneWithDeclSpec;
2645
2646 // As noted in C++ [class.qual]p2 (cited above), when the name
2647 // of the class is qualified in a context where it could name
2648 // a constructor, its a constructor name. However, we've
2649 // looked at the declarator, and the user probably meant this
2650 // to be a type. Complain that it isn't supposed to be treated
2651 // as a type, then proceed to parse it as a type.
2652 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2653 << Next.getIdentifierInfo();
2654 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002655
John McCallba7bf592010-08-24 05:47:05 +00002656 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2657 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002658 getCurScope(), &SS,
2659 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002660 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002661 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002662
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002663 // If the referenced identifier is not a type, then this declspec is
2664 // erroneous: We already checked about that it has no type specifier, and
2665 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002666 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002667 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002668 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002669 ParsedAttributesWithRange Attrs(AttrFactory);
2670 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2671 if (!Attrs.empty()) {
2672 AttrsLastTime = true;
2673 attrs.takeAllFrom(Attrs);
2674 }
2675 continue;
2676 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002677 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002678 }
Mike Stump11289f42009-09-09 15:08:12 +00002679
John McCall9dab4e62009-12-12 11:40:51 +00002680 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002681 ConsumeToken(); // The C++ scope.
2682
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002683 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002684 DiagID, TypeRep, Policy);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002685 if (isInvalid)
2686 break;
Mike Stump11289f42009-09-09 15:08:12 +00002687
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002688 DS.SetRangeEnd(Tok.getLocation());
2689 ConsumeToken(); // The typename.
2690
2691 continue;
2692 }
Mike Stump11289f42009-09-09 15:08:12 +00002693
Chris Lattnere387d9e2009-01-21 19:48:37 +00002694 case tok::annot_typename: {
Richard Smith404dfb42013-11-19 22:47:36 +00002695 // If we've previously seen a tag definition, we were almost surely
2696 // missing a semicolon after it.
2697 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
2698 goto DoneWithDeclSpec;
2699
John McCallba7bf592010-08-24 05:47:05 +00002700 if (Tok.getAnnotationValue()) {
2701 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002702 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002703 DiagID, T, Policy);
John McCallba7bf592010-08-24 05:47:05 +00002704 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002705 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002706
Chris Lattner005fc1b2010-04-05 18:18:31 +00002707 if (isInvalid)
2708 break;
2709
Chris Lattnere387d9e2009-01-21 19:48:37 +00002710 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2711 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002712
Chris Lattnere387d9e2009-01-21 19:48:37 +00002713 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2714 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002715 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002716 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002717 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002718
Chris Lattnere387d9e2009-01-21 19:48:37 +00002719 continue;
2720 }
Mike Stump11289f42009-09-09 15:08:12 +00002721
Douglas Gregor06873092011-04-28 15:48:45 +00002722 case tok::kw___is_signed:
2723 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2724 // typically treats it as a trait. If we see __is_signed as it appears
2725 // in libstdc++, e.g.,
2726 //
2727 // static const bool __is_signed;
2728 //
2729 // then treat __is_signed as an identifier rather than as a keyword.
2730 if (DS.getTypeSpecType() == TST_bool &&
2731 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
Alp Toker47642d22013-12-03 06:13:01 +00002732 DS.getStorageClassSpec() == DeclSpec::SCS_static)
2733 TryKeywordIdentFallback(true);
Douglas Gregor06873092011-04-28 15:48:45 +00002734
2735 // We're done with the declaration-specifiers.
2736 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002737
Chris Lattner16fac4f2008-07-26 01:18:38 +00002738 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002739 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002740 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002741 // In C++, check to see if this is a scope specifier like foo::bar::, if
2742 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002743 if (getLangOpts().CPlusPlus) {
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002744 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002745 if (!DS.hasTypeSpecifier())
2746 DS.SetTypeSpecError();
2747 goto DoneWithDeclSpec;
2748 }
2749 if (!Tok.is(tok::identifier))
2750 continue;
2751 }
Mike Stump11289f42009-09-09 15:08:12 +00002752
Chris Lattner16fac4f2008-07-26 01:18:38 +00002753 // This identifier can only be a typedef name if we haven't already seen
2754 // a type-specifier. Without this check we misparse:
2755 // typedef int X; struct Y { short X; }; as 'short int'.
2756 if (DS.hasTypeSpecifier())
2757 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002758
John Thompson22334602010-02-05 00:12:22 +00002759 // Check for need to substitute AltiVec keyword tokens.
2760 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2761 break;
2762
Richard Smith3092a3b2012-05-09 18:56:43 +00002763 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2764 // allow the use of a typedef name as a type specifier.
2765 if (DS.isTypeAltiVecVector())
2766 goto DoneWithDeclSpec;
2767
John McCallba7bf592010-08-24 05:47:05 +00002768 ParsedType TypeRep =
2769 Actions.getTypeName(*Tok.getIdentifierInfo(),
2770 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002771
Chris Lattner6cc055a2009-04-12 20:42:31 +00002772 // If this is not a typedef name, don't parse it as part of the declspec,
2773 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002774 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002775 ParsedAttributesWithRange Attrs(AttrFactory);
2776 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2777 if (!Attrs.empty()) {
2778 AttrsLastTime = true;
2779 attrs.takeAllFrom(Attrs);
2780 }
2781 continue;
2782 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002783 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002784 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002785
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002786 // If we're in a context where the identifier could be a class name,
2787 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002788 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002789 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002790 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002791 goto DoneWithDeclSpec;
2792
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002793 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002794 DiagID, TypeRep, Policy);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002795 if (isInvalid)
2796 break;
Mike Stump11289f42009-09-09 15:08:12 +00002797
Chris Lattner16fac4f2008-07-26 01:18:38 +00002798 DS.SetRangeEnd(Tok.getLocation());
2799 ConsumeToken(); // The identifier
2800
2801 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2802 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002803 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002804 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002805 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002806
Steve Naroffcd5e7822008-09-22 10:28:57 +00002807 // Need to support trailing type qualifiers (e.g. "id<p> const").
2808 // If a type specifier follows, it will be diagnosed elsewhere.
2809 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002810 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002811
2812 // type-name
2813 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002814 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002815 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002816 // This template-id does not refer to a type name, so we're
2817 // done with the type-specifiers.
2818 goto DoneWithDeclSpec;
2819 }
2820
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002821 // If we're in a context where the template-id could be a
2822 // constructor name or specialization, check whether this is a
2823 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002824 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002825 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002826 isConstructorDeclarator())
2827 goto DoneWithDeclSpec;
2828
Douglas Gregor7f741122009-02-25 19:37:18 +00002829 // Turn the template-id annotation token into a type annotation
2830 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002831 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002832 continue;
2833 }
2834
Chris Lattnere37e2332006-08-15 04:50:22 +00002835 // GNU attributes support.
2836 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002837 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002838 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002839
2840 // Microsoft declspec support.
2841 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002842 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002843 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002844
Steve Naroff44ac7772008-12-25 14:16:32 +00002845 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002846 case tok::kw___forceinline: {
Serge Pavlov750db652013-11-13 06:57:53 +00002847 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002848 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002849 SourceLocation AttrNameLoc = Tok.getLocation();
Aaron Ballman00e99962013-08-31 01:11:41 +00002850 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
Aaron Ballman3fe6ed52014-01-13 21:40:16 +00002851 AttributeList::AS_Keyword);
Richard Smithda837032012-09-14 18:27:01 +00002852 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002853 }
Eli Friedman53339e02009-06-08 23:27:34 +00002854
Aaron Ballman317a77f2013-05-22 23:25:32 +00002855 case tok::kw___sptr:
2856 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002857 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002858 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002859 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002860 case tok::kw___cdecl:
2861 case tok::kw___stdcall:
2862 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002863 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002864 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002865 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002866 continue;
2867
Dawn Perchik335e16b2010-09-03 01:29:35 +00002868 // Borland single token adornments.
2869 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002870 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002871 continue;
2872
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002873 // OpenCL single token adornments.
2874 case tok::kw___kernel:
2875 ParseOpenCLAttributes(DS.getAttributes());
2876 continue;
2877
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002878 // storage-class-specifier
2879 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002880 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002881 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002882 break;
2883 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002884 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002885 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002886 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002887 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002888 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002889 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002890 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002891 Loc, PrevSpec, DiagID, Policy);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002892 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002893 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002894 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002895 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002896 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002897 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002898 break;
2899 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002900 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002901 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002902 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002903 PrevSpec, DiagID, Policy);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002904 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002905 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002906 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002907 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002908 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002909 DiagID, Policy);
Richard Smith58c74332011-09-04 19:54:14 +00002910 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002911 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002912 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002913 break;
2914 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002915 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002916 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002917 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002918 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002919 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002920 PrevSpec, DiagID, Policy);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002921 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002922 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002923 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2924 PrevSpec, DiagID);
2925 break;
2926 case tok::kw_thread_local:
2927 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2928 PrevSpec, DiagID);
2929 break;
2930 case tok::kw__Thread_local:
2931 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2932 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002933 break;
Mike Stump11289f42009-09-09 15:08:12 +00002934
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002935 // function-specifier
2936 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00002937 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002938 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002939 case tok::kw_virtual:
Serge Pavlov750db652013-11-13 06:57:53 +00002940 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002941 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002942 case tok::kw_explicit:
Serge Pavlov750db652013-11-13 06:57:53 +00002943 isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002944 break;
Richard Smith0015f092013-01-17 22:16:11 +00002945 case tok::kw__Noreturn:
2946 if (!getLangOpts().C11)
2947 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlov750db652013-11-13 06:57:53 +00002948 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00002949 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002950
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002951 // alignment-specifier
2952 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002953 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002954 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002955 ParseAlignmentSpecifier(DS.getAttributes());
2956 continue;
2957
Anders Carlssoncd8db412009-05-06 04:46:28 +00002958 // friend
2959 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002960 if (DSContext == DSC_class)
2961 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2962 else {
2963 PrevSpec = ""; // not actually used by the diagnostic
2964 DiagID = diag::err_friend_invalid_in_context;
2965 isInvalid = true;
2966 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002967 break;
Mike Stump11289f42009-09-09 15:08:12 +00002968
Douglas Gregor26701a42011-09-09 02:06:17 +00002969 // Modules
2970 case tok::kw___module_private__:
2971 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2972 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002973
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002974 // constexpr
2975 case tok::kw_constexpr:
2976 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2977 break;
2978
Chris Lattnere387d9e2009-01-21 19:48:37 +00002979 // type-specifier
2980 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002981 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002982 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002983 break;
2984 case tok::kw_long:
2985 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002986 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002987 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002988 else
John McCall49bfce42009-08-03 20:12:06 +00002989 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002990 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002991 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002992 case tok::kw___int64:
2993 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002994 DiagID, Policy);
Francois Pichet84133e42011-04-28 01:59:37 +00002995 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002996 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002997 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2998 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002999 break;
3000 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003001 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3002 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003003 break;
3004 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00003005 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3006 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003007 break;
3008 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00003009 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3010 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003011 break;
3012 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003013 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003014 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003015 break;
3016 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003017 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003018 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003019 break;
3020 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003021 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003022 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003023 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003024 case tok::kw___int128:
3025 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003026 DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00003027 break;
3028 case tok::kw_half:
3029 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003030 DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00003031 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003032 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003033 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003034 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003035 break;
3036 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003037 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003038 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003039 break;
3040 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003041 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003042 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003043 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003044 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003045 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003046 DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003047 break;
3048 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003049 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003050 DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003051 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003052 case tok::kw_bool:
3053 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003054 if (Tok.is(tok::kw_bool) &&
3055 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3056 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3057 PrevSpec = ""; // Not used by the diagnostic.
3058 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003059 // For better error recovery.
3060 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003061 isInvalid = true;
3062 } else {
3063 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003064 DiagID, Policy);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003065 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003066 break;
3067 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003068 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003069 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003070 break;
3071 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003072 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003073 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003074 break;
3075 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003076 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003077 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003078 break;
John Thompson22334602010-02-05 00:12:22 +00003079 case tok::kw___vector:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003080 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
John Thompson22334602010-02-05 00:12:22 +00003081 break;
3082 case tok::kw___pixel:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003083 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
John Thompson22334602010-02-05 00:12:22 +00003084 break;
John McCall39439732011-04-09 22:50:59 +00003085 case tok::kw___unknown_anytype:
3086 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003087 PrevSpec, DiagID, Policy);
John McCall39439732011-04-09 22:50:59 +00003088 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003089
3090 // class-specifier:
3091 case tok::kw_class:
3092 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003093 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003094 case tok::kw_union: {
3095 tok::TokenKind Kind = Tok.getKind();
3096 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003097
3098 // These are attributes following class specifiers.
3099 // To produce better diagnostic, we parse them when
3100 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003101 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003102 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003103 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003104
3105 // If there are attributes following class specifier,
3106 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003107 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003108 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003109 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003110 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003111 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003112 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003113
3114 // enum-specifier:
3115 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003116 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003117 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003118 continue;
3119
3120 // cv-qualifier:
3121 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003122 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003123 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003124 break;
3125 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003126 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003127 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003128 break;
3129 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003130 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003131 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003132 break;
3133
Douglas Gregor333489b2009-03-27 23:10:48 +00003134 // C++ typename-specifier:
3135 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003136 if (TryAnnotateTypeOrScopeToken()) {
3137 DS.SetTypeSpecError();
3138 goto DoneWithDeclSpec;
3139 }
3140 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003141 continue;
3142 break;
3143
Chris Lattnere387d9e2009-01-21 19:48:37 +00003144 // GNU typeof support.
3145 case tok::kw_typeof:
3146 ParseTypeofSpecifier(DS);
3147 continue;
3148
David Blaikie15a430a2011-12-04 05:04:18 +00003149 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003150 ParseDecltypeSpecifier(DS);
3151 continue;
3152
Alexis Hunt4a257072011-05-19 05:37:45 +00003153 case tok::kw___underlying_type:
3154 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003155 continue;
3156
3157 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003158 // C11 6.7.2.4/4:
3159 // If the _Atomic keyword is immediately followed by a left parenthesis,
3160 // it is interpreted as a type specifier (with a type name), not as a
3161 // type qualifier.
3162 if (NextToken().is(tok::l_paren)) {
3163 ParseAtomicSpecifier(DS);
3164 continue;
3165 }
3166 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3167 getLangOpts());
3168 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003169
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003170 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003171 case tok::kw___private:
3172 case tok::kw___global:
3173 case tok::kw___local:
3174 case tok::kw___constant:
3175 case tok::kw___read_only:
3176 case tok::kw___write_only:
3177 case tok::kw___read_write:
Aaron Ballman05d76ea2014-01-14 01:29:54 +00003178 ParseOpenCLQualifiers(DS.getAttributes());
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003179 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003180
Steve Naroffcfdf6162008-06-05 00:02:44 +00003181 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003182 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003183 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3184 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003185 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003186 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003187
Douglas Gregor3a001f42010-11-19 17:10:50 +00003188 if (!ParseObjCProtocolQualifiers(DS))
3189 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3190 << FixItHint::CreateInsertion(Loc, "id")
3191 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003192
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003193 // Need to support trailing type qualifiers (e.g. "id<p> const").
3194 // If a type specifier follows, it will be diagnosed elsewhere.
3195 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003196 }
John McCall49bfce42009-08-03 20:12:06 +00003197 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003198 if (isInvalid) {
3199 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003200 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003201
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003202 if (DiagID == diag::ext_duplicate_declspec)
3203 Diag(Tok, DiagID)
3204 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3205 else
3206 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003207 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003208
Chris Lattner2e232092008-03-13 06:29:04 +00003209 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003210 if (DiagID != diag::err_bool_redeclaration)
3211 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003212
3213 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003214 }
3215}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003216
Chris Lattner70ae4912007-10-29 04:42:53 +00003217/// ParseStructDeclaration - Parse a struct declaration without the terminating
3218/// semicolon.
3219///
Chris Lattner90a26b02007-01-23 04:38:16 +00003220/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003221/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003222/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003223/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003224/// struct-declarator-list:
3225/// struct-declarator
3226/// struct-declarator-list ',' struct-declarator
3227/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3228/// struct-declarator:
3229/// declarator
3230/// [GNU] declarator attributes[opt]
3231/// declarator[opt] ':' constant-expression
3232/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3233///
Chris Lattnera12405b2008-04-10 06:46:29 +00003234void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003235ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003236
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003237 if (Tok.is(tok::kw___extension__)) {
3238 // __extension__ silences extension warnings in the subexpression.
3239 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003240 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003241 return ParseStructDeclaration(DS, Fields);
3242 }
Mike Stump11289f42009-09-09 15:08:12 +00003243
Steve Naroff97170802007-08-20 22:28:22 +00003244 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003245 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003246
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003247 // If there are no declarators, this is a free-standing declaration
3248 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003249 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003250 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3251 DS);
3252 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003253 return;
3254 }
3255
3256 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003257 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003258 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003259 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003260 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003261 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003262
Bill Wendling44426052012-12-20 19:22:21 +00003263 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003264 if (!FirstDeclarator)
3265 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003266
Steve Naroff97170802007-08-20 22:28:22 +00003267 /// struct-declarator: declarator
3268 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003269 if (Tok.isNot(tok::colon)) {
3270 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3271 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003272 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003273 }
Mike Stump11289f42009-09-09 15:08:12 +00003274
Alp Toker8fbec672013-12-17 23:29:36 +00003275 if (TryConsumeToken(tok::colon)) {
John McCalldadc5752010-08-24 06:29:42 +00003276 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003277 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003278 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00003279 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003280 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003281 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003282
Steve Naroff97170802007-08-20 22:28:22 +00003283 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003284 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003285
John McCallcfefb6d2009-11-03 02:38:08 +00003286 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003287 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003288
Steve Naroff97170802007-08-20 22:28:22 +00003289 // If we don't have a comma, it is either the end of the list (a ';')
3290 // or an error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00003291 if (!TryConsumeToken(tok::comma, CommaLoc))
Chris Lattner70ae4912007-10-29 04:42:53 +00003292 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003293
John McCallcfefb6d2009-11-03 02:38:08 +00003294 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003295 }
Steve Naroff97170802007-08-20 22:28:22 +00003296}
3297
3298/// ParseStructUnionBody
3299/// struct-contents:
3300/// struct-declaration-list
3301/// [EXT] empty
3302/// [GNU] "struct-declaration-list" without terminatoring ';'
3303/// struct-declaration-list:
3304/// struct-declaration
3305/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003306/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003307///
Chris Lattner1300fb92007-01-23 23:42:53 +00003308void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003309 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003310 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3311 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003312 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003313
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003314 BalancedDelimiterTracker T(*this, tok::l_brace);
3315 if (T.consumeOpen())
3316 return;
Mike Stump11289f42009-09-09 15:08:12 +00003317
Douglas Gregor658b9552009-01-09 22:42:13 +00003318 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003319 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003320
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003321 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003322
Chris Lattner7b9ace62007-01-23 20:11:08 +00003323 // While we still have something to read, read the declarations in the struct.
Richard Smith34f30512013-11-23 04:06:09 +00003324 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003325 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003326
Chris Lattner736ed5d2007-06-09 05:59:07 +00003327 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003328 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003329 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003330 continue;
3331 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003332
Andy Gibbsc804e082013-04-03 09:46:04 +00003333 // Parse _Static_assert declaration.
3334 if (Tok.is(tok::kw__Static_assert)) {
3335 SourceLocation DeclEnd;
3336 ParseStaticAssertDeclaration(DeclEnd);
3337 continue;
3338 }
3339
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003340 if (Tok.is(tok::annot_pragma_pack)) {
3341 HandlePragmaPack();
3342 continue;
3343 }
3344
3345 if (Tok.is(tok::annot_pragma_align)) {
3346 HandlePragmaAlign();
3347 continue;
3348 }
3349
John McCallcfefb6d2009-11-03 02:38:08 +00003350 if (!Tok.is(tok::at)) {
3351 struct CFieldCallback : FieldCallback {
3352 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003353 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003354 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003355
John McCall48871652010-08-21 09:40:31 +00003356 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003357 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003358 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3359
Eli Friedman934dbbf2012-08-08 23:53:27 +00003360 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003361 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003362 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003363 FD.D.getDeclSpec().getSourceRange().getBegin(),
3364 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003365 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003366 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003367 }
John McCallcfefb6d2009-11-03 02:38:08 +00003368 } Callback(*this, TagDecl, FieldDecls);
3369
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003370 // Parse all the comma separated declarators.
3371 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003372 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003373 } else { // Handle @defs
3374 ConsumeToken();
3375 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3376 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003377 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003378 continue;
3379 }
3380 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003381 ExpectAndConsume(tok::l_paren);
Chris Lattner535b8302008-06-21 19:39:06 +00003382 if (!Tok.is(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003383 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003384 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003385 continue;
3386 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003387 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003388 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003389 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003390 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3391 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003392 ExpectAndConsume(tok::r_paren);
Mike Stump11289f42009-09-09 15:08:12 +00003393 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003394
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003395 if (TryConsumeToken(tok::semi))
3396 continue;
3397
3398 if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003399 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003400 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003401 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003402
3403 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3404 // Skip to end of block or statement to avoid ext-warning on extra ';'.
3405 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
3406 // If we stopped at a ';', eat it.
3407 TryConsumeToken(tok::semi);
Chris Lattner90a26b02007-01-23 04:38:16 +00003408 }
Mike Stump11289f42009-09-09 15:08:12 +00003409
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003410 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003411
John McCall084e83d2011-03-24 11:26:52 +00003412 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003413 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003414 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003415
Douglas Gregor0be31a22010-07-02 17:43:08 +00003416 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003417 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003418 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003419 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003420 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003421 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3422 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003423}
3424
Chris Lattner3b561a32006-08-13 00:12:11 +00003425/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003426/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003427/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003428///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003429/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3430/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003431/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3432/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003433/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003434/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003435///
Richard Smith7d137e32012-03-23 03:33:32 +00003436/// [C++11] enum-head '{' enumerator-list[opt] '}'
3437/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003438///
Richard Smith7d137e32012-03-23 03:33:32 +00003439/// enum-head: [C++11]
3440/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3441/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3442/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003443///
Richard Smith7d137e32012-03-23 03:33:32 +00003444/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003445/// 'enum'
3446/// 'enum' 'class'
3447/// 'enum' 'struct'
3448///
Richard Smith7d137e32012-03-23 03:33:32 +00003449/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003450/// ':' type-specifier-seq
3451///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003452/// [C++] elaborated-type-specifier:
3453/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3454///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003455void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003456 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003457 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003458 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003459 if (Tok.is(tok::code_completion)) {
3460 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003461 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003462 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003463 }
John McCallcb432fa2011-07-06 05:58:41 +00003464
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003465 // If attributes exist after tag, parse them.
3466 ParsedAttributesWithRange attrs(AttrFactory);
3467 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003468 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003469
3470 // If declspecs exist after tag, parse them.
3471 while (Tok.is(tok::kw___declspec))
3472 ParseMicrosoftDeclSpec(attrs);
3473
Richard Smith0f8ee222012-01-10 01:33:14 +00003474 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003475 bool IsScopedUsingClassTag = false;
3476
John McCallbeae29a2012-06-23 22:30:04 +00003477 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003478 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3479 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3480 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003481 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003482 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003483
Bill Wendling44426052012-12-20 19:22:21 +00003484 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003485 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003486 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003487
3488 // They are allowed afterwards, though.
3489 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003490 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003491 while (Tok.is(tok::kw___declspec))
3492 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003493 }
Richard Smith7d137e32012-03-23 03:33:32 +00003494
John McCall6347b682012-05-07 06:16:58 +00003495 // C++11 [temp.explicit]p12:
3496 // The usual access controls do not apply to names used to specify
3497 // explicit instantiations.
3498 // We extend this to also cover explicit specializations. Note that
3499 // we don't suppress if this turns out to be an elaborated type
3500 // specifier.
3501 bool shouldDelayDiagsInTag =
3502 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3503 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3504 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003505
Richard Smithbfdb1082012-03-12 08:56:40 +00003506 // Enum definitions should not be parsed in a trailing-return-type.
3507 bool AllowDeclaration = DSC != DSC_trailing;
3508
3509 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003510 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003511 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003512
Abramo Bagnarad7548482010-05-19 21:37:53 +00003513 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003514 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003515 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3516 // if a fixed underlying type is allowed.
3517 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003518
3519 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003520 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003521 return;
3522
3523 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003524 Diag(Tok, diag::err_expected) << tok::identifier;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003525 if (Tok.isNot(tok::l_brace)) {
3526 // Has no name and is not a definition.
3527 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003528 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003529 return;
3530 }
3531 }
3532 }
Mike Stump11289f42009-09-09 15:08:12 +00003533
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003534 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003535 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003536 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Alp Tokerec543272013-12-24 09:48:30 +00003537 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
Mike Stump11289f42009-09-09 15:08:12 +00003538
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003539 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003540 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00003541 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003542 }
Mike Stump11289f42009-09-09 15:08:12 +00003543
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003544 // If an identifier is present, consume and remember it.
3545 IdentifierInfo *Name = 0;
3546 SourceLocation NameLoc;
3547 if (Tok.is(tok::identifier)) {
3548 Name = Tok.getIdentifierInfo();
3549 NameLoc = ConsumeToken();
3550 }
Mike Stump11289f42009-09-09 15:08:12 +00003551
Richard Smith0f8ee222012-01-10 01:33:14 +00003552 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003553 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3554 // declaration of a scoped enumeration.
3555 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003556 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003557 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003558 }
3559
John McCall6347b682012-05-07 06:16:58 +00003560 // Okay, end the suppression area. We'll decide whether to emit the
3561 // diagnostics in a second.
3562 if (shouldDelayDiagsInTag)
3563 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003564
Douglas Gregor0bf31402010-10-08 23:50:27 +00003565 TypeResult BaseType;
3566
Douglas Gregord1f69f62010-12-01 17:42:47 +00003567 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003568 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003569 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003570 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003571 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003572 // If we're in class scope, this can either be an enum declaration with
3573 // an underlying type, or a declaration of a bitfield member. We try to
3574 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003575 // (integer literal, sizeof); if it's still ambiguous, we then consider
3576 // anything that's a simple-type-specifier followed by '(' as an
3577 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003578 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003579 EnterExpressionEvaluationContext Unevaluated(Actions,
3580 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003581 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003582 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003583 // bit-field. This is the common case.
3584 if (TPR == TPResult::True())
3585 PossibleBitfield = true;
3586 // If the next token starts a type-specifier-seq, it may be either a
3587 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003588 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003589 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003590 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003591 GetLookAheadToken(2).getKind() == tok::semi) {
3592 // Consume the ':'.
3593 ConsumeToken();
3594 } else {
3595 // We have the start of a type-specifier-seq, so we have to perform
3596 // tentative parsing to determine whether we have an expression or a
3597 // type.
3598 TentativeParsingAction TPA(*this);
3599
3600 // Consume the ':'.
3601 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003602
3603 // If we see a type specifier followed by an open-brace, we have an
3604 // ambiguity between an underlying type and a C++11 braced
3605 // function-style cast. Resolve this by always treating it as an
3606 // underlying type.
3607 // FIXME: The standard is not entirely clear on how to disambiguate in
3608 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003609 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003610 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003611 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003612 // We'll parse this as a bitfield later.
3613 PossibleBitfield = true;
3614 TPA.Revert();
3615 } else {
3616 // We have a type-specifier-seq.
3617 TPA.Commit();
3618 }
3619 }
3620 } else {
3621 // Consume the ':'.
3622 ConsumeToken();
3623 }
3624
3625 if (!PossibleBitfield) {
3626 SourceRange Range;
3627 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003628
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003629 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003630 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003631 } else if (!getLangOpts().ObjC2) {
3632 if (getLangOpts().CPlusPlus)
3633 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3634 else
3635 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3636 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003637 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003638 }
3639
Richard Smith0f8ee222012-01-10 01:33:14 +00003640 // There are four options here. If we have 'friend enum foo;' then this is a
3641 // friend declaration, and cannot have an accompanying definition. If we have
3642 // 'enum foo;', then this is a forward declaration. If we have
3643 // 'enum foo {...' then this is a definition. Otherwise we have something
3644 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003645 //
3646 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3647 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3648 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3649 //
John McCallfaf5fb42010-08-26 23:41:50 +00003650 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003651 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003652 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003653 } else if (Tok.is(tok::l_brace)) {
3654 if (DS.isFriendSpecified()) {
3655 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3656 << SourceRange(DS.getFriendSpecLoc());
3657 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003658 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00003659 TUK = Sema::TUK_Friend;
3660 } else {
3661 TUK = Sema::TUK_Definition;
3662 }
Richard Smith649c7b062014-01-08 00:56:48 +00003663 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00003664 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003665 (Tok.isAtStartOfLine() &&
3666 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003667 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3668 if (Tok.isNot(tok::semi)) {
3669 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00003670 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003671 PP.EnterToken(Tok);
3672 Tok.setKind(tok::semi);
3673 }
John McCall6347b682012-05-07 06:16:58 +00003674 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003675 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003676 }
3677
3678 // If this is an elaborated type specifier, and we delayed
3679 // diagnostics before, just merge them into the current pool.
3680 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3681 diagsFromTag.redelay();
3682 }
Richard Smith7d137e32012-03-23 03:33:32 +00003683
3684 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003685 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003686 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003687 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003688 // Skip the rest of this declarator, up until the comma or semicolon.
3689 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003690 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00003691 return;
3692 }
3693
3694 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3695 // Enumerations can't be explicitly instantiated.
3696 DS.SetTypeSpecError();
3697 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3698 return;
3699 }
3700
3701 assert(TemplateInfo.TemplateParams && "no template parameters");
3702 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3703 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003704 }
Chad Rosierc1183952012-06-26 22:30:43 +00003705
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003706 if (TUK == Sema::TUK_Reference)
3707 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003708
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003709 if (!Name && TUK != Sema::TUK_Definition) {
3710 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003711
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003712 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003713 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003714 return;
3715 }
Richard Smith7d137e32012-03-23 03:33:32 +00003716
Douglas Gregord6ab8742009-05-28 23:31:59 +00003717 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003718 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003719 const char *PrevSpec = 0;
3720 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003721 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003722 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003723 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003724 Owned, IsDependent, ScopedEnumKWLoc,
Richard Smith649c7b062014-01-08 00:56:48 +00003725 IsScopedUsingClassTag, BaseType,
3726 DSC == DSC_type_specifier);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003727
Douglas Gregorba41d012010-04-24 16:38:41 +00003728 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003729 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003730 // dependent tag.
3731 if (!Name) {
3732 DS.SetTypeSpecError();
3733 Diag(Tok, diag::err_expected_type_name_after_typename);
3734 return;
3735 }
Chad Rosierc1183952012-06-26 22:30:43 +00003736
Douglas Gregor0be31a22010-07-02 17:43:08 +00003737 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003738 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003739 NameLoc);
3740 if (Type.isInvalid()) {
3741 DS.SetTypeSpecError();
3742 return;
3743 }
Chad Rosierc1183952012-06-26 22:30:43 +00003744
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003745 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3746 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003747 PrevSpec, DiagID, Type.get(),
3748 Actions.getASTContext().getPrintingPolicy()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003749 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003750
Douglas Gregorba41d012010-04-24 16:38:41 +00003751 return;
3752 }
Mike Stump11289f42009-09-09 15:08:12 +00003753
John McCall48871652010-08-21 09:40:31 +00003754 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003755 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003756 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003757 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003758 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003759 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00003760 }
Chad Rosierc1183952012-06-26 22:30:43 +00003761
Douglas Gregorba41d012010-04-24 16:38:41 +00003762 DS.SetTypeSpecError();
3763 return;
3764 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003765
Richard Smith369b9f92012-06-25 21:37:02 +00003766 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003767 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003768
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003769 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3770 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003771 PrevSpec, DiagID, TagDecl, Owned,
3772 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00003773 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003774}
3775
Chris Lattnerc1915e22007-01-25 07:29:02 +00003776/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3777/// enumerator-list:
3778/// enumerator
3779/// enumerator-list ',' enumerator
3780/// enumerator:
3781/// enumeration-constant
3782/// enumeration-constant '=' constant-expression
3783/// enumeration-constant:
3784/// identifier
3785///
John McCall48871652010-08-21 09:40:31 +00003786void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003787 // Enter the scope of the enum body and start the definition.
3788 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003789 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003790
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003791 BalancedDelimiterTracker T(*this, tok::l_brace);
3792 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003793
Chris Lattner37256fb2007-08-27 17:24:30 +00003794 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003795 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003796 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003797
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003798 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003799
John McCall48871652010-08-21 09:40:31 +00003800 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003801
Chris Lattnerc1915e22007-01-25 07:29:02 +00003802 // Parse the enumerator-list.
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003803 while (Tok.isNot(tok::r_brace)) {
3804 // Parse enumerator. If failed, try skipping till the start of the next
3805 // enumerator definition.
3806 if (Tok.isNot(tok::identifier)) {
3807 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
3808 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
3809 TryConsumeToken(tok::comma))
3810 continue;
3811 break;
3812 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003813 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3814 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003815
John McCall811a0f52010-10-22 23:36:17 +00003816 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003817 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003818 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003819 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003820 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003821
Chris Lattnerc1915e22007-01-25 07:29:02 +00003822 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003823 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003824 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003825
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003826 if (TryConsumeToken(tok::equal, EqualLoc)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003827 AssignedVal = ParseConstantExpression();
3828 if (AssignedVal.isInvalid())
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003829 SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003830 }
Mike Stump11289f42009-09-09 15:08:12 +00003831
Chris Lattnerc1915e22007-01-25 07:29:02 +00003832 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003833 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3834 LastEnumConstDecl,
3835 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003836 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003837 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003838 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003839
Chris Lattner4ef40012007-06-11 01:28:17 +00003840 EnumConstantDecls.push_back(EnumConstDecl);
3841 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003842
Douglas Gregorce66d022010-09-07 14:51:08 +00003843 if (Tok.is(tok::identifier)) {
3844 // We're missing a comma between enumerators.
3845 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003846 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003847 << FixItHint::CreateInsertion(Loc, ", ");
3848 continue;
3849 }
Chad Rosierc1183952012-06-26 22:30:43 +00003850
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003851 // Emumerator definition must be finished, only comma or r_brace are
3852 // allowed here.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003853 SourceLocation CommaLoc;
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003854 if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
3855 if (EqualLoc.isValid())
3856 Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
3857 << tok::comma;
3858 else
3859 Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
3860 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
3861 if (TryConsumeToken(tok::comma, CommaLoc))
3862 continue;
3863 } else {
3864 break;
3865 }
3866 }
Mike Stump11289f42009-09-09 15:08:12 +00003867
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003868 // If comma is followed by r_brace, emit appropriate warning.
3869 if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003870 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003871 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3872 diag::ext_enumerator_list_comma_cxx :
3873 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003874 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003875 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003876 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3877 << FixItHint::CreateRemoval(CommaLoc);
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003878 break;
Richard Smith5d164bc2011-10-15 05:09:34 +00003879 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003880 }
Mike Stump11289f42009-09-09 15:08:12 +00003881
Chris Lattnerc1915e22007-01-25 07:29:02 +00003882 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003883 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003884
Chris Lattnerc1915e22007-01-25 07:29:02 +00003885 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003886 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003887 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003888
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003889 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003890 EnumDecl, EnumConstantDecls,
3891 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003892 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003893
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003894 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003895 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3896 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003897
3898 // The next token must be valid after an enum definition. If not, a ';'
3899 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003900 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3901 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Alp Toker383d2c42014-01-01 03:08:43 +00003902 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003903 // Push this token back into the preprocessor and change our current token
3904 // to ';' so that the rest of the code recovers as though there were an
3905 // ';' after the definition.
3906 PP.EnterToken(Tok);
3907 Tok.setKind(tok::semi);
3908 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003909}
Chris Lattner3b561a32006-08-13 00:12:11 +00003910
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003911/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003912/// start of a type-qualifier-list.
3913bool Parser::isTypeQualifier() const {
3914 switch (Tok.getKind()) {
3915 default: return false;
Alp Tokerde50ff32013-12-17 18:17:46 +00003916 // type-qualifier
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003917 case tok::kw_const:
3918 case tok::kw_volatile:
3919 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003920 case tok::kw___private:
3921 case tok::kw___local:
3922 case tok::kw___global:
3923 case tok::kw___constant:
3924 case tok::kw___read_only:
3925 case tok::kw___read_write:
3926 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003927 return true;
3928 }
3929}
3930
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003931/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3932/// is definitely a type-specifier. Return false if it isn't part of a type
3933/// specifier or if we're not sure.
3934bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3935 switch (Tok.getKind()) {
3936 default: return false;
3937 // type-specifiers
3938 case tok::kw_short:
3939 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003940 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003941 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003942 case tok::kw_signed:
3943 case tok::kw_unsigned:
3944 case tok::kw__Complex:
3945 case tok::kw__Imaginary:
3946 case tok::kw_void:
3947 case tok::kw_char:
3948 case tok::kw_wchar_t:
3949 case tok::kw_char16_t:
3950 case tok::kw_char32_t:
3951 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003952 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003953 case tok::kw_float:
3954 case tok::kw_double:
3955 case tok::kw_bool:
3956 case tok::kw__Bool:
3957 case tok::kw__Decimal32:
3958 case tok::kw__Decimal64:
3959 case tok::kw__Decimal128:
3960 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003961
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003962 // struct-or-union-specifier (C99) or class-specifier (C++)
3963 case tok::kw_class:
3964 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003965 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003966 case tok::kw_union:
3967 // enum-specifier
3968 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003969
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003970 // typedef-name
3971 case tok::annot_typename:
3972 return true;
3973 }
3974}
3975
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003976/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003977/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003978bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003979 switch (Tok.getKind()) {
3980 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003981
Chris Lattner020bab92009-01-04 23:41:41 +00003982 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003983 if (TryAltiVecVectorToken())
3984 return true;
3985 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003986 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003987 // Annotate typenames and C++ scope specifiers. If we get one, just
3988 // recurse to handle whatever we get.
3989 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003990 return true;
3991 if (Tok.is(tok::identifier))
3992 return false;
3993 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003994
Chris Lattner020bab92009-01-04 23:41:41 +00003995 case tok::coloncolon: // ::foo::bar
3996 if (NextToken().is(tok::kw_new) || // ::new
3997 NextToken().is(tok::kw_delete)) // ::delete
3998 return false;
3999
Chris Lattner020bab92009-01-04 23:41:41 +00004000 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004001 return true;
4002 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004003
Chris Lattnere37e2332006-08-15 04:50:22 +00004004 // GNU attributes support.
4005 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004006 // GNU typeof support.
4007 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004008
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004009 // type-specifiers
4010 case tok::kw_short:
4011 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004012 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004013 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004014 case tok::kw_signed:
4015 case tok::kw_unsigned:
4016 case tok::kw__Complex:
4017 case tok::kw__Imaginary:
4018 case tok::kw_void:
4019 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004020 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004021 case tok::kw_char16_t:
4022 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004023 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004024 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004025 case tok::kw_float:
4026 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004027 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004028 case tok::kw__Bool:
4029 case tok::kw__Decimal32:
4030 case tok::kw__Decimal64:
4031 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004032 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004033
Chris Lattner861a2262008-04-13 18:59:07 +00004034 // struct-or-union-specifier (C99) or class-specifier (C++)
4035 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004036 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004037 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004038 case tok::kw_union:
4039 // enum-specifier
4040 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004041
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004042 // type-qualifier
4043 case tok::kw_const:
4044 case tok::kw_volatile:
4045 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004046
John McCallea0a39e2012-11-14 00:49:39 +00004047 // Debugger support.
4048 case tok::kw___unknown_anytype:
4049
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004050 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004051 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004052 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004053
Chris Lattner409bf7d2008-10-20 00:25:30 +00004054 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4055 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004056 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004057
Steve Naroff44ac7772008-12-25 14:16:32 +00004058 case tok::kw___cdecl:
4059 case tok::kw___stdcall:
4060 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004061 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004062 case tok::kw___w64:
4063 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004064 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004065 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004066 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004067
4068 case tok::kw___private:
4069 case tok::kw___local:
4070 case tok::kw___global:
4071 case tok::kw___constant:
4072 case tok::kw___read_only:
4073 case tok::kw___read_write:
4074 case tok::kw___write_only:
4075
Eli Friedman53339e02009-06-08 23:27:34 +00004076 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004077
Richard Smith8e1ac332013-03-28 01:55:44 +00004078 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004079 case tok::kw__Atomic:
4080 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004081 }
4082}
4083
Chris Lattneracd58a32006-08-06 17:24:14 +00004084/// isDeclarationSpecifier() - Return true if the current token is part of a
4085/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004086///
4087/// \param DisambiguatingWithExpression True to indicate that the purpose of
4088/// this check is to disambiguate between an expression and a declaration.
4089bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004090 switch (Tok.getKind()) {
4091 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004092
Chris Lattner020bab92009-01-04 23:41:41 +00004093 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004094 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004095 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004096 return false;
John Thompson22334602010-02-05 00:12:22 +00004097 if (TryAltiVecVectorToken())
4098 return true;
4099 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004100 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004101 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004102 // Annotate typenames and C++ scope specifiers. If we get one, just
4103 // recurse to handle whatever we get.
4104 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004105 return true;
4106 if (Tok.is(tok::identifier))
4107 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004108
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004109 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004110 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004111 // expression is permitted, then this is probably a class message send
4112 // missing the initial '['. In this case, we won't consider this to be
4113 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004114 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004115 isStartOfObjCClassMessageMissingOpenBracket())
4116 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004117
John McCall1f476a12010-02-26 08:45:28 +00004118 return isDeclarationSpecifier();
4119
Chris Lattner020bab92009-01-04 23:41:41 +00004120 case tok::coloncolon: // ::foo::bar
4121 if (NextToken().is(tok::kw_new) || // ::new
4122 NextToken().is(tok::kw_delete)) // ::delete
4123 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004124
Chris Lattner020bab92009-01-04 23:41:41 +00004125 // Annotate typenames and C++ scope specifiers. If we get one, just
4126 // recurse to handle whatever we get.
4127 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004128 return true;
4129 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004130
Chris Lattneracd58a32006-08-06 17:24:14 +00004131 // storage-class-specifier
4132 case tok::kw_typedef:
4133 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004134 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004135 case tok::kw_static:
4136 case tok::kw_auto:
4137 case tok::kw_register:
4138 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004139 case tok::kw_thread_local:
4140 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004141
Douglas Gregor26701a42011-09-09 02:06:17 +00004142 // Modules
4143 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004144
John McCallea0a39e2012-11-14 00:49:39 +00004145 // Debugger support
4146 case tok::kw___unknown_anytype:
4147
Chris Lattneracd58a32006-08-06 17:24:14 +00004148 // type-specifiers
4149 case tok::kw_short:
4150 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004151 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004152 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004153 case tok::kw_signed:
4154 case tok::kw_unsigned:
4155 case tok::kw__Complex:
4156 case tok::kw__Imaginary:
4157 case tok::kw_void:
4158 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004159 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004160 case tok::kw_char16_t:
4161 case tok::kw_char32_t:
4162
Chris Lattneracd58a32006-08-06 17:24:14 +00004163 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004164 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004165 case tok::kw_float:
4166 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004167 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004168 case tok::kw__Bool:
4169 case tok::kw__Decimal32:
4170 case tok::kw__Decimal64:
4171 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004172 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004173
Chris Lattner861a2262008-04-13 18:59:07 +00004174 // struct-or-union-specifier (C99) or class-specifier (C++)
4175 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004176 case tok::kw_struct:
4177 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004178 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004179 // enum-specifier
4180 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004181
Chris Lattneracd58a32006-08-06 17:24:14 +00004182 // type-qualifier
4183 case tok::kw_const:
4184 case tok::kw_volatile:
4185 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004186
Chris Lattneracd58a32006-08-06 17:24:14 +00004187 // function-specifier
4188 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004189 case tok::kw_virtual:
4190 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004191 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004192
Richard Smith1dba27c2013-01-29 09:02:09 +00004193 // alignment-specifier
4194 case tok::kw__Alignas:
4195
Richard Smithd16fe122012-10-25 00:00:53 +00004196 // friend keyword.
4197 case tok::kw_friend:
4198
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004199 // static_assert-declaration
4200 case tok::kw__Static_assert:
4201
Chris Lattner599e47e2007-08-09 17:01:07 +00004202 // GNU typeof support.
4203 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004204
Chris Lattner599e47e2007-08-09 17:01:07 +00004205 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004206 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004207
Richard Smithd16fe122012-10-25 00:00:53 +00004208 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004209 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004210 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004211
Richard Smith8e1ac332013-03-28 01:55:44 +00004212 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004213 case tok::kw__Atomic:
4214 return true;
4215
Chris Lattner8b2ec162008-07-26 03:38:44 +00004216 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4217 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004218 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004219
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004220 // typedef-name
4221 case tok::annot_typename:
4222 return !DisambiguatingWithExpression ||
4223 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004224
Steve Narofff192fab2009-01-06 19:34:12 +00004225 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004226 case tok::kw___cdecl:
4227 case tok::kw___stdcall:
4228 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004229 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004230 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004231 case tok::kw___sptr:
4232 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004233 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004234 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004235 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004236 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004237 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004238
4239 case tok::kw___private:
4240 case tok::kw___local:
4241 case tok::kw___global:
4242 case tok::kw___constant:
4243 case tok::kw___read_only:
4244 case tok::kw___read_write:
4245 case tok::kw___write_only:
4246
Eli Friedman53339e02009-06-08 23:27:34 +00004247 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004248 }
4249}
4250
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004251bool Parser::isConstructorDeclarator() {
4252 TentativeParsingAction TPA(*this);
4253
4254 // Parse the C++ scope specifier.
4255 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004256 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004257 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004258 TPA.Revert();
4259 return false;
4260 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004261
4262 // Parse the constructor name.
4263 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4264 // We already know that we have a constructor name; just consume
4265 // the token.
4266 ConsumeToken();
4267 } else {
4268 TPA.Revert();
4269 return false;
4270 }
4271
Richard Smith43f340f2012-03-27 23:05:05 +00004272 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004273 if (Tok.isNot(tok::l_paren)) {
4274 TPA.Revert();
4275 return false;
4276 }
4277 ConsumeParen();
4278
Richard Smith43f340f2012-03-27 23:05:05 +00004279 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4280 // that we have a constructor.
4281 if (Tok.is(tok::r_paren) ||
4282 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004283 TPA.Revert();
4284 return true;
4285 }
4286
Richard Smithf2163662013-09-06 00:12:20 +00004287 // A C++11 attribute here signals that we have a constructor, and is an
4288 // attribute on the first constructor parameter.
4289 if (getLangOpts().CPlusPlus11 &&
4290 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4291 /*OuterMightBeMessageSend*/ true)) {
4292 TPA.Revert();
4293 return true;
4294 }
4295
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004296 // If we need to, enter the specified scope.
4297 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004298 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004299 DeclScopeObj.EnterDeclaratorScope();
4300
Francois Pichet79f3a872011-01-31 04:54:32 +00004301 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004302 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004303 MaybeParseMicrosoftAttributes(Attrs);
4304
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004305 // Check whether the next token(s) are part of a declaration
4306 // specifier, in which case we have the start of a parameter and,
4307 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004308 bool IsConstructor = false;
4309 if (isDeclarationSpecifier())
4310 IsConstructor = true;
4311 else if (Tok.is(tok::identifier) ||
4312 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4313 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4314 // This might be a parenthesized member name, but is more likely to
4315 // be a constructor declaration with an invalid argument type. Keep
4316 // looking.
4317 if (Tok.is(tok::annot_cxxscope))
4318 ConsumeToken();
4319 ConsumeToken();
4320
4321 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004322 // which must have one of the following syntactic forms (see the
4323 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004324 switch (Tok.getKind()) {
4325 case tok::l_paren:
4326 // C(X ( int));
4327 case tok::l_square:
4328 // C(X [ 5]);
4329 // C(X [ [attribute]]);
4330 case tok::coloncolon:
4331 // C(X :: Y);
4332 // C(X :: *p);
4333 case tok::r_paren:
4334 // C(X )
4335 // Assume this isn't a constructor, rather than assuming it's a
4336 // constructor with an unnamed parameter of an ill-formed type.
4337 break;
4338
4339 default:
4340 IsConstructor = true;
4341 break;
4342 }
4343 }
4344
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004345 TPA.Revert();
4346 return IsConstructor;
4347}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004348
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004349/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004350/// type-qualifier-list: [C99 6.7.5]
4351/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004352/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004353/// [ only if VendorAttributesAllowed=true ]
4354/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004355/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004356/// [ only if VendorAttributesAllowed=true ]
4357/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004358/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004359/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004360///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004361void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4362 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004363 bool CXX11AttributesAllowed,
Alp Toker62c5b572013-11-26 01:30:10 +00004364 bool AtomicAllowed,
4365 bool IdentifierRequired) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004366 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004367 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004368 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004369 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004370 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004371 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004372
4373 SourceLocation EndLoc;
4374
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004375 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004376 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004377 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004378 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004379 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004380
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004381 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004382 case tok::code_completion:
4383 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004384 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004385
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004386 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004387 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004388 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004389 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004390 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004391 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004392 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004393 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004394 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004395 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004396 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004397 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004398 case tok::kw__Atomic:
4399 if (!AtomicAllowed)
4400 goto DoneWithTypeQuals;
4401 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4402 getLangOpts());
4403 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004404
4405 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004406 case tok::kw___private:
4407 case tok::kw___global:
4408 case tok::kw___local:
4409 case tok::kw___constant:
4410 case tok::kw___read_only:
4411 case tok::kw___write_only:
4412 case tok::kw___read_write:
Aaron Ballman05d76ea2014-01-14 01:29:54 +00004413 ParseOpenCLQualifiers(DS.getAttributes());
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004414 break;
4415
Aaron Ballman317a77f2013-05-22 23:25:32 +00004416 case tok::kw___uptr:
Alp Toker62c5b572013-11-26 01:30:10 +00004417 // GNU libc headers in C mode use '__uptr' as an identifer which conflicts
4418 // with the MS modifier keyword.
4419 if (VendorAttributesAllowed && !getLangOpts().CPlusPlus &&
Alp Toker47642d22013-12-03 06:13:01 +00004420 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
4421 if (TryKeywordIdentFallback(false))
4422 continue;
Alp Toker62c5b572013-11-26 01:30:10 +00004423 }
4424 case tok::kw___sptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004425 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004426 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004427 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004428 case tok::kw___cdecl:
4429 case tok::kw___stdcall:
4430 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004431 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004432 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004433 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004434 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004435 continue;
4436 }
4437 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004438 case tok::kw___pascal:
4439 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004440 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004441 continue;
4442 }
4443 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004444 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004445 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004446 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004447 continue; // do *not* consume the next token!
4448 }
4449 // otherwise, FALL THROUGH!
4450 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004451 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004452 // If this is not a type-qualifier token, we're done reading type
4453 // qualifiers. First verify that DeclSpec's are consistent.
Erik Verbruggen888d52a2014-01-15 09:15:43 +00004454 DS.Finish(Diags, PP, Actions.getASTContext().getPrintingPolicy());
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004455 if (EndLoc.isValid())
4456 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004457 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004458 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004459
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004460 // If the specifier combination wasn't legal, issue a diagnostic.
4461 if (isInvalid) {
4462 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004463 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004464 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004465 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004466 }
4467}
4468
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004469
4470/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4471///
4472void Parser::ParseDeclarator(Declarator &D) {
4473 /// This implements the 'declarator' production in the C grammar, then checks
4474 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004475 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004476}
4477
Richard Smith0efa75c2012-03-29 01:16:42 +00004478static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4479 if (Kind == tok::star || Kind == tok::caret)
4480 return true;
4481
4482 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4483 if (!Lang.CPlusPlus)
4484 return false;
4485
4486 return Kind == tok::amp || Kind == tok::ampamp;
4487}
4488
Sebastian Redlbd150f42008-11-21 19:14:01 +00004489/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4490/// is parsed by the function passed to it. Pass null, and the direct-declarator
4491/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004492/// ptr-operator production.
4493///
Richard Smith09f76ee2011-10-19 21:33:05 +00004494/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004495/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4496/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004497///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004498/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4499/// [C] pointer[opt] direct-declarator
4500/// [C++] direct-declarator
4501/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004502///
4503/// pointer: [C99 6.7.5]
4504/// '*' type-qualifier-list[opt]
4505/// '*' type-qualifier-list[opt] pointer
4506///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004507/// ptr-operator:
4508/// '*' cv-qualifier-seq[opt]
4509/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004510/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004511/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004512/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004513/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004514void Parser::ParseDeclaratorInternal(Declarator &D,
4515 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004516 if (Diags.hasAllExtensionsSilenced())
4517 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004518
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004519 // C++ member pointers start with a '::' or a nested-name.
4520 // Member pointers get special handling, since there's no place for the
4521 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004522 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004523 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4524 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004525 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4526 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004527 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004528 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004529
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004530 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004531 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004532 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004533 if (D.mayHaveIdentifier())
4534 D.getCXXScopeSpec() = SS;
4535 else
4536 AnnotateScopeToken(SS, true);
4537
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004538 if (DirectDeclParser)
4539 (this->*DirectDeclParser)(D);
4540 return;
4541 }
4542
4543 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004544 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004545 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004546 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004547 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004548
4549 // Recurse to parse whatever is left.
4550 ParseDeclaratorInternal(D, DirectDeclParser);
4551
4552 // Sema will have to catch (syntactically invalid) pointers into global
4553 // scope. It has to catch pointers into namespace scope anyway.
4554 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004555 Loc),
4556 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004557 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004558 return;
4559 }
4560 }
4561
4562 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004563 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004564 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004565 if (DirectDeclParser)
4566 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004567 return;
4568 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004569
Sebastian Redled0f3b02009-03-15 22:02:01 +00004570 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4571 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004572 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004573 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004574
Chris Lattner9eac9312009-03-27 04:18:06 +00004575 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004576 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004577 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004578
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004579 // FIXME: GNU attributes are not allowed here in a new-type-id.
Alp Toker62c5b572013-11-26 01:30:10 +00004580 ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004581 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004582
Bill Wendling3708c182007-05-27 10:15:43 +00004583 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004584 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004585 if (Kind == tok::star)
4586 // Remember that we parsed a pointer type, and remember the type-quals.
4587 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004588 DS.getConstSpecLoc(),
4589 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004590 DS.getRestrictSpecLoc()),
4591 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004592 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004593 else
4594 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004595 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004596 Loc),
4597 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004598 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004599 } else {
4600 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004601 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004602
Sebastian Redl3b27be62009-03-23 00:00:23 +00004603 // Complain about rvalue references in C++03, but then go on and build
4604 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004605 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004606 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004607 diag::warn_cxx98_compat_rvalue_reference :
4608 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004609
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004610 // GNU-style and C++11 attributes are allowed here, as is restrict.
4611 ParseTypeQualifierListOpt(DS);
4612 D.ExtendWithDeclSpec(DS);
4613
Bill Wendling93efb222007-06-02 23:28:54 +00004614 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4615 // cv-qualifiers are introduced through the use of a typedef or of a
4616 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004617 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4618 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4619 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004620 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004621 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4622 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004623 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004624 // 'restrict' is permitted as an extension.
4625 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4626 Diag(DS.getAtomicSpecLoc(),
4627 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004628 }
Bill Wendling3708c182007-05-27 10:15:43 +00004629
4630 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004631 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004632
Douglas Gregor66583c52008-11-03 15:51:28 +00004633 if (D.getNumTypeObjects() > 0) {
4634 // C++ [dcl.ref]p4: There shall be no references to references.
4635 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4636 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004637 if (const IdentifierInfo *II = D.getIdentifier())
4638 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4639 << II;
4640 else
4641 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4642 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004643
Sebastian Redlbd150f42008-11-21 19:14:01 +00004644 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004645 // can go ahead and build the (technically ill-formed)
4646 // declarator: reference collapsing will take care of it.
4647 }
4648 }
4649
Richard Smith8e1ac332013-03-28 01:55:44 +00004650 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004651 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004652 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004653 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004654 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004655 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004656}
4657
Richard Smith0efa75c2012-03-29 01:16:42 +00004658static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4659 SourceLocation EllipsisLoc) {
4660 if (EllipsisLoc.isValid()) {
4661 FixItHint Insertion;
4662 if (!D.getEllipsisLoc().isValid()) {
4663 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4664 D.setEllipsisLoc(EllipsisLoc);
4665 }
4666 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4667 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4668 }
4669}
4670
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004671/// ParseDirectDeclarator
4672/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004673/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004674/// '(' declarator ')'
4675/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004676/// [C90] direct-declarator '[' constant-expression[opt] ']'
4677/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4678/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4679/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4680/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004681/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4682/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004683/// direct-declarator '(' parameter-type-list ')'
4684/// direct-declarator '(' identifier-list[opt] ')'
4685/// [GNU] direct-declarator '(' parameter-forward-declarations
4686/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004687/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4688/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004689/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4690/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4691/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004692/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004693/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004694///
4695/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004696/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004697/// '::'[opt] nested-name-specifier[opt] type-name
4698///
4699/// id-expression: [C++ 5.1]
4700/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004701/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004702///
4703/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004704/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004705/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004706/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004707/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004708/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004709///
Richard Smith1453e312012-03-27 01:42:32 +00004710/// Note, any additional constructs added here may need corresponding changes
4711/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004712void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004713 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004714
David Blaikiebbafb8a2012-03-11 07:00:24 +00004715 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004716 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004717 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004718 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4719 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004720 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004721 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004722 }
4723
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004724 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004725 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004726 // Change the declaration context for name lookup, until this function
4727 // is exited (and the declarator has been parsed).
4728 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004729 }
4730
Douglas Gregor27b4c162010-12-23 22:44:42 +00004731 // C++0x [dcl.fct]p14:
4732 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004733 // of a parameter-declaration-clause without a preceding comma. In
4734 // this case, the ellipsis is parsed as part of the
4735 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004736 // parameter pack that has not been expanded; otherwise, it is parsed
4737 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004738 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004739 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004740 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004741 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004742 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004743 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004744 !Actions.containsUnexpandedParameterPacks(D))) {
4745 SourceLocation EllipsisLoc = ConsumeToken();
4746 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4747 // The ellipsis was put in the wrong place. Recover, and explain to
4748 // the user what they should have done.
4749 ParseDeclarator(D);
4750 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4751 return;
4752 } else
4753 D.setEllipsisLoc(EllipsisLoc);
4754
4755 // The ellipsis can't be followed by a parenthesized declarator. We
4756 // check for that in ParseParenDeclarator, after we have disambiguated
4757 // the l_paren token.
4758 }
4759
Douglas Gregor7861a802009-11-03 01:35:08 +00004760 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4761 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4762 // We found something that indicates the start of an unqualified-id.
4763 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004764 bool AllowConstructorName;
4765 if (D.getDeclSpec().hasTypeSpecifier())
4766 AllowConstructorName = false;
4767 else if (D.getCXXScopeSpec().isSet())
4768 AllowConstructorName =
4769 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004770 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004771 else
4772 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4773
Abramo Bagnara7945c982012-01-27 09:46:47 +00004774 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004775 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4776 /*EnteringContext=*/true,
4777 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004778 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004779 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004780 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004781 D.getName()) ||
4782 // Once we're past the identifier, if the scope was bad, mark the
4783 // whole declarator bad.
4784 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004785 D.SetIdentifier(0, Tok.getLocation());
4786 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004787 } else {
4788 // Parsed the unqualified-id; update range information and move along.
4789 if (D.getSourceRange().getBegin().isInvalid())
4790 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4791 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004792 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004793 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004794 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004795 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004796 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004797 "There's a C++-specific check for tok::identifier above");
4798 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4799 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4800 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004801 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004802 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004803 // A virt-specifier isn't treated as an identifier if it appears after a
4804 // trailing-return-type.
4805 if (D.getContext() != Declarator::TrailingReturnContext ||
4806 !isCXX11VirtSpecifier(Tok)) {
4807 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4808 << FixItHint::CreateRemoval(Tok.getLocation());
4809 D.SetIdentifier(0, Tok.getLocation());
4810 ConsumeToken();
4811 goto PastIdentifier;
4812 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004813 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004814
Douglas Gregor7861a802009-11-03 01:35:08 +00004815 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004816 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004817 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004818 // Example: 'char (*X)' or 'int (*XX)(void)'
4819 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004820
4821 // If the declarator was parenthesized, we entered the declarator
4822 // scope when parsing the parenthesized declarator, then exited
4823 // the scope already. Re-enter the scope, if we need to.
4824 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004825 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004826 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004827 if (!D.isInvalidType() &&
4828 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004829 // Change the declaration context for name lookup, until this function
4830 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004831 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004832 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004833 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004834 // This could be something simple like "int" (in which case the declarator
4835 // portion is empty), if an abstract-declarator is allowed.
4836 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004837
4838 // The grammar for abstract-pack-declarator does not allow grouping parens.
4839 // FIXME: Revisit this once core issue 1488 is resolved.
4840 if (D.hasEllipsis() && D.hasGroupingParens())
4841 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4842 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004843 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004844 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004845 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004846 if (D.getContext() == Declarator::MemberContext)
4847 Diag(Tok, diag::err_expected_member_name_or_semi)
4848 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004849 else if (getLangOpts().CPlusPlus) {
4850 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4851 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004852 else {
4853 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4854 if (Tok.isAtStartOfLine() && Loc.isValid())
4855 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4856 << getLangOpts().CPlusPlus;
4857 else
4858 Diag(Tok, diag::err_expected_unqualified_id)
4859 << getLangOpts().CPlusPlus;
4860 }
Richard Trieu9c672672013-01-26 02:31:38 +00004861 } else
Alp Tokerec543272013-12-24 09:48:30 +00004862 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_paren;
Chris Lattnereec40f92006-08-06 21:55:29 +00004863 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004864 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004865 }
Mike Stump11289f42009-09-09 15:08:12 +00004866
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004867 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004868 assert(D.isPastIdentifier() &&
4869 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004870
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004871 // Don't parse attributes unless we have parsed an unparenthesized name.
4872 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004873 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004874
Chris Lattneracd58a32006-08-06 17:24:14 +00004875 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004876 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004877 // Enter function-declaration scope, limiting any declarators to the
4878 // function prototype scope, including parameter declarators.
4879 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004880 Scope::FunctionPrototypeScope|Scope::DeclScope|
4881 (D.isFunctionDeclaratorAFunctionDeclaration()
4882 ? Scope::FunctionDeclarationScope : 0));
4883
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004884 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4885 // In such a case, check if we actually have a function declarator; if it
4886 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004887 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004888 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4889 // The name of the declarator, if any, is tentatively declared within
4890 // a possible direct initializer.
4891 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4892 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4893 TentativelyDeclaredIdentifiers.pop_back();
4894 if (!IsFunctionDecl)
4895 break;
4896 }
John McCall084e83d2011-03-24 11:26:52 +00004897 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004898 BalancedDelimiterTracker T(*this, tok::l_paren);
4899 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004900 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004901 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004902 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004903 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004904 } else {
4905 break;
4906 }
4907 }
Chad Rosierc1183952012-06-26 22:30:43 +00004908}
Chris Lattneracd58a32006-08-06 17:24:14 +00004909
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004910/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4911/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004912/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004913/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4914///
4915/// direct-declarator:
4916/// '(' declarator ')'
4917/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004918/// direct-declarator '(' parameter-type-list ')'
4919/// direct-declarator '(' identifier-list[opt] ')'
4920/// [GNU] direct-declarator '(' parameter-forward-declarations
4921/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004922///
4923void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004924 BalancedDelimiterTracker T(*this, tok::l_paren);
4925 T.consumeOpen();
4926
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004927 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004928
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004929 // Eat any attributes before we look at whether this is a grouping or function
4930 // declarator paren. If this is a grouping paren, the attribute applies to
4931 // the type being built up, for example:
4932 // int (__attribute__(()) *x)(long y)
4933 // If this ends up not being a grouping paren, the attribute applies to the
4934 // first argument, for example:
4935 // int (__attribute__(()) int x)
4936 // In either case, we need to eat any attributes to be able to determine what
4937 // sort of paren this is.
4938 //
John McCall084e83d2011-03-24 11:26:52 +00004939 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004940 bool RequiresArg = false;
4941 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004942 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004943
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004944 // We require that the argument list (if this is a non-grouping paren) be
4945 // present even if the attribute list was empty.
4946 RequiresArg = true;
4947 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004948
Steve Naroff44ac7772008-12-25 14:16:32 +00004949 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004950 ParseMicrosoftTypeAttributes(attrs);
4951
Dawn Perchik335e16b2010-09-03 01:29:35 +00004952 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004953 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004954 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004955
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004956 // If we haven't past the identifier yet (or where the identifier would be
4957 // stored, if this is an abstract declarator), then this is probably just
4958 // grouping parens. However, if this could be an abstract-declarator, then
4959 // this could also be the start of function arguments (consider 'void()').
4960 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004961
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004962 if (!D.mayOmitIdentifier()) {
4963 // If this can't be an abstract-declarator, this *must* be a grouping
4964 // paren, because we haven't seen the identifier yet.
4965 isGrouping = true;
4966 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004967 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4968 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004969 isDeclarationSpecifier() || // 'int(int)' is a function.
4970 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004971 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4972 // considered to be a type, not a K&R identifier-list.
4973 isGrouping = false;
4974 } else {
4975 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4976 isGrouping = true;
4977 }
Mike Stump11289f42009-09-09 15:08:12 +00004978
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004979 // If this is a grouping paren, handle:
4980 // direct-declarator: '(' declarator ')'
4981 // direct-declarator: '(' attributes declarator ')'
4982 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004983 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4984 D.setEllipsisLoc(SourceLocation());
4985
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004986 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004987 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004988 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004989 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004990 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004991 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004992 T.getCloseLocation()),
4993 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004994
4995 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004996
4997 // An ellipsis cannot be placed outside parentheses.
4998 if (EllipsisLoc.isValid())
4999 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
5000
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005001 return;
5002 }
Mike Stump11289f42009-09-09 15:08:12 +00005003
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005004 // Okay, if this wasn't a grouping paren, it must be the start of a function
5005 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005006 // identifier (and remember where it would have been), then call into
5007 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005008 D.SetIdentifier(0, Tok.getLocation());
5009
David Blaikie15a430a2011-12-04 05:04:18 +00005010 // Enter function-declaration scope, limiting any declarators to the
5011 // function prototype scope, including parameter declarators.
5012 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005013 Scope::FunctionPrototypeScope | Scope::DeclScope |
5014 (D.isFunctionDeclaratorAFunctionDeclaration()
5015 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00005016 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00005017 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005018}
5019
5020/// ParseFunctionDeclarator - We are after the identifier and have parsed the
5021/// declarator D up to a paren, which indicates that we are parsing function
5022/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00005023///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005024/// If FirstArgAttrs is non-null, then the caller parsed those arguments
5025/// immediately after the open paren - they should be considered to be the
5026/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005027///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005028/// If RequiresArg is true, then the first argument of the function is required
5029/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005030///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005031/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5032/// (C++11) ref-qualifier[opt], exception-specification[opt],
5033/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5034///
5035/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00005036/// dynamic-exception-specification
5037/// noexcept-specification
5038///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005039void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005040 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005041 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00005042 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005043 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00005044 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00005045 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00005046 // lparen is already consumed!
5047 assert(D.isPastIdentifier() && "Should not call before identifier!");
5048
5049 // This should be true when the function has typed arguments.
5050 // Otherwise, it is treated as a K&R-style function.
5051 bool HasProto = false;
5052 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005053 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005054 // Remember where we see an ellipsis, if any.
5055 SourceLocation EllipsisLoc;
5056
5057 DeclSpec DS(AttrFactory);
5058 bool RefQualifierIsLValueRef = true;
5059 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00005060 SourceLocation ConstQualifierLoc;
5061 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005062 ExceptionSpecificationType ESpecType = EST_None;
5063 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005064 SmallVector<ParsedType, 2> DynamicExceptions;
5065 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005066 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005067 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00005068 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005069
James Molloy6f8780b2012-02-29 10:24:19 +00005070 Actions.ActOnStartFunctionDeclarator();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005071 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5072 EndLoc is the end location for the function declarator.
5073 They differ for trailing return types. */
5074 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005075 SourceLocation LParenLoc, RParenLoc;
5076 LParenLoc = Tracker.getOpenLocation();
5077 StartLoc = LParenLoc;
5078
Douglas Gregor9e66af42011-07-05 16:44:18 +00005079 if (isFunctionDeclaratorIdentifierList()) {
5080 if (RequiresArg)
5081 Diag(Tok, diag::err_argument_required_after_attribute);
5082
5083 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5084
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005085 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005086 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005087 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005088 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005089 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005090 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005091 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5092 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005093 else if (RequiresArg)
5094 Diag(Tok, diag::err_argument_required_after_attribute);
5095
David Blaikiebbafb8a2012-03-11 07:00:24 +00005096 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005097
5098 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005099 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005100 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005101 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005102 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005103
David Blaikiebbafb8a2012-03-11 07:00:24 +00005104 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005105 // FIXME: Accept these components in any order, and produce fixits to
5106 // correct the order if the user gets it wrong. Ideally we should deal
5107 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005108
5109 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005110 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5111 /*CXX11AttributesAllowed*/ false,
5112 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005113 if (!DS.getSourceRange().getEnd().isInvalid()) {
5114 EndLoc = DS.getSourceRange().getEnd();
5115 ConstQualifierLoc = DS.getConstSpecLoc();
5116 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5117 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005118
5119 // Parse ref-qualifier[opt].
5120 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005121 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005122 diag::warn_cxx98_compat_ref_qualifier :
5123 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005124
Douglas Gregor9e66af42011-07-05 16:44:18 +00005125 RefQualifierIsLValueRef = Tok.is(tok::amp);
5126 RefQualifierLoc = ConsumeToken();
5127 EndLoc = RefQualifierLoc;
5128 }
5129
Douglas Gregor3024f072012-04-16 07:05:22 +00005130 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005131 // If a declaration declares a member function or member function
5132 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005133 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005134 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005135 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005136 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005137 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005138 getLangOpts().CPlusPlus11 &&
Richard Smith990a6922014-01-17 21:01:18 +00005139 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005140 (D.getContext() == Declarator::MemberContext
5141 ? !D.getDeclSpec().isFriendSpecified()
5142 : D.getContext() == Declarator::FileContext &&
5143 D.getCXXScopeSpec().isValid() &&
5144 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005145 Sema::CXXThisScopeRAII ThisScope(Actions,
5146 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005147 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005148 (D.getDeclSpec().isConstexprSpecified() &&
5149 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005150 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005151 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005152
Douglas Gregor9e66af42011-07-05 16:44:18 +00005153 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005154 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005155 DynamicExceptions,
5156 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005157 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005158 if (ESpecType != EST_None)
5159 EndLoc = ESpecRange.getEnd();
5160
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005161 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5162 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005163 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005164
Douglas Gregor9e66af42011-07-05 16:44:18 +00005165 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005166 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005167 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005168 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005169 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5170 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005171 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005172 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005173 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005174 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005175 }
5176 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005177 }
5178
5179 // Remember that we parsed a function type, and remember the attributes.
5180 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005181 IsAmbiguous,
5182 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005183 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005184 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005185 DS.getTypeQualifiers(),
5186 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005187 RefQualifierLoc, ConstQualifierLoc,
5188 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005189 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005190 ESpecType, ESpecRange.getBegin(),
5191 DynamicExceptions.data(),
5192 DynamicExceptionRanges.data(),
5193 DynamicExceptions.size(),
5194 NoexceptExpr.isUsable() ?
5195 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005196 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005197 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005198 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005199
5200 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005201}
5202
5203/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5204/// identifier list form for a K&R-style function: void foo(a,b,c)
5205///
5206/// Note that identifier-lists are only allowed for normal declarators, not for
5207/// abstract-declarators.
5208bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005209 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005210 && Tok.is(tok::identifier)
5211 && !TryAltiVecVectorToken()
5212 // K&R identifier lists can't have typedefs as identifiers, per C99
5213 // 6.7.5.3p11.
5214 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5215 // Identifier lists follow a really simple grammar: the identifiers can
5216 // be followed *only* by a ", identifier" or ")". However, K&R
5217 // identifier lists are really rare in the brave new modern world, and
5218 // it is very common for someone to typo a type in a non-K&R style
5219 // list. If we are presented with something like: "void foo(intptr x,
5220 // float y)", we don't want to start parsing the function declarator as
5221 // though it is a K&R style declarator just because intptr is an
5222 // invalid type.
5223 //
5224 // To handle this, we check to see if the token after the first
5225 // identifier is a "," or ")". Only then do we parse it as an
5226 // identifier list.
5227 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5228}
5229
5230/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5231/// we found a K&R-style identifier list instead of a typed parameter list.
5232///
5233/// After returning, ParamInfo will hold the parsed parameters.
5234///
5235/// identifier-list: [C99 6.7.5]
5236/// identifier
5237/// identifier-list ',' identifier
5238///
5239void Parser::ParseFunctionDeclaratorIdentifierList(
5240 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005241 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005242 // If there was no identifier specified for the declarator, either we are in
5243 // an abstract-declarator, or we are in a parameter declarator which was found
5244 // to be abstract. In abstract-declarators, identifier lists are not valid:
5245 // diagnose this.
5246 if (!D.getIdentifier())
5247 Diag(Tok, diag::ext_ident_list_in_param);
5248
5249 // Maintain an efficient lookup of params we have seen so far.
5250 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5251
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005252 do {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005253 // If this isn't an identifier, report the error and skip until ')'.
5254 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00005255 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00005256 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005257 // Forget we parsed anything.
5258 ParamInfo.clear();
5259 return;
5260 }
5261
5262 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5263
5264 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5265 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5266 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5267
5268 // Verify that the argument identifier has not already been mentioned.
5269 if (!ParamsSoFar.insert(ParmII)) {
5270 Diag(Tok, diag::err_param_redefinition) << ParmII;
5271 } else {
5272 // Remember this identifier in ParamInfo.
5273 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5274 Tok.getLocation(),
5275 0));
5276 }
5277
5278 // Eat the identifier.
5279 ConsumeToken();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005280 // The list continues if we see a comma.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005281 } while (TryConsumeToken(tok::comma));
Douglas Gregor9e66af42011-07-05 16:44:18 +00005282}
5283
5284/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5285/// after the opening parenthesis. This function will not parse a K&R-style
5286/// identifier list.
5287///
Richard Smith2620cd92012-04-11 04:01:28 +00005288/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5289/// caller parsed those arguments immediately after the open paren - they should
5290/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005291///
5292/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5293/// be the location of the ellipsis, if any was parsed.
5294///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005295/// parameter-type-list: [C99 6.7.5]
5296/// parameter-list
5297/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005298/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005299///
5300/// parameter-list: [C99 6.7.5]
5301/// parameter-declaration
5302/// parameter-list ',' parameter-declaration
5303///
5304/// parameter-declaration: [C99 6.7.5]
5305/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005306/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005307/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005308/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005309/// declaration-specifiers abstract-declarator[opt]
5310/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005311/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005312/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005313/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005314///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005315void Parser::ParseParameterDeclarationClause(
5316 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005317 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005318 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005319 SourceLocation &EllipsisLoc) {
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005320 do {
5321 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5322 // before deciding this was a parameter-declaration-clause.
5323 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Chris Lattner371ed4e2008-04-06 06:57:35 +00005324 break;
Mike Stump11289f42009-09-09 15:08:12 +00005325
Chris Lattner371ed4e2008-04-06 06:57:35 +00005326 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005327 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005328 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005329
Richard Smith2620cd92012-04-11 04:01:28 +00005330 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005331 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005332
John McCall53fa7142010-12-24 02:08:15 +00005333 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005334 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005335
5336 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005337
5338 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005339 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005340 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005341 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5342 // too much hassle.
5343 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005344
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005345 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005346
Faisal Vali2b391ab2013-09-26 19:54:12 +00005347
5348 // Parse the declarator. This is "PrototypeContext" or
5349 // "LambdaExprParameterContext", because we must accept either
5350 // 'declarator' or 'abstract-declarator' here.
5351 Declarator ParmDeclarator(DS,
5352 D.getContext() == Declarator::LambdaExprContext ?
5353 Declarator::LambdaExprParameterContext :
5354 Declarator::PrototypeContext);
5355 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005356
5357 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005358 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005359
Chris Lattner371ed4e2008-04-06 06:57:35 +00005360 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005361 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005362
Douglas Gregor4d87df52008-12-16 21:30:33 +00005363 // DefArgToks is used when the parsing of default arguments needs
5364 // to be delayed.
5365 CachedTokens *DefArgToks = 0;
5366
Chris Lattner371ed4e2008-04-06 06:57:35 +00005367 // If no parameter was specified, verify that *something* was specified,
5368 // otherwise we have a missing type and identifier.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005369 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5370 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005371 // Completely missing, emit error.
5372 Diag(DSStart, diag::err_missing_param);
5373 } else {
5374 // Otherwise, we have something. Add it and let semantic analysis try
5375 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005376
Chris Lattner371ed4e2008-04-06 06:57:35 +00005377 // Inform the actions module about the parameter declarator, so it gets
5378 // added to the current scope.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005379 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5380 ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005381 // Parse the default argument, if any. We parse the default
5382 // arguments in all dialects; the semantic analysis in
5383 // ActOnParamDefaultArgument will reject the default argument in
5384 // C.
5385 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005386 SourceLocation EqualLoc = Tok.getLocation();
5387
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005388 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005389 if (D.getContext() == Declarator::MemberContext) {
5390 // If we're inside a class definition, cache the tokens
5391 // corresponding to the default argument. We'll actually parse
5392 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005393 // FIXME: Can we use a smart pointer for Toks?
5394 DefArgToks = new CachedTokens;
5395
Richard Smith1fff95c2013-09-12 23:28:08 +00005396 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005397 delete DefArgToks;
5398 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005399 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005400 } else {
5401 // Mark the end of the default argument so that we know when to
5402 // stop when we parse it later on.
5403 Token DefArgEnd;
5404 DefArgEnd.startToken();
5405 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5406 DefArgEnd.setLocation(Tok.getLocation());
5407 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005408 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005409 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005410 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005411 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005412 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005413 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005414
Chad Rosierc1183952012-06-26 22:30:43 +00005415 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005416 // used.
5417 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005418 Sema::PotentiallyEvaluatedIfUsed,
5419 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005420
Sebastian Redldb63af22012-03-14 15:54:00 +00005421 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005422 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005423 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005424 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005425 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005426 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005427 if (DefArgResult.isInvalid()) {
5428 Actions.ActOnParamDefaultArgumentError(Param);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005429 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00005430 } else {
5431 // Inform the actions module about the default argument
5432 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005433 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005434 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005435 }
5436 }
Mike Stump11289f42009-09-09 15:08:12 +00005437
5438 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005439 ParmDeclarator.getIdentifierLoc(),
5440 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005441 }
5442
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005443 if (TryConsumeToken(tok::ellipsis, EllipsisLoc) &&
5444 !getLangOpts().CPlusPlus) {
5445 // We have ellipsis without a preceding ',', which is ill-formed
5446 // in C. Complain and provide the fix.
5447 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
5448 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005449 break;
5450 }
Mike Stump11289f42009-09-09 15:08:12 +00005451
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005452 // If the next token is a comma, consume it and keep reading arguments.
5453 } while (TryConsumeToken(tok::comma));
Chris Lattner6c940e62008-04-06 06:34:08 +00005454}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005455
Chris Lattnere8074e62006-08-06 18:30:15 +00005456/// [C90] direct-declarator '[' constant-expression[opt] ']'
5457/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5458/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5459/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5460/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005461/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5462/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005463void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005464 if (CheckProhibitedCXX11Attribute())
5465 return;
5466
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005467 BalancedDelimiterTracker T(*this, tok::l_square);
5468 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005469
Chris Lattner84a11622008-12-18 07:27:21 +00005470 // C array syntax has many features, but by-far the most common is [] and [4].
5471 // This code does a fast path to handle some of the most obvious cases.
5472 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005473 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005474 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005475 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005476
Chris Lattner84a11622008-12-18 07:27:21 +00005477 // Remember that we parsed the empty array type.
John McCall084e83d2011-03-24 11:26:52 +00005478 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005479 T.getOpenLocation(),
5480 T.getCloseLocation()),
5481 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005482 return;
5483 } else if (Tok.getKind() == tok::numeric_constant &&
5484 GetLookAheadToken(1).is(tok::r_square)) {
5485 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005486 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005487 ConsumeToken();
5488
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005489 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005490 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005491 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005492
Chris Lattner84a11622008-12-18 07:27:21 +00005493 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005494 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005495 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005496 T.getOpenLocation(),
5497 T.getCloseLocation()),
5498 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005499 return;
5500 }
Mike Stump11289f42009-09-09 15:08:12 +00005501
Chris Lattnere8074e62006-08-06 18:30:15 +00005502 // If valid, this location is the position where we read the 'static' keyword.
5503 SourceLocation StaticLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005504 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005505
Chris Lattnere8074e62006-08-06 18:30:15 +00005506 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005507 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005508 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005509 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005510
Chris Lattnere8074e62006-08-06 18:30:15 +00005511 // If we haven't already read 'static', check to see if there is one after the
5512 // type-qualifier-list.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005513 if (!StaticLoc.isValid())
5514 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005515
Chris Lattnere8074e62006-08-06 18:30:15 +00005516 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005517 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005518 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005519
Chris Lattner521ff2b2008-04-06 05:26:30 +00005520 // Handle the case where we have '[*]' as the array size. However, a leading
5521 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005522 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005523 // infrequent, use of lookahead is not costly here.
5524 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005525 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005526
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005527 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005528 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005529 StaticLoc = SourceLocation(); // Drop the static.
5530 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005531 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005532 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005533 // Note, in C89, this production uses the constant-expr production instead
5534 // of assignment-expr. The only difference is that assignment-expr allows
5535 // things like '=' and '*='. Sema rejects these in C89 mode because they
5536 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005537
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005538 // Parse the constant-expression or assignment-expression now (depending
5539 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005540 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005541 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005542 } else {
5543 EnterExpressionEvaluationContext Unevaluated(Actions,
5544 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005545 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005546 }
Chris Lattner62591722006-08-12 18:40:58 +00005547 }
Mike Stump11289f42009-09-09 15:08:12 +00005548
Chris Lattner62591722006-08-12 18:40:58 +00005549 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005550 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005551 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005552 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00005553 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00005554 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005555 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005556
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005557 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005558
John McCall084e83d2011-03-24 11:26:52 +00005559 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005560 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005561
Chris Lattner84a11622008-12-18 07:27:21 +00005562 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005563 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005564 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005565 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005566 T.getOpenLocation(),
5567 T.getCloseLocation()),
5568 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005569}
5570
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005571/// [GNU] typeof-specifier:
5572/// typeof ( expressions )
5573/// typeof ( type-name )
5574/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005575///
5576void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005577 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005578 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005579 SourceLocation StartLoc = ConsumeToken();
5580
John McCalle8595032010-01-13 20:03:27 +00005581 const bool hasParens = Tok.is(tok::l_paren);
5582
Eli Friedman15681d62012-09-26 04:34:21 +00005583 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5584 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005585
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005586 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005587 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005588 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005589 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5590 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005591 if (hasParens)
5592 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005593
5594 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005595 // FIXME: Not accurate, the range gets one token more than it should.
5596 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005597 else
5598 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005599
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005600 if (isCastExpr) {
5601 if (!CastTy) {
5602 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005603 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005604 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005605
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005606 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005607 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005608 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5609 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005610 DiagID, CastTy,
5611 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00005612 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005613 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005614 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005615
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005616 // If we get here, the operand to the typeof was an expresion.
5617 if (Operand.isInvalid()) {
5618 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005619 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005620 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005621
Eli Friedmane0afc982012-01-21 01:01:51 +00005622 // We might need to transform the operand if it is potentially evaluated.
5623 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5624 if (Operand.isInvalid()) {
5625 DS.SetTypeSpecError();
5626 return;
5627 }
5628
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005629 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005630 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005631 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5632 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005633 DiagID, Operand.get(),
5634 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00005635 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005636}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005637
Benjamin Kramere56f3932011-12-23 17:00:35 +00005638/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005639/// _Atomic ( type-name )
5640///
5641void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005642 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5643 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005644
5645 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005646 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005647 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005648 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005649
5650 TypeResult Result = ParseTypeName();
5651 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00005652 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00005653 return;
5654 }
5655
5656 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005657 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005658
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005659 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005660 return;
5661
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005662 DS.setTypeofParensRange(T.getRange());
5663 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005664
5665 const char *PrevSpec = 0;
5666 unsigned DiagID;
5667 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005668 DiagID, Result.release(),
5669 Actions.getASTContext().getPrintingPolicy()))
Eli Friedman0dfb8892011-10-06 23:00:33 +00005670 Diag(StartLoc, DiagID) << PrevSpec;
5671}
5672
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005673
5674/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5675/// from TryAltiVecVectorToken.
5676bool Parser::TryAltiVecVectorTokenOutOfLine() {
5677 Token Next = NextToken();
5678 switch (Next.getKind()) {
5679 default: return false;
5680 case tok::kw_short:
5681 case tok::kw_long:
5682 case tok::kw_signed:
5683 case tok::kw_unsigned:
5684 case tok::kw_void:
5685 case tok::kw_char:
5686 case tok::kw_int:
5687 case tok::kw_float:
5688 case tok::kw_double:
5689 case tok::kw_bool:
5690 case tok::kw___pixel:
5691 Tok.setKind(tok::kw___vector);
5692 return true;
5693 case tok::identifier:
5694 if (Next.getIdentifierInfo() == Ident_pixel) {
5695 Tok.setKind(tok::kw___vector);
5696 return true;
5697 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005698 if (Next.getIdentifierInfo() == Ident_bool) {
5699 Tok.setKind(tok::kw___vector);
5700 return true;
5701 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005702 return false;
5703 }
5704}
5705
5706bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5707 const char *&PrevSpec, unsigned &DiagID,
5708 bool &isInvalid) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005709 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005710 if (Tok.getIdentifierInfo() == Ident_vector) {
5711 Token Next = NextToken();
5712 switch (Next.getKind()) {
5713 case tok::kw_short:
5714 case tok::kw_long:
5715 case tok::kw_signed:
5716 case tok::kw_unsigned:
5717 case tok::kw_void:
5718 case tok::kw_char:
5719 case tok::kw_int:
5720 case tok::kw_float:
5721 case tok::kw_double:
5722 case tok::kw_bool:
5723 case tok::kw___pixel:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005724 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005725 return true;
5726 case tok::identifier:
5727 if (Next.getIdentifierInfo() == Ident_pixel) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005728 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005729 return true;
5730 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005731 if (Next.getIdentifierInfo() == Ident_bool) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005732 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
Bill Schmidt99a084b2013-07-03 20:54:09 +00005733 return true;
5734 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005735 break;
5736 default:
5737 break;
5738 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005739 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005740 DS.isTypeAltiVecVector()) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005741 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005742 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005743 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5744 DS.isTypeAltiVecVector()) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005745 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
Bill Schmidt99a084b2013-07-03 20:54:09 +00005746 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005747 }
5748 return false;
5749}