blob: d3e76c5e92d78ff434073511a1faf9764d522d41 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Larisse Voufo725de3e2013-06-21 00:08:46 +000016#include "clang/AST/DeclTemplate.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000017#include "clang/Basic/AddressSpaces.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000018#include "clang/Basic/CharInfo.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000019#include "clang/Basic/OpenCL.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000021#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Sema/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000025#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000026#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000027#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// C99 6.7: Declarations.
32//===----------------------------------------------------------------------===//
33
Chris Lattnerf5fbd792006-08-10 23:56:11 +000034/// ParseTypeName
35/// type-name: [C99 6.7.6]
36/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000037///
38/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000039TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000040 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000041 AccessSpecifier AS,
Richard Smith54ecd982013-02-20 19:22:51 +000042 Decl **OwnedType,
43 ParsedAttributes *Attrs) {
Richard Smith62dad822012-03-15 01:02:11 +000044 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000045 if (DSC == DSC_normal)
46 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000047
Chris Lattnerf5fbd792006-08-10 23:56:11 +000048 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000049 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +000050 if (Attrs)
51 DS.addAttributes(Attrs->getList());
Richard Smithbfdb1082012-03-12 08:56:40 +000052 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000053 if (OwnedType)
54 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000055
Chris Lattnerf5fbd792006-08-10 23:56:11 +000056 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000057 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000058 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000059 if (Range)
60 *Range = DeclaratorInfo.getSourceRange();
61
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000062 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000063 return true;
64
Douglas Gregor0be31a22010-07-02 17:43:08 +000065 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000066}
67
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000068
69/// isAttributeLateParsed - Return true if the attribute has arguments that
70/// require late parsing.
71static bool isAttributeLateParsed(const IdentifierInfo &II) {
72 return llvm::StringSwitch<bool>(II.getName())
73#include "clang/Parse/AttrLateParsed.inc"
74 .Default(false);
75}
76
Alexis Hunt96d5c762009-11-21 08:43:09 +000077/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000078///
79/// [GNU] attributes:
80/// attribute
81/// attributes attribute
82///
83/// [GNU] attribute:
84/// '__attribute__' '(' '(' attribute-list ')' ')'
85///
86/// [GNU] attribute-list:
87/// attrib
88/// attribute_list ',' attrib
89///
90/// [GNU] attrib:
91/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000092/// attrib-name
93/// attrib-name '(' identifier ')'
94/// attrib-name '(' identifier ',' nonempty-expr-list ')'
95/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000096///
Steve Naroff0f2fe172007-06-01 17:11:19 +000097/// [GNU] attrib-name:
98/// identifier
99/// typespec
100/// typequal
101/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +0000102///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000103/// Whether an attribute takes an 'identifier' is determined by the
104/// attrib-name. GCC's behavior here is not worth imitating:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000106/// * In C mode, if the attribute argument list starts with an identifier
107/// followed by a ',' or an ')', and the identifier doesn't resolve to
108/// a type, it is parsed as an identifier. If the attribute actually
109/// wanted an expression, it's out of luck (but it turns out that no
110/// attributes work that way, because C constant expressions are very
111/// limited).
112/// * In C++ mode, if the attribute argument list starts with an identifier,
113/// and the attribute *wants* an identifier, it is parsed as an identifier.
114/// At block scope, any additional tokens between the identifier and the
115/// ',' or ')' are ignored, otherwise they produce a parse error.
Richard Smithb12bf692011-10-17 21:20:17 +0000116///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000117/// We follow the C++ model, but don't allow junk after the identifier.
John McCall53fa7142010-12-24 02:08:15 +0000118void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000119 SourceLocation *endLoc,
120 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000121 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000122
Chris Lattner76c72282007-10-09 17:33:22 +0000123 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000124 ConsumeToken();
125 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
126 "attribute")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000127 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000128 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000129 }
130 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000131 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000132 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000133 }
134 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Alp Toker094e5212014-01-05 03:27:11 +0000135 while (true) {
136 // Allow empty/non-empty attributes. ((__vector_size__(16),,,,))
137 if (TryConsumeToken(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000138 continue;
Alp Toker094e5212014-01-05 03:27:11 +0000139
140 // Expect an identifier or declaration specifier (const, int, etc.)
141 if (Tok.isNot(tok::identifier) && !isDeclarationSpecifier())
142 break;
143
Steve Naroff0f2fe172007-06-01 17:11:19 +0000144 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
145 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000146
Alp Toker094e5212014-01-05 03:27:11 +0000147 if (Tok.isNot(tok::l_paren)) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000148 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
149 AttributeList::AS_GNU);
Alp Toker094e5212014-01-05 03:27:11 +0000150 continue;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000151 }
Alp Toker094e5212014-01-05 03:27:11 +0000152
153 // Handle "parameterized" attributes
154 if (!LateAttrs || !isAttributeLateParsed(*AttrName)) {
155 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, 0,
156 SourceLocation(), AttributeList::AS_GNU);
157 continue;
158 }
159
160 // Handle attributes with arguments that require late parsing.
161 LateParsedAttribute *LA =
162 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
163 LateAttrs->push_back(LA);
164
165 // Attributes in a class are parsed at the end of the class, along
166 // with other late-parsed declarations.
167 if (!ClassStack.empty() && !LateAttrs->parseSoon())
168 getCurrentClass().LateParsedDeclarations.push_back(LA);
169
170 // consume everything up to and including the matching right parens
171 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
172
173 Token Eof;
174 Eof.startToken();
175 Eof.setLocation(Tok.getLocation());
176 LA->Toks.push_back(Eof);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000177 }
Alp Toker094e5212014-01-05 03:27:11 +0000178
Alp Toker383d2c42014-01-01 03:08:43 +0000179 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000180 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000181 SourceLocation Loc = Tok.getLocation();
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);
John McCall53fa7142010-12-24 02:08:15 +0000184 if (endLoc)
185 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000186 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000187}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000188
Aaron Ballman4768b312013-11-04 12:55:56 +0000189/// \brief Normalizes an attribute name by dropping prefixed and suffixed __.
190static StringRef normalizeAttrName(StringRef Name) {
Richard Smith66e71682013-10-24 01:07:54 +0000191 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
192 Name = Name.drop_front(2).drop_back(2);
Aaron Ballman4768b312013-11-04 12:55:56 +0000193 return Name;
194}
195
196/// \brief Determine whether the given attribute has an identifier argument.
197static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
198 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Richard Smith66e71682013-10-24 01:07:54 +0000199#include "clang/Parse/AttrIdentifierArg.inc"
Douglas Gregord2472d42013-05-02 23:25:32 +0000200 .Default(false);
201}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000202
Aaron Ballman4768b312013-11-04 12:55:56 +0000203/// \brief Determine whether the given attribute parses a type argument.
204static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
205 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
206#include "clang/Parse/AttrTypeArg.inc"
207 .Default(false);
208}
209
Aaron Ballman15b27b92014-01-09 19:39:35 +0000210/// \brief Determine whether the given attribute requires parsing its arguments
211/// in an unevaluated context or not.
212static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) {
213 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
214#include "clang/Parse/AttrArgContext.inc"
215 .Default(false);
216}
217
Richard Smithfeefaf52013-09-03 18:01:40 +0000218IdentifierLoc *Parser::ParseIdentifierLoc() {
219 assert(Tok.is(tok::identifier) && "expected an identifier");
220 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
221 Tok.getLocation(),
222 Tok.getIdentifierInfo());
223 ConsumeToken();
224 return IL;
225}
226
Richard Smithb1f9a282013-10-31 01:56:18 +0000227void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
228 SourceLocation AttrNameLoc,
229 ParsedAttributes &Attrs,
230 SourceLocation *EndLoc) {
231 BalancedDelimiterTracker Parens(*this, tok::l_paren);
232 Parens.consumeOpen();
233
234 TypeResult T;
235 if (Tok.isNot(tok::r_paren))
236 T = ParseTypeName();
237
238 if (Parens.consumeClose())
239 return;
240
241 if (T.isInvalid())
242 return;
243
244 if (T.isUsable())
245 Attrs.addNewTypeAttr(&AttrName,
246 SourceRange(AttrNameLoc, Parens.getCloseLocation()), 0,
247 AttrNameLoc, T.get(), AttributeList::AS_GNU);
248 else
249 Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
250 0, AttrNameLoc, 0, 0, AttributeList::AS_GNU);
251}
252
Michael Han23214e52012-10-03 01:56:22 +0000253/// Parse the arguments to a parameterized GNU attribute or
254/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000255void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
256 SourceLocation AttrNameLoc,
257 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000258 SourceLocation *EndLoc,
259 IdentifierInfo *ScopeName,
260 SourceLocation ScopeLoc,
261 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000262
263 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
264
Richard Smith66e71682013-10-24 01:07:54 +0000265 AttributeList::Kind AttrKind =
Richard Smithb1f9a282013-10-31 01:56:18 +0000266 AttributeList::getKind(AttrName, ScopeName, Syntax);
Richard Smith66e71682013-10-24 01:07:54 +0000267
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000268 // Availability attributes have their own grammar.
Richard Smithb1f9a282013-10-31 01:56:18 +0000269 // FIXME: All these cases fail to pass in the syntax and scope, and might be
270 // written as C++11 gnu:: attributes.
Richard Smith66e71682013-10-24 01:07:54 +0000271 if (AttrKind == AttributeList::AT_Availability) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000272 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
273 return;
274 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000275
276 if (AttrKind == AttributeList::AT_ObjCBridgeRelated) {
277 ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
278 return;
279 }
280
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000281 // Type safety attributes have their own grammar.
Richard Smith66e71682013-10-24 01:07:54 +0000282 if (AttrKind == AttributeList::AT_TypeTagForDatatype) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000283 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
284 return;
285 }
Aaron Ballman4768b312013-11-04 12:55:56 +0000286 // Some attributes expect solely a type parameter.
287 if (attributeIsTypeArgAttr(*AttrName)) {
Richard Smithb1f9a282013-10-31 01:56:18 +0000288 ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc);
289 return;
290 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000291
Richard Smith66e71682013-10-24 01:07:54 +0000292 // Ignore the left paren location for now.
293 ConsumeParen();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000294
Aaron Ballman00e99962013-08-31 01:11:41 +0000295 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000296
Richard Smithb1f9a282013-10-31 01:56:18 +0000297 if (Tok.is(tok::identifier)) {
Richard Smith66e71682013-10-24 01:07:54 +0000298 // If this attribute wants an 'identifier' argument, make it so.
Richard Smithb1f9a282013-10-31 01:56:18 +0000299 bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName);
Richard Smith66e71682013-10-24 01:07:54 +0000300
301 // If we don't know how to parse this attribute, but this is the only
302 // token in this argument, assume it's meant to be an identifier.
Aaron Ballman66037472013-12-04 15:32:26 +0000303 if (AttrKind == AttributeList::UnknownAttribute ||
304 AttrKind == AttributeList::IgnoredAttribute) {
Richard Smith66e71682013-10-24 01:07:54 +0000305 const Token &Next = NextToken();
Richard Smithb1f9a282013-10-31 01:56:18 +0000306 IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma);
Richard Smith66e71682013-10-24 01:07:54 +0000307 }
Richard Smithb12bf692011-10-17 21:20:17 +0000308
Richard Smithb1f9a282013-10-31 01:56:18 +0000309 if (IsIdentifierArg)
310 ArgExprs.push_back(ParseIdentifierLoc());
Richard Smithb12bf692011-10-17 21:20:17 +0000311 }
312
Richard Smithb1f9a282013-10-31 01:56:18 +0000313 if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
Richard Smithb12bf692011-10-17 21:20:17 +0000314 // Eat the comma.
Aaron Ballman00e99962013-08-31 01:11:41 +0000315 if (!ArgExprs.empty())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000316 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000317
Richard Smithb12bf692011-10-17 21:20:17 +0000318 // Parse the non-empty comma-separated list of expressions.
Alp Toker8fbec672013-12-17 23:29:36 +0000319 do {
Aaron Ballman7c1fcf82014-01-09 20:12:12 +0000320 OwningPtr<EnterExpressionEvaluationContext> Unevaluated;
321 if (attributeParsedArgsUnevaluated(*AttrName))
322 Unevaluated.reset(new EnterExpressionEvaluationContext(Actions,
323 Sema::Unevaluated));
324
Richard Smithb12bf692011-10-17 21:20:17 +0000325 ExprResult ArgExpr(ParseAssignmentExpression());
326 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000327 SkipUntil(tok::r_paren, StopAtSemi);
Richard Smithb12bf692011-10-17 21:20:17 +0000328 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000329 }
Richard Smithb12bf692011-10-17 21:20:17 +0000330 ArgExprs.push_back(ArgExpr.release());
Alp Toker8fbec672013-12-17 23:29:36 +0000331 // Eat the comma, move to the next argument
332 } while (TryConsumeToken(tok::comma));
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000333 }
Richard Smithb12bf692011-10-17 21:20:17 +0000334
335 SourceLocation RParen = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000336 if (!ExpectAndConsume(tok::r_paren)) {
Michael Han360d2252012-10-04 16:42:52 +0000337 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithb1f9a282013-10-31 01:56:18 +0000338 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
339 ArgExprs.data(), ArgExprs.size(), Syntax);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000340 }
Aaron Ballman7c1fcf82014-01-09 20:12:12 +0000341
342 if (EndLoc)
343 *EndLoc = RParen;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000344}
345
Chad Rosierc1183952012-06-26 22:30:43 +0000346/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000347/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000348void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000349 SourceLocation AttrNameLoc,
350 ParsedAttributes &Attrs)
351{
352 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000353 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000354 AttrName->getNameStart(), tok::r_paren))
355 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000356
Aaron Ballman478faed2012-06-19 22:09:27 +0000357 ExprResult ArgExpr(ParseConstantExpression());
358 if (ArgExpr.isInvalid()) {
359 T.skipToEnd();
360 return;
361 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000362 ArgsUnion ExprList = ArgExpr.take();
363 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
364 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000365
366 T.consumeClose();
367}
368
Chad Rosierc1183952012-06-26 22:30:43 +0000369/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000370/// arguments.
371bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
372 return llvm::StringSwitch<bool>(Ident->getName())
373 .Case("dllimport", true)
374 .Case("dllexport", true)
375 .Case("noreturn", true)
376 .Case("nothrow", true)
377 .Case("noinline", true)
378 .Case("naked", true)
379 .Case("appdomain", true)
380 .Case("process", true)
381 .Case("jitintrinsic", true)
382 .Case("noalias", true)
383 .Case("restrict", true)
384 .Case("novtable", true)
385 .Case("selectany", true)
386 .Case("thread", true)
Aaron Ballman444eb6e2013-05-04 16:58:37 +0000387 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000388 .Default(false);
389}
390
Chad Rosierc1183952012-06-26 22:30:43 +0000391/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000392/// parameters). Will return false if we properly handled the declspec, or
393/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000394void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000395 SourceLocation Loc,
396 ParsedAttributes &Attrs) {
397 // Try to handle the easy case first -- these declspecs all take a single
398 // parameter as their argument.
399 if (llvm::StringSwitch<bool>(Ident->getName())
400 .Case("uuid", true)
401 .Case("align", true)
402 .Case("allocate", true)
403 .Default(false)) {
404 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
405 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000406 // The deprecated declspec has an optional single argument, so we will
407 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000408 // not.
409 if (Tok.getKind() == tok::l_paren)
410 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
411 else
Aaron Ballman00e99962013-08-31 01:11:41 +0000412 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000413 } else if (Ident->getName() == "property") {
414 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000415 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000416 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000417 if (Tok.isNot(tok::l_paren)) {
418 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
419 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000420 return;
John McCall5e77d762013-04-16 07:28:30 +0000421 }
422 BalancedDelimiterTracker T(*this, tok::l_paren);
423 T.expectAndConsume(diag::err_expected_lparen_after,
424 Ident->getNameStart(), tok::r_paren);
425
426 enum AccessorKind {
427 AK_Invalid = -1,
428 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
429 };
430 IdentifierInfo *AccessorNames[] = { 0, 0 };
431 bool HasInvalidAccessor = false;
432
433 // Parse the accessor specifications.
434 while (true) {
435 // Stop if this doesn't look like an accessor spec.
436 if (!Tok.is(tok::identifier)) {
437 // If the user wrote a completely empty list, use a special diagnostic.
438 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
439 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
440 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
441 break;
442 }
443
444 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
445 break;
446 }
447
448 AccessorKind Kind;
449 SourceLocation KindLoc = Tok.getLocation();
450 StringRef KindStr = Tok.getIdentifierInfo()->getName();
451 if (KindStr == "get") {
452 Kind = AK_Get;
453 } else if (KindStr == "put") {
454 Kind = AK_Put;
455
456 // Recover from the common mistake of using 'set' instead of 'put'.
457 } else if (KindStr == "set") {
458 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
459 << FixItHint::CreateReplacement(KindLoc, "put");
460 Kind = AK_Put;
461
462 // Handle the mistake of forgetting the accessor kind by skipping
463 // this accessor.
464 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
465 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
466 ConsumeToken();
467 HasInvalidAccessor = true;
468 goto next_property_accessor;
469
470 // Otherwise, complain about the unknown accessor kind.
471 } else {
472 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
473 HasInvalidAccessor = true;
474 Kind = AK_Invalid;
475
476 // Try to keep parsing unless it doesn't look like an accessor spec.
477 if (!NextToken().is(tok::equal)) break;
478 }
479
480 // Consume the identifier.
481 ConsumeToken();
482
483 // Consume the '='.
Alp Toker8fbec672013-12-17 23:29:36 +0000484 if (!TryConsumeToken(tok::equal)) {
John McCall5e77d762013-04-16 07:28:30 +0000485 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
486 << KindStr;
487 break;
488 }
489
490 // Expect the method name.
491 if (!Tok.is(tok::identifier)) {
492 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
493 break;
494 }
495
496 if (Kind == AK_Invalid) {
497 // Just drop invalid accessors.
498 } else if (AccessorNames[Kind] != NULL) {
499 // Complain about the repeated accessor, ignore it, and keep parsing.
500 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
501 } else {
502 AccessorNames[Kind] = Tok.getIdentifierInfo();
503 }
504 ConsumeToken();
505
506 next_property_accessor:
507 // Keep processing accessors until we run out.
Alp Toker094e5212014-01-05 03:27:11 +0000508 if (TryConsumeToken(tok::comma))
John McCall5e77d762013-04-16 07:28:30 +0000509 continue;
510
511 // If we run into the ')', stop without consuming it.
Alp Toker094e5212014-01-05 03:27:11 +0000512 if (Tok.is(tok::r_paren))
John McCall5e77d762013-04-16 07:28:30 +0000513 break;
Alp Toker094e5212014-01-05 03:27:11 +0000514
515 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
516 break;
John McCall5e77d762013-04-16 07:28:30 +0000517 }
518
519 // Only add the property attribute if it was well-formed.
520 if (!HasInvalidAccessor) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000521 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000522 AccessorNames[AK_Get], AccessorNames[AK_Put],
523 AttributeList::AS_Declspec);
524 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000525 T.skipToEnd();
526 } else {
527 // We don't recognize this as a valid declspec, but instead of creating the
528 // attribute and allowing sema to warn about it, we will warn here instead.
529 // This is because some attributes have multiple spellings, but we need to
530 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000531 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000532 // both locations.
533 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
534
535 // If there's an open paren, we should eat the open and close parens under
536 // the assumption that this unknown declspec has parameters.
537 BalancedDelimiterTracker T(*this, tok::l_paren);
538 if (!T.consumeOpen())
539 T.skipToEnd();
540 }
541}
542
Eli Friedman06de2b52009-06-08 07:21:15 +0000543/// [MS] decl-specifier:
544/// __declspec ( extended-decl-modifier-seq )
545///
546/// [MS] extended-decl-modifier-seq:
547/// extended-decl-modifier[opt]
548/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000549void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000550 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000551
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000552 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000553 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000554 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000555 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000556 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000557
Chad Rosierc1183952012-06-26 22:30:43 +0000558 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000559 // you can specify multiple attributes per declspec.
560 while (Tok.getKind() != tok::r_paren) {
561 // We expect either a well-known identifier or a generic string. Anything
562 // else is a malformed declspec.
563 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000564 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000565 Tok.getKind() != tok::kw_restrict) {
566 Diag(Tok, diag::err_ms_declspec_type);
567 T.skipToEnd();
568 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000569 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000570
571 IdentifierInfo *AttrName;
572 SourceLocation AttrNameLoc;
573 if (IsString) {
574 SmallString<8> StrBuffer;
575 bool Invalid = false;
576 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
577 if (Invalid) {
578 T.skipToEnd();
579 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000580 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000581 AttrName = PP.getIdentifierInfo(Str);
582 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000583 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000584 AttrName = Tok.getIdentifierInfo();
585 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000586 }
Chad Rosierc1183952012-06-26 22:30:43 +0000587
Aaron Ballman478faed2012-06-19 22:09:27 +0000588 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000589 // If we have a generic string, we will allow it because there is no
590 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000591 // (for instance, SAL declspecs in older versions of MSVC).
592 //
Chad Rosierc1183952012-06-26 22:30:43 +0000593 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000594 // arguments and can be turned into an attribute directly.
Aaron Ballman00e99962013-08-31 01:11:41 +0000595 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
596 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000597 else
598 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000599 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000600 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000601}
602
John McCall53fa7142010-12-24 02:08:15 +0000603void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000604 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000605 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000606 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000607 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballman317a77f2013-05-22 23:25:32 +0000608 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
609 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000610 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
611 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000612 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
613 AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000614 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000615}
616
John McCall53fa7142010-12-24 02:08:15 +0000617void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000618 // Treat these like attributes
619 while (Tok.is(tok::kw___pascal)) {
620 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
621 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000622 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
623 AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000624 }
John McCall53fa7142010-12-24 02:08:15 +0000625}
626
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000627void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
628 // Treat these like attributes
629 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000630 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000631 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000632 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
633 AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000634 }
635}
636
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000637void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000638 // FIXME: The mapping from attribute spelling to semantics should be
639 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000640 SourceLocation Loc = Tok.getLocation();
641 switch(Tok.getKind()) {
642 // OpenCL qualifiers:
643 case tok::kw___private:
John McCall084e83d2011-03-24 11:26:52 +0000644 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000645 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000646 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000647 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000648
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000649 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000650 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000651 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000652 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000653 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000654
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000655 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000656 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000657 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000658 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000659 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000660
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000661 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000662 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000663 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000664 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000665 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000666
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000667 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000668 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000669 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000670 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000671 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000672
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000673 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000674 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000675 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000676 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000677 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000678
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000679 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000680 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000681 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000682 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000683 break;
684 default: break;
685 }
686}
687
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000688/// \brief Parse a version number.
689///
690/// version:
691/// simple-integer
692/// simple-integer ',' simple-integer
693/// simple-integer ',' simple-integer ',' simple-integer
694VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
695 Range = Tok.getLocation();
696
697 if (!Tok.is(tok::numeric_constant)) {
698 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000699 SkipUntil(tok::comma, tok::r_paren,
700 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000701 return VersionTuple();
702 }
703
704 // Parse the major (and possibly minor and subminor) versions, which
705 // are stored in the numeric constant. We utilize a quirk of the
706 // lexer, which is that it handles something like 1.2.3 as a single
707 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000708 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000709 Buffer.resize(Tok.getLength()+1);
710 const char *ThisTokBegin = &Buffer[0];
711
712 // Get the spelling of the token, which eliminates trigraphs, etc.
713 bool Invalid = false;
714 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
715 if (Invalid)
716 return VersionTuple();
717
718 // Parse the major version.
719 unsigned AfterMajor = 0;
720 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000721 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000722 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
723 ++AfterMajor;
724 }
725
726 if (AfterMajor == 0) {
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 if (AfterMajor == ActualLength) {
734 ConsumeToken();
735
736 // We only had a single version component.
737 if (Major == 0) {
738 Diag(Tok, diag::err_zero_version);
739 return VersionTuple();
740 }
741
742 return VersionTuple(Major);
743 }
744
745 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
746 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000747 SkipUntil(tok::comma, tok::r_paren,
748 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000749 return VersionTuple();
750 }
751
752 // Parse the minor version.
753 unsigned AfterMinor = AfterMajor + 1;
754 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000755 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000756 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
757 ++AfterMinor;
758 }
759
760 if (AfterMinor == ActualLength) {
761 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000762
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000763 // We had major.minor.
764 if (Major == 0 && Minor == 0) {
765 Diag(Tok, diag::err_zero_version);
766 return VersionTuple();
767 }
768
Chad Rosierc1183952012-06-26 22:30:43 +0000769 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000770 }
771
772 // If what follows is not a '.', we have a problem.
773 if (ThisTokBegin[AfterMinor] != '.') {
774 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000775 SkipUntil(tok::comma, tok::r_paren,
776 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Chad Rosierc1183952012-06-26 22:30:43 +0000777 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000778 }
779
780 // Parse the subminor version.
781 unsigned AfterSubminor = AfterMinor + 1;
782 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000783 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000784 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
785 ++AfterSubminor;
786 }
787
788 if (AfterSubminor != ActualLength) {
789 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000790 SkipUntil(tok::comma, tok::r_paren,
791 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000792 return VersionTuple();
793 }
794 ConsumeToken();
795 return VersionTuple(Major, Minor, Subminor);
796}
797
798/// \brief Parse the contents of the "availability" attribute.
799///
800/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000801/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000802///
803/// platform:
804/// identifier
805///
806/// version-arg-list:
807/// version-arg
808/// version-arg ',' version-arg-list
809///
810/// version-arg:
811/// 'introduced' '=' version
812/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000813/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000814/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000815/// opt-message:
816/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000817void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
818 SourceLocation AvailabilityLoc,
819 ParsedAttributes &attrs,
820 SourceLocation *endLoc) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000821 enum { Introduced, Deprecated, Obsoleted, Unknown };
822 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000823 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000824
825 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000826 BalancedDelimiterTracker T(*this, tok::l_paren);
827 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000828 Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000829 return;
830 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000831
832 // Parse the platform name,
833 if (Tok.isNot(tok::identifier)) {
834 Diag(Tok, diag::err_availability_expected_platform);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000835 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000836 return;
837 }
Richard Smithfeefaf52013-09-03 18:01:40 +0000838 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000839
840 // Parse the ',' following the platform name.
Alp Toker383d2c42014-01-01 03:08:43 +0000841 if (ExpectAndConsume(tok::comma)) {
842 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000843 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000844 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000845
846 // If we haven't grabbed the pointers for the identifiers
847 // "introduced", "deprecated", and "obsoleted", do so now.
848 if (!Ident_introduced) {
849 Ident_introduced = PP.getIdentifierInfo("introduced");
850 Ident_deprecated = PP.getIdentifierInfo("deprecated");
851 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000852 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000853 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000854 }
855
856 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000857 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000858 do {
859 if (Tok.isNot(tok::identifier)) {
860 Diag(Tok, diag::err_availability_expected_change);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000861 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000862 return;
863 }
864 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
865 SourceLocation KeywordLoc = ConsumeToken();
866
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000867 if (Keyword == Ident_unavailable) {
868 if (UnavailableLoc.isValid()) {
869 Diag(KeywordLoc, diag::err_availability_redundant)
870 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000871 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000872 UnavailableLoc = KeywordLoc;
Alp Toker97650562014-01-10 11:19:30 +0000873 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000874 }
875
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000876 if (Tok.isNot(tok::equal)) {
Alp Tokerec543272013-12-24 09:48:30 +0000877 Diag(Tok, diag::err_expected_after) << Keyword << tok::equal;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000878 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000879 return;
880 }
881 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000882 if (Keyword == Ident_message) {
Benjamin Kramera9dfa922013-09-13 17:31:48 +0000883 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000884 Diag(Tok, diag::err_expected_string_literal)
885 << /*Source='availability attribute'*/2;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000886 SkipUntil(tok::r_paren, StopAtSemi);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000887 return;
888 }
889 MessageExpr = ParseStringLiteralExpression();
890 break;
891 }
Chad Rosierc1183952012-06-26 22:30:43 +0000892
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000893 SourceRange VersionRange;
894 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000895
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000896 if (Version.empty()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000897 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000898 return;
899 }
900
901 unsigned Index;
902 if (Keyword == Ident_introduced)
903 Index = Introduced;
904 else if (Keyword == Ident_deprecated)
905 Index = Deprecated;
906 else if (Keyword == Ident_obsoleted)
907 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000908 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000909 Index = Unknown;
910
911 if (Index < Unknown) {
912 if (!Changes[Index].KeywordLoc.isInvalid()) {
913 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000914 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000915 << SourceRange(Changes[Index].KeywordLoc,
916 Changes[Index].VersionRange.getEnd());
917 }
918
919 Changes[Index].KeywordLoc = KeywordLoc;
920 Changes[Index].Version = Version;
921 Changes[Index].VersionRange = VersionRange;
922 } else {
923 Diag(KeywordLoc, diag::err_availability_unknown_change)
924 << Keyword << VersionRange;
925 }
926
Alp Toker97650562014-01-10 11:19:30 +0000927 } while (TryConsumeToken(tok::comma));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000928
929 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000930 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000931 return;
932
933 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000934 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000935
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000936 // The 'unavailable' availability cannot be combined with any other
937 // availability changes. Make sure that hasn't happened.
938 if (UnavailableLoc.isValid()) {
939 bool Complained = false;
940 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
941 if (Changes[Index].KeywordLoc.isValid()) {
942 if (!Complained) {
943 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
944 << SourceRange(Changes[Index].KeywordLoc,
945 Changes[Index].VersionRange.getEnd());
946 Complained = true;
947 }
948
949 // Clear out the availability.
950 Changes[Index] = AvailabilityChange();
951 }
952 }
953 }
954
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000955 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000956 attrs.addNew(&Availability,
957 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000958 0, AvailabilityLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +0000959 Platform,
John McCall084e83d2011-03-24 11:26:52 +0000960 Changes[Introduced],
961 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000962 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000963 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000964 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000965}
966
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000967/// \brief Parse the contents of the "objc_bridge_related" attribute.
968/// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')'
969/// related_class:
970/// Identifier
971///
972/// opt-class_method:
973/// Identifier: | <empty>
974///
975/// opt-instance_method:
976/// Identifier | <empty>
977///
978void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated,
979 SourceLocation ObjCBridgeRelatedLoc,
980 ParsedAttributes &attrs,
981 SourceLocation *endLoc) {
982 // Opening '('.
983 BalancedDelimiterTracker T(*this, tok::l_paren);
984 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000985 Diag(Tok, diag::err_expected) << tok::l_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000986 return;
987 }
988
989 // Parse the related class name.
990 if (Tok.isNot(tok::identifier)) {
991 Diag(Tok, diag::err_objcbridge_related_expected_related_class);
992 SkipUntil(tok::r_paren, StopAtSemi);
993 return;
994 }
995 IdentifierLoc *RelatedClass = ParseIdentifierLoc();
Alp Toker97650562014-01-10 11:19:30 +0000996 if (ExpectAndConsume(tok::comma)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000997 SkipUntil(tok::r_paren, StopAtSemi);
998 return;
999 }
Alp Toker8fbec672013-12-17 23:29:36 +00001000
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001001 // Parse optional class method name.
1002 IdentifierLoc *ClassMethod = 0;
1003 if (Tok.is(tok::identifier)) {
1004 ClassMethod = ParseIdentifierLoc();
Alp Toker8fbec672013-12-17 23:29:36 +00001005 if (!TryConsumeToken(tok::colon)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001006 Diag(Tok, diag::err_objcbridge_related_selector_name);
1007 SkipUntil(tok::r_paren, StopAtSemi);
1008 return;
1009 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001010 }
Alp Toker8fbec672013-12-17 23:29:36 +00001011 if (!TryConsumeToken(tok::comma)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001012 if (Tok.is(tok::colon))
1013 Diag(Tok, diag::err_objcbridge_related_selector_name);
1014 else
Alp Tokerec543272013-12-24 09:48:30 +00001015 Diag(Tok, diag::err_expected) << tok::comma;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001016 SkipUntil(tok::r_paren, StopAtSemi);
1017 return;
1018 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001019
1020 // Parse optional instance method name.
1021 IdentifierLoc *InstanceMethod = 0;
1022 if (Tok.is(tok::identifier))
1023 InstanceMethod = ParseIdentifierLoc();
1024 else if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001025 Diag(Tok, diag::err_expected) << tok::r_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001026 SkipUntil(tok::r_paren, StopAtSemi);
1027 return;
1028 }
1029
1030 // Closing ')'.
1031 if (T.consumeClose())
1032 return;
1033
1034 if (endLoc)
1035 *endLoc = T.getCloseLocation();
1036
1037 // Record this attribute
1038 attrs.addNew(&ObjCBridgeRelated,
1039 SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
1040 0, ObjCBridgeRelatedLoc,
1041 RelatedClass,
1042 ClassMethod,
1043 InstanceMethod,
1044 AttributeList::AS_GNU);
1045
1046}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001047
Bill Wendling44426052012-12-20 19:22:21 +00001048// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001049// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
1050
1051void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
1052
1053void Parser::LateParsedClass::ParseLexedAttributes() {
1054 Self->ParseLexedAttributes(*Class);
1055}
1056
1057void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001058 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001059}
1060
1061/// Wrapper class which calls ParseLexedAttribute, after setting up the
1062/// scope appropriately.
1063void Parser::ParseLexedAttributes(ParsingClass &Class) {
1064 // Deal with templates
1065 // FIXME: Test cases to make sure this does the right thing for templates.
1066 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
1067 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
1068 HasTemplateScope);
1069 if (HasTemplateScope)
1070 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
1071
Douglas Gregor3024f072012-04-16 07:05:22 +00001072 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001073 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +00001074 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001075 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1076 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1077
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001078 // Enter the scope of nested classes
1079 if (!AlreadyHasClassScope)
1080 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1081 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001082 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001083 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1084 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1085 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001086 }
Chad Rosierc1183952012-06-26 22:30:43 +00001087
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001088 if (!AlreadyHasClassScope)
1089 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1090 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001091}
1092
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001093
1094/// \brief Parse all attributes in LAs, and attach them to Decl D.
1095void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1096 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001097 assert(LAs.parseSoon() &&
1098 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001099 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001100 if (D)
1101 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001102 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001103 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001104 }
1105 LAs.clear();
1106}
1107
1108
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001109/// \brief Finish parsing an attribute for which parsing was delayed.
1110/// This will be called at the end of parsing a class declaration
1111/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001112/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001113/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001114void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1115 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001116 // Save the current token position.
1117 SourceLocation OrigLoc = Tok.getLocation();
1118
1119 // Append the current token at the end of the new token stream so that it
1120 // doesn't get lost.
1121 LA.Toks.push_back(Tok);
1122 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1123 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001124 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001125
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001126 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +00001127 // FIXME: Do not warn on C++11 attributes, once we start supporting
1128 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001129 Diag(Tok, diag::warn_attribute_on_function_definition)
Aaron Ballman6d80b3c2014-01-02 18:10:17 +00001130 << &LA.AttrName;
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001131 }
1132
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001133 ParsedAttributes Attrs(AttrFactory);
1134 SourceLocation endLoc;
1135
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001136 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001137 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001138 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1139 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001140
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001141 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001142 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1143 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001144
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001145 if (LA.Decls.size() == 1) {
1146 // If the Decl is templatized, add template parameters to scope.
1147 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1148 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1149 if (HasTemplateScope)
1150 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001151
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001152 // If the Decl is on a function, add function parameters to the scope.
1153 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1154 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1155 if (HasFunScope)
1156 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001157
Michael Han23214e52012-10-03 01:56:22 +00001158 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001159 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001160
1161 if (HasFunScope) {
1162 Actions.ActOnExitFunctionContext();
1163 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1164 }
1165 if (HasTemplateScope) {
1166 TempScope.Exit();
1167 }
1168 } else {
1169 // If there are multiple decls, then the decl cannot be within the
1170 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001171 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001172 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001173 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001174 } else {
1175 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001176 }
1177
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001178 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1179 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1180 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001181
1182 if (Tok.getLocation() != OrigLoc) {
1183 // Due to a parsing error, we either went over the cached tokens or
1184 // there are still cached tokens left, so we skip the leftover tokens.
1185 // Since this is an uncommon situation that should be avoided, use the
1186 // expensive isBeforeInTranslationUnit call.
1187 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1188 OrigLoc))
1189 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001190 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001191 }
1192}
1193
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001194/// \brief Wrapper around a case statement checking if AttrName is
1195/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001196bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001197 return llvm::StringSwitch<bool>(AttrName)
1198 .Case("guarded_by", true)
1199 .Case("guarded_var", true)
1200 .Case("pt_guarded_by", true)
1201 .Case("pt_guarded_var", true)
1202 .Case("lockable", true)
1203 .Case("scoped_lockable", true)
1204 .Case("no_thread_safety_analysis", true)
1205 .Case("acquired_after", true)
1206 .Case("acquired_before", true)
1207 .Case("exclusive_lock_function", true)
1208 .Case("shared_lock_function", true)
1209 .Case("exclusive_trylock_function", true)
1210 .Case("shared_trylock_function", true)
1211 .Case("unlock_function", true)
1212 .Case("lock_returned", true)
1213 .Case("locks_excluded", true)
1214 .Case("exclusive_locks_required", true)
1215 .Case("shared_locks_required", true)
1216 .Default(false);
1217}
1218
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001219void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1220 SourceLocation AttrNameLoc,
1221 ParsedAttributes &Attrs,
1222 SourceLocation *EndLoc) {
1223 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1224
1225 BalancedDelimiterTracker T(*this, tok::l_paren);
1226 T.consumeOpen();
1227
1228 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001229 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001230 T.skipToEnd();
1231 return;
1232 }
Richard Smithfeefaf52013-09-03 18:01:40 +00001233 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001234
Alp Toker094e5212014-01-05 03:27:11 +00001235 if (ExpectAndConsume(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001236 T.skipToEnd();
1237 return;
1238 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001239
1240 SourceRange MatchingCTypeRange;
1241 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1242 if (MatchingCType.isInvalid()) {
1243 T.skipToEnd();
1244 return;
1245 }
1246
1247 bool LayoutCompatible = false;
1248 bool MustBeNull = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001249 while (TryConsumeToken(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001250 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001251 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001252 T.skipToEnd();
1253 return;
1254 }
1255 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1256 if (Flag->isStr("layout_compatible"))
1257 LayoutCompatible = true;
1258 else if (Flag->isStr("must_be_null"))
1259 MustBeNull = true;
1260 else {
1261 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1262 T.skipToEnd();
1263 return;
1264 }
1265 ConsumeToken(); // consume flag
1266 }
1267
1268 if (!T.consumeClose()) {
1269 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +00001270 ArgumentKind, MatchingCType.release(),
1271 LayoutCompatible, MustBeNull,
1272 AttributeList::AS_GNU);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001273 }
1274
1275 if (EndLoc)
1276 *EndLoc = T.getCloseLocation();
1277}
1278
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001279/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1280/// of a C++11 attribute-specifier in a location where an attribute is not
1281/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1282/// situation.
1283///
1284/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1285/// this doesn't appear to actually be an attribute-specifier, and the caller
1286/// should try to parse it.
1287bool Parser::DiagnoseProhibitedCXX11Attribute() {
1288 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1289
1290 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1291 case CAK_NotAttributeSpecifier:
1292 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1293 return false;
1294
1295 case CAK_InvalidAttributeSpecifier:
1296 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1297 return false;
1298
1299 case CAK_AttributeSpecifier:
1300 // Parse and discard the attributes.
1301 SourceLocation BeginLoc = ConsumeBracket();
1302 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001303 SkipUntil(tok::r_square);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001304 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1305 SourceLocation EndLoc = ConsumeBracket();
1306 Diag(BeginLoc, diag::err_attributes_not_allowed)
1307 << SourceRange(BeginLoc, EndLoc);
1308 return true;
1309 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001310 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001311}
1312
Richard Smith98155ad2013-02-20 01:17:14 +00001313/// \brief We have found the opening square brackets of a C++11
1314/// attribute-specifier in a location where an attribute is not permitted, but
1315/// we know where the attributes ought to be written. Parse them anyway, and
1316/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001317void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1318 SourceLocation CorrectLocation) {
1319 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1320 Tok.is(tok::kw_alignas));
1321
1322 // Consume the attributes.
1323 SourceLocation Loc = Tok.getLocation();
1324 ParseCXX11Attributes(Attrs);
1325 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1326
1327 Diag(Loc, diag::err_attributes_not_allowed)
1328 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1329 << FixItHint::CreateRemoval(AttrRange);
1330}
1331
John McCall53fa7142010-12-24 02:08:15 +00001332void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1333 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1334 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001335}
1336
Michael Han64536a62012-11-06 19:34:54 +00001337void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1338 AttributeList *AttrList = attrs.getList();
1339 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001340 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001341 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001342 << AttrList->getName();
1343 AttrList->setInvalid();
1344 }
1345 AttrList = AttrList->getNext();
1346 }
1347}
1348
Chris Lattner53361ac2006-08-10 05:19:57 +00001349/// ParseDeclaration - Parse a full 'declaration', which consists of
1350/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001351/// 'Context' should be a Declarator::TheContext value. This returns the
1352/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001353///
1354/// declaration: [C99 6.7]
1355/// block-declaration ->
1356/// simple-declaration
1357/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001358/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001359/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001360/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001361/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001362/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001363/// others... [FIXME]
1364///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001365Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1366 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001367 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001368 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001369 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001370 // Must temporarily exit the objective-c container scope for
1371 // parsing c none objective-c decls.
1372 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001373
John McCall48871652010-08-21 09:40:31 +00001374 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001375 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001376 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001377 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001378 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001379 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001380 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001381 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001382 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001383 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001384 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001385 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001386 SourceLocation InlineLoc = ConsumeToken();
1387 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1388 break;
1389 }
Chad Rosierc1183952012-06-26 22:30:43 +00001390 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001391 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001392 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001393 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001394 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001395 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001396 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001397 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001398 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001399 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001400 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001401 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001402 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001403 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001404 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001405 default:
John McCall53fa7142010-12-24 02:08:15 +00001406 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001407 }
Chad Rosierc1183952012-06-26 22:30:43 +00001408
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001409 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001410 // single decl, convert it now. Alias declarations can also declare a type;
1411 // include that too if it is present.
1412 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001413}
1414
1415/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1416/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001417/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1418/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001419///[C90/C++]init-declarator-list ';' [TODO]
1420/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001421///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001422/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001423/// attribute-specifier-seq[opt] type-specifier-seq declarator
1424///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001425/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001426/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001427///
1428/// If FRI is non-null, we might be parsing a for-range-declaration instead
1429/// of a simple-declaration. If we find that we are, we also parse the
1430/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001431Parser::DeclGroupPtrTy
1432Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1433 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001434 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001435 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001436 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001437 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001438
Richard Smith404dfb42013-11-19 22:47:36 +00001439 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
1440 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
1441
1442 // If we had a free-standing type definition with a missing semicolon, we
1443 // may get this far before the problem becomes obvious.
1444 if (DS.hasTagDefinition() &&
1445 DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
1446 return DeclGroupPtrTy();
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001447
Chris Lattner0e894622006-08-13 19:58:17 +00001448 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1449 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001450 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001451 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001452 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001453 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001454 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001455 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001456 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001457 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001458 }
Chad Rosierc1183952012-06-26 22:30:43 +00001459
Richard Smith2386c8b2013-02-22 09:06:26 +00001460 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001461 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001462}
Mike Stump11289f42009-09-09 15:08:12 +00001463
Richard Smith09f76ee2011-10-19 21:33:05 +00001464/// Returns true if this might be the start of a declarator, or a common typo
1465/// for a declarator.
1466bool Parser::MightBeDeclarator(unsigned Context) {
1467 switch (Tok.getKind()) {
1468 case tok::annot_cxxscope:
1469 case tok::annot_template_id:
1470 case tok::caret:
1471 case tok::code_completion:
1472 case tok::coloncolon:
1473 case tok::ellipsis:
1474 case tok::kw___attribute:
1475 case tok::kw_operator:
1476 case tok::l_paren:
1477 case tok::star:
1478 return true;
1479
1480 case tok::amp:
1481 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001482 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001483
Richard Smithc8a79032012-01-09 22:31:44 +00001484 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001485 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001486 NextToken().is(tok::l_square);
1487
1488 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001489 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001490
Richard Smith09f76ee2011-10-19 21:33:05 +00001491 case tok::identifier:
1492 switch (NextToken().getKind()) {
1493 case tok::code_completion:
1494 case tok::coloncolon:
1495 case tok::comma:
1496 case tok::equal:
1497 case tok::equalequal: // Might be a typo for '='.
1498 case tok::kw_alignas:
1499 case tok::kw_asm:
1500 case tok::kw___attribute:
1501 case tok::l_brace:
1502 case tok::l_paren:
1503 case tok::l_square:
1504 case tok::less:
1505 case tok::r_brace:
1506 case tok::r_paren:
1507 case tok::r_square:
1508 case tok::semi:
1509 return true;
1510
1511 case tok::colon:
1512 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001513 // and in block scope it's probably a label. Inside a class definition,
1514 // this is a bit-field.
1515 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001516 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001517
1518 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001519 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001520
1521 default:
1522 return false;
1523 }
1524
1525 default:
1526 return false;
1527 }
1528}
1529
Richard Smithb8caac82012-04-11 20:59:20 +00001530/// Skip until we reach something which seems like a sensible place to pick
1531/// up parsing after a malformed declaration. This will sometimes stop sooner
1532/// than SkipUntil(tok::r_brace) would, but will never stop later.
1533void Parser::SkipMalformedDecl() {
1534 while (true) {
1535 switch (Tok.getKind()) {
1536 case tok::l_brace:
1537 // Skip until matching }, then stop. We've probably skipped over
1538 // a malformed class or function definition or similar.
1539 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001540 SkipUntil(tok::r_brace);
Richard Smithb8caac82012-04-11 20:59:20 +00001541 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1542 // This declaration isn't over yet. Keep skipping.
1543 continue;
1544 }
Alp Toker8fbec672013-12-17 23:29:36 +00001545 TryConsumeToken(tok::semi);
Richard Smithb8caac82012-04-11 20:59:20 +00001546 return;
1547
1548 case tok::l_square:
1549 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001550 SkipUntil(tok::r_square);
Richard Smithb8caac82012-04-11 20:59:20 +00001551 continue;
1552
1553 case tok::l_paren:
1554 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001555 SkipUntil(tok::r_paren);
Richard Smithb8caac82012-04-11 20:59:20 +00001556 continue;
1557
1558 case tok::r_brace:
1559 return;
1560
1561 case tok::semi:
1562 ConsumeToken();
1563 return;
1564
1565 case tok::kw_inline:
1566 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001567 // a good place to pick back up parsing, except in an Objective-C
1568 // @interface context.
1569 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1570 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001571 return;
1572 break;
1573
1574 case tok::kw_namespace:
1575 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001576 // place to pick back up parsing, except in an Objective-C
1577 // @interface context.
1578 if (Tok.isAtStartOfLine() &&
1579 (!ParsingInObjCContainer || CurParsedObjCImpl))
1580 return;
1581 break;
1582
1583 case tok::at:
1584 // @end is very much like } in Objective-C contexts.
1585 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1586 ParsingInObjCContainer)
1587 return;
1588 break;
1589
1590 case tok::minus:
1591 case tok::plus:
1592 // - and + probably start new method declarations in Objective-C contexts.
1593 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001594 return;
1595 break;
1596
1597 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001598 case tok::annot_module_begin:
1599 case tok::annot_module_end:
1600 case tok::annot_module_include:
Richard Smithb8caac82012-04-11 20:59:20 +00001601 return;
1602
1603 default:
1604 break;
1605 }
1606
1607 ConsumeAnyToken();
1608 }
1609}
1610
John McCalld5a36322009-11-03 19:26:08 +00001611/// ParseDeclGroup - Having concluded that this is either a function
1612/// definition or a group of object declarations, actually parse the
1613/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001614Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1615 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001616 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001617 SourceLocation *DeclEnd,
1618 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001619 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001620 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001621 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001622
John McCalld5a36322009-11-03 19:26:08 +00001623 // Bail out if the first declarator didn't seem well-formed.
1624 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001625 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001626 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001627 }
Mike Stump11289f42009-09-09 15:08:12 +00001628
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001629 // Save late-parsed attributes for now; they need to be parsed in the
1630 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001631 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1632 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001633 if (D.isFunctionDeclarator())
1634 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1635
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001636 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001637 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001638 // Look at the next token to make sure that this isn't a function
1639 // declaration. We have to check this because __attribute__ might be the
1640 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001641 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001642
Douglas Gregor012efe22013-04-16 16:01:32 +00001643 if (AllowFunctionDefinitions) {
1644 if (isStartOfFunctionDefinition(D)) {
1645 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1646 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001647
Douglas Gregor012efe22013-04-16 16:01:32 +00001648 // Recover by treating the 'typedef' as spurious.
1649 DS.ClearStorageClassSpecs();
1650 }
1651
1652 Decl *TheDecl =
1653 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1654 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001655 }
1656
Douglas Gregor012efe22013-04-16 16:01:32 +00001657 if (isDeclarationSpecifier()) {
1658 // If there is an invalid declaration specifier right after the function
1659 // prototype, then we must be in a missing semicolon case where this isn't
1660 // actually a body. Just fall through into the code that handles it as a
1661 // prototype, and let the top-level code handle the erroneous declspec
1662 // where it would otherwise expect a comma or semicolon.
1663 } else {
1664 Diag(Tok, diag::err_expected_fn_body);
1665 SkipUntil(tok::semi);
1666 return DeclGroupPtrTy();
1667 }
John McCalld5a36322009-11-03 19:26:08 +00001668 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001669 if (Tok.is(tok::l_brace)) {
1670 Diag(Tok, diag::err_function_definition_not_allowed);
Serge Pavlov1de51512013-12-09 05:25:47 +00001671 SkipMalformedDecl();
1672 return DeclGroupPtrTy();
Douglas Gregor012efe22013-04-16 16:01:32 +00001673 }
John McCalld5a36322009-11-03 19:26:08 +00001674 }
1675 }
1676
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001677 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001678 return DeclGroupPtrTy();
1679
1680 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1681 // must parse and analyze the for-range-initializer before the declaration is
1682 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001683 //
1684 // Handle the Objective-C for-in loop variable similarly, although we
1685 // don't need to parse the container in advance.
1686 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1687 bool IsForRangeLoop = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001688 if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001689 IsForRangeLoop = true;
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001690 if (Tok.is(tok::l_brace))
1691 FRI->RangeExpr = ParseBraceInitializer();
1692 else
1693 FRI->RangeExpr = ParseExpression();
1694 }
1695
Richard Smith02e85f32011-04-14 22:09:26 +00001696 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001697 if (IsForRangeLoop)
1698 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001699 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001700 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001701 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001702 }
1703
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001704 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001705 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001706 if (LateParsedAttrs.size() > 0)
1707 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001708 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001709 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001710 DeclsInGroup.push_back(FirstDecl);
1711
Richard Smith09f76ee2011-10-19 21:33:05 +00001712 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001713
John McCalld5a36322009-11-03 19:26:08 +00001714 // If we don't have a comma, it is either the end of the list (a ';') or an
1715 // error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00001716 SourceLocation CommaLoc;
1717 while (TryConsumeToken(tok::comma, CommaLoc)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001718 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1719 // This comma was followed by a line-break and something which can't be
1720 // the start of a declarator. The comma was probably a typo for a
1721 // semicolon.
1722 Diag(CommaLoc, diag::err_expected_semi_declaration)
1723 << FixItHint::CreateReplacement(CommaLoc, ";");
1724 ExpectSemi = false;
1725 break;
1726 }
John McCalld5a36322009-11-03 19:26:08 +00001727
1728 // Parse the next declarator.
1729 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001730 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001731
1732 // Accept attributes in an init-declarator. In the first declarator in a
1733 // declaration, these would be part of the declspec. In subsequent
1734 // declarators, they become part of the declarator itself, so that they
1735 // don't apply to declarators after *this* one. Examples:
1736 // short __attribute__((common)) var; -> declspec
1737 // short var __attribute__((common)); -> declarator
1738 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001739 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001740
1741 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001742 if (!D.isInvalidType()) {
1743 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1744 D.complete(ThisDecl);
1745 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001746 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001747 }
John McCalld5a36322009-11-03 19:26:08 +00001748 }
1749
1750 if (DeclEnd)
1751 *DeclEnd = Tok.getLocation();
1752
Richard Smith09f76ee2011-10-19 21:33:05 +00001753 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001754 ExpectAndConsumeSemi(Context == Declarator::FileContext
1755 ? diag::err_invalid_token_after_toplevel_declarator
1756 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001757 // Okay, there was no semicolon and one was expected. If we see a
1758 // declaration specifier, just assume it was missing and continue parsing.
1759 // Otherwise things are very confused and we skip to recover.
1760 if (!isDeclarationSpecifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001761 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Alp Toker8fbec672013-12-17 23:29:36 +00001762 TryConsumeToken(tok::semi);
Chris Lattner13901342010-07-11 22:42:07 +00001763 }
John McCalld5a36322009-11-03 19:26:08 +00001764 }
1765
Rafael Espindolaab417692013-07-09 12:05:01 +00001766 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001767}
1768
Richard Smith02e85f32011-04-14 22:09:26 +00001769/// Parse an optional simple-asm-expr and attributes, and attach them to a
1770/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001771bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001772 // If a simple-asm-expr is present, parse it.
1773 if (Tok.is(tok::kw_asm)) {
1774 SourceLocation Loc;
1775 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1776 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001777 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smith02e85f32011-04-14 22:09:26 +00001778 return true;
1779 }
1780
1781 D.setAsmLabel(AsmLabel.release());
1782 D.SetRangeEnd(Loc);
1783 }
1784
1785 MaybeParseGNUAttributes(D);
1786 return false;
1787}
1788
Douglas Gregor23996282009-05-12 21:31:51 +00001789/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1790/// declarator'. This method parses the remainder of the declaration
1791/// (including any attributes or initializer, among other things) and
1792/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001793///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001794/// init-declarator: [C99 6.7]
1795/// declarator
1796/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001797/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1798/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001799/// [C++] declarator initializer[opt]
1800///
1801/// [C++] initializer:
1802/// [C++] '=' initializer-clause
1803/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001804/// [C++0x] '=' 'default' [TODO]
1805/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001806/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001807///
1808/// According to the standard grammar, =default and =delete are function
1809/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001810///
John McCall48871652010-08-21 09:40:31 +00001811Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001812 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001813 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001814 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001815
Richard Smith02e85f32011-04-14 22:09:26 +00001816 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1817}
Mike Stump11289f42009-09-09 15:08:12 +00001818
Richard Smith02e85f32011-04-14 22:09:26 +00001819Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1820 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001821 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001822 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001823 switch (TemplateInfo.Kind) {
1824 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001825 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001826 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001827
Douglas Gregor450f00842009-09-25 18:43:00 +00001828 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001829 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001830 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001831 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001832 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00001833 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001834 // Re-direct this decl to refer to the templated decl so that we can
1835 // initialize it.
1836 ThisDecl = VT->getTemplatedDecl();
1837 break;
1838 }
1839 case ParsedTemplateInfo::ExplicitInstantiation: {
1840 if (Tok.is(tok::semi)) {
1841 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1842 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1843 if (ThisRes.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001844 SkipUntil(tok::semi, StopBeforeMatch);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001845 return 0;
1846 }
1847 ThisDecl = ThisRes.get();
1848 } else {
1849 // FIXME: This check should be for a variable template instantiation only.
1850
1851 // Check that this is a valid instantiation
1852 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1853 // If the declarator-id is not a template-id, issue a diagnostic and
1854 // recover by ignoring the 'template' keyword.
1855 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1856 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1857 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1858 } else {
1859 SourceLocation LAngleLoc =
1860 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1861 Diag(D.getIdentifierLoc(),
1862 diag::err_explicit_instantiation_with_definition)
1863 << SourceRange(TemplateInfo.TemplateLoc)
1864 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1865
1866 // Recover as if it were an explicit specialization.
1867 TemplateParameterLists FakedParamLists;
1868 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1869 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1870 LAngleLoc));
1871
1872 ThisDecl =
1873 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1874 }
1875 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001876 break;
1877 }
1878 }
Mike Stump11289f42009-09-09 15:08:12 +00001879
Richard Smith74aeef52013-04-26 16:15:35 +00001880 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001881
Douglas Gregor23996282009-05-12 21:31:51 +00001882 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001883 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001884 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001885 ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001886
Anders Carlsson991285e2010-09-24 21:25:25 +00001887 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001888 if (D.isFunctionDeclarator())
1889 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1890 << 1 /* delete */;
1891 else
1892 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001893 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001894 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001895 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1896 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001897 else
1898 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001899 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001900 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001901 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001902 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001903 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001904
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001905 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001906 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001907 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001908 cutOffParsing();
1909 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001910 }
Chad Rosierc1183952012-06-26 22:30:43 +00001911
John McCalldadc5752010-08-24 06:29:42 +00001912 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001913
David Blaikiebbafb8a2012-03-11 07:00:24 +00001914 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001915 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001916 ExitScope();
1917 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001918
Douglas Gregor23996282009-05-12 21:31:51 +00001919 if (Init.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001920 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor604c3022010-03-01 18:27:54 +00001921 Actions.ActOnInitializerError(ThisDecl);
1922 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001923 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1924 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001925 }
1926 } else if (Tok.is(tok::l_paren)) {
1927 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001928 BalancedDelimiterTracker T(*this, tok::l_paren);
1929 T.consumeOpen();
1930
Benjamin Kramerf0623432012-08-23 22:51:59 +00001931 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001932 CommaLocsTy CommaLocs;
1933
David Blaikiebbafb8a2012-03-11 07:00:24 +00001934 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001935 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001936 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001937 }
1938
Douglas Gregor23996282009-05-12 21:31:51 +00001939 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001940 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001941 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor613bf102009-12-22 17:47:17 +00001942
David Blaikiebbafb8a2012-03-11 07:00:24 +00001943 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001944 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001945 ExitScope();
1946 }
Douglas Gregor23996282009-05-12 21:31:51 +00001947 } else {
1948 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001949 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001950
1951 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1952 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001953
David Blaikiebbafb8a2012-03-11 07:00:24 +00001954 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001955 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001956 ExitScope();
1957 }
1958
Sebastian Redla9351792012-02-11 23:51:47 +00001959 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1960 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001961 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001962 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1963 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001964 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001965 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001966 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001967 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001968 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1969
Sebastian Redl3da34892011-06-05 12:23:16 +00001970 if (D.getCXXScopeSpec().isSet()) {
1971 EnterScope(0);
1972 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1973 }
1974
1975 ExprResult Init(ParseBraceInitializer());
1976
1977 if (D.getCXXScopeSpec().isSet()) {
1978 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1979 ExitScope();
1980 }
1981
1982 if (Init.isInvalid()) {
1983 Actions.ActOnInitializerError(ThisDecl);
1984 } else
1985 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1986 /*DirectInit=*/true, TypeContainsAuto);
1987
Douglas Gregor23996282009-05-12 21:31:51 +00001988 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001989 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001990 }
1991
Richard Smithb2bc2e62011-02-21 20:05:19 +00001992 Actions.FinalizeDeclaration(ThisDecl);
1993
Douglas Gregor23996282009-05-12 21:31:51 +00001994 return ThisDecl;
1995}
1996
Chris Lattner1890ac82006-08-13 01:16:23 +00001997/// ParseSpecifierQualifierList
1998/// specifier-qualifier-list:
1999/// type-specifier specifier-qualifier-list[opt]
2000/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002001/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00002002///
Richard Smithc5b05522012-03-12 07:56:15 +00002003void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
2004 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002005 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
2006 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002007 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00002008 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00002009
Chris Lattner1890ac82006-08-13 01:16:23 +00002010 // Validate declspec for type-name.
2011 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith649c7b062014-01-08 00:56:48 +00002012 if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00002013 Diag(Tok, diag::err_expected_type);
2014 DS.SetTypeSpecError();
2015 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
2016 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002017 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00002018 if (!DS.hasTypeSpecifier())
2019 DS.SetTypeSpecError();
2020 }
Mike Stump11289f42009-09-09 15:08:12 +00002021
Chris Lattner1b22eed2006-11-28 05:12:07 +00002022 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002023 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00002024 if (DS.getStorageClassSpecLoc().isValid())
2025 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2026 else
Richard Smithb4a9e862013-04-12 22:46:28 +00002027 Diag(DS.getThreadStorageClassSpecLoc(),
2028 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00002029 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002030 }
Mike Stump11289f42009-09-09 15:08:12 +00002031
Chris Lattner1b22eed2006-11-28 05:12:07 +00002032 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002033 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002034 if (DS.isInlineSpecified())
2035 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2036 if (DS.isVirtualSpecified())
2037 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2038 if (DS.isExplicitSpecified())
2039 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002040 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002041 }
Richard Smithc5b05522012-03-12 07:56:15 +00002042
2043 // Issue diagnostic and remove constexpr specfier if present.
2044 if (DS.isConstexprSpecified()) {
2045 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2046 DS.ClearConstexprSpec();
2047 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002048}
Chris Lattner53361ac2006-08-10 05:19:57 +00002049
Chris Lattner6cc055a2009-04-12 20:42:31 +00002050/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2051/// specified token is valid after the identifier in a declarator which
2052/// immediately follows the declspec. For example, these things are valid:
2053///
2054/// int x [ 4]; // direct-declarator
2055/// int x ( int y); // direct-declarator
2056/// int(int x ) // direct-declarator
2057/// int x ; // simple-declaration
2058/// int x = 17; // init-declarator-list
2059/// int x , y; // init-declarator-list
2060/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002061/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002062/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002063///
2064/// This is not, because 'x' does not immediately follow the declspec (though
2065/// ')' happens to be valid anyway).
2066/// int (x)
2067///
2068static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2069 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2070 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002071 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002072}
2073
Chris Lattner20a0c612009-04-14 21:34:55 +00002074
2075/// ParseImplicitInt - This method is called when we have an non-typename
2076/// identifier in a declspec (which normally terminates the decl spec) when
2077/// the declspec has no type specifier. In this case, the declspec is either
2078/// malformed or is "implicit int" (in K&R and C89).
2079///
2080/// This method handles diagnosing this prettily and returns false if the
2081/// declspec is done being processed. If it recovers and thinks there may be
2082/// other pieces of declspec after it, it returns true.
2083///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002084bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002085 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002086 AccessSpecifier AS, DeclSpecContext DSC,
2087 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002088 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002089
Chris Lattner20a0c612009-04-14 21:34:55 +00002090 SourceLocation Loc = Tok.getLocation();
2091 // If we see an identifier that is not a type name, we normally would
2092 // parse it as the identifer being declared. However, when a typename
2093 // is typo'd or the definition is not included, this will incorrectly
2094 // parse the typename as the identifier name and fall over misparsing
2095 // later parts of the diagnostic.
2096 //
2097 // As such, we try to do some look-ahead in cases where this would
2098 // otherwise be an "implicit-int" case to see if this is invalid. For
2099 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2100 // an identifier with implicit int, we'd get a parse error because the
2101 // next token is obviously invalid for a type. Parse these as a case
2102 // with an invalid type specifier.
2103 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002104
Chris Lattner20a0c612009-04-14 21:34:55 +00002105 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002106 // error, do lookahead to try to do better recovery. This never applies
2107 // within a type specifier. Outside of C++, we allow this even if the
2108 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002109 // implicit int as an extension in C99 and C11.
Richard Smith649c7b062014-01-08 00:56:48 +00002110 if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002111 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002112 // If this token is valid for implicit int, e.g. "static x = 4", then
2113 // we just avoid eating the identifier, so it will be parsed as the
2114 // identifier in the declarator.
2115 return false;
2116 }
Mike Stump11289f42009-09-09 15:08:12 +00002117
Richard Smitha952ebb2012-05-15 21:01:51 +00002118 if (getLangOpts().CPlusPlus &&
2119 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2120 // Don't require a type specifier if we have the 'auto' storage class
2121 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002122 if (SS)
2123 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002124 return false;
2125 }
2126
Chris Lattner20a0c612009-04-14 21:34:55 +00002127 // Otherwise, if we don't consume this token, we are going to emit an
2128 // error anyway. Try to recover from various common problems. Check
2129 // to see if this was a reference to a tag name without a tag specified.
2130 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002131 //
2132 // C++ doesn't need this, and isTagName doesn't take SS.
2133 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002134 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002135 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002136
Douglas Gregor0be31a22010-07-02 17:43:08 +00002137 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002138 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002139 case DeclSpec::TST_enum:
2140 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2141 case DeclSpec::TST_union:
2142 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2143 case DeclSpec::TST_struct:
2144 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002145 case DeclSpec::TST_interface:
2146 TagName="__interface"; FixitTagName = "__interface ";
2147 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002148 case DeclSpec::TST_class:
2149 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002150 }
Mike Stump11289f42009-09-09 15:08:12 +00002151
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002152 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002153 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2154 LookupResult R(Actions, TokenName, SourceLocation(),
2155 Sema::LookupOrdinaryName);
2156
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002157 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002158 << TokenName << TagName << getLangOpts().CPlusPlus
2159 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2160
2161 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2162 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2163 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002164 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002165 << TokenName << TagName;
2166 }
Mike Stump11289f42009-09-09 15:08:12 +00002167
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002168 // Parse this as a tag as if the missing tag were present.
2169 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002170 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002171 else
Richard Smithc5b05522012-03-12 07:56:15 +00002172 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002173 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002174 return true;
2175 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002176 }
Mike Stump11289f42009-09-09 15:08:12 +00002177
Richard Smithfe904f02012-05-15 21:29:55 +00002178 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002179 // being declared (with a missing type).
Richard Smith649c7b062014-01-08 00:56:48 +00002180 if (!isTypeSpecifier(DSC) &&
Richard Smithfe904f02012-05-15 21:29:55 +00002181 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002182 // Look ahead to the next token to try to figure out what this declaration
2183 // was supposed to be.
2184 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002185 case tok::l_paren: {
2186 // static x(4); // 'x' is not a type
2187 // x(int n); // 'x' is not a type
2188 // x (*p)[]; // 'x' is a type
2189 //
2190 // Since we're in an error case (or the rare 'implicit int in C++' MS
2191 // extension), we can afford to perform a tentative parse to determine
2192 // which case we're in.
2193 TentativeParsingAction PA(*this);
2194 ConsumeToken();
2195 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2196 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002197
2198 if (TPR != TPResult::False()) {
2199 // The identifier is followed by a parenthesized declarator.
2200 // It's supposed to be a type.
2201 break;
2202 }
2203
2204 // If we're in a context where we could be declaring a constructor,
2205 // check whether this is a constructor declaration with a bogus name.
2206 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2207 IdentifierInfo *II = Tok.getIdentifierInfo();
2208 if (Actions.isCurrentClassNameTypo(II, SS)) {
2209 Diag(Loc, diag::err_constructor_bad_name)
2210 << Tok.getIdentifierInfo() << II
2211 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2212 Tok.setIdentifierInfo(II);
2213 }
2214 }
2215 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002216 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002217 case tok::comma:
2218 case tok::equal:
2219 case tok::kw_asm:
2220 case tok::l_brace:
2221 case tok::l_square:
2222 case tok::semi:
2223 // This looks like a variable or function declaration. The type is
2224 // probably missing. We're done parsing decl-specifiers.
2225 if (SS)
2226 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2227 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002228
2229 default:
2230 // This is probably supposed to be a type. This includes cases like:
2231 // int f(itn);
2232 // struct S { unsinged : 4; };
2233 break;
2234 }
2235 }
2236
Chad Rosierc1183952012-06-26 22:30:43 +00002237 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002238 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002239 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002240 IdentifierInfo *II = Tok.getIdentifierInfo();
2241 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002242 // The action emitted a diagnostic, so we don't have to.
2243 if (T) {
2244 // The action has suggested that the type T could be used. Set that as
2245 // the type in the declaration specifiers, consume the would-be type
2246 // name token, and we're done.
2247 const char *PrevSpec;
2248 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002249 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002250 DS.SetRangeEnd(Tok.getLocation());
2251 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002252 // There may be other declaration specifiers after this.
2253 return true;
2254 } else if (II != Tok.getIdentifierInfo()) {
2255 // If no type was suggested, the correction is to a keyword
2256 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002257 // There may be other declaration specifiers after this.
2258 return true;
2259 }
Chad Rosierc1183952012-06-26 22:30:43 +00002260
Douglas Gregor15e56022009-10-13 23:27:22 +00002261 // Fall through; the action had no suggestion for us.
2262 } else {
2263 // The action did not emit a diagnostic, so emit one now.
2264 SourceRange R;
2265 if (SS) R = SS->getRange();
2266 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2267 }
Mike Stump11289f42009-09-09 15:08:12 +00002268
Douglas Gregor15e56022009-10-13 23:27:22 +00002269 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002270 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002271 DS.SetRangeEnd(Tok.getLocation());
2272 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002273
Chris Lattner20a0c612009-04-14 21:34:55 +00002274 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2275 // avoid rippling error messages on subsequent uses of the same type,
2276 // could be useful if #include was forgotten.
2277 return false;
2278}
2279
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002280/// \brief Determine the declaration specifier context from the declarator
2281/// context.
2282///
2283/// \param Context the declarator context, which is one of the
2284/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002285Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002286Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2287 if (Context == Declarator::MemberContext)
2288 return DSC_class;
2289 if (Context == Declarator::FileContext)
2290 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002291 if (Context == Declarator::TrailingReturnContext)
2292 return DSC_trailing;
Richard Smith649c7b062014-01-08 00:56:48 +00002293 if (Context == Declarator::AliasDeclContext ||
2294 Context == Declarator::AliasTemplateContext)
2295 return DSC_alias_declaration;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002296 return DSC_normal;
2297}
2298
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002299/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2300///
2301/// FIXME: Simply returns an alignof() expression if the argument is a
2302/// type. Ideally, the type should be propagated directly into Sema.
2303///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002304/// [C11] type-id
2305/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002306/// [C++0x] type-id ...[opt]
2307/// [C++0x] assignment-expression ...[opt]
2308ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2309 SourceLocation &EllipsisLoc) {
2310 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002311 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002312 SourceLocation TypeLoc = Tok.getLocation();
2313 ParsedType Ty = ParseTypeName().get();
2314 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002315 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2316 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002317 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002318 ER = ParseConstantExpression();
2319
Alp Toker8fbec672013-12-17 23:29:36 +00002320 if (getLangOpts().CPlusPlus11)
2321 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002322
2323 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002324}
2325
2326/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2327/// attribute to Attrs.
2328///
2329/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002330/// [C11] '_Alignas' '(' type-id ')'
2331/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002332/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2333/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002334void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002335 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002336 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2337 "Not an alignment-specifier!");
2338
Richard Smithd11c7a12013-01-29 01:48:07 +00002339 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2340 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002341
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002342 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002343 if (T.expectAndConsume())
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002344 return;
2345
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002346 SourceLocation EllipsisLoc;
2347 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002348 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002349 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002350 return;
2351 }
2352
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002353 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002354 if (EndLoc)
2355 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002356
Aaron Ballman00e99962013-08-31 01:11:41 +00002357 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002358 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002359 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2360 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002361}
2362
Richard Smith404dfb42013-11-19 22:47:36 +00002363/// Determine whether we're looking at something that might be a declarator
2364/// in a simple-declaration. If it can't possibly be a declarator, maybe
2365/// diagnose a missing semicolon after a prior tag definition in the decl
2366/// specifier.
2367///
2368/// \return \c true if an error occurred and this can't be any kind of
2369/// declaration.
2370bool
2371Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2372 DeclSpecContext DSContext,
2373 LateParsedAttrList *LateAttrs) {
2374 assert(DS.hasTagDefinition() && "shouldn't call this");
2375
2376 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002377
2378 if (getLangOpts().CPlusPlus &&
2379 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2380 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
2381 TryAnnotateCXXScopeToken(EnteringContext)) {
2382 SkipMalformedDecl();
2383 return true;
2384 }
2385
Richard Smith698875a2013-11-20 23:40:57 +00002386 bool HasScope = Tok.is(tok::annot_cxxscope);
2387 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2388 Token AfterScope = HasScope ? NextToken() : Tok;
2389
Richard Smith404dfb42013-11-19 22:47:36 +00002390 // Determine whether the following tokens could possibly be a
2391 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002392 bool MightBeDeclarator = true;
2393 if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) {
2394 // A declarator-id can't start with 'typename'.
2395 MightBeDeclarator = false;
2396 } else if (AfterScope.is(tok::annot_template_id)) {
2397 // If we have a type expressed as a template-id, this cannot be a
2398 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2399 TemplateIdAnnotation *Annot =
2400 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2401 if (Annot->Kind == TNK_Type_template)
2402 MightBeDeclarator = false;
2403 } else if (AfterScope.is(tok::identifier)) {
2404 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2405
Richard Smith404dfb42013-11-19 22:47:36 +00002406 // These tokens cannot come after the declarator-id in a
2407 // simple-declaration, and are likely to come after a type-specifier.
Richard Smith698875a2013-11-20 23:40:57 +00002408 if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) ||
2409 Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) ||
2410 Next.is(tok::coloncolon)) {
2411 // Missing a semicolon.
2412 MightBeDeclarator = false;
2413 } else if (HasScope) {
2414 // If the declarator-id has a scope specifier, it must redeclare a
2415 // previously-declared entity. If that's a type (and this is not a
2416 // typedef), that's an error.
2417 CXXScopeSpec SS;
2418 Actions.RestoreNestedNameSpecifierAnnotation(
2419 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2420 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2421 Sema::NameClassification Classification = Actions.ClassifyName(
2422 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2423 /*IsAddressOfOperand*/false);
2424 switch (Classification.getKind()) {
2425 case Sema::NC_Error:
2426 SkipMalformedDecl();
2427 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002428
Richard Smith698875a2013-11-20 23:40:57 +00002429 case Sema::NC_Keyword:
2430 case Sema::NC_NestedNameSpecifier:
2431 llvm_unreachable("typo correction and nested name specifiers not "
2432 "possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002433
Richard Smith698875a2013-11-20 23:40:57 +00002434 case Sema::NC_Type:
2435 case Sema::NC_TypeTemplate:
2436 // Not a previously-declared non-type entity.
2437 MightBeDeclarator = false;
2438 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002439
Richard Smith698875a2013-11-20 23:40:57 +00002440 case Sema::NC_Unknown:
2441 case Sema::NC_Expression:
2442 case Sema::NC_VarTemplate:
2443 case Sema::NC_FunctionTemplate:
2444 // Might be a redeclaration of a prior entity.
2445 break;
2446 }
Richard Smith404dfb42013-11-19 22:47:36 +00002447 }
Richard Smith404dfb42013-11-19 22:47:36 +00002448 }
2449
Richard Smith698875a2013-11-20 23:40:57 +00002450 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002451 return false;
2452
2453 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
Alp Toker383d2c42014-01-01 03:08:43 +00002454 diag::err_expected_after)
2455 << DeclSpec::getSpecifierName(DS.getTypeSpecType()) << tok::semi;
Richard Smith404dfb42013-11-19 22:47:36 +00002456
2457 // Try to recover from the typo, by dropping the tag definition and parsing
2458 // the problematic tokens as a type.
2459 //
2460 // FIXME: Split the DeclSpec into pieces for the standalone
2461 // declaration and pieces for the following declaration, instead
2462 // of assuming that all the other pieces attach to new declaration,
2463 // and call ParsedFreeStandingDeclSpec as appropriate.
2464 DS.ClearTypeSpecType();
2465 ParsedTemplateInfo NotATemplate;
2466 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2467 return false;
2468}
2469
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002470/// ParseDeclarationSpecifiers
2471/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002472/// storage-class-specifier declaration-specifiers[opt]
2473/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002474/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002475/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002476/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002477/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002478///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002479/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002480/// 'typedef'
2481/// 'extern'
2482/// 'static'
2483/// 'auto'
2484/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002485/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002486/// [C++11] 'thread_local'
2487/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002488/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002489/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002490/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002491/// [C++] 'virtual'
2492/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002493/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002494/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002495/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002496
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002497///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002498void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002499 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002500 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002501 DeclSpecContext DSContext,
2502 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002503 if (DS.getSourceRange().isInvalid()) {
2504 DS.SetRangeStart(Tok.getLocation());
2505 DS.SetRangeEnd(Tok.getLocation());
2506 }
Chad Rosierc1183952012-06-26 22:30:43 +00002507
Douglas Gregordf593fb2011-11-07 17:33:42 +00002508 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002509 bool AttrsLastTime = false;
2510 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002511 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002512 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002513 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002514 unsigned DiagID = 0;
2515
Chris Lattner4d8f8732006-11-28 05:05:08 +00002516 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002517
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002518 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002519 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002520 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002521 if (!AttrsLastTime)
2522 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002523 else {
2524 // Reject C++11 attributes that appertain to decl specifiers as
2525 // we don't support any C++11 attributes that appertain to decl
2526 // specifiers. This also conforms to what g++ 4.8 is doing.
2527 ProhibitCXX11Attributes(attrs);
2528
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002529 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002530 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002531
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002532 // If this is not a declaration specifier token, we're done reading decl
2533 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002534 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002535 return;
Mike Stump11289f42009-09-09 15:08:12 +00002536
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002537 case tok::l_square:
2538 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002539 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002540 goto DoneWithDeclSpec;
2541
2542 ProhibitAttributes(attrs);
2543 // FIXME: It would be good to recover by accepting the attributes,
2544 // but attempting to do that now would cause serious
2545 // madness in terms of diagnostics.
2546 attrs.clear();
2547 attrs.Range = SourceRange();
2548
2549 ParseCXX11Attributes(attrs);
2550 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002551 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002552
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002553 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002554 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002555 if (DS.hasTypeSpecifier()) {
2556 bool AllowNonIdentifiers
2557 = (getCurScope()->getFlags() & (Scope::ControlScope |
2558 Scope::BlockScope |
2559 Scope::TemplateParamScope |
2560 Scope::FunctionPrototypeScope |
2561 Scope::AtCatchScope)) == 0;
2562 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002563 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002564 (DSContext == DSC_class && DS.isFriendSpecified());
2565
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002566 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002567 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002568 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002569 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002570 }
2571
Douglas Gregor80039242011-02-15 20:33:25 +00002572 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2573 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2574 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002575 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002576 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002577 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002578 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002579 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002580 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002581
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002582 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002583 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002584 }
2585
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002586 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002587 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002588 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002589 if (!DS.hasTypeSpecifier())
2590 DS.SetTypeSpecError();
2591 goto DoneWithDeclSpec;
2592 }
John McCall8bc2a702010-03-01 18:20:46 +00002593 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2594 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002595 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002596
2597 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002598 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002599 goto DoneWithDeclSpec;
2600
John McCall9dab4e62009-12-12 11:40:51 +00002601 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002602 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2603 Tok.getAnnotationRange(),
2604 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002605
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002606 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002607 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002608 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002609 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002610 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002611 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002612
2613 // C++ [class.qual]p2:
2614 // In a lookup in which the constructor is an acceptable lookup
2615 // result and the nested-name-specifier nominates a class C:
2616 //
2617 // - if the name specified after the
2618 // nested-name-specifier, when looked up in C, is the
2619 // injected-class-name of C (Clause 9), or
2620 //
2621 // - if the name specified after the nested-name-specifier
2622 // is the same as the identifier or the
2623 // simple-template-id's template-name in the last
2624 // component of the nested-name-specifier,
2625 //
2626 // the name is instead considered to name the constructor of
2627 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002628 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002629 // Thus, if the template-name is actually the constructor
2630 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002631 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002632 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002633 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002634 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002635 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002636 if (isConstructorDeclarator()) {
2637 // The user meant this to be an out-of-line constructor
2638 // definition, but template arguments are not allowed
2639 // there. Just allow this as a constructor; we'll
2640 // complain about it later.
2641 goto DoneWithDeclSpec;
2642 }
2643
2644 // The user meant this to name a type, but it actually names
2645 // a constructor with some extraneous template
2646 // arguments. Complain, then parse it as a type as the user
2647 // intended.
2648 Diag(TemplateId->TemplateNameLoc,
2649 diag::err_out_of_line_template_id_names_constructor)
2650 << TemplateId->Name;
2651 }
2652
John McCall9dab4e62009-12-12 11:40:51 +00002653 DS.getTypeSpecScope() = SS;
2654 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002655 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002656 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002657 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002658 continue;
2659 }
2660
Douglas Gregorc5790df2009-09-28 07:26:33 +00002661 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002662 DS.getTypeSpecScope() = SS;
2663 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002664 if (Tok.getAnnotationValue()) {
2665 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002666 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002667 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002668 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002669 if (isInvalid)
2670 break;
John McCallba7bf592010-08-24 05:47:05 +00002671 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002672 else
2673 DS.SetTypeSpecError();
2674 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2675 ConsumeToken(); // The typename
2676 }
2677
Douglas Gregor167fa622009-03-25 15:40:00 +00002678 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002679 goto DoneWithDeclSpec;
2680
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002681 // If we're in a context where the identifier could be a class name,
2682 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002683 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002684 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002685 &SS)) {
2686 if (isConstructorDeclarator())
2687 goto DoneWithDeclSpec;
2688
2689 // As noted in C++ [class.qual]p2 (cited above), when the name
2690 // of the class is qualified in a context where it could name
2691 // a constructor, its a constructor name. However, we've
2692 // looked at the declarator, and the user probably meant this
2693 // to be a type. Complain that it isn't supposed to be treated
2694 // as a type, then proceed to parse it as a type.
2695 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2696 << Next.getIdentifierInfo();
2697 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002698
John McCallba7bf592010-08-24 05:47:05 +00002699 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2700 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002701 getCurScope(), &SS,
2702 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002703 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002704 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002705
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002706 // If the referenced identifier is not a type, then this declspec is
2707 // erroneous: We already checked about that it has no type specifier, and
2708 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002709 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002710 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002711 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002712 ParsedAttributesWithRange Attrs(AttrFactory);
2713 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2714 if (!Attrs.empty()) {
2715 AttrsLastTime = true;
2716 attrs.takeAllFrom(Attrs);
2717 }
2718 continue;
2719 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002720 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002721 }
Mike Stump11289f42009-09-09 15:08:12 +00002722
John McCall9dab4e62009-12-12 11:40:51 +00002723 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002724 ConsumeToken(); // The C++ scope.
2725
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002726 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002727 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002728 if (isInvalid)
2729 break;
Mike Stump11289f42009-09-09 15:08:12 +00002730
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002731 DS.SetRangeEnd(Tok.getLocation());
2732 ConsumeToken(); // The typename.
2733
2734 continue;
2735 }
Mike Stump11289f42009-09-09 15:08:12 +00002736
Chris Lattnere387d9e2009-01-21 19:48:37 +00002737 case tok::annot_typename: {
Richard Smith404dfb42013-11-19 22:47:36 +00002738 // If we've previously seen a tag definition, we were almost surely
2739 // missing a semicolon after it.
2740 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
2741 goto DoneWithDeclSpec;
2742
John McCallba7bf592010-08-24 05:47:05 +00002743 if (Tok.getAnnotationValue()) {
2744 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002745 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002746 DiagID, T);
2747 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002748 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002749
Chris Lattner005fc1b2010-04-05 18:18:31 +00002750 if (isInvalid)
2751 break;
2752
Chris Lattnere387d9e2009-01-21 19:48:37 +00002753 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2754 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002755
Chris Lattnere387d9e2009-01-21 19:48:37 +00002756 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2757 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002758 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002759 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002760 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002761
Chris Lattnere387d9e2009-01-21 19:48:37 +00002762 continue;
2763 }
Mike Stump11289f42009-09-09 15:08:12 +00002764
Douglas Gregor06873092011-04-28 15:48:45 +00002765 case tok::kw___is_signed:
2766 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2767 // typically treats it as a trait. If we see __is_signed as it appears
2768 // in libstdc++, e.g.,
2769 //
2770 // static const bool __is_signed;
2771 //
2772 // then treat __is_signed as an identifier rather than as a keyword.
2773 if (DS.getTypeSpecType() == TST_bool &&
2774 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
Alp Toker47642d22013-12-03 06:13:01 +00002775 DS.getStorageClassSpec() == DeclSpec::SCS_static)
2776 TryKeywordIdentFallback(true);
Douglas Gregor06873092011-04-28 15:48:45 +00002777
2778 // We're done with the declaration-specifiers.
2779 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002780
Chris Lattner16fac4f2008-07-26 01:18:38 +00002781 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002782 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002783 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002784 // In C++, check to see if this is a scope specifier like foo::bar::, if
2785 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002786 if (getLangOpts().CPlusPlus) {
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002787 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002788 if (!DS.hasTypeSpecifier())
2789 DS.SetTypeSpecError();
2790 goto DoneWithDeclSpec;
2791 }
2792 if (!Tok.is(tok::identifier))
2793 continue;
2794 }
Mike Stump11289f42009-09-09 15:08:12 +00002795
Chris Lattner16fac4f2008-07-26 01:18:38 +00002796 // This identifier can only be a typedef name if we haven't already seen
2797 // a type-specifier. Without this check we misparse:
2798 // typedef int X; struct Y { short X; }; as 'short int'.
2799 if (DS.hasTypeSpecifier())
2800 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002801
John Thompson22334602010-02-05 00:12:22 +00002802 // Check for need to substitute AltiVec keyword tokens.
2803 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2804 break;
2805
Richard Smith3092a3b2012-05-09 18:56:43 +00002806 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2807 // allow the use of a typedef name as a type specifier.
2808 if (DS.isTypeAltiVecVector())
2809 goto DoneWithDeclSpec;
2810
John McCallba7bf592010-08-24 05:47:05 +00002811 ParsedType TypeRep =
2812 Actions.getTypeName(*Tok.getIdentifierInfo(),
2813 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002814
Chris Lattner6cc055a2009-04-12 20:42:31 +00002815 // If this is not a typedef name, don't parse it as part of the declspec,
2816 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002817 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002818 ParsedAttributesWithRange Attrs(AttrFactory);
2819 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2820 if (!Attrs.empty()) {
2821 AttrsLastTime = true;
2822 attrs.takeAllFrom(Attrs);
2823 }
2824 continue;
2825 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002826 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002827 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002828
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002829 // If we're in a context where the identifier could be a class name,
2830 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002831 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002832 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002833 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002834 goto DoneWithDeclSpec;
2835
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002836 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002837 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002838 if (isInvalid)
2839 break;
Mike Stump11289f42009-09-09 15:08:12 +00002840
Chris Lattner16fac4f2008-07-26 01:18:38 +00002841 DS.SetRangeEnd(Tok.getLocation());
2842 ConsumeToken(); // The identifier
2843
2844 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2845 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002846 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002847 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002848 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002849
Steve Naroffcd5e7822008-09-22 10:28:57 +00002850 // Need to support trailing type qualifiers (e.g. "id<p> const").
2851 // If a type specifier follows, it will be diagnosed elsewhere.
2852 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002853 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002854
2855 // type-name
2856 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002857 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002858 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002859 // This template-id does not refer to a type name, so we're
2860 // done with the type-specifiers.
2861 goto DoneWithDeclSpec;
2862 }
2863
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002864 // If we're in a context where the template-id could be a
2865 // constructor name or specialization, check whether this is a
2866 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002867 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002868 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002869 isConstructorDeclarator())
2870 goto DoneWithDeclSpec;
2871
Douglas Gregor7f741122009-02-25 19:37:18 +00002872 // Turn the template-id annotation token into a type annotation
2873 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002874 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002875 continue;
2876 }
2877
Chris Lattnere37e2332006-08-15 04:50:22 +00002878 // GNU attributes support.
2879 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002880 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002881 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002882
2883 // Microsoft declspec support.
2884 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002885 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002886 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002887
Steve Naroff44ac7772008-12-25 14:16:32 +00002888 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002889 case tok::kw___forceinline: {
Serge Pavlov750db652013-11-13 06:57:53 +00002890 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002891 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002892 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002893 // FIXME: This does not work correctly if it is set to be a declspec
2894 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman00e99962013-08-31 01:11:41 +00002895 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2896 AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002897 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002898 }
Eli Friedman53339e02009-06-08 23:27:34 +00002899
Aaron Ballman317a77f2013-05-22 23:25:32 +00002900 case tok::kw___sptr:
2901 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002902 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002903 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002904 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002905 case tok::kw___cdecl:
2906 case tok::kw___stdcall:
2907 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002908 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002909 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002910 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002911 continue;
2912
Dawn Perchik335e16b2010-09-03 01:29:35 +00002913 // Borland single token adornments.
2914 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002915 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002916 continue;
2917
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002918 // OpenCL single token adornments.
2919 case tok::kw___kernel:
2920 ParseOpenCLAttributes(DS.getAttributes());
2921 continue;
2922
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002923 // storage-class-specifier
2924 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002925 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2926 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002927 break;
2928 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002929 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002930 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002931 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2932 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002933 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002934 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002935 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2936 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002937 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002938 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002939 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002940 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002941 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2942 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002943 break;
2944 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002945 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002946 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002947 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2948 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002949 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002950 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002951 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002952 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002953 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2954 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002955 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002956 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2957 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002958 break;
2959 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002960 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2961 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002962 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002963 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002964 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2965 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002966 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002967 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002968 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2969 PrevSpec, DiagID);
2970 break;
2971 case tok::kw_thread_local:
2972 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2973 PrevSpec, DiagID);
2974 break;
2975 case tok::kw__Thread_local:
2976 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2977 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002978 break;
Mike Stump11289f42009-09-09 15:08:12 +00002979
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002980 // function-specifier
2981 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00002982 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002983 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002984 case tok::kw_virtual:
Serge Pavlov750db652013-11-13 06:57:53 +00002985 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002986 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002987 case tok::kw_explicit:
Serge Pavlov750db652013-11-13 06:57:53 +00002988 isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002989 break;
Richard Smith0015f092013-01-17 22:16:11 +00002990 case tok::kw__Noreturn:
2991 if (!getLangOpts().C11)
2992 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlov750db652013-11-13 06:57:53 +00002993 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00002994 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002995
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002996 // alignment-specifier
2997 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002998 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002999 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003000 ParseAlignmentSpecifier(DS.getAttributes());
3001 continue;
3002
Anders Carlssoncd8db412009-05-06 04:46:28 +00003003 // friend
3004 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00003005 if (DSContext == DSC_class)
3006 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
3007 else {
3008 PrevSpec = ""; // not actually used by the diagnostic
3009 DiagID = diag::err_friend_invalid_in_context;
3010 isInvalid = true;
3011 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00003012 break;
Mike Stump11289f42009-09-09 15:08:12 +00003013
Douglas Gregor26701a42011-09-09 02:06:17 +00003014 // Modules
3015 case tok::kw___module_private__:
3016 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
3017 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003018
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003019 // constexpr
3020 case tok::kw_constexpr:
3021 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
3022 break;
3023
Chris Lattnere387d9e2009-01-21 19:48:37 +00003024 // type-specifier
3025 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00003026 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
3027 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003028 break;
3029 case tok::kw_long:
3030 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00003031 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
3032 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003033 else
John McCall49bfce42009-08-03 20:12:06 +00003034 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3035 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003036 break;
Francois Pichet84133e42011-04-28 01:59:37 +00003037 case tok::kw___int64:
3038 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3039 DiagID);
3040 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003041 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00003042 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3043 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003044 break;
3045 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003046 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3047 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003048 break;
3049 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00003050 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3051 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003052 break;
3053 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00003054 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3055 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003056 break;
3057 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003058 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
3059 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003060 break;
3061 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003062 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
3063 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003064 break;
3065 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003066 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
3067 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003068 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003069 case tok::kw___int128:
3070 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
3071 DiagID);
3072 break;
3073 case tok::kw_half:
3074 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
3075 DiagID);
3076 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003077 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003078 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
3079 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003080 break;
3081 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003082 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
3083 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003084 break;
3085 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003086 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
3087 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003088 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003089 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003090 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
3091 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003092 break;
3093 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003094 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
3095 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003096 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003097 case tok::kw_bool:
3098 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003099 if (Tok.is(tok::kw_bool) &&
3100 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3101 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3102 PrevSpec = ""; // Not used by the diagnostic.
3103 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003104 // For better error recovery.
3105 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003106 isInvalid = true;
3107 } else {
3108 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
3109 DiagID);
3110 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003111 break;
3112 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003113 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
3114 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003115 break;
3116 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003117 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
3118 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003119 break;
3120 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003121 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
3122 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003123 break;
John Thompson22334602010-02-05 00:12:22 +00003124 case tok::kw___vector:
3125 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3126 break;
3127 case tok::kw___pixel:
3128 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3129 break;
John McCall39439732011-04-09 22:50:59 +00003130 case tok::kw___unknown_anytype:
3131 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3132 PrevSpec, DiagID);
3133 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003134
3135 // class-specifier:
3136 case tok::kw_class:
3137 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003138 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003139 case tok::kw_union: {
3140 tok::TokenKind Kind = Tok.getKind();
3141 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003142
3143 // These are attributes following class specifiers.
3144 // To produce better diagnostic, we parse them when
3145 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003146 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003147 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003148 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003149
3150 // If there are attributes following class specifier,
3151 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003152 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003153 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003154 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003155 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003156 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003157 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003158
3159 // enum-specifier:
3160 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003161 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003162 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003163 continue;
3164
3165 // cv-qualifier:
3166 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003167 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003168 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003169 break;
3170 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003171 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003172 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003173 break;
3174 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003175 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003176 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003177 break;
3178
Douglas Gregor333489b2009-03-27 23:10:48 +00003179 // C++ typename-specifier:
3180 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003181 if (TryAnnotateTypeOrScopeToken()) {
3182 DS.SetTypeSpecError();
3183 goto DoneWithDeclSpec;
3184 }
3185 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003186 continue;
3187 break;
3188
Chris Lattnere387d9e2009-01-21 19:48:37 +00003189 // GNU typeof support.
3190 case tok::kw_typeof:
3191 ParseTypeofSpecifier(DS);
3192 continue;
3193
David Blaikie15a430a2011-12-04 05:04:18 +00003194 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003195 ParseDecltypeSpecifier(DS);
3196 continue;
3197
Alexis Hunt4a257072011-05-19 05:37:45 +00003198 case tok::kw___underlying_type:
3199 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003200 continue;
3201
3202 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003203 // C11 6.7.2.4/4:
3204 // If the _Atomic keyword is immediately followed by a left parenthesis,
3205 // it is interpreted as a type specifier (with a type name), not as a
3206 // type qualifier.
3207 if (NextToken().is(tok::l_paren)) {
3208 ParseAtomicSpecifier(DS);
3209 continue;
3210 }
3211 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3212 getLangOpts());
3213 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003214
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003215 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003216 case tok::kw___private:
3217 case tok::kw___global:
3218 case tok::kw___local:
3219 case tok::kw___constant:
3220 case tok::kw___read_only:
3221 case tok::kw___write_only:
3222 case tok::kw___read_write:
3223 ParseOpenCLQualifiers(DS);
3224 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003225
Steve Naroffcfdf6162008-06-05 00:02:44 +00003226 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003227 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003228 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3229 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003230 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003231 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003232
Douglas Gregor3a001f42010-11-19 17:10:50 +00003233 if (!ParseObjCProtocolQualifiers(DS))
3234 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3235 << FixItHint::CreateInsertion(Loc, "id")
3236 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003237
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003238 // Need to support trailing type qualifiers (e.g. "id<p> const").
3239 // If a type specifier follows, it will be diagnosed elsewhere.
3240 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003241 }
John McCall49bfce42009-08-03 20:12:06 +00003242 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003243 if (isInvalid) {
3244 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003245 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003246
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003247 if (DiagID == diag::ext_duplicate_declspec)
3248 Diag(Tok, DiagID)
3249 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3250 else
3251 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003252 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003253
Chris Lattner2e232092008-03-13 06:29:04 +00003254 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003255 if (DiagID != diag::err_bool_redeclaration)
3256 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003257
3258 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003259 }
3260}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003261
Chris Lattner70ae4912007-10-29 04:42:53 +00003262/// ParseStructDeclaration - Parse a struct declaration without the terminating
3263/// semicolon.
3264///
Chris Lattner90a26b02007-01-23 04:38:16 +00003265/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003266/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003267/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003268/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003269/// struct-declarator-list:
3270/// struct-declarator
3271/// struct-declarator-list ',' struct-declarator
3272/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3273/// struct-declarator:
3274/// declarator
3275/// [GNU] declarator attributes[opt]
3276/// declarator[opt] ':' constant-expression
3277/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3278///
Chris Lattnera12405b2008-04-10 06:46:29 +00003279void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003280ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003281
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003282 if (Tok.is(tok::kw___extension__)) {
3283 // __extension__ silences extension warnings in the subexpression.
3284 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003285 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003286 return ParseStructDeclaration(DS, Fields);
3287 }
Mike Stump11289f42009-09-09 15:08:12 +00003288
Steve Naroff97170802007-08-20 22:28:22 +00003289 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003290 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003291
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003292 // If there are no declarators, this is a free-standing declaration
3293 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003294 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003295 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3296 DS);
3297 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003298 return;
3299 }
3300
3301 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003302 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003303 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003304 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003305 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003306 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003307
Bill Wendling44426052012-12-20 19:22:21 +00003308 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003309 if (!FirstDeclarator)
3310 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003311
Steve Naroff97170802007-08-20 22:28:22 +00003312 /// struct-declarator: declarator
3313 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003314 if (Tok.isNot(tok::colon)) {
3315 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3316 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003317 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003318 }
Mike Stump11289f42009-09-09 15:08:12 +00003319
Alp Toker8fbec672013-12-17 23:29:36 +00003320 if (TryConsumeToken(tok::colon)) {
John McCalldadc5752010-08-24 06:29:42 +00003321 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003322 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003323 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00003324 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003325 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003326 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003327
Steve Naroff97170802007-08-20 22:28:22 +00003328 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003329 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003330
John McCallcfefb6d2009-11-03 02:38:08 +00003331 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003332 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003333
Steve Naroff97170802007-08-20 22:28:22 +00003334 // If we don't have a comma, it is either the end of the list (a ';')
3335 // or an error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00003336 if (!TryConsumeToken(tok::comma, CommaLoc))
Chris Lattner70ae4912007-10-29 04:42:53 +00003337 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003338
John McCallcfefb6d2009-11-03 02:38:08 +00003339 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003340 }
Steve Naroff97170802007-08-20 22:28:22 +00003341}
3342
3343/// ParseStructUnionBody
3344/// struct-contents:
3345/// struct-declaration-list
3346/// [EXT] empty
3347/// [GNU] "struct-declaration-list" without terminatoring ';'
3348/// struct-declaration-list:
3349/// struct-declaration
3350/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003351/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003352///
Chris Lattner1300fb92007-01-23 23:42:53 +00003353void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003354 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003355 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3356 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003357 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003358
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003359 BalancedDelimiterTracker T(*this, tok::l_brace);
3360 if (T.consumeOpen())
3361 return;
Mike Stump11289f42009-09-09 15:08:12 +00003362
Douglas Gregor658b9552009-01-09 22:42:13 +00003363 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003364 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003365
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003366 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003367
Chris Lattner7b9ace62007-01-23 20:11:08 +00003368 // While we still have something to read, read the declarations in the struct.
Richard Smith34f30512013-11-23 04:06:09 +00003369 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003370 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003371
Chris Lattner736ed5d2007-06-09 05:59:07 +00003372 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003373 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003374 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003375 continue;
3376 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003377
Andy Gibbsc804e082013-04-03 09:46:04 +00003378 // Parse _Static_assert declaration.
3379 if (Tok.is(tok::kw__Static_assert)) {
3380 SourceLocation DeclEnd;
3381 ParseStaticAssertDeclaration(DeclEnd);
3382 continue;
3383 }
3384
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003385 if (Tok.is(tok::annot_pragma_pack)) {
3386 HandlePragmaPack();
3387 continue;
3388 }
3389
3390 if (Tok.is(tok::annot_pragma_align)) {
3391 HandlePragmaAlign();
3392 continue;
3393 }
3394
John McCallcfefb6d2009-11-03 02:38:08 +00003395 if (!Tok.is(tok::at)) {
3396 struct CFieldCallback : FieldCallback {
3397 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003398 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003399 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003400
John McCall48871652010-08-21 09:40:31 +00003401 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003402 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003403 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3404
Eli Friedman934dbbf2012-08-08 23:53:27 +00003405 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003406 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003407 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003408 FD.D.getDeclSpec().getSourceRange().getBegin(),
3409 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003410 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003411 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003412 }
John McCallcfefb6d2009-11-03 02:38:08 +00003413 } Callback(*this, TagDecl, FieldDecls);
3414
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003415 // Parse all the comma separated declarators.
3416 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003417 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003418 } else { // Handle @defs
3419 ConsumeToken();
3420 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3421 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003422 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003423 continue;
3424 }
3425 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003426 ExpectAndConsume(tok::l_paren);
Chris Lattner535b8302008-06-21 19:39:06 +00003427 if (!Tok.is(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003428 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003429 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003430 continue;
3431 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003432 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003433 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003434 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003435 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3436 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003437 ExpectAndConsume(tok::r_paren);
Mike Stump11289f42009-09-09 15:08:12 +00003438 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003439
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003440 if (TryConsumeToken(tok::semi))
3441 continue;
3442
3443 if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003444 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003445 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003446 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003447
3448 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3449 // Skip to end of block or statement to avoid ext-warning on extra ';'.
3450 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
3451 // If we stopped at a ';', eat it.
3452 TryConsumeToken(tok::semi);
Chris Lattner90a26b02007-01-23 04:38:16 +00003453 }
Mike Stump11289f42009-09-09 15:08:12 +00003454
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003455 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003456
John McCall084e83d2011-03-24 11:26:52 +00003457 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003458 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003459 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003460
Douglas Gregor0be31a22010-07-02 17:43:08 +00003461 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003462 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003463 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003464 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003465 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003466 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3467 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003468}
3469
Chris Lattner3b561a32006-08-13 00:12:11 +00003470/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003471/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003472/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003473///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003474/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3475/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003476/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3477/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003478/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003479/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003480///
Richard Smith7d137e32012-03-23 03:33:32 +00003481/// [C++11] enum-head '{' enumerator-list[opt] '}'
3482/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003483///
Richard Smith7d137e32012-03-23 03:33:32 +00003484/// enum-head: [C++11]
3485/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3486/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3487/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003488///
Richard Smith7d137e32012-03-23 03:33:32 +00003489/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003490/// 'enum'
3491/// 'enum' 'class'
3492/// 'enum' 'struct'
3493///
Richard Smith7d137e32012-03-23 03:33:32 +00003494/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003495/// ':' type-specifier-seq
3496///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003497/// [C++] elaborated-type-specifier:
3498/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3499///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003500void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003501 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003502 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003503 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003504 if (Tok.is(tok::code_completion)) {
3505 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003506 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003507 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003508 }
John McCallcb432fa2011-07-06 05:58:41 +00003509
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003510 // If attributes exist after tag, parse them.
3511 ParsedAttributesWithRange attrs(AttrFactory);
3512 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003513 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003514
3515 // If declspecs exist after tag, parse them.
3516 while (Tok.is(tok::kw___declspec))
3517 ParseMicrosoftDeclSpec(attrs);
3518
Richard Smith0f8ee222012-01-10 01:33:14 +00003519 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003520 bool IsScopedUsingClassTag = false;
3521
John McCallbeae29a2012-06-23 22:30:04 +00003522 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003523 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3524 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3525 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003526 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003527 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003528
Bill Wendling44426052012-12-20 19:22:21 +00003529 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003530 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003531 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003532
3533 // They are allowed afterwards, though.
3534 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003535 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003536 while (Tok.is(tok::kw___declspec))
3537 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003538 }
Richard Smith7d137e32012-03-23 03:33:32 +00003539
John McCall6347b682012-05-07 06:16:58 +00003540 // C++11 [temp.explicit]p12:
3541 // The usual access controls do not apply to names used to specify
3542 // explicit instantiations.
3543 // We extend this to also cover explicit specializations. Note that
3544 // we don't suppress if this turns out to be an elaborated type
3545 // specifier.
3546 bool shouldDelayDiagsInTag =
3547 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3548 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3549 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003550
Richard Smithbfdb1082012-03-12 08:56:40 +00003551 // Enum definitions should not be parsed in a trailing-return-type.
3552 bool AllowDeclaration = DSC != DSC_trailing;
3553
3554 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003555 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003556 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003557
Abramo Bagnarad7548482010-05-19 21:37:53 +00003558 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003559 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003560 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3561 // if a fixed underlying type is allowed.
3562 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003563
3564 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003565 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003566 return;
3567
3568 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003569 Diag(Tok, diag::err_expected) << tok::identifier;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003570 if (Tok.isNot(tok::l_brace)) {
3571 // Has no name and is not a definition.
3572 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003573 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003574 return;
3575 }
3576 }
3577 }
Mike Stump11289f42009-09-09 15:08:12 +00003578
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003579 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003580 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003581 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Alp Tokerec543272013-12-24 09:48:30 +00003582 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
Mike Stump11289f42009-09-09 15:08:12 +00003583
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003584 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003585 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00003586 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003587 }
Mike Stump11289f42009-09-09 15:08:12 +00003588
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003589 // If an identifier is present, consume and remember it.
3590 IdentifierInfo *Name = 0;
3591 SourceLocation NameLoc;
3592 if (Tok.is(tok::identifier)) {
3593 Name = Tok.getIdentifierInfo();
3594 NameLoc = ConsumeToken();
3595 }
Mike Stump11289f42009-09-09 15:08:12 +00003596
Richard Smith0f8ee222012-01-10 01:33:14 +00003597 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003598 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3599 // declaration of a scoped enumeration.
3600 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003601 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003602 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003603 }
3604
John McCall6347b682012-05-07 06:16:58 +00003605 // Okay, end the suppression area. We'll decide whether to emit the
3606 // diagnostics in a second.
3607 if (shouldDelayDiagsInTag)
3608 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003609
Douglas Gregor0bf31402010-10-08 23:50:27 +00003610 TypeResult BaseType;
3611
Douglas Gregord1f69f62010-12-01 17:42:47 +00003612 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003613 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003614 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003615 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003616 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003617 // If we're in class scope, this can either be an enum declaration with
3618 // an underlying type, or a declaration of a bitfield member. We try to
3619 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003620 // (integer literal, sizeof); if it's still ambiguous, we then consider
3621 // anything that's a simple-type-specifier followed by '(' as an
3622 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003623 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003624 EnterExpressionEvaluationContext Unevaluated(Actions,
3625 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003626 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003627 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003628 // bit-field. This is the common case.
3629 if (TPR == TPResult::True())
3630 PossibleBitfield = true;
3631 // If the next token starts a type-specifier-seq, it may be either a
3632 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003633 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003634 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003635 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003636 GetLookAheadToken(2).getKind() == tok::semi) {
3637 // Consume the ':'.
3638 ConsumeToken();
3639 } else {
3640 // We have the start of a type-specifier-seq, so we have to perform
3641 // tentative parsing to determine whether we have an expression or a
3642 // type.
3643 TentativeParsingAction TPA(*this);
3644
3645 // Consume the ':'.
3646 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003647
3648 // If we see a type specifier followed by an open-brace, we have an
3649 // ambiguity between an underlying type and a C++11 braced
3650 // function-style cast. Resolve this by always treating it as an
3651 // underlying type.
3652 // FIXME: The standard is not entirely clear on how to disambiguate in
3653 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003654 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003655 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003656 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003657 // We'll parse this as a bitfield later.
3658 PossibleBitfield = true;
3659 TPA.Revert();
3660 } else {
3661 // We have a type-specifier-seq.
3662 TPA.Commit();
3663 }
3664 }
3665 } else {
3666 // Consume the ':'.
3667 ConsumeToken();
3668 }
3669
3670 if (!PossibleBitfield) {
3671 SourceRange Range;
3672 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003673
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003674 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003675 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003676 } else if (!getLangOpts().ObjC2) {
3677 if (getLangOpts().CPlusPlus)
3678 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3679 else
3680 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3681 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003682 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003683 }
3684
Richard Smith0f8ee222012-01-10 01:33:14 +00003685 // There are four options here. If we have 'friend enum foo;' then this is a
3686 // friend declaration, and cannot have an accompanying definition. If we have
3687 // 'enum foo;', then this is a forward declaration. If we have
3688 // 'enum foo {...' then this is a definition. Otherwise we have something
3689 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003690 //
3691 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3692 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3693 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3694 //
John McCallfaf5fb42010-08-26 23:41:50 +00003695 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003696 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003697 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003698 } else if (Tok.is(tok::l_brace)) {
3699 if (DS.isFriendSpecified()) {
3700 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3701 << SourceRange(DS.getFriendSpecLoc());
3702 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003703 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00003704 TUK = Sema::TUK_Friend;
3705 } else {
3706 TUK = Sema::TUK_Definition;
3707 }
Richard Smith649c7b062014-01-08 00:56:48 +00003708 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00003709 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003710 (Tok.isAtStartOfLine() &&
3711 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003712 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3713 if (Tok.isNot(tok::semi)) {
3714 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00003715 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003716 PP.EnterToken(Tok);
3717 Tok.setKind(tok::semi);
3718 }
John McCall6347b682012-05-07 06:16:58 +00003719 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003720 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003721 }
3722
3723 // If this is an elaborated type specifier, and we delayed
3724 // diagnostics before, just merge them into the current pool.
3725 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3726 diagsFromTag.redelay();
3727 }
Richard Smith7d137e32012-03-23 03:33:32 +00003728
3729 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003730 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003731 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003732 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003733 // Skip the rest of this declarator, up until the comma or semicolon.
3734 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003735 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00003736 return;
3737 }
3738
3739 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3740 // Enumerations can't be explicitly instantiated.
3741 DS.SetTypeSpecError();
3742 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3743 return;
3744 }
3745
3746 assert(TemplateInfo.TemplateParams && "no template parameters");
3747 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3748 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003749 }
Chad Rosierc1183952012-06-26 22:30:43 +00003750
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003751 if (TUK == Sema::TUK_Reference)
3752 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003753
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003754 if (!Name && TUK != Sema::TUK_Definition) {
3755 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003756
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003757 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003758 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003759 return;
3760 }
Richard Smith7d137e32012-03-23 03:33:32 +00003761
Douglas Gregord6ab8742009-05-28 23:31:59 +00003762 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003763 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003764 const char *PrevSpec = 0;
3765 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003766 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003767 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003768 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003769 Owned, IsDependent, ScopedEnumKWLoc,
Richard Smith649c7b062014-01-08 00:56:48 +00003770 IsScopedUsingClassTag, BaseType,
3771 DSC == DSC_type_specifier);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003772
Douglas Gregorba41d012010-04-24 16:38:41 +00003773 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003774 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003775 // dependent tag.
3776 if (!Name) {
3777 DS.SetTypeSpecError();
3778 Diag(Tok, diag::err_expected_type_name_after_typename);
3779 return;
3780 }
Chad Rosierc1183952012-06-26 22:30:43 +00003781
Douglas Gregor0be31a22010-07-02 17:43:08 +00003782 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003783 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003784 NameLoc);
3785 if (Type.isInvalid()) {
3786 DS.SetTypeSpecError();
3787 return;
3788 }
Chad Rosierc1183952012-06-26 22:30:43 +00003789
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003790 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3791 NameLoc.isValid() ? NameLoc : StartLoc,
3792 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003793 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003794
Douglas Gregorba41d012010-04-24 16:38:41 +00003795 return;
3796 }
Mike Stump11289f42009-09-09 15:08:12 +00003797
John McCall48871652010-08-21 09:40:31 +00003798 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003799 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003800 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003801 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003802 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003803 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00003804 }
Chad Rosierc1183952012-06-26 22:30:43 +00003805
Douglas Gregorba41d012010-04-24 16:38:41 +00003806 DS.SetTypeSpecError();
3807 return;
3808 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003809
Richard Smith369b9f92012-06-25 21:37:02 +00003810 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003811 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003812
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003813 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3814 NameLoc.isValid() ? NameLoc : StartLoc,
3815 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003816 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003817}
3818
Chris Lattnerc1915e22007-01-25 07:29:02 +00003819/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3820/// enumerator-list:
3821/// enumerator
3822/// enumerator-list ',' enumerator
3823/// enumerator:
3824/// enumeration-constant
3825/// enumeration-constant '=' constant-expression
3826/// enumeration-constant:
3827/// identifier
3828///
John McCall48871652010-08-21 09:40:31 +00003829void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003830 // Enter the scope of the enum body and start the definition.
3831 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003832 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003833
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003834 BalancedDelimiterTracker T(*this, tok::l_brace);
3835 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003836
Chris Lattner37256fb2007-08-27 17:24:30 +00003837 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003838 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003839 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003840
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003841 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003842
John McCall48871652010-08-21 09:40:31 +00003843 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003844
Chris Lattnerc1915e22007-01-25 07:29:02 +00003845 // Parse the enumerator-list.
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003846 while (Tok.isNot(tok::r_brace)) {
3847 // Parse enumerator. If failed, try skipping till the start of the next
3848 // enumerator definition.
3849 if (Tok.isNot(tok::identifier)) {
3850 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
3851 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
3852 TryConsumeToken(tok::comma))
3853 continue;
3854 break;
3855 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003856 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3857 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003858
John McCall811a0f52010-10-22 23:36:17 +00003859 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003860 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003861 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003862 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003863 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003864
Chris Lattnerc1915e22007-01-25 07:29:02 +00003865 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003866 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003867 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003868
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003869 if (TryConsumeToken(tok::equal, EqualLoc)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003870 AssignedVal = ParseConstantExpression();
3871 if (AssignedVal.isInvalid())
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003872 SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003873 }
Mike Stump11289f42009-09-09 15:08:12 +00003874
Chris Lattnerc1915e22007-01-25 07:29:02 +00003875 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003876 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3877 LastEnumConstDecl,
3878 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003879 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003880 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003881 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003882
Chris Lattner4ef40012007-06-11 01:28:17 +00003883 EnumConstantDecls.push_back(EnumConstDecl);
3884 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003885
Douglas Gregorce66d022010-09-07 14:51:08 +00003886 if (Tok.is(tok::identifier)) {
3887 // We're missing a comma between enumerators.
3888 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003889 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003890 << FixItHint::CreateInsertion(Loc, ", ");
3891 continue;
3892 }
Chad Rosierc1183952012-06-26 22:30:43 +00003893
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003894 // Emumerator definition must be finished, only comma or r_brace are
3895 // allowed here.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003896 SourceLocation CommaLoc;
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003897 if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
3898 if (EqualLoc.isValid())
3899 Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
3900 << tok::comma;
3901 else
3902 Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
3903 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
3904 if (TryConsumeToken(tok::comma, CommaLoc))
3905 continue;
3906 } else {
3907 break;
3908 }
3909 }
Mike Stump11289f42009-09-09 15:08:12 +00003910
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003911 // If comma is followed by r_brace, emit appropriate warning.
3912 if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003913 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003914 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3915 diag::ext_enumerator_list_comma_cxx :
3916 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003917 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003918 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003919 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3920 << FixItHint::CreateRemoval(CommaLoc);
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003921 break;
Richard Smith5d164bc2011-10-15 05:09:34 +00003922 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003923 }
Mike Stump11289f42009-09-09 15:08:12 +00003924
Chris Lattnerc1915e22007-01-25 07:29:02 +00003925 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003926 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003927
Chris Lattnerc1915e22007-01-25 07:29:02 +00003928 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003929 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003930 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003931
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003932 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003933 EnumDecl, EnumConstantDecls,
3934 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003935 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003936
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003937 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003938 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3939 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003940
3941 // The next token must be valid after an enum definition. If not, a ';'
3942 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003943 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3944 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Alp Toker383d2c42014-01-01 03:08:43 +00003945 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003946 // Push this token back into the preprocessor and change our current token
3947 // to ';' so that the rest of the code recovers as though there were an
3948 // ';' after the definition.
3949 PP.EnterToken(Tok);
3950 Tok.setKind(tok::semi);
3951 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003952}
Chris Lattner3b561a32006-08-13 00:12:11 +00003953
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003954/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003955/// start of a type-qualifier-list.
3956bool Parser::isTypeQualifier() const {
3957 switch (Tok.getKind()) {
3958 default: return false;
Alp Tokerde50ff32013-12-17 18:17:46 +00003959 // type-qualifier
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003960 case tok::kw_const:
3961 case tok::kw_volatile:
3962 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003963 case tok::kw___private:
3964 case tok::kw___local:
3965 case tok::kw___global:
3966 case tok::kw___constant:
3967 case tok::kw___read_only:
3968 case tok::kw___read_write:
3969 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003970 return true;
3971 }
3972}
3973
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003974/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3975/// is definitely a type-specifier. Return false if it isn't part of a type
3976/// specifier or if we're not sure.
3977bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3978 switch (Tok.getKind()) {
3979 default: return false;
3980 // type-specifiers
3981 case tok::kw_short:
3982 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003983 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003984 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003985 case tok::kw_signed:
3986 case tok::kw_unsigned:
3987 case tok::kw__Complex:
3988 case tok::kw__Imaginary:
3989 case tok::kw_void:
3990 case tok::kw_char:
3991 case tok::kw_wchar_t:
3992 case tok::kw_char16_t:
3993 case tok::kw_char32_t:
3994 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003995 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003996 case tok::kw_float:
3997 case tok::kw_double:
3998 case tok::kw_bool:
3999 case tok::kw__Bool:
4000 case tok::kw__Decimal32:
4001 case tok::kw__Decimal64:
4002 case tok::kw__Decimal128:
4003 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00004004
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004005 // struct-or-union-specifier (C99) or class-specifier (C++)
4006 case tok::kw_class:
4007 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004008 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004009 case tok::kw_union:
4010 // enum-specifier
4011 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00004012
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004013 // typedef-name
4014 case tok::annot_typename:
4015 return true;
4016 }
4017}
4018
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004019/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004020/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004021bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004022 switch (Tok.getKind()) {
4023 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004024
Chris Lattner020bab92009-01-04 23:41:41 +00004025 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00004026 if (TryAltiVecVectorToken())
4027 return true;
4028 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00004029 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004030 // Annotate typenames and C++ scope specifiers. If we get one, just
4031 // recurse to handle whatever we get.
4032 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004033 return true;
4034 if (Tok.is(tok::identifier))
4035 return false;
4036 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00004037
Chris Lattner020bab92009-01-04 23:41:41 +00004038 case tok::coloncolon: // ::foo::bar
4039 if (NextToken().is(tok::kw_new) || // ::new
4040 NextToken().is(tok::kw_delete)) // ::delete
4041 return false;
4042
Chris Lattner020bab92009-01-04 23:41:41 +00004043 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004044 return true;
4045 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004046
Chris Lattnere37e2332006-08-15 04:50:22 +00004047 // GNU attributes support.
4048 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004049 // GNU typeof support.
4050 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004051
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004052 // type-specifiers
4053 case tok::kw_short:
4054 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004055 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004056 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004057 case tok::kw_signed:
4058 case tok::kw_unsigned:
4059 case tok::kw__Complex:
4060 case tok::kw__Imaginary:
4061 case tok::kw_void:
4062 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004063 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004064 case tok::kw_char16_t:
4065 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004066 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004067 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004068 case tok::kw_float:
4069 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004070 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004071 case tok::kw__Bool:
4072 case tok::kw__Decimal32:
4073 case tok::kw__Decimal64:
4074 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004075 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004076
Chris Lattner861a2262008-04-13 18:59:07 +00004077 // struct-or-union-specifier (C99) or class-specifier (C++)
4078 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004079 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004080 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004081 case tok::kw_union:
4082 // enum-specifier
4083 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004084
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004085 // type-qualifier
4086 case tok::kw_const:
4087 case tok::kw_volatile:
4088 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004089
John McCallea0a39e2012-11-14 00:49:39 +00004090 // Debugger support.
4091 case tok::kw___unknown_anytype:
4092
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004093 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004094 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004095 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004096
Chris Lattner409bf7d2008-10-20 00:25:30 +00004097 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4098 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004099 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004100
Steve Naroff44ac7772008-12-25 14:16:32 +00004101 case tok::kw___cdecl:
4102 case tok::kw___stdcall:
4103 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004104 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004105 case tok::kw___w64:
4106 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004107 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004108 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004109 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004110
4111 case tok::kw___private:
4112 case tok::kw___local:
4113 case tok::kw___global:
4114 case tok::kw___constant:
4115 case tok::kw___read_only:
4116 case tok::kw___read_write:
4117 case tok::kw___write_only:
4118
Eli Friedman53339e02009-06-08 23:27:34 +00004119 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004120
Richard Smith8e1ac332013-03-28 01:55:44 +00004121 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004122 case tok::kw__Atomic:
4123 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004124 }
4125}
4126
Chris Lattneracd58a32006-08-06 17:24:14 +00004127/// isDeclarationSpecifier() - Return true if the current token is part of a
4128/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004129///
4130/// \param DisambiguatingWithExpression True to indicate that the purpose of
4131/// this check is to disambiguate between an expression and a declaration.
4132bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004133 switch (Tok.getKind()) {
4134 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004135
Chris Lattner020bab92009-01-04 23:41:41 +00004136 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004137 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004138 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004139 return false;
John Thompson22334602010-02-05 00:12:22 +00004140 if (TryAltiVecVectorToken())
4141 return true;
4142 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004143 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004144 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004145 // Annotate typenames and C++ scope specifiers. If we get one, just
4146 // recurse to handle whatever we get.
4147 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004148 return true;
4149 if (Tok.is(tok::identifier))
4150 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004151
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004152 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004153 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004154 // expression is permitted, then this is probably a class message send
4155 // missing the initial '['. In this case, we won't consider this to be
4156 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004157 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004158 isStartOfObjCClassMessageMissingOpenBracket())
4159 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004160
John McCall1f476a12010-02-26 08:45:28 +00004161 return isDeclarationSpecifier();
4162
Chris Lattner020bab92009-01-04 23:41:41 +00004163 case tok::coloncolon: // ::foo::bar
4164 if (NextToken().is(tok::kw_new) || // ::new
4165 NextToken().is(tok::kw_delete)) // ::delete
4166 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004167
Chris Lattner020bab92009-01-04 23:41:41 +00004168 // Annotate typenames and C++ scope specifiers. If we get one, just
4169 // recurse to handle whatever we get.
4170 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004171 return true;
4172 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004173
Chris Lattneracd58a32006-08-06 17:24:14 +00004174 // storage-class-specifier
4175 case tok::kw_typedef:
4176 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004177 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004178 case tok::kw_static:
4179 case tok::kw_auto:
4180 case tok::kw_register:
4181 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004182 case tok::kw_thread_local:
4183 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004184
Douglas Gregor26701a42011-09-09 02:06:17 +00004185 // Modules
4186 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004187
John McCallea0a39e2012-11-14 00:49:39 +00004188 // Debugger support
4189 case tok::kw___unknown_anytype:
4190
Chris Lattneracd58a32006-08-06 17:24:14 +00004191 // type-specifiers
4192 case tok::kw_short:
4193 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004194 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004195 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004196 case tok::kw_signed:
4197 case tok::kw_unsigned:
4198 case tok::kw__Complex:
4199 case tok::kw__Imaginary:
4200 case tok::kw_void:
4201 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004202 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004203 case tok::kw_char16_t:
4204 case tok::kw_char32_t:
4205
Chris Lattneracd58a32006-08-06 17:24:14 +00004206 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004207 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004208 case tok::kw_float:
4209 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004210 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004211 case tok::kw__Bool:
4212 case tok::kw__Decimal32:
4213 case tok::kw__Decimal64:
4214 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004215 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004216
Chris Lattner861a2262008-04-13 18:59:07 +00004217 // struct-or-union-specifier (C99) or class-specifier (C++)
4218 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004219 case tok::kw_struct:
4220 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004221 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004222 // enum-specifier
4223 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004224
Chris Lattneracd58a32006-08-06 17:24:14 +00004225 // type-qualifier
4226 case tok::kw_const:
4227 case tok::kw_volatile:
4228 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004229
Chris Lattneracd58a32006-08-06 17:24:14 +00004230 // function-specifier
4231 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004232 case tok::kw_virtual:
4233 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004234 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004235
Richard Smith1dba27c2013-01-29 09:02:09 +00004236 // alignment-specifier
4237 case tok::kw__Alignas:
4238
Richard Smithd16fe122012-10-25 00:00:53 +00004239 // friend keyword.
4240 case tok::kw_friend:
4241
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004242 // static_assert-declaration
4243 case tok::kw__Static_assert:
4244
Chris Lattner599e47e2007-08-09 17:01:07 +00004245 // GNU typeof support.
4246 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004247
Chris Lattner599e47e2007-08-09 17:01:07 +00004248 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004249 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004250
Richard Smithd16fe122012-10-25 00:00:53 +00004251 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004252 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004253 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004254
Richard Smith8e1ac332013-03-28 01:55:44 +00004255 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004256 case tok::kw__Atomic:
4257 return true;
4258
Chris Lattner8b2ec162008-07-26 03:38:44 +00004259 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4260 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004261 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004262
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004263 // typedef-name
4264 case tok::annot_typename:
4265 return !DisambiguatingWithExpression ||
4266 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004267
Steve Narofff192fab2009-01-06 19:34:12 +00004268 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004269 case tok::kw___cdecl:
4270 case tok::kw___stdcall:
4271 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004272 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004273 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004274 case tok::kw___sptr:
4275 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004276 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004277 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004278 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004279 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004280 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004281
4282 case tok::kw___private:
4283 case tok::kw___local:
4284 case tok::kw___global:
4285 case tok::kw___constant:
4286 case tok::kw___read_only:
4287 case tok::kw___read_write:
4288 case tok::kw___write_only:
4289
Eli Friedman53339e02009-06-08 23:27:34 +00004290 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004291 }
4292}
4293
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004294bool Parser::isConstructorDeclarator() {
4295 TentativeParsingAction TPA(*this);
4296
4297 // Parse the C++ scope specifier.
4298 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004299 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004300 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004301 TPA.Revert();
4302 return false;
4303 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004304
4305 // Parse the constructor name.
4306 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4307 // We already know that we have a constructor name; just consume
4308 // the token.
4309 ConsumeToken();
4310 } else {
4311 TPA.Revert();
4312 return false;
4313 }
4314
Richard Smith43f340f2012-03-27 23:05:05 +00004315 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004316 if (Tok.isNot(tok::l_paren)) {
4317 TPA.Revert();
4318 return false;
4319 }
4320 ConsumeParen();
4321
Richard Smith43f340f2012-03-27 23:05:05 +00004322 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4323 // that we have a constructor.
4324 if (Tok.is(tok::r_paren) ||
4325 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004326 TPA.Revert();
4327 return true;
4328 }
4329
Richard Smithf2163662013-09-06 00:12:20 +00004330 // A C++11 attribute here signals that we have a constructor, and is an
4331 // attribute on the first constructor parameter.
4332 if (getLangOpts().CPlusPlus11 &&
4333 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4334 /*OuterMightBeMessageSend*/ true)) {
4335 TPA.Revert();
4336 return true;
4337 }
4338
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004339 // If we need to, enter the specified scope.
4340 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004341 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004342 DeclScopeObj.EnterDeclaratorScope();
4343
Francois Pichet79f3a872011-01-31 04:54:32 +00004344 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004345 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004346 MaybeParseMicrosoftAttributes(Attrs);
4347
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004348 // Check whether the next token(s) are part of a declaration
4349 // specifier, in which case we have the start of a parameter and,
4350 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004351 bool IsConstructor = false;
4352 if (isDeclarationSpecifier())
4353 IsConstructor = true;
4354 else if (Tok.is(tok::identifier) ||
4355 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4356 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4357 // This might be a parenthesized member name, but is more likely to
4358 // be a constructor declaration with an invalid argument type. Keep
4359 // looking.
4360 if (Tok.is(tok::annot_cxxscope))
4361 ConsumeToken();
4362 ConsumeToken();
4363
4364 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004365 // which must have one of the following syntactic forms (see the
4366 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004367 switch (Tok.getKind()) {
4368 case tok::l_paren:
4369 // C(X ( int));
4370 case tok::l_square:
4371 // C(X [ 5]);
4372 // C(X [ [attribute]]);
4373 case tok::coloncolon:
4374 // C(X :: Y);
4375 // C(X :: *p);
4376 case tok::r_paren:
4377 // C(X )
4378 // Assume this isn't a constructor, rather than assuming it's a
4379 // constructor with an unnamed parameter of an ill-formed type.
4380 break;
4381
4382 default:
4383 IsConstructor = true;
4384 break;
4385 }
4386 }
4387
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004388 TPA.Revert();
4389 return IsConstructor;
4390}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004391
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004392/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004393/// type-qualifier-list: [C99 6.7.5]
4394/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004395/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004396/// [ only if VendorAttributesAllowed=true ]
4397/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004398/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004399/// [ only if VendorAttributesAllowed=true ]
4400/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004401/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004402/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004403///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004404void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4405 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004406 bool CXX11AttributesAllowed,
Alp Toker62c5b572013-11-26 01:30:10 +00004407 bool AtomicAllowed,
4408 bool IdentifierRequired) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004409 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004410 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004411 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004412 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004413 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004414 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004415
4416 SourceLocation EndLoc;
4417
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004418 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004419 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004420 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004421 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004422 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004423
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004424 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004425 case tok::code_completion:
4426 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004427 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004428
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004429 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004430 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004431 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004432 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004433 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004434 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004435 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004436 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004437 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004438 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004439 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004440 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004441 case tok::kw__Atomic:
4442 if (!AtomicAllowed)
4443 goto DoneWithTypeQuals;
4444 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4445 getLangOpts());
4446 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004447
4448 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004449 case tok::kw___private:
4450 case tok::kw___global:
4451 case tok::kw___local:
4452 case tok::kw___constant:
4453 case tok::kw___read_only:
4454 case tok::kw___write_only:
4455 case tok::kw___read_write:
4456 ParseOpenCLQualifiers(DS);
4457 break;
4458
Aaron Ballman317a77f2013-05-22 23:25:32 +00004459 case tok::kw___uptr:
Alp Toker62c5b572013-11-26 01:30:10 +00004460 // GNU libc headers in C mode use '__uptr' as an identifer which conflicts
4461 // with the MS modifier keyword.
4462 if (VendorAttributesAllowed && !getLangOpts().CPlusPlus &&
Alp Toker47642d22013-12-03 06:13:01 +00004463 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
4464 if (TryKeywordIdentFallback(false))
4465 continue;
Alp Toker62c5b572013-11-26 01:30:10 +00004466 }
4467 case tok::kw___sptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004468 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004469 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004470 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004471 case tok::kw___cdecl:
4472 case tok::kw___stdcall:
4473 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004474 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004475 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004476 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004477 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004478 continue;
4479 }
4480 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004481 case tok::kw___pascal:
4482 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004483 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004484 continue;
4485 }
4486 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004487 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004488 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004489 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004490 continue; // do *not* consume the next token!
4491 }
4492 // otherwise, FALL THROUGH!
4493 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004494 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004495 // If this is not a type-qualifier token, we're done reading type
4496 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004497 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004498 if (EndLoc.isValid())
4499 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004500 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004501 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004502
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004503 // If the specifier combination wasn't legal, issue a diagnostic.
4504 if (isInvalid) {
4505 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004506 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004507 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004508 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004509 }
4510}
4511
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004512
4513/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4514///
4515void Parser::ParseDeclarator(Declarator &D) {
4516 /// This implements the 'declarator' production in the C grammar, then checks
4517 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004518 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004519}
4520
Richard Smith0efa75c2012-03-29 01:16:42 +00004521static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4522 if (Kind == tok::star || Kind == tok::caret)
4523 return true;
4524
4525 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4526 if (!Lang.CPlusPlus)
4527 return false;
4528
4529 return Kind == tok::amp || Kind == tok::ampamp;
4530}
4531
Sebastian Redlbd150f42008-11-21 19:14:01 +00004532/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4533/// is parsed by the function passed to it. Pass null, and the direct-declarator
4534/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004535/// ptr-operator production.
4536///
Richard Smith09f76ee2011-10-19 21:33:05 +00004537/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004538/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4539/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004540///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004541/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4542/// [C] pointer[opt] direct-declarator
4543/// [C++] direct-declarator
4544/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004545///
4546/// pointer: [C99 6.7.5]
4547/// '*' type-qualifier-list[opt]
4548/// '*' type-qualifier-list[opt] pointer
4549///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004550/// ptr-operator:
4551/// '*' cv-qualifier-seq[opt]
4552/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004553/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004554/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004555/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004556/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004557void Parser::ParseDeclaratorInternal(Declarator &D,
4558 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004559 if (Diags.hasAllExtensionsSilenced())
4560 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004561
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004562 // C++ member pointers start with a '::' or a nested-name.
4563 // Member pointers get special handling, since there's no place for the
4564 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004565 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004566 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4567 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004568 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4569 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004570 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004571 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004572
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004573 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004574 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004575 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004576 if (D.mayHaveIdentifier())
4577 D.getCXXScopeSpec() = SS;
4578 else
4579 AnnotateScopeToken(SS, true);
4580
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004581 if (DirectDeclParser)
4582 (this->*DirectDeclParser)(D);
4583 return;
4584 }
4585
4586 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004587 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004588 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004589 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004590 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004591
4592 // Recurse to parse whatever is left.
4593 ParseDeclaratorInternal(D, DirectDeclParser);
4594
4595 // Sema will have to catch (syntactically invalid) pointers into global
4596 // scope. It has to catch pointers into namespace scope anyway.
4597 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004598 Loc),
4599 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004600 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004601 return;
4602 }
4603 }
4604
4605 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004606 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004607 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004608 if (DirectDeclParser)
4609 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004610 return;
4611 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004612
Sebastian Redled0f3b02009-03-15 22:02:01 +00004613 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4614 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004615 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004616 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004617
Chris Lattner9eac9312009-03-27 04:18:06 +00004618 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004619 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004620 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004621
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004622 // FIXME: GNU attributes are not allowed here in a new-type-id.
Alp Toker62c5b572013-11-26 01:30:10 +00004623 ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004624 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004625
Bill Wendling3708c182007-05-27 10:15:43 +00004626 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004627 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004628 if (Kind == tok::star)
4629 // Remember that we parsed a pointer type, and remember the type-quals.
4630 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004631 DS.getConstSpecLoc(),
4632 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004633 DS.getRestrictSpecLoc()),
4634 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004635 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004636 else
4637 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004638 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004639 Loc),
4640 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004641 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004642 } else {
4643 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004644 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004645
Sebastian Redl3b27be62009-03-23 00:00:23 +00004646 // Complain about rvalue references in C++03, but then go on and build
4647 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004648 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004649 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004650 diag::warn_cxx98_compat_rvalue_reference :
4651 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004652
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004653 // GNU-style and C++11 attributes are allowed here, as is restrict.
4654 ParseTypeQualifierListOpt(DS);
4655 D.ExtendWithDeclSpec(DS);
4656
Bill Wendling93efb222007-06-02 23:28:54 +00004657 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4658 // cv-qualifiers are introduced through the use of a typedef or of a
4659 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004660 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4661 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4662 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004663 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004664 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4665 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004666 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004667 // 'restrict' is permitted as an extension.
4668 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4669 Diag(DS.getAtomicSpecLoc(),
4670 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004671 }
Bill Wendling3708c182007-05-27 10:15:43 +00004672
4673 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004674 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004675
Douglas Gregor66583c52008-11-03 15:51:28 +00004676 if (D.getNumTypeObjects() > 0) {
4677 // C++ [dcl.ref]p4: There shall be no references to references.
4678 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4679 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004680 if (const IdentifierInfo *II = D.getIdentifier())
4681 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4682 << II;
4683 else
4684 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4685 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004686
Sebastian Redlbd150f42008-11-21 19:14:01 +00004687 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004688 // can go ahead and build the (technically ill-formed)
4689 // declarator: reference collapsing will take care of it.
4690 }
4691 }
4692
Richard Smith8e1ac332013-03-28 01:55:44 +00004693 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004694 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004695 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004696 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004697 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004698 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004699}
4700
Richard Smith0efa75c2012-03-29 01:16:42 +00004701static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4702 SourceLocation EllipsisLoc) {
4703 if (EllipsisLoc.isValid()) {
4704 FixItHint Insertion;
4705 if (!D.getEllipsisLoc().isValid()) {
4706 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4707 D.setEllipsisLoc(EllipsisLoc);
4708 }
4709 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4710 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4711 }
4712}
4713
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004714/// ParseDirectDeclarator
4715/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004716/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004717/// '(' declarator ')'
4718/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004719/// [C90] direct-declarator '[' constant-expression[opt] ']'
4720/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4721/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4722/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4723/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004724/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4725/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004726/// direct-declarator '(' parameter-type-list ')'
4727/// direct-declarator '(' identifier-list[opt] ')'
4728/// [GNU] direct-declarator '(' parameter-forward-declarations
4729/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004730/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4731/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004732/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4733/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4734/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004735/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004736/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004737///
4738/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004739/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004740/// '::'[opt] nested-name-specifier[opt] type-name
4741///
4742/// id-expression: [C++ 5.1]
4743/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004744/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004745///
4746/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004747/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004748/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004749/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004750/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004751/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004752///
Richard Smith1453e312012-03-27 01:42:32 +00004753/// Note, any additional constructs added here may need corresponding changes
4754/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004755void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004756 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004757
David Blaikiebbafb8a2012-03-11 07:00:24 +00004758 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004759 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004760 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004761 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4762 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004763 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004764 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004765 }
4766
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004767 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004768 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004769 // Change the declaration context for name lookup, until this function
4770 // is exited (and the declarator has been parsed).
4771 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004772 }
4773
Douglas Gregor27b4c162010-12-23 22:44:42 +00004774 // C++0x [dcl.fct]p14:
4775 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004776 // of a parameter-declaration-clause without a preceding comma. In
4777 // this case, the ellipsis is parsed as part of the
4778 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004779 // parameter pack that has not been expanded; otherwise, it is parsed
4780 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004781 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004782 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004783 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004784 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004785 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004786 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004787 !Actions.containsUnexpandedParameterPacks(D))) {
4788 SourceLocation EllipsisLoc = ConsumeToken();
4789 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4790 // The ellipsis was put in the wrong place. Recover, and explain to
4791 // the user what they should have done.
4792 ParseDeclarator(D);
4793 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4794 return;
4795 } else
4796 D.setEllipsisLoc(EllipsisLoc);
4797
4798 // The ellipsis can't be followed by a parenthesized declarator. We
4799 // check for that in ParseParenDeclarator, after we have disambiguated
4800 // the l_paren token.
4801 }
4802
Douglas Gregor7861a802009-11-03 01:35:08 +00004803 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4804 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4805 // We found something that indicates the start of an unqualified-id.
4806 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004807 bool AllowConstructorName;
4808 if (D.getDeclSpec().hasTypeSpecifier())
4809 AllowConstructorName = false;
4810 else if (D.getCXXScopeSpec().isSet())
4811 AllowConstructorName =
4812 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004813 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004814 else
4815 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4816
Abramo Bagnara7945c982012-01-27 09:46:47 +00004817 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004818 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4819 /*EnteringContext=*/true,
4820 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004821 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004822 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004823 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004824 D.getName()) ||
4825 // Once we're past the identifier, if the scope was bad, mark the
4826 // whole declarator bad.
4827 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004828 D.SetIdentifier(0, Tok.getLocation());
4829 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004830 } else {
4831 // Parsed the unqualified-id; update range information and move along.
4832 if (D.getSourceRange().getBegin().isInvalid())
4833 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4834 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004835 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004836 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004837 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004838 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004839 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004840 "There's a C++-specific check for tok::identifier above");
4841 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4842 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4843 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004844 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004845 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004846 // A virt-specifier isn't treated as an identifier if it appears after a
4847 // trailing-return-type.
4848 if (D.getContext() != Declarator::TrailingReturnContext ||
4849 !isCXX11VirtSpecifier(Tok)) {
4850 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4851 << FixItHint::CreateRemoval(Tok.getLocation());
4852 D.SetIdentifier(0, Tok.getLocation());
4853 ConsumeToken();
4854 goto PastIdentifier;
4855 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004856 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004857
Douglas Gregor7861a802009-11-03 01:35:08 +00004858 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004859 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004860 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004861 // Example: 'char (*X)' or 'int (*XX)(void)'
4862 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004863
4864 // If the declarator was parenthesized, we entered the declarator
4865 // scope when parsing the parenthesized declarator, then exited
4866 // the scope already. Re-enter the scope, if we need to.
4867 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004868 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004869 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004870 if (!D.isInvalidType() &&
4871 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004872 // Change the declaration context for name lookup, until this function
4873 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004874 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004875 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004876 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004877 // This could be something simple like "int" (in which case the declarator
4878 // portion is empty), if an abstract-declarator is allowed.
4879 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004880
4881 // The grammar for abstract-pack-declarator does not allow grouping parens.
4882 // FIXME: Revisit this once core issue 1488 is resolved.
4883 if (D.hasEllipsis() && D.hasGroupingParens())
4884 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4885 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004886 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004887 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004888 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004889 if (D.getContext() == Declarator::MemberContext)
4890 Diag(Tok, diag::err_expected_member_name_or_semi)
4891 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004892 else if (getLangOpts().CPlusPlus) {
4893 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4894 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004895 else {
4896 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4897 if (Tok.isAtStartOfLine() && Loc.isValid())
4898 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4899 << getLangOpts().CPlusPlus;
4900 else
4901 Diag(Tok, diag::err_expected_unqualified_id)
4902 << getLangOpts().CPlusPlus;
4903 }
Richard Trieu9c672672013-01-26 02:31:38 +00004904 } else
Alp Tokerec543272013-12-24 09:48:30 +00004905 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_paren;
Chris Lattnereec40f92006-08-06 21:55:29 +00004906 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004907 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004908 }
Mike Stump11289f42009-09-09 15:08:12 +00004909
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004910 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004911 assert(D.isPastIdentifier() &&
4912 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004913
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004914 // Don't parse attributes unless we have parsed an unparenthesized name.
4915 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004916 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004917
Chris Lattneracd58a32006-08-06 17:24:14 +00004918 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004919 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004920 // Enter function-declaration scope, limiting any declarators to the
4921 // function prototype scope, including parameter declarators.
4922 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004923 Scope::FunctionPrototypeScope|Scope::DeclScope|
4924 (D.isFunctionDeclaratorAFunctionDeclaration()
4925 ? Scope::FunctionDeclarationScope : 0));
4926
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004927 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4928 // In such a case, check if we actually have a function declarator; if it
4929 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004930 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004931 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4932 // The name of the declarator, if any, is tentatively declared within
4933 // a possible direct initializer.
4934 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4935 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4936 TentativelyDeclaredIdentifiers.pop_back();
4937 if (!IsFunctionDecl)
4938 break;
4939 }
John McCall084e83d2011-03-24 11:26:52 +00004940 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004941 BalancedDelimiterTracker T(*this, tok::l_paren);
4942 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004943 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004944 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004945 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004946 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004947 } else {
4948 break;
4949 }
4950 }
Chad Rosierc1183952012-06-26 22:30:43 +00004951}
Chris Lattneracd58a32006-08-06 17:24:14 +00004952
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004953/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4954/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004955/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004956/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4957///
4958/// direct-declarator:
4959/// '(' declarator ')'
4960/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004961/// direct-declarator '(' parameter-type-list ')'
4962/// direct-declarator '(' identifier-list[opt] ')'
4963/// [GNU] direct-declarator '(' parameter-forward-declarations
4964/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004965///
4966void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004967 BalancedDelimiterTracker T(*this, tok::l_paren);
4968 T.consumeOpen();
4969
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004970 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004971
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004972 // Eat any attributes before we look at whether this is a grouping or function
4973 // declarator paren. If this is a grouping paren, the attribute applies to
4974 // the type being built up, for example:
4975 // int (__attribute__(()) *x)(long y)
4976 // If this ends up not being a grouping paren, the attribute applies to the
4977 // first argument, for example:
4978 // int (__attribute__(()) int x)
4979 // In either case, we need to eat any attributes to be able to determine what
4980 // sort of paren this is.
4981 //
John McCall084e83d2011-03-24 11:26:52 +00004982 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004983 bool RequiresArg = false;
4984 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004985 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004986
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004987 // We require that the argument list (if this is a non-grouping paren) be
4988 // present even if the attribute list was empty.
4989 RequiresArg = true;
4990 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004991
Steve Naroff44ac7772008-12-25 14:16:32 +00004992 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004993 ParseMicrosoftTypeAttributes(attrs);
4994
Dawn Perchik335e16b2010-09-03 01:29:35 +00004995 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004996 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004997 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004998
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004999 // If we haven't past the identifier yet (or where the identifier would be
5000 // stored, if this is an abstract declarator), then this is probably just
5001 // grouping parens. However, if this could be an abstract-declarator, then
5002 // this could also be the start of function arguments (consider 'void()').
5003 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00005004
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005005 if (!D.mayOmitIdentifier()) {
5006 // If this can't be an abstract-declarator, this *must* be a grouping
5007 // paren, because we haven't seen the identifier yet.
5008 isGrouping = true;
5009 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00005010 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
5011 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00005012 isDeclarationSpecifier() || // 'int(int)' is a function.
5013 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005014 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
5015 // considered to be a type, not a K&R identifier-list.
5016 isGrouping = false;
5017 } else {
5018 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
5019 isGrouping = true;
5020 }
Mike Stump11289f42009-09-09 15:08:12 +00005021
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005022 // If this is a grouping paren, handle:
5023 // direct-declarator: '(' declarator ')'
5024 // direct-declarator: '(' attributes declarator ')'
5025 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005026 SourceLocation EllipsisLoc = D.getEllipsisLoc();
5027 D.setEllipsisLoc(SourceLocation());
5028
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005029 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005030 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00005031 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005032 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005033 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00005034 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005035 T.getCloseLocation()),
5036 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005037
5038 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00005039
5040 // An ellipsis cannot be placed outside parentheses.
5041 if (EllipsisLoc.isValid())
5042 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
5043
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005044 return;
5045 }
Mike Stump11289f42009-09-09 15:08:12 +00005046
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005047 // Okay, if this wasn't a grouping paren, it must be the start of a function
5048 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005049 // identifier (and remember where it would have been), then call into
5050 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005051 D.SetIdentifier(0, Tok.getLocation());
5052
David Blaikie15a430a2011-12-04 05:04:18 +00005053 // Enter function-declaration scope, limiting any declarators to the
5054 // function prototype scope, including parameter declarators.
5055 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005056 Scope::FunctionPrototypeScope | Scope::DeclScope |
5057 (D.isFunctionDeclaratorAFunctionDeclaration()
5058 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00005059 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00005060 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005061}
5062
5063/// ParseFunctionDeclarator - We are after the identifier and have parsed the
5064/// declarator D up to a paren, which indicates that we are parsing function
5065/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00005066///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005067/// If FirstArgAttrs is non-null, then the caller parsed those arguments
5068/// immediately after the open paren - they should be considered to be the
5069/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005070///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005071/// If RequiresArg is true, then the first argument of the function is required
5072/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005073///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005074/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5075/// (C++11) ref-qualifier[opt], exception-specification[opt],
5076/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5077///
5078/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00005079/// dynamic-exception-specification
5080/// noexcept-specification
5081///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005082void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005083 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005084 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00005085 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005086 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00005087 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00005088 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00005089 // lparen is already consumed!
5090 assert(D.isPastIdentifier() && "Should not call before identifier!");
5091
5092 // This should be true when the function has typed arguments.
5093 // Otherwise, it is treated as a K&R-style function.
5094 bool HasProto = false;
5095 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005096 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005097 // Remember where we see an ellipsis, if any.
5098 SourceLocation EllipsisLoc;
5099
5100 DeclSpec DS(AttrFactory);
5101 bool RefQualifierIsLValueRef = true;
5102 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00005103 SourceLocation ConstQualifierLoc;
5104 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005105 ExceptionSpecificationType ESpecType = EST_None;
5106 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005107 SmallVector<ParsedType, 2> DynamicExceptions;
5108 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005109 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005110 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00005111 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005112
James Molloy6f8780b2012-02-29 10:24:19 +00005113 Actions.ActOnStartFunctionDeclarator();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005114 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5115 EndLoc is the end location for the function declarator.
5116 They differ for trailing return types. */
5117 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005118 SourceLocation LParenLoc, RParenLoc;
5119 LParenLoc = Tracker.getOpenLocation();
5120 StartLoc = LParenLoc;
5121
Douglas Gregor9e66af42011-07-05 16:44:18 +00005122 if (isFunctionDeclaratorIdentifierList()) {
5123 if (RequiresArg)
5124 Diag(Tok, diag::err_argument_required_after_attribute);
5125
5126 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5127
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005128 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005129 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005130 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005131 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005132 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005133 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005134 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5135 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005136 else if (RequiresArg)
5137 Diag(Tok, diag::err_argument_required_after_attribute);
5138
David Blaikiebbafb8a2012-03-11 07:00:24 +00005139 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005140
5141 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005142 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005143 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005144 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005145 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005146
David Blaikiebbafb8a2012-03-11 07:00:24 +00005147 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005148 // FIXME: Accept these components in any order, and produce fixits to
5149 // correct the order if the user gets it wrong. Ideally we should deal
5150 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005151
5152 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005153 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5154 /*CXX11AttributesAllowed*/ false,
5155 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005156 if (!DS.getSourceRange().getEnd().isInvalid()) {
5157 EndLoc = DS.getSourceRange().getEnd();
5158 ConstQualifierLoc = DS.getConstSpecLoc();
5159 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5160 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005161
5162 // Parse ref-qualifier[opt].
5163 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005164 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005165 diag::warn_cxx98_compat_ref_qualifier :
5166 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005167
Douglas Gregor9e66af42011-07-05 16:44:18 +00005168 RefQualifierIsLValueRef = Tok.is(tok::amp);
5169 RefQualifierLoc = ConsumeToken();
5170 EndLoc = RefQualifierLoc;
5171 }
5172
Douglas Gregor3024f072012-04-16 07:05:22 +00005173 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005174 // If a declaration declares a member function or member function
5175 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005176 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005177 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005178 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005179 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005180 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005181 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005182 (D.getContext() == Declarator::MemberContext
5183 ? !D.getDeclSpec().isFriendSpecified()
5184 : D.getContext() == Declarator::FileContext &&
5185 D.getCXXScopeSpec().isValid() &&
5186 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005187 Sema::CXXThisScopeRAII ThisScope(Actions,
5188 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005189 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005190 (D.getDeclSpec().isConstexprSpecified() &&
5191 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005192 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005193 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005194
Douglas Gregor9e66af42011-07-05 16:44:18 +00005195 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005196 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005197 DynamicExceptions,
5198 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005199 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005200 if (ESpecType != EST_None)
5201 EndLoc = ESpecRange.getEnd();
5202
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005203 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5204 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005205 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005206
Douglas Gregor9e66af42011-07-05 16:44:18 +00005207 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005208 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005209 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005210 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005211 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5212 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005213 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005214 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005215 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005216 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005217 }
5218 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005219 }
5220
5221 // Remember that we parsed a function type, and remember the attributes.
5222 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005223 IsAmbiguous,
5224 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005225 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005226 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005227 DS.getTypeQualifiers(),
5228 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005229 RefQualifierLoc, ConstQualifierLoc,
5230 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005231 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005232 ESpecType, ESpecRange.getBegin(),
5233 DynamicExceptions.data(),
5234 DynamicExceptionRanges.data(),
5235 DynamicExceptions.size(),
5236 NoexceptExpr.isUsable() ?
5237 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005238 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005239 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005240 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005241
5242 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005243}
5244
5245/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5246/// identifier list form for a K&R-style function: void foo(a,b,c)
5247///
5248/// Note that identifier-lists are only allowed for normal declarators, not for
5249/// abstract-declarators.
5250bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005251 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005252 && Tok.is(tok::identifier)
5253 && !TryAltiVecVectorToken()
5254 // K&R identifier lists can't have typedefs as identifiers, per C99
5255 // 6.7.5.3p11.
5256 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5257 // Identifier lists follow a really simple grammar: the identifiers can
5258 // be followed *only* by a ", identifier" or ")". However, K&R
5259 // identifier lists are really rare in the brave new modern world, and
5260 // it is very common for someone to typo a type in a non-K&R style
5261 // list. If we are presented with something like: "void foo(intptr x,
5262 // float y)", we don't want to start parsing the function declarator as
5263 // though it is a K&R style declarator just because intptr is an
5264 // invalid type.
5265 //
5266 // To handle this, we check to see if the token after the first
5267 // identifier is a "," or ")". Only then do we parse it as an
5268 // identifier list.
5269 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5270}
5271
5272/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5273/// we found a K&R-style identifier list instead of a typed parameter list.
5274///
5275/// After returning, ParamInfo will hold the parsed parameters.
5276///
5277/// identifier-list: [C99 6.7.5]
5278/// identifier
5279/// identifier-list ',' identifier
5280///
5281void Parser::ParseFunctionDeclaratorIdentifierList(
5282 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005283 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005284 // If there was no identifier specified for the declarator, either we are in
5285 // an abstract-declarator, or we are in a parameter declarator which was found
5286 // to be abstract. In abstract-declarators, identifier lists are not valid:
5287 // diagnose this.
5288 if (!D.getIdentifier())
5289 Diag(Tok, diag::ext_ident_list_in_param);
5290
5291 // Maintain an efficient lookup of params we have seen so far.
5292 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5293
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005294 do {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005295 // If this isn't an identifier, report the error and skip until ')'.
5296 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00005297 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00005298 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005299 // Forget we parsed anything.
5300 ParamInfo.clear();
5301 return;
5302 }
5303
5304 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5305
5306 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5307 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5308 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5309
5310 // Verify that the argument identifier has not already been mentioned.
5311 if (!ParamsSoFar.insert(ParmII)) {
5312 Diag(Tok, diag::err_param_redefinition) << ParmII;
5313 } else {
5314 // Remember this identifier in ParamInfo.
5315 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5316 Tok.getLocation(),
5317 0));
5318 }
5319
5320 // Eat the identifier.
5321 ConsumeToken();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005322 // The list continues if we see a comma.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005323 } while (TryConsumeToken(tok::comma));
Douglas Gregor9e66af42011-07-05 16:44:18 +00005324}
5325
5326/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5327/// after the opening parenthesis. This function will not parse a K&R-style
5328/// identifier list.
5329///
Richard Smith2620cd92012-04-11 04:01:28 +00005330/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5331/// caller parsed those arguments immediately after the open paren - they should
5332/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005333///
5334/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5335/// be the location of the ellipsis, if any was parsed.
5336///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005337/// parameter-type-list: [C99 6.7.5]
5338/// parameter-list
5339/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005340/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005341///
5342/// parameter-list: [C99 6.7.5]
5343/// parameter-declaration
5344/// parameter-list ',' parameter-declaration
5345///
5346/// parameter-declaration: [C99 6.7.5]
5347/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005348/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005349/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005350/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005351/// declaration-specifiers abstract-declarator[opt]
5352/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005353/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005354/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005355/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005356///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005357void Parser::ParseParameterDeclarationClause(
5358 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005359 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005360 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005361 SourceLocation &EllipsisLoc) {
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005362 do {
5363 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5364 // before deciding this was a parameter-declaration-clause.
5365 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Chris Lattner371ed4e2008-04-06 06:57:35 +00005366 break;
Mike Stump11289f42009-09-09 15:08:12 +00005367
Chris Lattner371ed4e2008-04-06 06:57:35 +00005368 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005369 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005370 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005371
Richard Smith2620cd92012-04-11 04:01:28 +00005372 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005373 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005374
John McCall53fa7142010-12-24 02:08:15 +00005375 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005376 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005377
5378 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005379
5380 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005381 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005382 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005383 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5384 // too much hassle.
5385 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005386
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005387 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005388
Faisal Vali2b391ab2013-09-26 19:54:12 +00005389
5390 // Parse the declarator. This is "PrototypeContext" or
5391 // "LambdaExprParameterContext", because we must accept either
5392 // 'declarator' or 'abstract-declarator' here.
5393 Declarator ParmDeclarator(DS,
5394 D.getContext() == Declarator::LambdaExprContext ?
5395 Declarator::LambdaExprParameterContext :
5396 Declarator::PrototypeContext);
5397 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005398
5399 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005400 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005401
Chris Lattner371ed4e2008-04-06 06:57:35 +00005402 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005403 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005404
Douglas Gregor4d87df52008-12-16 21:30:33 +00005405 // DefArgToks is used when the parsing of default arguments needs
5406 // to be delayed.
5407 CachedTokens *DefArgToks = 0;
5408
Chris Lattner371ed4e2008-04-06 06:57:35 +00005409 // If no parameter was specified, verify that *something* was specified,
5410 // otherwise we have a missing type and identifier.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005411 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5412 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005413 // Completely missing, emit error.
5414 Diag(DSStart, diag::err_missing_param);
5415 } else {
5416 // Otherwise, we have something. Add it and let semantic analysis try
5417 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005418
Chris Lattner371ed4e2008-04-06 06:57:35 +00005419 // Inform the actions module about the parameter declarator, so it gets
5420 // added to the current scope.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005421 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5422 ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005423 // Parse the default argument, if any. We parse the default
5424 // arguments in all dialects; the semantic analysis in
5425 // ActOnParamDefaultArgument will reject the default argument in
5426 // C.
5427 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005428 SourceLocation EqualLoc = Tok.getLocation();
5429
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005430 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005431 if (D.getContext() == Declarator::MemberContext) {
5432 // If we're inside a class definition, cache the tokens
5433 // corresponding to the default argument. We'll actually parse
5434 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005435 // FIXME: Can we use a smart pointer for Toks?
5436 DefArgToks = new CachedTokens;
5437
Richard Smith1fff95c2013-09-12 23:28:08 +00005438 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005439 delete DefArgToks;
5440 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005441 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005442 } else {
5443 // Mark the end of the default argument so that we know when to
5444 // stop when we parse it later on.
5445 Token DefArgEnd;
5446 DefArgEnd.startToken();
5447 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5448 DefArgEnd.setLocation(Tok.getLocation());
5449 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005450 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005451 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005452 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005453 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005454 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005455 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005456
Chad Rosierc1183952012-06-26 22:30:43 +00005457 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005458 // used.
5459 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005460 Sema::PotentiallyEvaluatedIfUsed,
5461 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005462
Sebastian Redldb63af22012-03-14 15:54:00 +00005463 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005464 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005465 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005466 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005467 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005468 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005469 if (DefArgResult.isInvalid()) {
5470 Actions.ActOnParamDefaultArgumentError(Param);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005471 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00005472 } else {
5473 // Inform the actions module about the default argument
5474 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005475 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005476 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005477 }
5478 }
Mike Stump11289f42009-09-09 15:08:12 +00005479
5480 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005481 ParmDeclarator.getIdentifierLoc(),
5482 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005483 }
5484
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005485 if (TryConsumeToken(tok::ellipsis, EllipsisLoc) &&
5486 !getLangOpts().CPlusPlus) {
5487 // We have ellipsis without a preceding ',', which is ill-formed
5488 // in C. Complain and provide the fix.
5489 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
5490 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005491 break;
5492 }
Mike Stump11289f42009-09-09 15:08:12 +00005493
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005494 // If the next token is a comma, consume it and keep reading arguments.
5495 } while (TryConsumeToken(tok::comma));
Chris Lattner6c940e62008-04-06 06:34:08 +00005496}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005497
Chris Lattnere8074e62006-08-06 18:30:15 +00005498/// [C90] direct-declarator '[' constant-expression[opt] ']'
5499/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5500/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5501/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5502/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005503/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5504/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005505void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005506 if (CheckProhibitedCXX11Attribute())
5507 return;
5508
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005509 BalancedDelimiterTracker T(*this, tok::l_square);
5510 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005511
Chris Lattner84a11622008-12-18 07:27:21 +00005512 // C array syntax has many features, but by-far the most common is [] and [4].
5513 // This code does a fast path to handle some of the most obvious cases.
5514 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005515 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005516 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005517 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005518
Chris Lattner84a11622008-12-18 07:27:21 +00005519 // Remember that we parsed the empty array type.
John McCall084e83d2011-03-24 11:26:52 +00005520 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005521 T.getOpenLocation(),
5522 T.getCloseLocation()),
5523 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005524 return;
5525 } else if (Tok.getKind() == tok::numeric_constant &&
5526 GetLookAheadToken(1).is(tok::r_square)) {
5527 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005528 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005529 ConsumeToken();
5530
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005531 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005532 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005533 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005534
Chris Lattner84a11622008-12-18 07:27:21 +00005535 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005536 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005537 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005538 T.getOpenLocation(),
5539 T.getCloseLocation()),
5540 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005541 return;
5542 }
Mike Stump11289f42009-09-09 15:08:12 +00005543
Chris Lattnere8074e62006-08-06 18:30:15 +00005544 // If valid, this location is the position where we read the 'static' keyword.
5545 SourceLocation StaticLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005546 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005547
Chris Lattnere8074e62006-08-06 18:30:15 +00005548 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005549 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005550 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005551 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005552
Chris Lattnere8074e62006-08-06 18:30:15 +00005553 // If we haven't already read 'static', check to see if there is one after the
5554 // type-qualifier-list.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005555 if (!StaticLoc.isValid())
5556 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005557
Chris Lattnere8074e62006-08-06 18:30:15 +00005558 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005559 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005560 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005561
Chris Lattner521ff2b2008-04-06 05:26:30 +00005562 // Handle the case where we have '[*]' as the array size. However, a leading
5563 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005564 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005565 // infrequent, use of lookahead is not costly here.
5566 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005567 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005568
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005569 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005570 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005571 StaticLoc = SourceLocation(); // Drop the static.
5572 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005573 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005574 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005575 // Note, in C89, this production uses the constant-expr production instead
5576 // of assignment-expr. The only difference is that assignment-expr allows
5577 // things like '=' and '*='. Sema rejects these in C89 mode because they
5578 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005579
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005580 // Parse the constant-expression or assignment-expression now (depending
5581 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005582 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005583 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005584 } else {
5585 EnterExpressionEvaluationContext Unevaluated(Actions,
5586 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005587 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005588 }
Chris Lattner62591722006-08-12 18:40:58 +00005589 }
Mike Stump11289f42009-09-09 15:08:12 +00005590
Chris Lattner62591722006-08-12 18:40:58 +00005591 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005592 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005593 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005594 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00005595 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00005596 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005597 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005598
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005599 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005600
John McCall084e83d2011-03-24 11:26:52 +00005601 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005602 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005603
Chris Lattner84a11622008-12-18 07:27:21 +00005604 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005605 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005606 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005607 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005608 T.getOpenLocation(),
5609 T.getCloseLocation()),
5610 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005611}
5612
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005613/// [GNU] typeof-specifier:
5614/// typeof ( expressions )
5615/// typeof ( type-name )
5616/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005617///
5618void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005619 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005620 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005621 SourceLocation StartLoc = ConsumeToken();
5622
John McCalle8595032010-01-13 20:03:27 +00005623 const bool hasParens = Tok.is(tok::l_paren);
5624
Eli Friedman15681d62012-09-26 04:34:21 +00005625 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5626 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005627
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005628 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005629 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005630 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005631 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5632 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005633 if (hasParens)
5634 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005635
5636 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005637 // FIXME: Not accurate, the range gets one token more than it should.
5638 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005639 else
5640 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005641
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005642 if (isCastExpr) {
5643 if (!CastTy) {
5644 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005645 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005646 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005647
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005648 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005649 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005650 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5651 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005652 DiagID, CastTy))
5653 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005654 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005655 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005656
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005657 // If we get here, the operand to the typeof was an expresion.
5658 if (Operand.isInvalid()) {
5659 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005660 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005661 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005662
Eli Friedmane0afc982012-01-21 01:01:51 +00005663 // We might need to transform the operand if it is potentially evaluated.
5664 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5665 if (Operand.isInvalid()) {
5666 DS.SetTypeSpecError();
5667 return;
5668 }
5669
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005670 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005671 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005672 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5673 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005674 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005675 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005676}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005677
Benjamin Kramere56f3932011-12-23 17:00:35 +00005678/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005679/// _Atomic ( type-name )
5680///
5681void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005682 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5683 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005684
5685 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005686 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005687 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005688 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005689
5690 TypeResult Result = ParseTypeName();
5691 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00005692 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00005693 return;
5694 }
5695
5696 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005697 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005698
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005699 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005700 return;
5701
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005702 DS.setTypeofParensRange(T.getRange());
5703 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005704
5705 const char *PrevSpec = 0;
5706 unsigned DiagID;
5707 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5708 DiagID, Result.release()))
5709 Diag(StartLoc, DiagID) << PrevSpec;
5710}
5711
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005712
5713/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5714/// from TryAltiVecVectorToken.
5715bool Parser::TryAltiVecVectorTokenOutOfLine() {
5716 Token Next = NextToken();
5717 switch (Next.getKind()) {
5718 default: return false;
5719 case tok::kw_short:
5720 case tok::kw_long:
5721 case tok::kw_signed:
5722 case tok::kw_unsigned:
5723 case tok::kw_void:
5724 case tok::kw_char:
5725 case tok::kw_int:
5726 case tok::kw_float:
5727 case tok::kw_double:
5728 case tok::kw_bool:
5729 case tok::kw___pixel:
5730 Tok.setKind(tok::kw___vector);
5731 return true;
5732 case tok::identifier:
5733 if (Next.getIdentifierInfo() == Ident_pixel) {
5734 Tok.setKind(tok::kw___vector);
5735 return true;
5736 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005737 if (Next.getIdentifierInfo() == Ident_bool) {
5738 Tok.setKind(tok::kw___vector);
5739 return true;
5740 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005741 return false;
5742 }
5743}
5744
5745bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5746 const char *&PrevSpec, unsigned &DiagID,
5747 bool &isInvalid) {
5748 if (Tok.getIdentifierInfo() == Ident_vector) {
5749 Token Next = NextToken();
5750 switch (Next.getKind()) {
5751 case tok::kw_short:
5752 case tok::kw_long:
5753 case tok::kw_signed:
5754 case tok::kw_unsigned:
5755 case tok::kw_void:
5756 case tok::kw_char:
5757 case tok::kw_int:
5758 case tok::kw_float:
5759 case tok::kw_double:
5760 case tok::kw_bool:
5761 case tok::kw___pixel:
5762 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5763 return true;
5764 case tok::identifier:
5765 if (Next.getIdentifierInfo() == Ident_pixel) {
5766 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5767 return true;
5768 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005769 if (Next.getIdentifierInfo() == Ident_bool) {
5770 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5771 return true;
5772 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005773 break;
5774 default:
5775 break;
5776 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005777 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005778 DS.isTypeAltiVecVector()) {
5779 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5780 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005781 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5782 DS.isTypeAltiVecVector()) {
5783 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5784 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005785 }
5786 return false;
5787}