blob: 32f7a7a85de3d9c0f7d9bea8d8838406fa4281d5 [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") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000135 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
136 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000137 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000138 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
139 ConsumeToken();
140 continue;
141 }
142 // we have an identifier or declaration specifier (const, int, etc.)
143 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
144 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000145
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000146 if (Tok.is(tok::l_paren)) {
147 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000148 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000149 LateParsedAttribute *LA =
150 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
151 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000152
Bill Wendling44426052012-12-20 19:22:21 +0000153 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000154 // with other late-parsed declarations.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000155 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000156 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000157
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000158 // consume everything up to and including the matching right parens
159 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000160
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000161 Token Eof;
162 Eof.startToken();
163 Eof.setLocation(Tok.getLocation());
164 LA->Toks.push_back(Eof);
165 } else {
Michael Han23214e52012-10-03 01:56:22 +0000166 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000167 0, SourceLocation(), AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000168 }
169 } else {
Aaron Ballman00e99962013-08-31 01:11:41 +0000170 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
171 AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000172 }
173 }
Alp Toker383d2c42014-01-01 03:08:43 +0000174 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000175 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000176 SourceLocation Loc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000177 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000178 SkipUntil(tok::r_paren, StopAtSemi);
John McCall53fa7142010-12-24 02:08:15 +0000179 if (endLoc)
180 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000181 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000182}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000183
Aaron Ballman4768b312013-11-04 12:55:56 +0000184/// \brief Normalizes an attribute name by dropping prefixed and suffixed __.
185static StringRef normalizeAttrName(StringRef Name) {
Richard Smith66e71682013-10-24 01:07:54 +0000186 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
187 Name = Name.drop_front(2).drop_back(2);
Aaron Ballman4768b312013-11-04 12:55:56 +0000188 return Name;
189}
190
191/// \brief Determine whether the given attribute has an identifier argument.
192static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
193 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Richard Smith66e71682013-10-24 01:07:54 +0000194#include "clang/Parse/AttrIdentifierArg.inc"
Douglas Gregord2472d42013-05-02 23:25:32 +0000195 .Default(false);
196}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000197
Aaron Ballman4768b312013-11-04 12:55:56 +0000198/// \brief Determine whether the given attribute parses a type argument.
199static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
200 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
201#include "clang/Parse/AttrTypeArg.inc"
202 .Default(false);
203}
204
Richard Smithfeefaf52013-09-03 18:01:40 +0000205IdentifierLoc *Parser::ParseIdentifierLoc() {
206 assert(Tok.is(tok::identifier) && "expected an identifier");
207 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
208 Tok.getLocation(),
209 Tok.getIdentifierInfo());
210 ConsumeToken();
211 return IL;
212}
213
Richard Smithb1f9a282013-10-31 01:56:18 +0000214void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
215 SourceLocation AttrNameLoc,
216 ParsedAttributes &Attrs,
217 SourceLocation *EndLoc) {
218 BalancedDelimiterTracker Parens(*this, tok::l_paren);
219 Parens.consumeOpen();
220
221 TypeResult T;
222 if (Tok.isNot(tok::r_paren))
223 T = ParseTypeName();
224
225 if (Parens.consumeClose())
226 return;
227
228 if (T.isInvalid())
229 return;
230
231 if (T.isUsable())
232 Attrs.addNewTypeAttr(&AttrName,
233 SourceRange(AttrNameLoc, Parens.getCloseLocation()), 0,
234 AttrNameLoc, T.get(), AttributeList::AS_GNU);
235 else
236 Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
237 0, AttrNameLoc, 0, 0, AttributeList::AS_GNU);
238}
239
Michael Han23214e52012-10-03 01:56:22 +0000240/// Parse the arguments to a parameterized GNU attribute or
241/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000242void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
243 SourceLocation AttrNameLoc,
244 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000245 SourceLocation *EndLoc,
246 IdentifierInfo *ScopeName,
247 SourceLocation ScopeLoc,
248 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000249
250 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
251
Richard Smith66e71682013-10-24 01:07:54 +0000252 AttributeList::Kind AttrKind =
Richard Smithb1f9a282013-10-31 01:56:18 +0000253 AttributeList::getKind(AttrName, ScopeName, Syntax);
Richard Smith66e71682013-10-24 01:07:54 +0000254
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000255 // Availability attributes have their own grammar.
Richard Smithb1f9a282013-10-31 01:56:18 +0000256 // FIXME: All these cases fail to pass in the syntax and scope, and might be
257 // written as C++11 gnu:: attributes.
Richard Smith66e71682013-10-24 01:07:54 +0000258 if (AttrKind == AttributeList::AT_Availability) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000259 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
260 return;
261 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000262
263 if (AttrKind == AttributeList::AT_ObjCBridgeRelated) {
264 ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
265 return;
266 }
267
Richard Smithb1f9a282013-10-31 01:56:18 +0000268 // Thread safety attributes are parsed in an unevaluated context.
269 // FIXME: Share the bulk of the parsing code here and just pull out
270 // the unevaluated context.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000271 if (IsThreadSafetyAttribute(AttrName->getName())) {
272 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
273 return;
274 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000275 // Type safety attributes have their own grammar.
Richard Smith66e71682013-10-24 01:07:54 +0000276 if (AttrKind == AttributeList::AT_TypeTagForDatatype) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000277 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
278 return;
279 }
Aaron Ballman4768b312013-11-04 12:55:56 +0000280 // Some attributes expect solely a type parameter.
281 if (attributeIsTypeArgAttr(*AttrName)) {
Richard Smithb1f9a282013-10-31 01:56:18 +0000282 ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc);
283 return;
284 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000285
Richard Smith66e71682013-10-24 01:07:54 +0000286 // Ignore the left paren location for now.
287 ConsumeParen();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000288
Aaron Ballman00e99962013-08-31 01:11:41 +0000289 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000290
Richard Smithb1f9a282013-10-31 01:56:18 +0000291 if (Tok.is(tok::identifier)) {
Richard Smith66e71682013-10-24 01:07:54 +0000292 // If this attribute wants an 'identifier' argument, make it so.
Richard Smithb1f9a282013-10-31 01:56:18 +0000293 bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName);
Richard Smith66e71682013-10-24 01:07:54 +0000294
295 // If we don't know how to parse this attribute, but this is the only
296 // token in this argument, assume it's meant to be an identifier.
Aaron Ballman66037472013-12-04 15:32:26 +0000297 if (AttrKind == AttributeList::UnknownAttribute ||
298 AttrKind == AttributeList::IgnoredAttribute) {
Richard Smith66e71682013-10-24 01:07:54 +0000299 const Token &Next = NextToken();
Richard Smithb1f9a282013-10-31 01:56:18 +0000300 IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma);
Richard Smith66e71682013-10-24 01:07:54 +0000301 }
Richard Smithb12bf692011-10-17 21:20:17 +0000302
Richard Smithb1f9a282013-10-31 01:56:18 +0000303 if (IsIdentifierArg)
304 ArgExprs.push_back(ParseIdentifierLoc());
Richard Smithb12bf692011-10-17 21:20:17 +0000305 }
306
Richard Smithb1f9a282013-10-31 01:56:18 +0000307 if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
Richard Smithb12bf692011-10-17 21:20:17 +0000308 // Eat the comma.
Aaron Ballman00e99962013-08-31 01:11:41 +0000309 if (!ArgExprs.empty())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000310 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000311
Richard Smithb12bf692011-10-17 21:20:17 +0000312 // Parse the non-empty comma-separated list of expressions.
Alp Toker8fbec672013-12-17 23:29:36 +0000313 do {
Richard Smithb12bf692011-10-17 21:20:17 +0000314 ExprResult ArgExpr(ParseAssignmentExpression());
315 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000316 SkipUntil(tok::r_paren, StopAtSemi);
Richard Smithb12bf692011-10-17 21:20:17 +0000317 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000318 }
Richard Smithb12bf692011-10-17 21:20:17 +0000319 ArgExprs.push_back(ArgExpr.release());
Alp Toker8fbec672013-12-17 23:29:36 +0000320 // Eat the comma, move to the next argument
321 } while (TryConsumeToken(tok::comma));
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000322 }
Richard Smithb12bf692011-10-17 21:20:17 +0000323
324 SourceLocation RParen = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000325 if (!ExpectAndConsume(tok::r_paren)) {
Michael Han360d2252012-10-04 16:42:52 +0000326 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithb1f9a282013-10-31 01:56:18 +0000327 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
328 ArgExprs.data(), ArgExprs.size(), Syntax);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000329 }
330}
331
Chad Rosierc1183952012-06-26 22:30:43 +0000332/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000333/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000334void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000335 SourceLocation AttrNameLoc,
336 ParsedAttributes &Attrs)
337{
338 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000339 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000340 AttrName->getNameStart(), tok::r_paren))
341 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000342
Aaron Ballman478faed2012-06-19 22:09:27 +0000343 ExprResult ArgExpr(ParseConstantExpression());
344 if (ArgExpr.isInvalid()) {
345 T.skipToEnd();
346 return;
347 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000348 ArgsUnion ExprList = ArgExpr.take();
349 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
350 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000351
352 T.consumeClose();
353}
354
Chad Rosierc1183952012-06-26 22:30:43 +0000355/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000356/// arguments.
357bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
358 return llvm::StringSwitch<bool>(Ident->getName())
359 .Case("dllimport", true)
360 .Case("dllexport", true)
361 .Case("noreturn", true)
362 .Case("nothrow", true)
363 .Case("noinline", true)
364 .Case("naked", true)
365 .Case("appdomain", true)
366 .Case("process", true)
367 .Case("jitintrinsic", true)
368 .Case("noalias", true)
369 .Case("restrict", true)
370 .Case("novtable", true)
371 .Case("selectany", true)
372 .Case("thread", true)
Aaron Ballman444eb6e2013-05-04 16:58:37 +0000373 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000374 .Default(false);
375}
376
Chad Rosierc1183952012-06-26 22:30:43 +0000377/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000378/// parameters). Will return false if we properly handled the declspec, or
379/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000380void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000381 SourceLocation Loc,
382 ParsedAttributes &Attrs) {
383 // Try to handle the easy case first -- these declspecs all take a single
384 // parameter as their argument.
385 if (llvm::StringSwitch<bool>(Ident->getName())
386 .Case("uuid", true)
387 .Case("align", true)
388 .Case("allocate", true)
389 .Default(false)) {
390 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
391 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000392 // The deprecated declspec has an optional single argument, so we will
393 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000394 // not.
395 if (Tok.getKind() == tok::l_paren)
396 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
397 else
Aaron Ballman00e99962013-08-31 01:11:41 +0000398 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000399 } else if (Ident->getName() == "property") {
400 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000401 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000402 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000403 if (Tok.isNot(tok::l_paren)) {
404 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
405 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000406 return;
John McCall5e77d762013-04-16 07:28:30 +0000407 }
408 BalancedDelimiterTracker T(*this, tok::l_paren);
409 T.expectAndConsume(diag::err_expected_lparen_after,
410 Ident->getNameStart(), tok::r_paren);
411
412 enum AccessorKind {
413 AK_Invalid = -1,
414 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
415 };
416 IdentifierInfo *AccessorNames[] = { 0, 0 };
417 bool HasInvalidAccessor = false;
418
419 // Parse the accessor specifications.
420 while (true) {
421 // Stop if this doesn't look like an accessor spec.
422 if (!Tok.is(tok::identifier)) {
423 // If the user wrote a completely empty list, use a special diagnostic.
424 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
425 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
426 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
427 break;
428 }
429
430 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
431 break;
432 }
433
434 AccessorKind Kind;
435 SourceLocation KindLoc = Tok.getLocation();
436 StringRef KindStr = Tok.getIdentifierInfo()->getName();
437 if (KindStr == "get") {
438 Kind = AK_Get;
439 } else if (KindStr == "put") {
440 Kind = AK_Put;
441
442 // Recover from the common mistake of using 'set' instead of 'put'.
443 } else if (KindStr == "set") {
444 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
445 << FixItHint::CreateReplacement(KindLoc, "put");
446 Kind = AK_Put;
447
448 // Handle the mistake of forgetting the accessor kind by skipping
449 // this accessor.
450 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
451 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
452 ConsumeToken();
453 HasInvalidAccessor = true;
454 goto next_property_accessor;
455
456 // Otherwise, complain about the unknown accessor kind.
457 } else {
458 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
459 HasInvalidAccessor = true;
460 Kind = AK_Invalid;
461
462 // Try to keep parsing unless it doesn't look like an accessor spec.
463 if (!NextToken().is(tok::equal)) break;
464 }
465
466 // Consume the identifier.
467 ConsumeToken();
468
469 // Consume the '='.
Alp Toker8fbec672013-12-17 23:29:36 +0000470 if (!TryConsumeToken(tok::equal)) {
John McCall5e77d762013-04-16 07:28:30 +0000471 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
472 << KindStr;
473 break;
474 }
475
476 // Expect the method name.
477 if (!Tok.is(tok::identifier)) {
478 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
479 break;
480 }
481
482 if (Kind == AK_Invalid) {
483 // Just drop invalid accessors.
484 } else if (AccessorNames[Kind] != NULL) {
485 // Complain about the repeated accessor, ignore it, and keep parsing.
486 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
487 } else {
488 AccessorNames[Kind] = Tok.getIdentifierInfo();
489 }
490 ConsumeToken();
491
492 next_property_accessor:
493 // Keep processing accessors until we run out.
494 if (Tok.is(tok::comma)) {
495 ConsumeAnyToken();
496 continue;
497
498 // If we run into the ')', stop without consuming it.
499 } else if (Tok.is(tok::r_paren)) {
500 break;
501 } else {
502 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
503 break;
504 }
505 }
506
507 // Only add the property attribute if it was well-formed.
508 if (!HasInvalidAccessor) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000509 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000510 AccessorNames[AK_Get], AccessorNames[AK_Put],
511 AttributeList::AS_Declspec);
512 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000513 T.skipToEnd();
514 } else {
515 // We don't recognize this as a valid declspec, but instead of creating the
516 // attribute and allowing sema to warn about it, we will warn here instead.
517 // This is because some attributes have multiple spellings, but we need to
518 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000519 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000520 // both locations.
521 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
522
523 // If there's an open paren, we should eat the open and close parens under
524 // the assumption that this unknown declspec has parameters.
525 BalancedDelimiterTracker T(*this, tok::l_paren);
526 if (!T.consumeOpen())
527 T.skipToEnd();
528 }
529}
530
Eli Friedman06de2b52009-06-08 07:21:15 +0000531/// [MS] decl-specifier:
532/// __declspec ( extended-decl-modifier-seq )
533///
534/// [MS] extended-decl-modifier-seq:
535/// extended-decl-modifier[opt]
536/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000537void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000538 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000539
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000540 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000541 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000542 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000543 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000544 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000545
Chad Rosierc1183952012-06-26 22:30:43 +0000546 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000547 // you can specify multiple attributes per declspec.
548 while (Tok.getKind() != tok::r_paren) {
549 // We expect either a well-known identifier or a generic string. Anything
550 // else is a malformed declspec.
551 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000552 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000553 Tok.getKind() != tok::kw_restrict) {
554 Diag(Tok, diag::err_ms_declspec_type);
555 T.skipToEnd();
556 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000557 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000558
559 IdentifierInfo *AttrName;
560 SourceLocation AttrNameLoc;
561 if (IsString) {
562 SmallString<8> StrBuffer;
563 bool Invalid = false;
564 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
565 if (Invalid) {
566 T.skipToEnd();
567 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000568 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000569 AttrName = PP.getIdentifierInfo(Str);
570 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000571 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000572 AttrName = Tok.getIdentifierInfo();
573 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000574 }
Chad Rosierc1183952012-06-26 22:30:43 +0000575
Aaron Ballman478faed2012-06-19 22:09:27 +0000576 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000577 // If we have a generic string, we will allow it because there is no
578 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000579 // (for instance, SAL declspecs in older versions of MSVC).
580 //
Chad Rosierc1183952012-06-26 22:30:43 +0000581 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000582 // arguments and can be turned into an attribute directly.
Aaron Ballman00e99962013-08-31 01:11:41 +0000583 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
584 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000585 else
586 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000587 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000588 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000589}
590
John McCall53fa7142010-12-24 02:08:15 +0000591void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000592 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000593 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000594 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000595 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballman317a77f2013-05-22 23:25:32 +0000596 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
597 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000598 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
599 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000600 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
601 AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000602 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000603}
604
John McCall53fa7142010-12-24 02:08:15 +0000605void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000606 // Treat these like attributes
607 while (Tok.is(tok::kw___pascal)) {
608 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
609 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000610 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
611 AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000612 }
John McCall53fa7142010-12-24 02:08:15 +0000613}
614
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000615void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
616 // Treat these like attributes
617 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000618 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000619 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000620 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
621 AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000622 }
623}
624
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000625void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000626 // FIXME: The mapping from attribute spelling to semantics should be
627 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000628 SourceLocation Loc = Tok.getLocation();
629 switch(Tok.getKind()) {
630 // OpenCL qualifiers:
631 case tok::kw___private:
John McCall084e83d2011-03-24 11:26:52 +0000632 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000633 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000634 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000635 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000636
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000637 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000638 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000639 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000640 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000641 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000642
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000643 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000644 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000645 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000646 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
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___constant:
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_constant);
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___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000656 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000657 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000658 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
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___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000662 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000663 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000664 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
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_write:
John McCall084e83d2011-03-24 11:26:52 +0000668 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000669 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000670 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000671 break;
672 default: break;
673 }
674}
675
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000676/// \brief Parse a version number.
677///
678/// version:
679/// simple-integer
680/// simple-integer ',' simple-integer
681/// simple-integer ',' simple-integer ',' simple-integer
682VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
683 Range = Tok.getLocation();
684
685 if (!Tok.is(tok::numeric_constant)) {
686 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000687 SkipUntil(tok::comma, tok::r_paren,
688 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000689 return VersionTuple();
690 }
691
692 // Parse the major (and possibly minor and subminor) versions, which
693 // are stored in the numeric constant. We utilize a quirk of the
694 // lexer, which is that it handles something like 1.2.3 as a single
695 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000696 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000697 Buffer.resize(Tok.getLength()+1);
698 const char *ThisTokBegin = &Buffer[0];
699
700 // Get the spelling of the token, which eliminates trigraphs, etc.
701 bool Invalid = false;
702 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
703 if (Invalid)
704 return VersionTuple();
705
706 // Parse the major version.
707 unsigned AfterMajor = 0;
708 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000709 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000710 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
711 ++AfterMajor;
712 }
713
714 if (AfterMajor == 0) {
715 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000716 SkipUntil(tok::comma, tok::r_paren,
717 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000718 return VersionTuple();
719 }
720
721 if (AfterMajor == ActualLength) {
722 ConsumeToken();
723
724 // We only had a single version component.
725 if (Major == 0) {
726 Diag(Tok, diag::err_zero_version);
727 return VersionTuple();
728 }
729
730 return VersionTuple(Major);
731 }
732
733 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
734 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000735 SkipUntil(tok::comma, tok::r_paren,
736 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000737 return VersionTuple();
738 }
739
740 // Parse the minor version.
741 unsigned AfterMinor = AfterMajor + 1;
742 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000743 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000744 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
745 ++AfterMinor;
746 }
747
748 if (AfterMinor == ActualLength) {
749 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000750
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000751 // We had major.minor.
752 if (Major == 0 && Minor == 0) {
753 Diag(Tok, diag::err_zero_version);
754 return VersionTuple();
755 }
756
Chad Rosierc1183952012-06-26 22:30:43 +0000757 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000758 }
759
760 // If what follows is not a '.', we have a problem.
761 if (ThisTokBegin[AfterMinor] != '.') {
762 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000763 SkipUntil(tok::comma, tok::r_paren,
764 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Chad Rosierc1183952012-06-26 22:30:43 +0000765 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000766 }
767
768 // Parse the subminor version.
769 unsigned AfterSubminor = AfterMinor + 1;
770 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000771 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000772 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
773 ++AfterSubminor;
774 }
775
776 if (AfterSubminor != ActualLength) {
777 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000778 SkipUntil(tok::comma, tok::r_paren,
779 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000780 return VersionTuple();
781 }
782 ConsumeToken();
783 return VersionTuple(Major, Minor, Subminor);
784}
785
786/// \brief Parse the contents of the "availability" attribute.
787///
788/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000789/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000790///
791/// platform:
792/// identifier
793///
794/// version-arg-list:
795/// version-arg
796/// version-arg ',' version-arg-list
797///
798/// version-arg:
799/// 'introduced' '=' version
800/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000801/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000802/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000803/// opt-message:
804/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000805void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
806 SourceLocation AvailabilityLoc,
807 ParsedAttributes &attrs,
808 SourceLocation *endLoc) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000809 enum { Introduced, Deprecated, Obsoleted, Unknown };
810 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000811 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000812
813 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000814 BalancedDelimiterTracker T(*this, tok::l_paren);
815 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000816 Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000817 return;
818 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000819
820 // Parse the platform name,
821 if (Tok.isNot(tok::identifier)) {
822 Diag(Tok, diag::err_availability_expected_platform);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000823 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000824 return;
825 }
Richard Smithfeefaf52013-09-03 18:01:40 +0000826 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000827
828 // Parse the ',' following the platform name.
Alp Toker383d2c42014-01-01 03:08:43 +0000829 if (ExpectAndConsume(tok::comma)) {
830 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000831 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000832 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000833
834 // If we haven't grabbed the pointers for the identifiers
835 // "introduced", "deprecated", and "obsoleted", do so now.
836 if (!Ident_introduced) {
837 Ident_introduced = PP.getIdentifierInfo("introduced");
838 Ident_deprecated = PP.getIdentifierInfo("deprecated");
839 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000840 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000841 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000842 }
843
844 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000845 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000846 do {
847 if (Tok.isNot(tok::identifier)) {
848 Diag(Tok, diag::err_availability_expected_change);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000849 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000850 return;
851 }
852 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
853 SourceLocation KeywordLoc = ConsumeToken();
854
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000855 if (Keyword == Ident_unavailable) {
856 if (UnavailableLoc.isValid()) {
857 Diag(KeywordLoc, diag::err_availability_redundant)
858 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000859 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000860 UnavailableLoc = KeywordLoc;
861
Alp Toker383d2c42014-01-01 03:08:43 +0000862 if (TryConsumeToken(tok::comma))
863 continue;
864 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000865 }
866
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000867 if (Tok.isNot(tok::equal)) {
Alp Tokerec543272013-12-24 09:48:30 +0000868 Diag(Tok, diag::err_expected_after) << Keyword << tok::equal;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000869 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000870 return;
871 }
872 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000873 if (Keyword == Ident_message) {
Benjamin Kramera9dfa922013-09-13 17:31:48 +0000874 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000875 Diag(Tok, diag::err_expected_string_literal)
876 << /*Source='availability attribute'*/2;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000877 SkipUntil(tok::r_paren, StopAtSemi);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000878 return;
879 }
880 MessageExpr = ParseStringLiteralExpression();
881 break;
882 }
Chad Rosierc1183952012-06-26 22:30:43 +0000883
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000884 SourceRange VersionRange;
885 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000886
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000887 if (Version.empty()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000888 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000889 return;
890 }
891
892 unsigned Index;
893 if (Keyword == Ident_introduced)
894 Index = Introduced;
895 else if (Keyword == Ident_deprecated)
896 Index = Deprecated;
897 else if (Keyword == Ident_obsoleted)
898 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000899 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000900 Index = Unknown;
901
902 if (Index < Unknown) {
903 if (!Changes[Index].KeywordLoc.isInvalid()) {
904 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000905 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000906 << SourceRange(Changes[Index].KeywordLoc,
907 Changes[Index].VersionRange.getEnd());
908 }
909
910 Changes[Index].KeywordLoc = KeywordLoc;
911 Changes[Index].Version = Version;
912 Changes[Index].VersionRange = VersionRange;
913 } else {
914 Diag(KeywordLoc, diag::err_availability_unknown_change)
915 << Keyword << VersionRange;
916 }
917
918 if (Tok.isNot(tok::comma))
919 break;
920
921 ConsumeToken();
922 } while (true);
923
924 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000925 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000926 return;
927
928 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000929 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000930
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000931 // The 'unavailable' availability cannot be combined with any other
932 // availability changes. Make sure that hasn't happened.
933 if (UnavailableLoc.isValid()) {
934 bool Complained = false;
935 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
936 if (Changes[Index].KeywordLoc.isValid()) {
937 if (!Complained) {
938 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
939 << SourceRange(Changes[Index].KeywordLoc,
940 Changes[Index].VersionRange.getEnd());
941 Complained = true;
942 }
943
944 // Clear out the availability.
945 Changes[Index] = AvailabilityChange();
946 }
947 }
948 }
949
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000950 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000951 attrs.addNew(&Availability,
952 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000953 0, AvailabilityLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +0000954 Platform,
John McCall084e83d2011-03-24 11:26:52 +0000955 Changes[Introduced],
956 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000957 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000958 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000959 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000960}
961
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000962/// \brief Parse the contents of the "objc_bridge_related" attribute.
963/// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')'
964/// related_class:
965/// Identifier
966///
967/// opt-class_method:
968/// Identifier: | <empty>
969///
970/// opt-instance_method:
971/// Identifier | <empty>
972///
973void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated,
974 SourceLocation ObjCBridgeRelatedLoc,
975 ParsedAttributes &attrs,
976 SourceLocation *endLoc) {
977 // Opening '('.
978 BalancedDelimiterTracker T(*this, tok::l_paren);
979 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000980 Diag(Tok, diag::err_expected) << tok::l_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000981 return;
982 }
983
984 // Parse the related class name.
985 if (Tok.isNot(tok::identifier)) {
986 Diag(Tok, diag::err_objcbridge_related_expected_related_class);
987 SkipUntil(tok::r_paren, StopAtSemi);
988 return;
989 }
990 IdentifierLoc *RelatedClass = ParseIdentifierLoc();
Alp Toker8fbec672013-12-17 23:29:36 +0000991 if (!TryConsumeToken(tok::comma)) {
Alp Tokerec543272013-12-24 09:48:30 +0000992 Diag(Tok, diag::err_expected) << tok::comma;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000993 SkipUntil(tok::r_paren, StopAtSemi);
994 return;
995 }
Alp Toker8fbec672013-12-17 23:29:36 +0000996
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000997 // Parse optional class method name.
998 IdentifierLoc *ClassMethod = 0;
999 if (Tok.is(tok::identifier)) {
1000 ClassMethod = ParseIdentifierLoc();
Alp Toker8fbec672013-12-17 23:29:36 +00001001 if (!TryConsumeToken(tok::colon)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001002 Diag(Tok, diag::err_objcbridge_related_selector_name);
1003 SkipUntil(tok::r_paren, StopAtSemi);
1004 return;
1005 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001006 }
Alp Toker8fbec672013-12-17 23:29:36 +00001007 if (!TryConsumeToken(tok::comma)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001008 if (Tok.is(tok::colon))
1009 Diag(Tok, diag::err_objcbridge_related_selector_name);
1010 else
Alp Tokerec543272013-12-24 09:48:30 +00001011 Diag(Tok, diag::err_expected) << tok::comma;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001012 SkipUntil(tok::r_paren, StopAtSemi);
1013 return;
1014 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001015
1016 // Parse optional instance method name.
1017 IdentifierLoc *InstanceMethod = 0;
1018 if (Tok.is(tok::identifier))
1019 InstanceMethod = ParseIdentifierLoc();
1020 else if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001021 Diag(Tok, diag::err_expected) << tok::r_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001022 SkipUntil(tok::r_paren, StopAtSemi);
1023 return;
1024 }
1025
1026 // Closing ')'.
1027 if (T.consumeClose())
1028 return;
1029
1030 if (endLoc)
1031 *endLoc = T.getCloseLocation();
1032
1033 // Record this attribute
1034 attrs.addNew(&ObjCBridgeRelated,
1035 SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
1036 0, ObjCBridgeRelatedLoc,
1037 RelatedClass,
1038 ClassMethod,
1039 InstanceMethod,
1040 AttributeList::AS_GNU);
1041
1042}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001043
Bill Wendling44426052012-12-20 19:22:21 +00001044// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001045// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
1046
1047void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
1048
1049void Parser::LateParsedClass::ParseLexedAttributes() {
1050 Self->ParseLexedAttributes(*Class);
1051}
1052
1053void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001054 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001055}
1056
1057/// Wrapper class which calls ParseLexedAttribute, after setting up the
1058/// scope appropriately.
1059void Parser::ParseLexedAttributes(ParsingClass &Class) {
1060 // Deal with templates
1061 // FIXME: Test cases to make sure this does the right thing for templates.
1062 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
1063 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
1064 HasTemplateScope);
1065 if (HasTemplateScope)
1066 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
1067
Douglas Gregor3024f072012-04-16 07:05:22 +00001068 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001069 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +00001070 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001071 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1072 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1073
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001074 // Enter the scope of nested classes
1075 if (!AlreadyHasClassScope)
1076 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1077 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001078 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001079 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1080 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1081 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001082 }
Chad Rosierc1183952012-06-26 22:30:43 +00001083
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001084 if (!AlreadyHasClassScope)
1085 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1086 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001087}
1088
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001089
1090/// \brief Parse all attributes in LAs, and attach them to Decl D.
1091void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1092 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001093 assert(LAs.parseSoon() &&
1094 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001095 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001096 if (D)
1097 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001098 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001099 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001100 }
1101 LAs.clear();
1102}
1103
1104
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001105/// \brief Finish parsing an attribute for which parsing was delayed.
1106/// This will be called at the end of parsing a class declaration
1107/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001108/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001109/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001110void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1111 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001112 // Save the current token position.
1113 SourceLocation OrigLoc = Tok.getLocation();
1114
1115 // Append the current token at the end of the new token stream so that it
1116 // doesn't get lost.
1117 LA.Toks.push_back(Tok);
1118 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1119 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001120 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001121
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001122 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +00001123 // FIXME: Do not warn on C++11 attributes, once we start supporting
1124 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001125 Diag(Tok, diag::warn_attribute_on_function_definition)
Aaron Ballman6d80b3c2014-01-02 18:10:17 +00001126 << &LA.AttrName;
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001127 }
1128
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001129 ParsedAttributes Attrs(AttrFactory);
1130 SourceLocation endLoc;
1131
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001132 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001133 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001134 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1135 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001136
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001137 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001138 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1139 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001140
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001141 if (LA.Decls.size() == 1) {
1142 // If the Decl is templatized, add template parameters to scope.
1143 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1144 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1145 if (HasTemplateScope)
1146 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001147
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001148 // If the Decl is on a function, add function parameters to the scope.
1149 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1150 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1151 if (HasFunScope)
1152 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001153
Michael Han23214e52012-10-03 01:56:22 +00001154 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001155 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001156
1157 if (HasFunScope) {
1158 Actions.ActOnExitFunctionContext();
1159 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1160 }
1161 if (HasTemplateScope) {
1162 TempScope.Exit();
1163 }
1164 } else {
1165 // If there are multiple decls, then the decl cannot be within the
1166 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001167 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001168 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001169 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001170 } else {
1171 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001172 }
1173
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001174 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1175 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1176 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001177
1178 if (Tok.getLocation() != OrigLoc) {
1179 // Due to a parsing error, we either went over the cached tokens or
1180 // there are still cached tokens left, so we skip the leftover tokens.
1181 // Since this is an uncommon situation that should be avoided, use the
1182 // expensive isBeforeInTranslationUnit call.
1183 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1184 OrigLoc))
1185 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001186 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001187 }
1188}
1189
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001190/// \brief Wrapper around a case statement checking if AttrName is
1191/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001192bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001193 return llvm::StringSwitch<bool>(AttrName)
1194 .Case("guarded_by", true)
1195 .Case("guarded_var", true)
1196 .Case("pt_guarded_by", true)
1197 .Case("pt_guarded_var", true)
1198 .Case("lockable", true)
1199 .Case("scoped_lockable", true)
1200 .Case("no_thread_safety_analysis", true)
1201 .Case("acquired_after", true)
1202 .Case("acquired_before", true)
1203 .Case("exclusive_lock_function", true)
1204 .Case("shared_lock_function", true)
1205 .Case("exclusive_trylock_function", true)
1206 .Case("shared_trylock_function", true)
1207 .Case("unlock_function", true)
1208 .Case("lock_returned", true)
1209 .Case("locks_excluded", true)
1210 .Case("exclusive_locks_required", true)
1211 .Case("shared_locks_required", true)
1212 .Default(false);
1213}
1214
1215/// \brief Parse the contents of thread safety attributes. These
1216/// should always be parsed as an expression list.
1217///
1218/// We need to special case the parsing due to the fact that if the first token
1219/// of the first argument is an identifier, the main parse loop will store
1220/// that token as a "parameter" and the rest of
1221/// the arguments will be added to a list of "arguments". However,
1222/// subsequent tokens in the first argument are lost. We instead parse each
1223/// argument as an expression and add all arguments to the list of "arguments".
1224/// In future, we will take advantage of this special case to also
1225/// deal with some argument scoping issues here (for example, referring to a
1226/// function parameter in the attribute on that function).
1227void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1228 SourceLocation AttrNameLoc,
1229 ParsedAttributes &Attrs,
1230 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001231 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001232
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001233 BalancedDelimiterTracker T(*this, tok::l_paren);
1234 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001235
Aaron Ballman00e99962013-08-31 01:11:41 +00001236 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001237 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001238
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001239 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001240 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001241 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001242 ExprResult ArgExpr(ParseAssignmentExpression());
1243 if (ArgExpr.isInvalid()) {
1244 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001245 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001246 break;
1247 } else {
1248 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001249 }
Alp Toker8fbec672013-12-17 23:29:36 +00001250 // Eat the comma, move to the next argument
1251 if (!TryConsumeToken(tok::comma))
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001252 break;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001253 }
1254 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001255 if (ArgExprsOk && !T.consumeClose()) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001256 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(),
1257 ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001258 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001259 if (EndLoc)
1260 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001261}
1262
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001263void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1264 SourceLocation AttrNameLoc,
1265 ParsedAttributes &Attrs,
1266 SourceLocation *EndLoc) {
1267 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1268
1269 BalancedDelimiterTracker T(*this, tok::l_paren);
1270 T.consumeOpen();
1271
1272 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001273 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001274 T.skipToEnd();
1275 return;
1276 }
Richard Smithfeefaf52013-09-03 18:01:40 +00001277 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001278
1279 if (Tok.isNot(tok::comma)) {
Alp Tokerec543272013-12-24 09:48:30 +00001280 Diag(Tok, diag::err_expected) << tok::comma;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001281 T.skipToEnd();
1282 return;
1283 }
1284 ConsumeToken();
1285
1286 SourceRange MatchingCTypeRange;
1287 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1288 if (MatchingCType.isInvalid()) {
1289 T.skipToEnd();
1290 return;
1291 }
1292
1293 bool LayoutCompatible = false;
1294 bool MustBeNull = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001295 while (TryConsumeToken(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001296 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001297 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001298 T.skipToEnd();
1299 return;
1300 }
1301 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1302 if (Flag->isStr("layout_compatible"))
1303 LayoutCompatible = true;
1304 else if (Flag->isStr("must_be_null"))
1305 MustBeNull = true;
1306 else {
1307 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1308 T.skipToEnd();
1309 return;
1310 }
1311 ConsumeToken(); // consume flag
1312 }
1313
1314 if (!T.consumeClose()) {
1315 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +00001316 ArgumentKind, MatchingCType.release(),
1317 LayoutCompatible, MustBeNull,
1318 AttributeList::AS_GNU);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001319 }
1320
1321 if (EndLoc)
1322 *EndLoc = T.getCloseLocation();
1323}
1324
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001325/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1326/// of a C++11 attribute-specifier in a location where an attribute is not
1327/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1328/// situation.
1329///
1330/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1331/// this doesn't appear to actually be an attribute-specifier, and the caller
1332/// should try to parse it.
1333bool Parser::DiagnoseProhibitedCXX11Attribute() {
1334 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1335
1336 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1337 case CAK_NotAttributeSpecifier:
1338 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1339 return false;
1340
1341 case CAK_InvalidAttributeSpecifier:
1342 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1343 return false;
1344
1345 case CAK_AttributeSpecifier:
1346 // Parse and discard the attributes.
1347 SourceLocation BeginLoc = ConsumeBracket();
1348 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001349 SkipUntil(tok::r_square);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001350 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1351 SourceLocation EndLoc = ConsumeBracket();
1352 Diag(BeginLoc, diag::err_attributes_not_allowed)
1353 << SourceRange(BeginLoc, EndLoc);
1354 return true;
1355 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001356 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001357}
1358
Richard Smith98155ad2013-02-20 01:17:14 +00001359/// \brief We have found the opening square brackets of a C++11
1360/// attribute-specifier in a location where an attribute is not permitted, but
1361/// we know where the attributes ought to be written. Parse them anyway, and
1362/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001363void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1364 SourceLocation CorrectLocation) {
1365 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1366 Tok.is(tok::kw_alignas));
1367
1368 // Consume the attributes.
1369 SourceLocation Loc = Tok.getLocation();
1370 ParseCXX11Attributes(Attrs);
1371 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1372
1373 Diag(Loc, diag::err_attributes_not_allowed)
1374 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1375 << FixItHint::CreateRemoval(AttrRange);
1376}
1377
John McCall53fa7142010-12-24 02:08:15 +00001378void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1379 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1380 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001381}
1382
Michael Han64536a62012-11-06 19:34:54 +00001383void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1384 AttributeList *AttrList = attrs.getList();
1385 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001386 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001387 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001388 << AttrList->getName();
1389 AttrList->setInvalid();
1390 }
1391 AttrList = AttrList->getNext();
1392 }
1393}
1394
Chris Lattner53361ac2006-08-10 05:19:57 +00001395/// ParseDeclaration - Parse a full 'declaration', which consists of
1396/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001397/// 'Context' should be a Declarator::TheContext value. This returns the
1398/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001399///
1400/// declaration: [C99 6.7]
1401/// block-declaration ->
1402/// simple-declaration
1403/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001404/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001405/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001406/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001407/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001408/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001409/// others... [FIXME]
1410///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001411Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1412 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001413 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001414 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001415 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001416 // Must temporarily exit the objective-c container scope for
1417 // parsing c none objective-c decls.
1418 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001419
John McCall48871652010-08-21 09:40:31 +00001420 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001421 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001422 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001423 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001424 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001425 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001426 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001427 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001428 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001429 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001430 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001431 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001432 SourceLocation InlineLoc = ConsumeToken();
1433 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1434 break;
1435 }
Chad Rosierc1183952012-06-26 22:30:43 +00001436 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001437 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001438 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001439 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001440 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001441 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001442 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001443 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001444 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001445 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001446 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001447 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001448 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001449 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001450 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001451 default:
John McCall53fa7142010-12-24 02:08:15 +00001452 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001453 }
Chad Rosierc1183952012-06-26 22:30:43 +00001454
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001455 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001456 // single decl, convert it now. Alias declarations can also declare a type;
1457 // include that too if it is present.
1458 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001459}
1460
1461/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1462/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001463/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1464/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001465///[C90/C++]init-declarator-list ';' [TODO]
1466/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001467///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001468/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001469/// attribute-specifier-seq[opt] type-specifier-seq declarator
1470///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001471/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001472/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001473///
1474/// If FRI is non-null, we might be parsing a for-range-declaration instead
1475/// of a simple-declaration. If we find that we are, we also parse the
1476/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001477Parser::DeclGroupPtrTy
1478Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1479 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001480 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001481 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001482 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001483 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001484
Richard Smith404dfb42013-11-19 22:47:36 +00001485 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
1486 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
1487
1488 // If we had a free-standing type definition with a missing semicolon, we
1489 // may get this far before the problem becomes obvious.
1490 if (DS.hasTagDefinition() &&
1491 DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
1492 return DeclGroupPtrTy();
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001493
Chris Lattner0e894622006-08-13 19:58:17 +00001494 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1495 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001496 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001497 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001498 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001499 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001500 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001501 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001502 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001503 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001504 }
Chad Rosierc1183952012-06-26 22:30:43 +00001505
Richard Smith2386c8b2013-02-22 09:06:26 +00001506 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001507 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001508}
Mike Stump11289f42009-09-09 15:08:12 +00001509
Richard Smith09f76ee2011-10-19 21:33:05 +00001510/// Returns true if this might be the start of a declarator, or a common typo
1511/// for a declarator.
1512bool Parser::MightBeDeclarator(unsigned Context) {
1513 switch (Tok.getKind()) {
1514 case tok::annot_cxxscope:
1515 case tok::annot_template_id:
1516 case tok::caret:
1517 case tok::code_completion:
1518 case tok::coloncolon:
1519 case tok::ellipsis:
1520 case tok::kw___attribute:
1521 case tok::kw_operator:
1522 case tok::l_paren:
1523 case tok::star:
1524 return true;
1525
1526 case tok::amp:
1527 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001528 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001529
Richard Smithc8a79032012-01-09 22:31:44 +00001530 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001531 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001532 NextToken().is(tok::l_square);
1533
1534 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001535 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001536
Richard Smith09f76ee2011-10-19 21:33:05 +00001537 case tok::identifier:
1538 switch (NextToken().getKind()) {
1539 case tok::code_completion:
1540 case tok::coloncolon:
1541 case tok::comma:
1542 case tok::equal:
1543 case tok::equalequal: // Might be a typo for '='.
1544 case tok::kw_alignas:
1545 case tok::kw_asm:
1546 case tok::kw___attribute:
1547 case tok::l_brace:
1548 case tok::l_paren:
1549 case tok::l_square:
1550 case tok::less:
1551 case tok::r_brace:
1552 case tok::r_paren:
1553 case tok::r_square:
1554 case tok::semi:
1555 return true;
1556
1557 case tok::colon:
1558 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001559 // and in block scope it's probably a label. Inside a class definition,
1560 // this is a bit-field.
1561 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001562 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001563
1564 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001565 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001566
1567 default:
1568 return false;
1569 }
1570
1571 default:
1572 return false;
1573 }
1574}
1575
Richard Smithb8caac82012-04-11 20:59:20 +00001576/// Skip until we reach something which seems like a sensible place to pick
1577/// up parsing after a malformed declaration. This will sometimes stop sooner
1578/// than SkipUntil(tok::r_brace) would, but will never stop later.
1579void Parser::SkipMalformedDecl() {
1580 while (true) {
1581 switch (Tok.getKind()) {
1582 case tok::l_brace:
1583 // Skip until matching }, then stop. We've probably skipped over
1584 // a malformed class or function definition or similar.
1585 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001586 SkipUntil(tok::r_brace);
Richard Smithb8caac82012-04-11 20:59:20 +00001587 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1588 // This declaration isn't over yet. Keep skipping.
1589 continue;
1590 }
Alp Toker8fbec672013-12-17 23:29:36 +00001591 TryConsumeToken(tok::semi);
Richard Smithb8caac82012-04-11 20:59:20 +00001592 return;
1593
1594 case tok::l_square:
1595 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001596 SkipUntil(tok::r_square);
Richard Smithb8caac82012-04-11 20:59:20 +00001597 continue;
1598
1599 case tok::l_paren:
1600 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001601 SkipUntil(tok::r_paren);
Richard Smithb8caac82012-04-11 20:59:20 +00001602 continue;
1603
1604 case tok::r_brace:
1605 return;
1606
1607 case tok::semi:
1608 ConsumeToken();
1609 return;
1610
1611 case tok::kw_inline:
1612 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001613 // a good place to pick back up parsing, except in an Objective-C
1614 // @interface context.
1615 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1616 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001617 return;
1618 break;
1619
1620 case tok::kw_namespace:
1621 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001622 // place to pick back up parsing, except in an Objective-C
1623 // @interface context.
1624 if (Tok.isAtStartOfLine() &&
1625 (!ParsingInObjCContainer || CurParsedObjCImpl))
1626 return;
1627 break;
1628
1629 case tok::at:
1630 // @end is very much like } in Objective-C contexts.
1631 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1632 ParsingInObjCContainer)
1633 return;
1634 break;
1635
1636 case tok::minus:
1637 case tok::plus:
1638 // - and + probably start new method declarations in Objective-C contexts.
1639 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001640 return;
1641 break;
1642
1643 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001644 case tok::annot_module_begin:
1645 case tok::annot_module_end:
1646 case tok::annot_module_include:
Richard Smithb8caac82012-04-11 20:59:20 +00001647 return;
1648
1649 default:
1650 break;
1651 }
1652
1653 ConsumeAnyToken();
1654 }
1655}
1656
John McCalld5a36322009-11-03 19:26:08 +00001657/// ParseDeclGroup - Having concluded that this is either a function
1658/// definition or a group of object declarations, actually parse the
1659/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001660Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1661 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001662 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001663 SourceLocation *DeclEnd,
1664 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001665 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001666 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001667 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001668
John McCalld5a36322009-11-03 19:26:08 +00001669 // Bail out if the first declarator didn't seem well-formed.
1670 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001671 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001672 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001673 }
Mike Stump11289f42009-09-09 15:08:12 +00001674
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001675 // Save late-parsed attributes for now; they need to be parsed in the
1676 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001677 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1678 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001679 if (D.isFunctionDeclarator())
1680 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1681
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001682 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001683 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001684 // Look at the next token to make sure that this isn't a function
1685 // declaration. We have to check this because __attribute__ might be the
1686 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001687 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001688
Douglas Gregor012efe22013-04-16 16:01:32 +00001689 if (AllowFunctionDefinitions) {
1690 if (isStartOfFunctionDefinition(D)) {
1691 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1692 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001693
Douglas Gregor012efe22013-04-16 16:01:32 +00001694 // Recover by treating the 'typedef' as spurious.
1695 DS.ClearStorageClassSpecs();
1696 }
1697
1698 Decl *TheDecl =
1699 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1700 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001701 }
1702
Douglas Gregor012efe22013-04-16 16:01:32 +00001703 if (isDeclarationSpecifier()) {
1704 // If there is an invalid declaration specifier right after the function
1705 // prototype, then we must be in a missing semicolon case where this isn't
1706 // actually a body. Just fall through into the code that handles it as a
1707 // prototype, and let the top-level code handle the erroneous declspec
1708 // where it would otherwise expect a comma or semicolon.
1709 } else {
1710 Diag(Tok, diag::err_expected_fn_body);
1711 SkipUntil(tok::semi);
1712 return DeclGroupPtrTy();
1713 }
John McCalld5a36322009-11-03 19:26:08 +00001714 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001715 if (Tok.is(tok::l_brace)) {
1716 Diag(Tok, diag::err_function_definition_not_allowed);
Serge Pavlov1de51512013-12-09 05:25:47 +00001717 SkipMalformedDecl();
1718 return DeclGroupPtrTy();
Douglas Gregor012efe22013-04-16 16:01:32 +00001719 }
John McCalld5a36322009-11-03 19:26:08 +00001720 }
1721 }
1722
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001723 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001724 return DeclGroupPtrTy();
1725
1726 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1727 // must parse and analyze the for-range-initializer before the declaration is
1728 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001729 //
1730 // Handle the Objective-C for-in loop variable similarly, although we
1731 // don't need to parse the container in advance.
1732 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1733 bool IsForRangeLoop = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001734 if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001735 IsForRangeLoop = true;
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001736 if (Tok.is(tok::l_brace))
1737 FRI->RangeExpr = ParseBraceInitializer();
1738 else
1739 FRI->RangeExpr = ParseExpression();
1740 }
1741
Richard Smith02e85f32011-04-14 22:09:26 +00001742 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001743 if (IsForRangeLoop)
1744 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001745 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001746 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001747 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001748 }
1749
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001750 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001751 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001752 if (LateParsedAttrs.size() > 0)
1753 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001754 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001755 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001756 DeclsInGroup.push_back(FirstDecl);
1757
Richard Smith09f76ee2011-10-19 21:33:05 +00001758 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001759
John McCalld5a36322009-11-03 19:26:08 +00001760 // If we don't have a comma, it is either the end of the list (a ';') or an
1761 // error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00001762 SourceLocation CommaLoc;
1763 while (TryConsumeToken(tok::comma, CommaLoc)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001764 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1765 // This comma was followed by a line-break and something which can't be
1766 // the start of a declarator. The comma was probably a typo for a
1767 // semicolon.
1768 Diag(CommaLoc, diag::err_expected_semi_declaration)
1769 << FixItHint::CreateReplacement(CommaLoc, ";");
1770 ExpectSemi = false;
1771 break;
1772 }
John McCalld5a36322009-11-03 19:26:08 +00001773
1774 // Parse the next declarator.
1775 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001776 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001777
1778 // Accept attributes in an init-declarator. In the first declarator in a
1779 // declaration, these would be part of the declspec. In subsequent
1780 // declarators, they become part of the declarator itself, so that they
1781 // don't apply to declarators after *this* one. Examples:
1782 // short __attribute__((common)) var; -> declspec
1783 // short var __attribute__((common)); -> declarator
1784 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001785 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001786
1787 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001788 if (!D.isInvalidType()) {
1789 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1790 D.complete(ThisDecl);
1791 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001792 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001793 }
John McCalld5a36322009-11-03 19:26:08 +00001794 }
1795
1796 if (DeclEnd)
1797 *DeclEnd = Tok.getLocation();
1798
Richard Smith09f76ee2011-10-19 21:33:05 +00001799 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001800 ExpectAndConsumeSemi(Context == Declarator::FileContext
1801 ? diag::err_invalid_token_after_toplevel_declarator
1802 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001803 // Okay, there was no semicolon and one was expected. If we see a
1804 // declaration specifier, just assume it was missing and continue parsing.
1805 // Otherwise things are very confused and we skip to recover.
1806 if (!isDeclarationSpecifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001807 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Alp Toker8fbec672013-12-17 23:29:36 +00001808 TryConsumeToken(tok::semi);
Chris Lattner13901342010-07-11 22:42:07 +00001809 }
John McCalld5a36322009-11-03 19:26:08 +00001810 }
1811
Rafael Espindolaab417692013-07-09 12:05:01 +00001812 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001813}
1814
Richard Smith02e85f32011-04-14 22:09:26 +00001815/// Parse an optional simple-asm-expr and attributes, and attach them to a
1816/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001817bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001818 // If a simple-asm-expr is present, parse it.
1819 if (Tok.is(tok::kw_asm)) {
1820 SourceLocation Loc;
1821 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1822 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001823 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smith02e85f32011-04-14 22:09:26 +00001824 return true;
1825 }
1826
1827 D.setAsmLabel(AsmLabel.release());
1828 D.SetRangeEnd(Loc);
1829 }
1830
1831 MaybeParseGNUAttributes(D);
1832 return false;
1833}
1834
Douglas Gregor23996282009-05-12 21:31:51 +00001835/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1836/// declarator'. This method parses the remainder of the declaration
1837/// (including any attributes or initializer, among other things) and
1838/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001839///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001840/// init-declarator: [C99 6.7]
1841/// declarator
1842/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001843/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1844/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001845/// [C++] declarator initializer[opt]
1846///
1847/// [C++] initializer:
1848/// [C++] '=' initializer-clause
1849/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001850/// [C++0x] '=' 'default' [TODO]
1851/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001852/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001853///
1854/// According to the standard grammar, =default and =delete are function
1855/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001856///
John McCall48871652010-08-21 09:40:31 +00001857Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001858 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001859 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001860 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001861
Richard Smith02e85f32011-04-14 22:09:26 +00001862 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1863}
Mike Stump11289f42009-09-09 15:08:12 +00001864
Richard Smith02e85f32011-04-14 22:09:26 +00001865Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1866 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001867 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001868 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001869 switch (TemplateInfo.Kind) {
1870 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001871 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001872 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001873
Douglas Gregor450f00842009-09-25 18:43:00 +00001874 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001875 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001876 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001877 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001878 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00001879 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001880 // Re-direct this decl to refer to the templated decl so that we can
1881 // initialize it.
1882 ThisDecl = VT->getTemplatedDecl();
1883 break;
1884 }
1885 case ParsedTemplateInfo::ExplicitInstantiation: {
1886 if (Tok.is(tok::semi)) {
1887 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1888 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1889 if (ThisRes.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001890 SkipUntil(tok::semi, StopBeforeMatch);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001891 return 0;
1892 }
1893 ThisDecl = ThisRes.get();
1894 } else {
1895 // FIXME: This check should be for a variable template instantiation only.
1896
1897 // Check that this is a valid instantiation
1898 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1899 // If the declarator-id is not a template-id, issue a diagnostic and
1900 // recover by ignoring the 'template' keyword.
1901 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1902 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1903 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1904 } else {
1905 SourceLocation LAngleLoc =
1906 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1907 Diag(D.getIdentifierLoc(),
1908 diag::err_explicit_instantiation_with_definition)
1909 << SourceRange(TemplateInfo.TemplateLoc)
1910 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1911
1912 // Recover as if it were an explicit specialization.
1913 TemplateParameterLists FakedParamLists;
1914 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1915 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1916 LAngleLoc));
1917
1918 ThisDecl =
1919 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1920 }
1921 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001922 break;
1923 }
1924 }
Mike Stump11289f42009-09-09 15:08:12 +00001925
Richard Smith74aeef52013-04-26 16:15:35 +00001926 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001927
Douglas Gregor23996282009-05-12 21:31:51 +00001928 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001929 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001930 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001931 ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001932
Anders Carlsson991285e2010-09-24 21:25:25 +00001933 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001934 if (D.isFunctionDeclarator())
1935 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1936 << 1 /* delete */;
1937 else
1938 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001939 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001940 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001941 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1942 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001943 else
1944 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001945 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001946 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001947 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001948 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001949 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001950
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001951 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001952 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001953 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001954 cutOffParsing();
1955 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001956 }
Chad Rosierc1183952012-06-26 22:30:43 +00001957
John McCalldadc5752010-08-24 06:29:42 +00001958 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001959
David Blaikiebbafb8a2012-03-11 07:00:24 +00001960 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001961 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001962 ExitScope();
1963 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001964
Douglas Gregor23996282009-05-12 21:31:51 +00001965 if (Init.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001966 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor604c3022010-03-01 18:27:54 +00001967 Actions.ActOnInitializerError(ThisDecl);
1968 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001969 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1970 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001971 }
1972 } else if (Tok.is(tok::l_paren)) {
1973 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001974 BalancedDelimiterTracker T(*this, tok::l_paren);
1975 T.consumeOpen();
1976
Benjamin Kramerf0623432012-08-23 22:51:59 +00001977 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001978 CommaLocsTy CommaLocs;
1979
David Blaikiebbafb8a2012-03-11 07:00:24 +00001980 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001981 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001982 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001983 }
1984
Douglas Gregor23996282009-05-12 21:31:51 +00001985 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001986 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001987 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor613bf102009-12-22 17:47:17 +00001988
David Blaikiebbafb8a2012-03-11 07:00:24 +00001989 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001990 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001991 ExitScope();
1992 }
Douglas Gregor23996282009-05-12 21:31:51 +00001993 } else {
1994 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001995 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001996
1997 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1998 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001999
David Blaikiebbafb8a2012-03-11 07:00:24 +00002000 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002001 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00002002 ExitScope();
2003 }
2004
Sebastian Redla9351792012-02-11 23:51:47 +00002005 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
2006 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002007 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00002008 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
2009 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00002010 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002011 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00002012 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00002013 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00002014 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2015
Sebastian Redl3da34892011-06-05 12:23:16 +00002016 if (D.getCXXScopeSpec().isSet()) {
2017 EnterScope(0);
2018 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
2019 }
2020
2021 ExprResult Init(ParseBraceInitializer());
2022
2023 if (D.getCXXScopeSpec().isSet()) {
2024 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
2025 ExitScope();
2026 }
2027
2028 if (Init.isInvalid()) {
2029 Actions.ActOnInitializerError(ThisDecl);
2030 } else
2031 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
2032 /*DirectInit=*/true, TypeContainsAuto);
2033
Douglas Gregor23996282009-05-12 21:31:51 +00002034 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00002035 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00002036 }
2037
Richard Smithb2bc2e62011-02-21 20:05:19 +00002038 Actions.FinalizeDeclaration(ThisDecl);
2039
Douglas Gregor23996282009-05-12 21:31:51 +00002040 return ThisDecl;
2041}
2042
Chris Lattner1890ac82006-08-13 01:16:23 +00002043/// ParseSpecifierQualifierList
2044/// specifier-qualifier-list:
2045/// type-specifier specifier-qualifier-list[opt]
2046/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002047/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00002048///
Richard Smithc5b05522012-03-12 07:56:15 +00002049void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
2050 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002051 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
2052 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002053 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00002054 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00002055
Chris Lattner1890ac82006-08-13 01:16:23 +00002056 // Validate declspec for type-name.
2057 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00002058 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
2059 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00002060 Diag(Tok, diag::err_expected_type);
2061 DS.SetTypeSpecError();
2062 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
2063 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002064 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00002065 if (!DS.hasTypeSpecifier())
2066 DS.SetTypeSpecError();
2067 }
Mike Stump11289f42009-09-09 15:08:12 +00002068
Chris Lattner1b22eed2006-11-28 05:12:07 +00002069 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002070 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00002071 if (DS.getStorageClassSpecLoc().isValid())
2072 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2073 else
Richard Smithb4a9e862013-04-12 22:46:28 +00002074 Diag(DS.getThreadStorageClassSpecLoc(),
2075 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00002076 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002077 }
Mike Stump11289f42009-09-09 15:08:12 +00002078
Chris Lattner1b22eed2006-11-28 05:12:07 +00002079 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002080 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002081 if (DS.isInlineSpecified())
2082 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2083 if (DS.isVirtualSpecified())
2084 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2085 if (DS.isExplicitSpecified())
2086 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002087 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002088 }
Richard Smithc5b05522012-03-12 07:56:15 +00002089
2090 // Issue diagnostic and remove constexpr specfier if present.
2091 if (DS.isConstexprSpecified()) {
2092 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2093 DS.ClearConstexprSpec();
2094 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002095}
Chris Lattner53361ac2006-08-10 05:19:57 +00002096
Chris Lattner6cc055a2009-04-12 20:42:31 +00002097/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2098/// specified token is valid after the identifier in a declarator which
2099/// immediately follows the declspec. For example, these things are valid:
2100///
2101/// int x [ 4]; // direct-declarator
2102/// int x ( int y); // direct-declarator
2103/// int(int x ) // direct-declarator
2104/// int x ; // simple-declaration
2105/// int x = 17; // init-declarator-list
2106/// int x , y; // init-declarator-list
2107/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002108/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002109/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002110///
2111/// This is not, because 'x' does not immediately follow the declspec (though
2112/// ')' happens to be valid anyway).
2113/// int (x)
2114///
2115static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2116 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2117 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002118 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002119}
2120
Chris Lattner20a0c612009-04-14 21:34:55 +00002121
2122/// ParseImplicitInt - This method is called when we have an non-typename
2123/// identifier in a declspec (which normally terminates the decl spec) when
2124/// the declspec has no type specifier. In this case, the declspec is either
2125/// malformed or is "implicit int" (in K&R and C89).
2126///
2127/// This method handles diagnosing this prettily and returns false if the
2128/// declspec is done being processed. If it recovers and thinks there may be
2129/// other pieces of declspec after it, it returns true.
2130///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002131bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002132 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002133 AccessSpecifier AS, DeclSpecContext DSC,
2134 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002135 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002136
Chris Lattner20a0c612009-04-14 21:34:55 +00002137 SourceLocation Loc = Tok.getLocation();
2138 // If we see an identifier that is not a type name, we normally would
2139 // parse it as the identifer being declared. However, when a typename
2140 // is typo'd or the definition is not included, this will incorrectly
2141 // parse the typename as the identifier name and fall over misparsing
2142 // later parts of the diagnostic.
2143 //
2144 // As such, we try to do some look-ahead in cases where this would
2145 // otherwise be an "implicit-int" case to see if this is invalid. For
2146 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2147 // an identifier with implicit int, we'd get a parse error because the
2148 // next token is obviously invalid for a type. Parse these as a case
2149 // with an invalid type specifier.
2150 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002151
Chris Lattner20a0c612009-04-14 21:34:55 +00002152 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002153 // error, do lookahead to try to do better recovery. This never applies
2154 // within a type specifier. Outside of C++, we allow this even if the
2155 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002156 // implicit int as an extension in C99 and C11.
Richard Smith2f07ad52012-05-09 20:55:26 +00002157 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith3b870382013-04-30 22:43:51 +00002158 !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002159 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002160 // If this token is valid for implicit int, e.g. "static x = 4", then
2161 // we just avoid eating the identifier, so it will be parsed as the
2162 // identifier in the declarator.
2163 return false;
2164 }
Mike Stump11289f42009-09-09 15:08:12 +00002165
Richard Smitha952ebb2012-05-15 21:01:51 +00002166 if (getLangOpts().CPlusPlus &&
2167 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2168 // Don't require a type specifier if we have the 'auto' storage class
2169 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002170 if (SS)
2171 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002172 return false;
2173 }
2174
Chris Lattner20a0c612009-04-14 21:34:55 +00002175 // Otherwise, if we don't consume this token, we are going to emit an
2176 // error anyway. Try to recover from various common problems. Check
2177 // to see if this was a reference to a tag name without a tag specified.
2178 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002179 //
2180 // C++ doesn't need this, and isTagName doesn't take SS.
2181 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002182 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002183 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002184
Douglas Gregor0be31a22010-07-02 17:43:08 +00002185 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002186 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002187 case DeclSpec::TST_enum:
2188 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2189 case DeclSpec::TST_union:
2190 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2191 case DeclSpec::TST_struct:
2192 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002193 case DeclSpec::TST_interface:
2194 TagName="__interface"; FixitTagName = "__interface ";
2195 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002196 case DeclSpec::TST_class:
2197 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002198 }
Mike Stump11289f42009-09-09 15:08:12 +00002199
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002200 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002201 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2202 LookupResult R(Actions, TokenName, SourceLocation(),
2203 Sema::LookupOrdinaryName);
2204
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002205 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002206 << TokenName << TagName << getLangOpts().CPlusPlus
2207 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2208
2209 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2210 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2211 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002212 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002213 << TokenName << TagName;
2214 }
Mike Stump11289f42009-09-09 15:08:12 +00002215
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002216 // Parse this as a tag as if the missing tag were present.
2217 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002218 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002219 else
Richard Smithc5b05522012-03-12 07:56:15 +00002220 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002221 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002222 return true;
2223 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002224 }
Mike Stump11289f42009-09-09 15:08:12 +00002225
Richard Smithfe904f02012-05-15 21:29:55 +00002226 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002227 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00002228 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2229 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002230 // Look ahead to the next token to try to figure out what this declaration
2231 // was supposed to be.
2232 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002233 case tok::l_paren: {
2234 // static x(4); // 'x' is not a type
2235 // x(int n); // 'x' is not a type
2236 // x (*p)[]; // 'x' is a type
2237 //
2238 // Since we're in an error case (or the rare 'implicit int in C++' MS
2239 // extension), we can afford to perform a tentative parse to determine
2240 // which case we're in.
2241 TentativeParsingAction PA(*this);
2242 ConsumeToken();
2243 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2244 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002245
2246 if (TPR != TPResult::False()) {
2247 // The identifier is followed by a parenthesized declarator.
2248 // It's supposed to be a type.
2249 break;
2250 }
2251
2252 // If we're in a context where we could be declaring a constructor,
2253 // check whether this is a constructor declaration with a bogus name.
2254 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2255 IdentifierInfo *II = Tok.getIdentifierInfo();
2256 if (Actions.isCurrentClassNameTypo(II, SS)) {
2257 Diag(Loc, diag::err_constructor_bad_name)
2258 << Tok.getIdentifierInfo() << II
2259 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2260 Tok.setIdentifierInfo(II);
2261 }
2262 }
2263 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002264 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002265 case tok::comma:
2266 case tok::equal:
2267 case tok::kw_asm:
2268 case tok::l_brace:
2269 case tok::l_square:
2270 case tok::semi:
2271 // This looks like a variable or function declaration. The type is
2272 // probably missing. We're done parsing decl-specifiers.
2273 if (SS)
2274 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2275 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002276
2277 default:
2278 // This is probably supposed to be a type. This includes cases like:
2279 // int f(itn);
2280 // struct S { unsinged : 4; };
2281 break;
2282 }
2283 }
2284
Chad Rosierc1183952012-06-26 22:30:43 +00002285 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002286 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002287 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002288 IdentifierInfo *II = Tok.getIdentifierInfo();
2289 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002290 // The action emitted a diagnostic, so we don't have to.
2291 if (T) {
2292 // The action has suggested that the type T could be used. Set that as
2293 // the type in the declaration specifiers, consume the would-be type
2294 // name token, and we're done.
2295 const char *PrevSpec;
2296 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002297 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002298 DS.SetRangeEnd(Tok.getLocation());
2299 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002300 // There may be other declaration specifiers after this.
2301 return true;
2302 } else if (II != Tok.getIdentifierInfo()) {
2303 // If no type was suggested, the correction is to a keyword
2304 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002305 // There may be other declaration specifiers after this.
2306 return true;
2307 }
Chad Rosierc1183952012-06-26 22:30:43 +00002308
Douglas Gregor15e56022009-10-13 23:27:22 +00002309 // Fall through; the action had no suggestion for us.
2310 } else {
2311 // The action did not emit a diagnostic, so emit one now.
2312 SourceRange R;
2313 if (SS) R = SS->getRange();
2314 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2315 }
Mike Stump11289f42009-09-09 15:08:12 +00002316
Douglas Gregor15e56022009-10-13 23:27:22 +00002317 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002318 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002319 DS.SetRangeEnd(Tok.getLocation());
2320 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002321
Chris Lattner20a0c612009-04-14 21:34:55 +00002322 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2323 // avoid rippling error messages on subsequent uses of the same type,
2324 // could be useful if #include was forgotten.
2325 return false;
2326}
2327
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002328/// \brief Determine the declaration specifier context from the declarator
2329/// context.
2330///
2331/// \param Context the declarator context, which is one of the
2332/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002333Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002334Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2335 if (Context == Declarator::MemberContext)
2336 return DSC_class;
2337 if (Context == Declarator::FileContext)
2338 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002339 if (Context == Declarator::TrailingReturnContext)
2340 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002341 return DSC_normal;
2342}
2343
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002344/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2345///
2346/// FIXME: Simply returns an alignof() expression if the argument is a
2347/// type. Ideally, the type should be propagated directly into Sema.
2348///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002349/// [C11] type-id
2350/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002351/// [C++0x] type-id ...[opt]
2352/// [C++0x] assignment-expression ...[opt]
2353ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2354 SourceLocation &EllipsisLoc) {
2355 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002356 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002357 SourceLocation TypeLoc = Tok.getLocation();
2358 ParsedType Ty = ParseTypeName().get();
2359 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002360 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2361 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002362 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002363 ER = ParseConstantExpression();
2364
Alp Toker8fbec672013-12-17 23:29:36 +00002365 if (getLangOpts().CPlusPlus11)
2366 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002367
2368 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002369}
2370
2371/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2372/// attribute to Attrs.
2373///
2374/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002375/// [C11] '_Alignas' '(' type-id ')'
2376/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002377/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2378/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002379void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002380 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002381 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2382 "Not an alignment-specifier!");
2383
Richard Smithd11c7a12013-01-29 01:48:07 +00002384 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2385 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002386
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002387 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002388 if (T.expectAndConsume())
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002389 return;
2390
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002391 SourceLocation EllipsisLoc;
2392 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002393 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002394 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002395 return;
2396 }
2397
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002398 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002399 if (EndLoc)
2400 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002401
Aaron Ballman00e99962013-08-31 01:11:41 +00002402 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002403 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002404 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2405 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002406}
2407
Richard Smith404dfb42013-11-19 22:47:36 +00002408/// Determine whether we're looking at something that might be a declarator
2409/// in a simple-declaration. If it can't possibly be a declarator, maybe
2410/// diagnose a missing semicolon after a prior tag definition in the decl
2411/// specifier.
2412///
2413/// \return \c true if an error occurred and this can't be any kind of
2414/// declaration.
2415bool
2416Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2417 DeclSpecContext DSContext,
2418 LateParsedAttrList *LateAttrs) {
2419 assert(DS.hasTagDefinition() && "shouldn't call this");
2420
2421 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002422
2423 if (getLangOpts().CPlusPlus &&
2424 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2425 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
2426 TryAnnotateCXXScopeToken(EnteringContext)) {
2427 SkipMalformedDecl();
2428 return true;
2429 }
2430
Richard Smith698875a2013-11-20 23:40:57 +00002431 bool HasScope = Tok.is(tok::annot_cxxscope);
2432 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2433 Token AfterScope = HasScope ? NextToken() : Tok;
2434
Richard Smith404dfb42013-11-19 22:47:36 +00002435 // Determine whether the following tokens could possibly be a
2436 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002437 bool MightBeDeclarator = true;
2438 if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) {
2439 // A declarator-id can't start with 'typename'.
2440 MightBeDeclarator = false;
2441 } else if (AfterScope.is(tok::annot_template_id)) {
2442 // If we have a type expressed as a template-id, this cannot be a
2443 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2444 TemplateIdAnnotation *Annot =
2445 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2446 if (Annot->Kind == TNK_Type_template)
2447 MightBeDeclarator = false;
2448 } else if (AfterScope.is(tok::identifier)) {
2449 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2450
Richard Smith404dfb42013-11-19 22:47:36 +00002451 // These tokens cannot come after the declarator-id in a
2452 // simple-declaration, and are likely to come after a type-specifier.
Richard Smith698875a2013-11-20 23:40:57 +00002453 if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) ||
2454 Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) ||
2455 Next.is(tok::coloncolon)) {
2456 // Missing a semicolon.
2457 MightBeDeclarator = false;
2458 } else if (HasScope) {
2459 // If the declarator-id has a scope specifier, it must redeclare a
2460 // previously-declared entity. If that's a type (and this is not a
2461 // typedef), that's an error.
2462 CXXScopeSpec SS;
2463 Actions.RestoreNestedNameSpecifierAnnotation(
2464 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2465 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2466 Sema::NameClassification Classification = Actions.ClassifyName(
2467 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2468 /*IsAddressOfOperand*/false);
2469 switch (Classification.getKind()) {
2470 case Sema::NC_Error:
2471 SkipMalformedDecl();
2472 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002473
Richard Smith698875a2013-11-20 23:40:57 +00002474 case Sema::NC_Keyword:
2475 case Sema::NC_NestedNameSpecifier:
2476 llvm_unreachable("typo correction and nested name specifiers not "
2477 "possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002478
Richard Smith698875a2013-11-20 23:40:57 +00002479 case Sema::NC_Type:
2480 case Sema::NC_TypeTemplate:
2481 // Not a previously-declared non-type entity.
2482 MightBeDeclarator = false;
2483 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002484
Richard Smith698875a2013-11-20 23:40:57 +00002485 case Sema::NC_Unknown:
2486 case Sema::NC_Expression:
2487 case Sema::NC_VarTemplate:
2488 case Sema::NC_FunctionTemplate:
2489 // Might be a redeclaration of a prior entity.
2490 break;
2491 }
Richard Smith404dfb42013-11-19 22:47:36 +00002492 }
Richard Smith404dfb42013-11-19 22:47:36 +00002493 }
2494
Richard Smith698875a2013-11-20 23:40:57 +00002495 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002496 return false;
2497
2498 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
Alp Toker383d2c42014-01-01 03:08:43 +00002499 diag::err_expected_after)
2500 << DeclSpec::getSpecifierName(DS.getTypeSpecType()) << tok::semi;
Richard Smith404dfb42013-11-19 22:47:36 +00002501
2502 // Try to recover from the typo, by dropping the tag definition and parsing
2503 // the problematic tokens as a type.
2504 //
2505 // FIXME: Split the DeclSpec into pieces for the standalone
2506 // declaration and pieces for the following declaration, instead
2507 // of assuming that all the other pieces attach to new declaration,
2508 // and call ParsedFreeStandingDeclSpec as appropriate.
2509 DS.ClearTypeSpecType();
2510 ParsedTemplateInfo NotATemplate;
2511 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2512 return false;
2513}
2514
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002515/// ParseDeclarationSpecifiers
2516/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002517/// storage-class-specifier declaration-specifiers[opt]
2518/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002519/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002520/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002521/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002522/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002523///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002524/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002525/// 'typedef'
2526/// 'extern'
2527/// 'static'
2528/// 'auto'
2529/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002530/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002531/// [C++11] 'thread_local'
2532/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002533/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002534/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002535/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002536/// [C++] 'virtual'
2537/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002538/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002539/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002540/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002541
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002542///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002543void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002544 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002545 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002546 DeclSpecContext DSContext,
2547 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002548 if (DS.getSourceRange().isInvalid()) {
2549 DS.SetRangeStart(Tok.getLocation());
2550 DS.SetRangeEnd(Tok.getLocation());
2551 }
Chad Rosierc1183952012-06-26 22:30:43 +00002552
Douglas Gregordf593fb2011-11-07 17:33:42 +00002553 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002554 bool AttrsLastTime = false;
2555 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002556 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002557 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002558 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002559 unsigned DiagID = 0;
2560
Chris Lattner4d8f8732006-11-28 05:05:08 +00002561 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002562
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002563 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002564 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002565 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002566 if (!AttrsLastTime)
2567 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002568 else {
2569 // Reject C++11 attributes that appertain to decl specifiers as
2570 // we don't support any C++11 attributes that appertain to decl
2571 // specifiers. This also conforms to what g++ 4.8 is doing.
2572 ProhibitCXX11Attributes(attrs);
2573
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002574 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002575 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002576
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002577 // If this is not a declaration specifier token, we're done reading decl
2578 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002579 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002580 return;
Mike Stump11289f42009-09-09 15:08:12 +00002581
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002582 case tok::l_square:
2583 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002584 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002585 goto DoneWithDeclSpec;
2586
2587 ProhibitAttributes(attrs);
2588 // FIXME: It would be good to recover by accepting the attributes,
2589 // but attempting to do that now would cause serious
2590 // madness in terms of diagnostics.
2591 attrs.clear();
2592 attrs.Range = SourceRange();
2593
2594 ParseCXX11Attributes(attrs);
2595 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002596 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002597
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002598 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002599 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002600 if (DS.hasTypeSpecifier()) {
2601 bool AllowNonIdentifiers
2602 = (getCurScope()->getFlags() & (Scope::ControlScope |
2603 Scope::BlockScope |
2604 Scope::TemplateParamScope |
2605 Scope::FunctionPrototypeScope |
2606 Scope::AtCatchScope)) == 0;
2607 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002608 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002609 (DSContext == DSC_class && DS.isFriendSpecified());
2610
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002611 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002612 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002613 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002614 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002615 }
2616
Douglas Gregor80039242011-02-15 20:33:25 +00002617 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2618 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2619 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002620 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002621 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002622 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002623 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002624 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002625 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002626
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002627 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002628 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002629 }
2630
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002631 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002632 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002633 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002634 if (!DS.hasTypeSpecifier())
2635 DS.SetTypeSpecError();
2636 goto DoneWithDeclSpec;
2637 }
John McCall8bc2a702010-03-01 18:20:46 +00002638 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2639 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002640 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002641
2642 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002643 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002644 goto DoneWithDeclSpec;
2645
John McCall9dab4e62009-12-12 11:40:51 +00002646 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002647 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2648 Tok.getAnnotationRange(),
2649 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002650
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002651 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002652 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002653 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002654 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002655 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002656 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002657
2658 // C++ [class.qual]p2:
2659 // In a lookup in which the constructor is an acceptable lookup
2660 // result and the nested-name-specifier nominates a class C:
2661 //
2662 // - if the name specified after the
2663 // nested-name-specifier, when looked up in C, is the
2664 // injected-class-name of C (Clause 9), or
2665 //
2666 // - if the name specified after the nested-name-specifier
2667 // is the same as the identifier or the
2668 // simple-template-id's template-name in the last
2669 // component of the nested-name-specifier,
2670 //
2671 // the name is instead considered to name the constructor of
2672 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002673 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002674 // Thus, if the template-name is actually the constructor
2675 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002676 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002677 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002678 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002679 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002680 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002681 if (isConstructorDeclarator()) {
2682 // The user meant this to be an out-of-line constructor
2683 // definition, but template arguments are not allowed
2684 // there. Just allow this as a constructor; we'll
2685 // complain about it later.
2686 goto DoneWithDeclSpec;
2687 }
2688
2689 // The user meant this to name a type, but it actually names
2690 // a constructor with some extraneous template
2691 // arguments. Complain, then parse it as a type as the user
2692 // intended.
2693 Diag(TemplateId->TemplateNameLoc,
2694 diag::err_out_of_line_template_id_names_constructor)
2695 << TemplateId->Name;
2696 }
2697
John McCall9dab4e62009-12-12 11:40:51 +00002698 DS.getTypeSpecScope() = SS;
2699 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002700 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002701 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002702 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002703 continue;
2704 }
2705
Douglas Gregorc5790df2009-09-28 07:26:33 +00002706 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002707 DS.getTypeSpecScope() = SS;
2708 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002709 if (Tok.getAnnotationValue()) {
2710 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002711 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002712 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002713 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002714 if (isInvalid)
2715 break;
John McCallba7bf592010-08-24 05:47:05 +00002716 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002717 else
2718 DS.SetTypeSpecError();
2719 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2720 ConsumeToken(); // The typename
2721 }
2722
Douglas Gregor167fa622009-03-25 15:40:00 +00002723 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002724 goto DoneWithDeclSpec;
2725
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002726 // If we're in a context where the identifier could be a class name,
2727 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002728 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002729 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002730 &SS)) {
2731 if (isConstructorDeclarator())
2732 goto DoneWithDeclSpec;
2733
2734 // As noted in C++ [class.qual]p2 (cited above), when the name
2735 // of the class is qualified in a context where it could name
2736 // a constructor, its a constructor name. However, we've
2737 // looked at the declarator, and the user probably meant this
2738 // to be a type. Complain that it isn't supposed to be treated
2739 // as a type, then proceed to parse it as a type.
2740 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2741 << Next.getIdentifierInfo();
2742 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002743
John McCallba7bf592010-08-24 05:47:05 +00002744 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2745 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002746 getCurScope(), &SS,
2747 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002748 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002749 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002750
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002751 // If the referenced identifier is not a type, then this declspec is
2752 // erroneous: We already checked about that it has no type specifier, and
2753 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002754 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002755 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002756 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002757 ParsedAttributesWithRange Attrs(AttrFactory);
2758 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2759 if (!Attrs.empty()) {
2760 AttrsLastTime = true;
2761 attrs.takeAllFrom(Attrs);
2762 }
2763 continue;
2764 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002765 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002766 }
Mike Stump11289f42009-09-09 15:08:12 +00002767
John McCall9dab4e62009-12-12 11:40:51 +00002768 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002769 ConsumeToken(); // The C++ scope.
2770
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002771 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002772 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002773 if (isInvalid)
2774 break;
Mike Stump11289f42009-09-09 15:08:12 +00002775
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002776 DS.SetRangeEnd(Tok.getLocation());
2777 ConsumeToken(); // The typename.
2778
2779 continue;
2780 }
Mike Stump11289f42009-09-09 15:08:12 +00002781
Chris Lattnere387d9e2009-01-21 19:48:37 +00002782 case tok::annot_typename: {
Richard Smith404dfb42013-11-19 22:47:36 +00002783 // If we've previously seen a tag definition, we were almost surely
2784 // missing a semicolon after it.
2785 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
2786 goto DoneWithDeclSpec;
2787
John McCallba7bf592010-08-24 05:47:05 +00002788 if (Tok.getAnnotationValue()) {
2789 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002790 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002791 DiagID, T);
2792 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002793 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002794
Chris Lattner005fc1b2010-04-05 18:18:31 +00002795 if (isInvalid)
2796 break;
2797
Chris Lattnere387d9e2009-01-21 19:48:37 +00002798 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2799 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002800
Chris Lattnere387d9e2009-01-21 19:48:37 +00002801 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2802 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002803 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002804 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002805 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002806
Chris Lattnere387d9e2009-01-21 19:48:37 +00002807 continue;
2808 }
Mike Stump11289f42009-09-09 15:08:12 +00002809
Douglas Gregor06873092011-04-28 15:48:45 +00002810 case tok::kw___is_signed:
2811 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2812 // typically treats it as a trait. If we see __is_signed as it appears
2813 // in libstdc++, e.g.,
2814 //
2815 // static const bool __is_signed;
2816 //
2817 // then treat __is_signed as an identifier rather than as a keyword.
2818 if (DS.getTypeSpecType() == TST_bool &&
2819 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
Alp Toker47642d22013-12-03 06:13:01 +00002820 DS.getStorageClassSpec() == DeclSpec::SCS_static)
2821 TryKeywordIdentFallback(true);
Douglas Gregor06873092011-04-28 15:48:45 +00002822
2823 // We're done with the declaration-specifiers.
2824 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002825
Chris Lattner16fac4f2008-07-26 01:18:38 +00002826 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002827 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002828 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002829 // In C++, check to see if this is a scope specifier like foo::bar::, if
2830 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002831 if (getLangOpts().CPlusPlus) {
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002832 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002833 if (!DS.hasTypeSpecifier())
2834 DS.SetTypeSpecError();
2835 goto DoneWithDeclSpec;
2836 }
2837 if (!Tok.is(tok::identifier))
2838 continue;
2839 }
Mike Stump11289f42009-09-09 15:08:12 +00002840
Chris Lattner16fac4f2008-07-26 01:18:38 +00002841 // This identifier can only be a typedef name if we haven't already seen
2842 // a type-specifier. Without this check we misparse:
2843 // typedef int X; struct Y { short X; }; as 'short int'.
2844 if (DS.hasTypeSpecifier())
2845 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002846
John Thompson22334602010-02-05 00:12:22 +00002847 // Check for need to substitute AltiVec keyword tokens.
2848 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2849 break;
2850
Richard Smith3092a3b2012-05-09 18:56:43 +00002851 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2852 // allow the use of a typedef name as a type specifier.
2853 if (DS.isTypeAltiVecVector())
2854 goto DoneWithDeclSpec;
2855
John McCallba7bf592010-08-24 05:47:05 +00002856 ParsedType TypeRep =
2857 Actions.getTypeName(*Tok.getIdentifierInfo(),
2858 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002859
Chris Lattner6cc055a2009-04-12 20:42:31 +00002860 // If this is not a typedef name, don't parse it as part of the declspec,
2861 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002862 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002863 ParsedAttributesWithRange Attrs(AttrFactory);
2864 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2865 if (!Attrs.empty()) {
2866 AttrsLastTime = true;
2867 attrs.takeAllFrom(Attrs);
2868 }
2869 continue;
2870 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002871 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002872 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002873
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002874 // If we're in a context where the identifier could be a class name,
2875 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002876 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002877 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002878 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002879 goto DoneWithDeclSpec;
2880
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002881 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002882 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002883 if (isInvalid)
2884 break;
Mike Stump11289f42009-09-09 15:08:12 +00002885
Chris Lattner16fac4f2008-07-26 01:18:38 +00002886 DS.SetRangeEnd(Tok.getLocation());
2887 ConsumeToken(); // The identifier
2888
2889 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2890 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002891 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002892 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002893 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002894
Steve Naroffcd5e7822008-09-22 10:28:57 +00002895 // Need to support trailing type qualifiers (e.g. "id<p> const").
2896 // If a type specifier follows, it will be diagnosed elsewhere.
2897 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002898 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002899
2900 // type-name
2901 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002902 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002903 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002904 // This template-id does not refer to a type name, so we're
2905 // done with the type-specifiers.
2906 goto DoneWithDeclSpec;
2907 }
2908
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002909 // If we're in a context where the template-id could be a
2910 // constructor name or specialization, check whether this is a
2911 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002912 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002913 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002914 isConstructorDeclarator())
2915 goto DoneWithDeclSpec;
2916
Douglas Gregor7f741122009-02-25 19:37:18 +00002917 // Turn the template-id annotation token into a type annotation
2918 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002919 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002920 continue;
2921 }
2922
Chris Lattnere37e2332006-08-15 04:50:22 +00002923 // GNU attributes support.
2924 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002925 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002926 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002927
2928 // Microsoft declspec support.
2929 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002930 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002931 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002932
Steve Naroff44ac7772008-12-25 14:16:32 +00002933 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002934 case tok::kw___forceinline: {
Serge Pavlov750db652013-11-13 06:57:53 +00002935 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002936 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002937 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002938 // FIXME: This does not work correctly if it is set to be a declspec
2939 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman00e99962013-08-31 01:11:41 +00002940 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2941 AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002942 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002943 }
Eli Friedman53339e02009-06-08 23:27:34 +00002944
Aaron Ballman317a77f2013-05-22 23:25:32 +00002945 case tok::kw___sptr:
2946 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002947 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002948 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002949 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002950 case tok::kw___cdecl:
2951 case tok::kw___stdcall:
2952 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002953 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002954 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002955 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002956 continue;
2957
Dawn Perchik335e16b2010-09-03 01:29:35 +00002958 // Borland single token adornments.
2959 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002960 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002961 continue;
2962
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002963 // OpenCL single token adornments.
2964 case tok::kw___kernel:
2965 ParseOpenCLAttributes(DS.getAttributes());
2966 continue;
2967
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002968 // storage-class-specifier
2969 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002970 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2971 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002972 break;
2973 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002974 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002975 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002976 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2977 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002978 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002979 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002980 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2981 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002982 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002983 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002984 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002985 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002986 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2987 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002988 break;
2989 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002990 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002991 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002992 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2993 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002994 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002995 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002996 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002997 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002998 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2999 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00003000 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003001 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
3002 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003003 break;
3004 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003005 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
3006 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003007 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00003008 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003009 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
3010 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00003011 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003012 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00003013 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
3014 PrevSpec, DiagID);
3015 break;
3016 case tok::kw_thread_local:
3017 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
3018 PrevSpec, DiagID);
3019 break;
3020 case tok::kw__Thread_local:
3021 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
3022 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003023 break;
Mike Stump11289f42009-09-09 15:08:12 +00003024
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003025 // function-specifier
3026 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00003027 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003028 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00003029 case tok::kw_virtual:
Serge Pavlov750db652013-11-13 06:57:53 +00003030 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00003031 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00003032 case tok::kw_explicit:
Serge Pavlov750db652013-11-13 06:57:53 +00003033 isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00003034 break;
Richard Smith0015f092013-01-17 22:16:11 +00003035 case tok::kw__Noreturn:
3036 if (!getLangOpts().C11)
3037 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlov750db652013-11-13 06:57:53 +00003038 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00003039 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003040
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003041 // alignment-specifier
3042 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003043 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00003044 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003045 ParseAlignmentSpecifier(DS.getAttributes());
3046 continue;
3047
Anders Carlssoncd8db412009-05-06 04:46:28 +00003048 // friend
3049 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00003050 if (DSContext == DSC_class)
3051 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
3052 else {
3053 PrevSpec = ""; // not actually used by the diagnostic
3054 DiagID = diag::err_friend_invalid_in_context;
3055 isInvalid = true;
3056 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00003057 break;
Mike Stump11289f42009-09-09 15:08:12 +00003058
Douglas Gregor26701a42011-09-09 02:06:17 +00003059 // Modules
3060 case tok::kw___module_private__:
3061 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
3062 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003063
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003064 // constexpr
3065 case tok::kw_constexpr:
3066 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
3067 break;
3068
Chris Lattnere387d9e2009-01-21 19:48:37 +00003069 // type-specifier
3070 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00003071 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
3072 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003073 break;
3074 case tok::kw_long:
3075 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00003076 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
3077 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003078 else
John McCall49bfce42009-08-03 20:12:06 +00003079 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3080 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003081 break;
Francois Pichet84133e42011-04-28 01:59:37 +00003082 case tok::kw___int64:
3083 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3084 DiagID);
3085 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003086 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00003087 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3088 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003089 break;
3090 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003091 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3092 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003093 break;
3094 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00003095 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3096 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003097 break;
3098 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00003099 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3100 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003101 break;
3102 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003103 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
3104 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003105 break;
3106 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003107 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
3108 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003109 break;
3110 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003111 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
3112 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003113 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003114 case tok::kw___int128:
3115 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
3116 DiagID);
3117 break;
3118 case tok::kw_half:
3119 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
3120 DiagID);
3121 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003122 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003123 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
3124 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003125 break;
3126 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003127 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
3128 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003129 break;
3130 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003131 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
3132 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003133 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003134 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003135 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
3136 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003137 break;
3138 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003139 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
3140 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003141 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003142 case tok::kw_bool:
3143 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003144 if (Tok.is(tok::kw_bool) &&
3145 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3146 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3147 PrevSpec = ""; // Not used by the diagnostic.
3148 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003149 // For better error recovery.
3150 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003151 isInvalid = true;
3152 } else {
3153 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
3154 DiagID);
3155 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003156 break;
3157 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003158 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
3159 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003160 break;
3161 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003162 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
3163 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003164 break;
3165 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003166 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
3167 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003168 break;
John Thompson22334602010-02-05 00:12:22 +00003169 case tok::kw___vector:
3170 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3171 break;
3172 case tok::kw___pixel:
3173 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3174 break;
John McCall39439732011-04-09 22:50:59 +00003175 case tok::kw___unknown_anytype:
3176 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3177 PrevSpec, DiagID);
3178 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003179
3180 // class-specifier:
3181 case tok::kw_class:
3182 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003183 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003184 case tok::kw_union: {
3185 tok::TokenKind Kind = Tok.getKind();
3186 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003187
3188 // These are attributes following class specifiers.
3189 // To produce better diagnostic, we parse them when
3190 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003191 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003192 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003193 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003194
3195 // If there are attributes following class specifier,
3196 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003197 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003198 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003199 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003200 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003201 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003202 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003203
3204 // enum-specifier:
3205 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003206 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003207 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003208 continue;
3209
3210 // cv-qualifier:
3211 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003212 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003213 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003214 break;
3215 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003216 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003217 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003218 break;
3219 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003220 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003221 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003222 break;
3223
Douglas Gregor333489b2009-03-27 23:10:48 +00003224 // C++ typename-specifier:
3225 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003226 if (TryAnnotateTypeOrScopeToken()) {
3227 DS.SetTypeSpecError();
3228 goto DoneWithDeclSpec;
3229 }
3230 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003231 continue;
3232 break;
3233
Chris Lattnere387d9e2009-01-21 19:48:37 +00003234 // GNU typeof support.
3235 case tok::kw_typeof:
3236 ParseTypeofSpecifier(DS);
3237 continue;
3238
David Blaikie15a430a2011-12-04 05:04:18 +00003239 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003240 ParseDecltypeSpecifier(DS);
3241 continue;
3242
Alexis Hunt4a257072011-05-19 05:37:45 +00003243 case tok::kw___underlying_type:
3244 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003245 continue;
3246
3247 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003248 // C11 6.7.2.4/4:
3249 // If the _Atomic keyword is immediately followed by a left parenthesis,
3250 // it is interpreted as a type specifier (with a type name), not as a
3251 // type qualifier.
3252 if (NextToken().is(tok::l_paren)) {
3253 ParseAtomicSpecifier(DS);
3254 continue;
3255 }
3256 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3257 getLangOpts());
3258 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003259
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003260 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003261 case tok::kw___private:
3262 case tok::kw___global:
3263 case tok::kw___local:
3264 case tok::kw___constant:
3265 case tok::kw___read_only:
3266 case tok::kw___write_only:
3267 case tok::kw___read_write:
3268 ParseOpenCLQualifiers(DS);
3269 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003270
Steve Naroffcfdf6162008-06-05 00:02:44 +00003271 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003272 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003273 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3274 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003275 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003276 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003277
Douglas Gregor3a001f42010-11-19 17:10:50 +00003278 if (!ParseObjCProtocolQualifiers(DS))
3279 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3280 << FixItHint::CreateInsertion(Loc, "id")
3281 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003282
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003283 // Need to support trailing type qualifiers (e.g. "id<p> const").
3284 // If a type specifier follows, it will be diagnosed elsewhere.
3285 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003286 }
John McCall49bfce42009-08-03 20:12:06 +00003287 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003288 if (isInvalid) {
3289 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003290 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003291
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003292 if (DiagID == diag::ext_duplicate_declspec)
3293 Diag(Tok, DiagID)
3294 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3295 else
3296 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003297 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003298
Chris Lattner2e232092008-03-13 06:29:04 +00003299 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003300 if (DiagID != diag::err_bool_redeclaration)
3301 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003302
3303 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003304 }
3305}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003306
Chris Lattner70ae4912007-10-29 04:42:53 +00003307/// ParseStructDeclaration - Parse a struct declaration without the terminating
3308/// semicolon.
3309///
Chris Lattner90a26b02007-01-23 04:38:16 +00003310/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003311/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003312/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003313/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003314/// struct-declarator-list:
3315/// struct-declarator
3316/// struct-declarator-list ',' struct-declarator
3317/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3318/// struct-declarator:
3319/// declarator
3320/// [GNU] declarator attributes[opt]
3321/// declarator[opt] ':' constant-expression
3322/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3323///
Chris Lattnera12405b2008-04-10 06:46:29 +00003324void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003325ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003326
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003327 if (Tok.is(tok::kw___extension__)) {
3328 // __extension__ silences extension warnings in the subexpression.
3329 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003330 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003331 return ParseStructDeclaration(DS, Fields);
3332 }
Mike Stump11289f42009-09-09 15:08:12 +00003333
Steve Naroff97170802007-08-20 22:28:22 +00003334 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003335 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003336
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003337 // If there are no declarators, this is a free-standing declaration
3338 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003339 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003340 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3341 DS);
3342 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003343 return;
3344 }
3345
3346 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003347 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003348 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003349 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003350 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003351 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003352
Bill Wendling44426052012-12-20 19:22:21 +00003353 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003354 if (!FirstDeclarator)
3355 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003356
Steve Naroff97170802007-08-20 22:28:22 +00003357 /// struct-declarator: declarator
3358 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003359 if (Tok.isNot(tok::colon)) {
3360 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3361 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003362 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003363 }
Mike Stump11289f42009-09-09 15:08:12 +00003364
Alp Toker8fbec672013-12-17 23:29:36 +00003365 if (TryConsumeToken(tok::colon)) {
John McCalldadc5752010-08-24 06:29:42 +00003366 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003367 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003368 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00003369 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003370 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003371 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003372
Steve Naroff97170802007-08-20 22:28:22 +00003373 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003374 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003375
John McCallcfefb6d2009-11-03 02:38:08 +00003376 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003377 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003378
Steve Naroff97170802007-08-20 22:28:22 +00003379 // If we don't have a comma, it is either the end of the list (a ';')
3380 // or an error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00003381 if (!TryConsumeToken(tok::comma, CommaLoc))
Chris Lattner70ae4912007-10-29 04:42:53 +00003382 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003383
John McCallcfefb6d2009-11-03 02:38:08 +00003384 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003385 }
Steve Naroff97170802007-08-20 22:28:22 +00003386}
3387
3388/// ParseStructUnionBody
3389/// struct-contents:
3390/// struct-declaration-list
3391/// [EXT] empty
3392/// [GNU] "struct-declaration-list" without terminatoring ';'
3393/// struct-declaration-list:
3394/// struct-declaration
3395/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003396/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003397///
Chris Lattner1300fb92007-01-23 23:42:53 +00003398void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003399 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003400 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3401 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003402 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003403
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003404 BalancedDelimiterTracker T(*this, tok::l_brace);
3405 if (T.consumeOpen())
3406 return;
Mike Stump11289f42009-09-09 15:08:12 +00003407
Douglas Gregor658b9552009-01-09 22:42:13 +00003408 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003409 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003410
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003411 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003412
Chris Lattner7b9ace62007-01-23 20:11:08 +00003413 // While we still have something to read, read the declarations in the struct.
Richard Smith34f30512013-11-23 04:06:09 +00003414 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003415 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003416
Chris Lattner736ed5d2007-06-09 05:59:07 +00003417 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003418 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003419 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003420 continue;
3421 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003422
Andy Gibbsc804e082013-04-03 09:46:04 +00003423 // Parse _Static_assert declaration.
3424 if (Tok.is(tok::kw__Static_assert)) {
3425 SourceLocation DeclEnd;
3426 ParseStaticAssertDeclaration(DeclEnd);
3427 continue;
3428 }
3429
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003430 if (Tok.is(tok::annot_pragma_pack)) {
3431 HandlePragmaPack();
3432 continue;
3433 }
3434
3435 if (Tok.is(tok::annot_pragma_align)) {
3436 HandlePragmaAlign();
3437 continue;
3438 }
3439
John McCallcfefb6d2009-11-03 02:38:08 +00003440 if (!Tok.is(tok::at)) {
3441 struct CFieldCallback : FieldCallback {
3442 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003443 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003444 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003445
John McCall48871652010-08-21 09:40:31 +00003446 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003447 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003448 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3449
Eli Friedman934dbbf2012-08-08 23:53:27 +00003450 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003451 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003452 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003453 FD.D.getDeclSpec().getSourceRange().getBegin(),
3454 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003455 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003456 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003457 }
John McCallcfefb6d2009-11-03 02:38:08 +00003458 } Callback(*this, TagDecl, FieldDecls);
3459
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003460 // Parse all the comma separated declarators.
3461 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003462 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003463 } else { // Handle @defs
3464 ConsumeToken();
3465 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3466 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003467 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003468 continue;
3469 }
3470 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003471 ExpectAndConsume(tok::l_paren);
Chris Lattner535b8302008-06-21 19:39:06 +00003472 if (!Tok.is(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003473 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003474 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003475 continue;
3476 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003477 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003478 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003479 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003480 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3481 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003482 ExpectAndConsume(tok::r_paren);
Mike Stump11289f42009-09-09 15:08:12 +00003483 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003484
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003485 if (TryConsumeToken(tok::semi))
3486 continue;
3487
3488 if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003489 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003490 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003491 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003492
3493 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3494 // Skip to end of block or statement to avoid ext-warning on extra ';'.
3495 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
3496 // If we stopped at a ';', eat it.
3497 TryConsumeToken(tok::semi);
Chris Lattner90a26b02007-01-23 04:38:16 +00003498 }
Mike Stump11289f42009-09-09 15:08:12 +00003499
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003500 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003501
John McCall084e83d2011-03-24 11:26:52 +00003502 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003503 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003504 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003505
Douglas Gregor0be31a22010-07-02 17:43:08 +00003506 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003507 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003508 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003509 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003510 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003511 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3512 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003513}
3514
Chris Lattner3b561a32006-08-13 00:12:11 +00003515/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003516/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003517/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003518///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003519/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3520/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003521/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3522/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003523/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003524/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003525///
Richard Smith7d137e32012-03-23 03:33:32 +00003526/// [C++11] enum-head '{' enumerator-list[opt] '}'
3527/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003528///
Richard Smith7d137e32012-03-23 03:33:32 +00003529/// enum-head: [C++11]
3530/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3531/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3532/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003533///
Richard Smith7d137e32012-03-23 03:33:32 +00003534/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003535/// 'enum'
3536/// 'enum' 'class'
3537/// 'enum' 'struct'
3538///
Richard Smith7d137e32012-03-23 03:33:32 +00003539/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003540/// ':' type-specifier-seq
3541///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003542/// [C++] elaborated-type-specifier:
3543/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3544///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003545void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003546 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003547 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003548 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003549 if (Tok.is(tok::code_completion)) {
3550 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003551 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003552 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003553 }
John McCallcb432fa2011-07-06 05:58:41 +00003554
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003555 // If attributes exist after tag, parse them.
3556 ParsedAttributesWithRange attrs(AttrFactory);
3557 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003558 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003559
3560 // If declspecs exist after tag, parse them.
3561 while (Tok.is(tok::kw___declspec))
3562 ParseMicrosoftDeclSpec(attrs);
3563
Richard Smith0f8ee222012-01-10 01:33:14 +00003564 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003565 bool IsScopedUsingClassTag = false;
3566
John McCallbeae29a2012-06-23 22:30:04 +00003567 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003568 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3569 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3570 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003571 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003572 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003573
Bill Wendling44426052012-12-20 19:22:21 +00003574 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003575 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003576 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003577
3578 // They are allowed afterwards, though.
3579 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003580 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003581 while (Tok.is(tok::kw___declspec))
3582 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003583 }
Richard Smith7d137e32012-03-23 03:33:32 +00003584
John McCall6347b682012-05-07 06:16:58 +00003585 // C++11 [temp.explicit]p12:
3586 // The usual access controls do not apply to names used to specify
3587 // explicit instantiations.
3588 // We extend this to also cover explicit specializations. Note that
3589 // we don't suppress if this turns out to be an elaborated type
3590 // specifier.
3591 bool shouldDelayDiagsInTag =
3592 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3593 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3594 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003595
Richard Smithbfdb1082012-03-12 08:56:40 +00003596 // Enum definitions should not be parsed in a trailing-return-type.
3597 bool AllowDeclaration = DSC != DSC_trailing;
3598
3599 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003600 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003601 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003602
Abramo Bagnarad7548482010-05-19 21:37:53 +00003603 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003604 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003605 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3606 // if a fixed underlying type is allowed.
3607 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003608
3609 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003610 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003611 return;
3612
3613 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003614 Diag(Tok, diag::err_expected) << tok::identifier;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003615 if (Tok.isNot(tok::l_brace)) {
3616 // Has no name and is not a definition.
3617 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003618 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003619 return;
3620 }
3621 }
3622 }
Mike Stump11289f42009-09-09 15:08:12 +00003623
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003624 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003625 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003626 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Alp Tokerec543272013-12-24 09:48:30 +00003627 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
Mike Stump11289f42009-09-09 15:08:12 +00003628
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003629 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003630 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00003631 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003632 }
Mike Stump11289f42009-09-09 15:08:12 +00003633
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003634 // If an identifier is present, consume and remember it.
3635 IdentifierInfo *Name = 0;
3636 SourceLocation NameLoc;
3637 if (Tok.is(tok::identifier)) {
3638 Name = Tok.getIdentifierInfo();
3639 NameLoc = ConsumeToken();
3640 }
Mike Stump11289f42009-09-09 15:08:12 +00003641
Richard Smith0f8ee222012-01-10 01:33:14 +00003642 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003643 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3644 // declaration of a scoped enumeration.
3645 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003646 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003647 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003648 }
3649
John McCall6347b682012-05-07 06:16:58 +00003650 // Okay, end the suppression area. We'll decide whether to emit the
3651 // diagnostics in a second.
3652 if (shouldDelayDiagsInTag)
3653 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003654
Douglas Gregor0bf31402010-10-08 23:50:27 +00003655 TypeResult BaseType;
3656
Douglas Gregord1f69f62010-12-01 17:42:47 +00003657 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003658 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003659 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003660 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003661 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003662 // If we're in class scope, this can either be an enum declaration with
3663 // an underlying type, or a declaration of a bitfield member. We try to
3664 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003665 // (integer literal, sizeof); if it's still ambiguous, we then consider
3666 // anything that's a simple-type-specifier followed by '(' as an
3667 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003668 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003669 EnterExpressionEvaluationContext Unevaluated(Actions,
3670 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003671 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003672 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003673 // bit-field. This is the common case.
3674 if (TPR == TPResult::True())
3675 PossibleBitfield = true;
3676 // If the next token starts a type-specifier-seq, it may be either a
3677 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003678 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003679 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003680 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003681 GetLookAheadToken(2).getKind() == tok::semi) {
3682 // Consume the ':'.
3683 ConsumeToken();
3684 } else {
3685 // We have the start of a type-specifier-seq, so we have to perform
3686 // tentative parsing to determine whether we have an expression or a
3687 // type.
3688 TentativeParsingAction TPA(*this);
3689
3690 // Consume the ':'.
3691 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003692
3693 // If we see a type specifier followed by an open-brace, we have an
3694 // ambiguity between an underlying type and a C++11 braced
3695 // function-style cast. Resolve this by always treating it as an
3696 // underlying type.
3697 // FIXME: The standard is not entirely clear on how to disambiguate in
3698 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003699 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003700 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003701 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003702 // We'll parse this as a bitfield later.
3703 PossibleBitfield = true;
3704 TPA.Revert();
3705 } else {
3706 // We have a type-specifier-seq.
3707 TPA.Commit();
3708 }
3709 }
3710 } else {
3711 // Consume the ':'.
3712 ConsumeToken();
3713 }
3714
3715 if (!PossibleBitfield) {
3716 SourceRange Range;
3717 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003718
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003719 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003720 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003721 } else if (!getLangOpts().ObjC2) {
3722 if (getLangOpts().CPlusPlus)
3723 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3724 else
3725 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3726 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003727 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003728 }
3729
Richard Smith0f8ee222012-01-10 01:33:14 +00003730 // There are four options here. If we have 'friend enum foo;' then this is a
3731 // friend declaration, and cannot have an accompanying definition. If we have
3732 // 'enum foo;', then this is a forward declaration. If we have
3733 // 'enum foo {...' then this is a definition. Otherwise we have something
3734 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003735 //
3736 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3737 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3738 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3739 //
John McCallfaf5fb42010-08-26 23:41:50 +00003740 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003741 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003742 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003743 } else if (Tok.is(tok::l_brace)) {
3744 if (DS.isFriendSpecified()) {
3745 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3746 << SourceRange(DS.getFriendSpecLoc());
3747 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003748 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00003749 TUK = Sema::TUK_Friend;
3750 } else {
3751 TUK = Sema::TUK_Definition;
3752 }
Richard Smith369b9f92012-06-25 21:37:02 +00003753 } else if (DSC != DSC_type_specifier &&
3754 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003755 (Tok.isAtStartOfLine() &&
3756 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003757 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3758 if (Tok.isNot(tok::semi)) {
3759 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00003760 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003761 PP.EnterToken(Tok);
3762 Tok.setKind(tok::semi);
3763 }
John McCall6347b682012-05-07 06:16:58 +00003764 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003765 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003766 }
3767
3768 // If this is an elaborated type specifier, and we delayed
3769 // diagnostics before, just merge them into the current pool.
3770 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3771 diagsFromTag.redelay();
3772 }
Richard Smith7d137e32012-03-23 03:33:32 +00003773
3774 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003775 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003776 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003777 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003778 // Skip the rest of this declarator, up until the comma or semicolon.
3779 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003780 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00003781 return;
3782 }
3783
3784 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3785 // Enumerations can't be explicitly instantiated.
3786 DS.SetTypeSpecError();
3787 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3788 return;
3789 }
3790
3791 assert(TemplateInfo.TemplateParams && "no template parameters");
3792 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3793 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003794 }
Chad Rosierc1183952012-06-26 22:30:43 +00003795
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003796 if (TUK == Sema::TUK_Reference)
3797 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003798
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003799 if (!Name && TUK != Sema::TUK_Definition) {
3800 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003801
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003802 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003803 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003804 return;
3805 }
Richard Smith7d137e32012-03-23 03:33:32 +00003806
Douglas Gregord6ab8742009-05-28 23:31:59 +00003807 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003808 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003809 const char *PrevSpec = 0;
3810 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003811 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003812 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003813 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003814 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003815 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003816
Douglas Gregorba41d012010-04-24 16:38:41 +00003817 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003818 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003819 // dependent tag.
3820 if (!Name) {
3821 DS.SetTypeSpecError();
3822 Diag(Tok, diag::err_expected_type_name_after_typename);
3823 return;
3824 }
Chad Rosierc1183952012-06-26 22:30:43 +00003825
Douglas Gregor0be31a22010-07-02 17:43:08 +00003826 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003827 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003828 NameLoc);
3829 if (Type.isInvalid()) {
3830 DS.SetTypeSpecError();
3831 return;
3832 }
Chad Rosierc1183952012-06-26 22:30:43 +00003833
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003834 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3835 NameLoc.isValid() ? NameLoc : StartLoc,
3836 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003837 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003838
Douglas Gregorba41d012010-04-24 16:38:41 +00003839 return;
3840 }
Mike Stump11289f42009-09-09 15:08:12 +00003841
John McCall48871652010-08-21 09:40:31 +00003842 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003843 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003844 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003845 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003846 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003847 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00003848 }
Chad Rosierc1183952012-06-26 22:30:43 +00003849
Douglas Gregorba41d012010-04-24 16:38:41 +00003850 DS.SetTypeSpecError();
3851 return;
3852 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003853
Richard Smith369b9f92012-06-25 21:37:02 +00003854 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003855 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003856
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003857 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3858 NameLoc.isValid() ? NameLoc : StartLoc,
3859 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003860 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003861}
3862
Chris Lattnerc1915e22007-01-25 07:29:02 +00003863/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3864/// enumerator-list:
3865/// enumerator
3866/// enumerator-list ',' enumerator
3867/// enumerator:
3868/// enumeration-constant
3869/// enumeration-constant '=' constant-expression
3870/// enumeration-constant:
3871/// identifier
3872///
John McCall48871652010-08-21 09:40:31 +00003873void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003874 // Enter the scope of the enum body and start the definition.
3875 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003876 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003877
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003878 BalancedDelimiterTracker T(*this, tok::l_brace);
3879 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003880
Chris Lattner37256fb2007-08-27 17:24:30 +00003881 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003882 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003883 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003884
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003885 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003886
John McCall48871652010-08-21 09:40:31 +00003887 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003888
Chris Lattnerc1915e22007-01-25 07:29:02 +00003889 // Parse the enumerator-list.
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003890 while (Tok.isNot(tok::r_brace)) {
3891 // Parse enumerator. If failed, try skipping till the start of the next
3892 // enumerator definition.
3893 if (Tok.isNot(tok::identifier)) {
3894 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
3895 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
3896 TryConsumeToken(tok::comma))
3897 continue;
3898 break;
3899 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003900 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3901 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003902
John McCall811a0f52010-10-22 23:36:17 +00003903 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003904 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003905 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003906 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003907 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003908
Chris Lattnerc1915e22007-01-25 07:29:02 +00003909 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003910 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003911 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003912
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003913 if (TryConsumeToken(tok::equal, EqualLoc)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003914 AssignedVal = ParseConstantExpression();
3915 if (AssignedVal.isInvalid())
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003916 SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003917 }
Mike Stump11289f42009-09-09 15:08:12 +00003918
Chris Lattnerc1915e22007-01-25 07:29:02 +00003919 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003920 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3921 LastEnumConstDecl,
3922 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003923 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003924 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003925 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003926
Chris Lattner4ef40012007-06-11 01:28:17 +00003927 EnumConstantDecls.push_back(EnumConstDecl);
3928 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003929
Douglas Gregorce66d022010-09-07 14:51:08 +00003930 if (Tok.is(tok::identifier)) {
3931 // We're missing a comma between enumerators.
3932 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003933 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003934 << FixItHint::CreateInsertion(Loc, ", ");
3935 continue;
3936 }
Chad Rosierc1183952012-06-26 22:30:43 +00003937
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003938 // Emumerator definition must be finished, only comma or r_brace are
3939 // allowed here.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003940 SourceLocation CommaLoc;
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003941 if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
3942 if (EqualLoc.isValid())
3943 Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
3944 << tok::comma;
3945 else
3946 Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
3947 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
3948 if (TryConsumeToken(tok::comma, CommaLoc))
3949 continue;
3950 } else {
3951 break;
3952 }
3953 }
Mike Stump11289f42009-09-09 15:08:12 +00003954
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003955 // If comma is followed by r_brace, emit appropriate warning.
3956 if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003957 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003958 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3959 diag::ext_enumerator_list_comma_cxx :
3960 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003961 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003962 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003963 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3964 << FixItHint::CreateRemoval(CommaLoc);
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003965 break;
Richard Smith5d164bc2011-10-15 05:09:34 +00003966 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003967 }
Mike Stump11289f42009-09-09 15:08:12 +00003968
Chris Lattnerc1915e22007-01-25 07:29:02 +00003969 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003970 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003971
Chris Lattnerc1915e22007-01-25 07:29:02 +00003972 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003973 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003974 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003975
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003976 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003977 EnumDecl, EnumConstantDecls,
3978 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003979 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003980
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003981 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003982 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3983 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003984
3985 // The next token must be valid after an enum definition. If not, a ';'
3986 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003987 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3988 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Alp Toker383d2c42014-01-01 03:08:43 +00003989 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003990 // Push this token back into the preprocessor and change our current token
3991 // to ';' so that the rest of the code recovers as though there were an
3992 // ';' after the definition.
3993 PP.EnterToken(Tok);
3994 Tok.setKind(tok::semi);
3995 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003996}
Chris Lattner3b561a32006-08-13 00:12:11 +00003997
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003998/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003999/// start of a type-qualifier-list.
4000bool Parser::isTypeQualifier() const {
4001 switch (Tok.getKind()) {
4002 default: return false;
Alp Tokerde50ff32013-12-17 18:17:46 +00004003 // type-qualifier
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004004 case tok::kw_const:
4005 case tok::kw_volatile:
4006 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004007 case tok::kw___private:
4008 case tok::kw___local:
4009 case tok::kw___global:
4010 case tok::kw___constant:
4011 case tok::kw___read_only:
4012 case tok::kw___read_write:
4013 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004014 return true;
4015 }
4016}
4017
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004018/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
4019/// is definitely a type-specifier. Return false if it isn't part of a type
4020/// specifier or if we're not sure.
4021bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
4022 switch (Tok.getKind()) {
4023 default: return false;
4024 // type-specifiers
4025 case tok::kw_short:
4026 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004027 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004028 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004029 case tok::kw_signed:
4030 case tok::kw_unsigned:
4031 case tok::kw__Complex:
4032 case tok::kw__Imaginary:
4033 case tok::kw_void:
4034 case tok::kw_char:
4035 case tok::kw_wchar_t:
4036 case tok::kw_char16_t:
4037 case tok::kw_char32_t:
4038 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004039 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004040 case tok::kw_float:
4041 case tok::kw_double:
4042 case tok::kw_bool:
4043 case tok::kw__Bool:
4044 case tok::kw__Decimal32:
4045 case tok::kw__Decimal64:
4046 case tok::kw__Decimal128:
4047 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00004048
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004049 // struct-or-union-specifier (C99) or class-specifier (C++)
4050 case tok::kw_class:
4051 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004052 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004053 case tok::kw_union:
4054 // enum-specifier
4055 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00004056
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004057 // typedef-name
4058 case tok::annot_typename:
4059 return true;
4060 }
4061}
4062
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004063/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004064/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004065bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004066 switch (Tok.getKind()) {
4067 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004068
Chris Lattner020bab92009-01-04 23:41:41 +00004069 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00004070 if (TryAltiVecVectorToken())
4071 return true;
4072 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00004073 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004074 // Annotate typenames and C++ scope specifiers. If we get one, just
4075 // recurse to handle whatever we get.
4076 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004077 return true;
4078 if (Tok.is(tok::identifier))
4079 return false;
4080 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00004081
Chris Lattner020bab92009-01-04 23:41:41 +00004082 case tok::coloncolon: // ::foo::bar
4083 if (NextToken().is(tok::kw_new) || // ::new
4084 NextToken().is(tok::kw_delete)) // ::delete
4085 return false;
4086
Chris Lattner020bab92009-01-04 23:41:41 +00004087 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004088 return true;
4089 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004090
Chris Lattnere37e2332006-08-15 04:50:22 +00004091 // GNU attributes support.
4092 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004093 // GNU typeof support.
4094 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004095
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004096 // type-specifiers
4097 case tok::kw_short:
4098 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004099 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004100 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004101 case tok::kw_signed:
4102 case tok::kw_unsigned:
4103 case tok::kw__Complex:
4104 case tok::kw__Imaginary:
4105 case tok::kw_void:
4106 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004107 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004108 case tok::kw_char16_t:
4109 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004110 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004111 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004112 case tok::kw_float:
4113 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004114 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004115 case tok::kw__Bool:
4116 case tok::kw__Decimal32:
4117 case tok::kw__Decimal64:
4118 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004119 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004120
Chris Lattner861a2262008-04-13 18:59:07 +00004121 // struct-or-union-specifier (C99) or class-specifier (C++)
4122 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004123 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004124 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004125 case tok::kw_union:
4126 // enum-specifier
4127 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004128
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004129 // type-qualifier
4130 case tok::kw_const:
4131 case tok::kw_volatile:
4132 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004133
John McCallea0a39e2012-11-14 00:49:39 +00004134 // Debugger support.
4135 case tok::kw___unknown_anytype:
4136
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004137 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004138 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004139 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004140
Chris Lattner409bf7d2008-10-20 00:25:30 +00004141 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4142 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004143 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004144
Steve Naroff44ac7772008-12-25 14:16:32 +00004145 case tok::kw___cdecl:
4146 case tok::kw___stdcall:
4147 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004148 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004149 case tok::kw___w64:
4150 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004151 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004152 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004153 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004154
4155 case tok::kw___private:
4156 case tok::kw___local:
4157 case tok::kw___global:
4158 case tok::kw___constant:
4159 case tok::kw___read_only:
4160 case tok::kw___read_write:
4161 case tok::kw___write_only:
4162
Eli Friedman53339e02009-06-08 23:27:34 +00004163 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004164
Richard Smith8e1ac332013-03-28 01:55:44 +00004165 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004166 case tok::kw__Atomic:
4167 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004168 }
4169}
4170
Chris Lattneracd58a32006-08-06 17:24:14 +00004171/// isDeclarationSpecifier() - Return true if the current token is part of a
4172/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004173///
4174/// \param DisambiguatingWithExpression True to indicate that the purpose of
4175/// this check is to disambiguate between an expression and a declaration.
4176bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004177 switch (Tok.getKind()) {
4178 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004179
Chris Lattner020bab92009-01-04 23:41:41 +00004180 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004181 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004182 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004183 return false;
John Thompson22334602010-02-05 00:12:22 +00004184 if (TryAltiVecVectorToken())
4185 return true;
4186 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004187 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004188 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004189 // Annotate typenames and C++ scope specifiers. If we get one, just
4190 // recurse to handle whatever we get.
4191 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004192 return true;
4193 if (Tok.is(tok::identifier))
4194 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004195
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004196 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004197 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004198 // expression is permitted, then this is probably a class message send
4199 // missing the initial '['. In this case, we won't consider this to be
4200 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004201 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004202 isStartOfObjCClassMessageMissingOpenBracket())
4203 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004204
John McCall1f476a12010-02-26 08:45:28 +00004205 return isDeclarationSpecifier();
4206
Chris Lattner020bab92009-01-04 23:41:41 +00004207 case tok::coloncolon: // ::foo::bar
4208 if (NextToken().is(tok::kw_new) || // ::new
4209 NextToken().is(tok::kw_delete)) // ::delete
4210 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004211
Chris Lattner020bab92009-01-04 23:41:41 +00004212 // Annotate typenames and C++ scope specifiers. If we get one, just
4213 // recurse to handle whatever we get.
4214 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004215 return true;
4216 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004217
Chris Lattneracd58a32006-08-06 17:24:14 +00004218 // storage-class-specifier
4219 case tok::kw_typedef:
4220 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004221 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004222 case tok::kw_static:
4223 case tok::kw_auto:
4224 case tok::kw_register:
4225 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004226 case tok::kw_thread_local:
4227 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004228
Douglas Gregor26701a42011-09-09 02:06:17 +00004229 // Modules
4230 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004231
John McCallea0a39e2012-11-14 00:49:39 +00004232 // Debugger support
4233 case tok::kw___unknown_anytype:
4234
Chris Lattneracd58a32006-08-06 17:24:14 +00004235 // type-specifiers
4236 case tok::kw_short:
4237 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004238 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004239 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004240 case tok::kw_signed:
4241 case tok::kw_unsigned:
4242 case tok::kw__Complex:
4243 case tok::kw__Imaginary:
4244 case tok::kw_void:
4245 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004246 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004247 case tok::kw_char16_t:
4248 case tok::kw_char32_t:
4249
Chris Lattneracd58a32006-08-06 17:24:14 +00004250 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004251 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004252 case tok::kw_float:
4253 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004254 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004255 case tok::kw__Bool:
4256 case tok::kw__Decimal32:
4257 case tok::kw__Decimal64:
4258 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004259 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004260
Chris Lattner861a2262008-04-13 18:59:07 +00004261 // struct-or-union-specifier (C99) or class-specifier (C++)
4262 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004263 case tok::kw_struct:
4264 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004265 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004266 // enum-specifier
4267 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004268
Chris Lattneracd58a32006-08-06 17:24:14 +00004269 // type-qualifier
4270 case tok::kw_const:
4271 case tok::kw_volatile:
4272 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004273
Chris Lattneracd58a32006-08-06 17:24:14 +00004274 // function-specifier
4275 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004276 case tok::kw_virtual:
4277 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004278 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004279
Richard Smith1dba27c2013-01-29 09:02:09 +00004280 // alignment-specifier
4281 case tok::kw__Alignas:
4282
Richard Smithd16fe122012-10-25 00:00:53 +00004283 // friend keyword.
4284 case tok::kw_friend:
4285
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004286 // static_assert-declaration
4287 case tok::kw__Static_assert:
4288
Chris Lattner599e47e2007-08-09 17:01:07 +00004289 // GNU typeof support.
4290 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004291
Chris Lattner599e47e2007-08-09 17:01:07 +00004292 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004293 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004294
Richard Smithd16fe122012-10-25 00:00:53 +00004295 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004296 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004297 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004298
Richard Smith8e1ac332013-03-28 01:55:44 +00004299 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004300 case tok::kw__Atomic:
4301 return true;
4302
Chris Lattner8b2ec162008-07-26 03:38:44 +00004303 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4304 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004305 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004306
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004307 // typedef-name
4308 case tok::annot_typename:
4309 return !DisambiguatingWithExpression ||
4310 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004311
Steve Narofff192fab2009-01-06 19:34:12 +00004312 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004313 case tok::kw___cdecl:
4314 case tok::kw___stdcall:
4315 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004316 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004317 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004318 case tok::kw___sptr:
4319 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004320 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004321 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004322 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004323 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004324 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004325
4326 case tok::kw___private:
4327 case tok::kw___local:
4328 case tok::kw___global:
4329 case tok::kw___constant:
4330 case tok::kw___read_only:
4331 case tok::kw___read_write:
4332 case tok::kw___write_only:
4333
Eli Friedman53339e02009-06-08 23:27:34 +00004334 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004335 }
4336}
4337
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004338bool Parser::isConstructorDeclarator() {
4339 TentativeParsingAction TPA(*this);
4340
4341 // Parse the C++ scope specifier.
4342 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004343 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004344 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004345 TPA.Revert();
4346 return false;
4347 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004348
4349 // Parse the constructor name.
4350 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4351 // We already know that we have a constructor name; just consume
4352 // the token.
4353 ConsumeToken();
4354 } else {
4355 TPA.Revert();
4356 return false;
4357 }
4358
Richard Smith43f340f2012-03-27 23:05:05 +00004359 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004360 if (Tok.isNot(tok::l_paren)) {
4361 TPA.Revert();
4362 return false;
4363 }
4364 ConsumeParen();
4365
Richard Smith43f340f2012-03-27 23:05:05 +00004366 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4367 // that we have a constructor.
4368 if (Tok.is(tok::r_paren) ||
4369 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004370 TPA.Revert();
4371 return true;
4372 }
4373
Richard Smithf2163662013-09-06 00:12:20 +00004374 // A C++11 attribute here signals that we have a constructor, and is an
4375 // attribute on the first constructor parameter.
4376 if (getLangOpts().CPlusPlus11 &&
4377 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4378 /*OuterMightBeMessageSend*/ true)) {
4379 TPA.Revert();
4380 return true;
4381 }
4382
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004383 // If we need to, enter the specified scope.
4384 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004385 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004386 DeclScopeObj.EnterDeclaratorScope();
4387
Francois Pichet79f3a872011-01-31 04:54:32 +00004388 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004389 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004390 MaybeParseMicrosoftAttributes(Attrs);
4391
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004392 // Check whether the next token(s) are part of a declaration
4393 // specifier, in which case we have the start of a parameter and,
4394 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004395 bool IsConstructor = false;
4396 if (isDeclarationSpecifier())
4397 IsConstructor = true;
4398 else if (Tok.is(tok::identifier) ||
4399 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4400 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4401 // This might be a parenthesized member name, but is more likely to
4402 // be a constructor declaration with an invalid argument type. Keep
4403 // looking.
4404 if (Tok.is(tok::annot_cxxscope))
4405 ConsumeToken();
4406 ConsumeToken();
4407
4408 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004409 // which must have one of the following syntactic forms (see the
4410 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004411 switch (Tok.getKind()) {
4412 case tok::l_paren:
4413 // C(X ( int));
4414 case tok::l_square:
4415 // C(X [ 5]);
4416 // C(X [ [attribute]]);
4417 case tok::coloncolon:
4418 // C(X :: Y);
4419 // C(X :: *p);
4420 case tok::r_paren:
4421 // C(X )
4422 // Assume this isn't a constructor, rather than assuming it's a
4423 // constructor with an unnamed parameter of an ill-formed type.
4424 break;
4425
4426 default:
4427 IsConstructor = true;
4428 break;
4429 }
4430 }
4431
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004432 TPA.Revert();
4433 return IsConstructor;
4434}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004435
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004436/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004437/// type-qualifier-list: [C99 6.7.5]
4438/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004439/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004440/// [ only if VendorAttributesAllowed=true ]
4441/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004442/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004443/// [ only if VendorAttributesAllowed=true ]
4444/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004445/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004446/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004447///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004448void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4449 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004450 bool CXX11AttributesAllowed,
Alp Toker62c5b572013-11-26 01:30:10 +00004451 bool AtomicAllowed,
4452 bool IdentifierRequired) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004453 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004454 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004455 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004456 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004457 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004458 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004459
4460 SourceLocation EndLoc;
4461
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004462 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004463 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004464 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004465 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004466 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004467
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004468 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004469 case tok::code_completion:
4470 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004471 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004472
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004473 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004474 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004475 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004476 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004477 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004478 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004479 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004480 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004481 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004482 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004483 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004484 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004485 case tok::kw__Atomic:
4486 if (!AtomicAllowed)
4487 goto DoneWithTypeQuals;
4488 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4489 getLangOpts());
4490 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004491
4492 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004493 case tok::kw___private:
4494 case tok::kw___global:
4495 case tok::kw___local:
4496 case tok::kw___constant:
4497 case tok::kw___read_only:
4498 case tok::kw___write_only:
4499 case tok::kw___read_write:
4500 ParseOpenCLQualifiers(DS);
4501 break;
4502
Aaron Ballman317a77f2013-05-22 23:25:32 +00004503 case tok::kw___uptr:
Alp Toker62c5b572013-11-26 01:30:10 +00004504 // GNU libc headers in C mode use '__uptr' as an identifer which conflicts
4505 // with the MS modifier keyword.
4506 if (VendorAttributesAllowed && !getLangOpts().CPlusPlus &&
Alp Toker47642d22013-12-03 06:13:01 +00004507 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
4508 if (TryKeywordIdentFallback(false))
4509 continue;
Alp Toker62c5b572013-11-26 01:30:10 +00004510 }
4511 case tok::kw___sptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004512 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004513 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004514 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004515 case tok::kw___cdecl:
4516 case tok::kw___stdcall:
4517 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004518 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004519 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004520 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004521 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004522 continue;
4523 }
4524 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004525 case tok::kw___pascal:
4526 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004527 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004528 continue;
4529 }
4530 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004531 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004532 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004533 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004534 continue; // do *not* consume the next token!
4535 }
4536 // otherwise, FALL THROUGH!
4537 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004538 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004539 // If this is not a type-qualifier token, we're done reading type
4540 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004541 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004542 if (EndLoc.isValid())
4543 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004544 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004545 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004546
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004547 // If the specifier combination wasn't legal, issue a diagnostic.
4548 if (isInvalid) {
4549 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004550 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004551 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004552 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004553 }
4554}
4555
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004556
4557/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4558///
4559void Parser::ParseDeclarator(Declarator &D) {
4560 /// This implements the 'declarator' production in the C grammar, then checks
4561 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004562 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004563}
4564
Richard Smith0efa75c2012-03-29 01:16:42 +00004565static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4566 if (Kind == tok::star || Kind == tok::caret)
4567 return true;
4568
4569 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4570 if (!Lang.CPlusPlus)
4571 return false;
4572
4573 return Kind == tok::amp || Kind == tok::ampamp;
4574}
4575
Sebastian Redlbd150f42008-11-21 19:14:01 +00004576/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4577/// is parsed by the function passed to it. Pass null, and the direct-declarator
4578/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004579/// ptr-operator production.
4580///
Richard Smith09f76ee2011-10-19 21:33:05 +00004581/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004582/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4583/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004584///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004585/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4586/// [C] pointer[opt] direct-declarator
4587/// [C++] direct-declarator
4588/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004589///
4590/// pointer: [C99 6.7.5]
4591/// '*' type-qualifier-list[opt]
4592/// '*' type-qualifier-list[opt] pointer
4593///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004594/// ptr-operator:
4595/// '*' cv-qualifier-seq[opt]
4596/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004597/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004598/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004599/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004600/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004601void Parser::ParseDeclaratorInternal(Declarator &D,
4602 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004603 if (Diags.hasAllExtensionsSilenced())
4604 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004605
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004606 // C++ member pointers start with a '::' or a nested-name.
4607 // Member pointers get special handling, since there's no place for the
4608 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004609 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004610 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4611 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004612 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4613 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004614 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004615 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004616
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004617 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004618 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004619 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004620 if (D.mayHaveIdentifier())
4621 D.getCXXScopeSpec() = SS;
4622 else
4623 AnnotateScopeToken(SS, true);
4624
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004625 if (DirectDeclParser)
4626 (this->*DirectDeclParser)(D);
4627 return;
4628 }
4629
4630 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004631 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004632 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004633 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004634 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004635
4636 // Recurse to parse whatever is left.
4637 ParseDeclaratorInternal(D, DirectDeclParser);
4638
4639 // Sema will have to catch (syntactically invalid) pointers into global
4640 // scope. It has to catch pointers into namespace scope anyway.
4641 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004642 Loc),
4643 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004644 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004645 return;
4646 }
4647 }
4648
4649 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004650 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004651 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004652 if (DirectDeclParser)
4653 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004654 return;
4655 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004656
Sebastian Redled0f3b02009-03-15 22:02:01 +00004657 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4658 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004659 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004660 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004661
Chris Lattner9eac9312009-03-27 04:18:06 +00004662 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004663 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004664 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004665
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004666 // FIXME: GNU attributes are not allowed here in a new-type-id.
Alp Toker62c5b572013-11-26 01:30:10 +00004667 ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004668 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004669
Bill Wendling3708c182007-05-27 10:15:43 +00004670 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004671 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004672 if (Kind == tok::star)
4673 // Remember that we parsed a pointer type, and remember the type-quals.
4674 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004675 DS.getConstSpecLoc(),
4676 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004677 DS.getRestrictSpecLoc()),
4678 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004679 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004680 else
4681 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004682 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004683 Loc),
4684 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004685 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004686 } else {
4687 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004688 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004689
Sebastian Redl3b27be62009-03-23 00:00:23 +00004690 // Complain about rvalue references in C++03, but then go on and build
4691 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004692 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004693 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004694 diag::warn_cxx98_compat_rvalue_reference :
4695 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004696
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004697 // GNU-style and C++11 attributes are allowed here, as is restrict.
4698 ParseTypeQualifierListOpt(DS);
4699 D.ExtendWithDeclSpec(DS);
4700
Bill Wendling93efb222007-06-02 23:28:54 +00004701 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4702 // cv-qualifiers are introduced through the use of a typedef or of a
4703 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004704 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4705 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4706 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004707 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004708 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4709 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004710 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004711 // 'restrict' is permitted as an extension.
4712 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4713 Diag(DS.getAtomicSpecLoc(),
4714 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004715 }
Bill Wendling3708c182007-05-27 10:15:43 +00004716
4717 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004718 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004719
Douglas Gregor66583c52008-11-03 15:51:28 +00004720 if (D.getNumTypeObjects() > 0) {
4721 // C++ [dcl.ref]p4: There shall be no references to references.
4722 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4723 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004724 if (const IdentifierInfo *II = D.getIdentifier())
4725 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4726 << II;
4727 else
4728 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4729 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004730
Sebastian Redlbd150f42008-11-21 19:14:01 +00004731 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004732 // can go ahead and build the (technically ill-formed)
4733 // declarator: reference collapsing will take care of it.
4734 }
4735 }
4736
Richard Smith8e1ac332013-03-28 01:55:44 +00004737 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004738 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004739 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004740 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004741 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004742 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004743}
4744
Richard Smith0efa75c2012-03-29 01:16:42 +00004745static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4746 SourceLocation EllipsisLoc) {
4747 if (EllipsisLoc.isValid()) {
4748 FixItHint Insertion;
4749 if (!D.getEllipsisLoc().isValid()) {
4750 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4751 D.setEllipsisLoc(EllipsisLoc);
4752 }
4753 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4754 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4755 }
4756}
4757
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004758/// ParseDirectDeclarator
4759/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004760/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004761/// '(' declarator ')'
4762/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004763/// [C90] direct-declarator '[' constant-expression[opt] ']'
4764/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4765/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4766/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4767/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004768/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4769/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004770/// direct-declarator '(' parameter-type-list ')'
4771/// direct-declarator '(' identifier-list[opt] ')'
4772/// [GNU] direct-declarator '(' parameter-forward-declarations
4773/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004774/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4775/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004776/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4777/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4778/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004779/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004780/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004781///
4782/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004783/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004784/// '::'[opt] nested-name-specifier[opt] type-name
4785///
4786/// id-expression: [C++ 5.1]
4787/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004788/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004789///
4790/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004791/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004792/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004793/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004794/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004795/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004796///
Richard Smith1453e312012-03-27 01:42:32 +00004797/// Note, any additional constructs added here may need corresponding changes
4798/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004799void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004800 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004801
David Blaikiebbafb8a2012-03-11 07:00:24 +00004802 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004803 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004804 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004805 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4806 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004807 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004808 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004809 }
4810
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004811 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004812 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004813 // Change the declaration context for name lookup, until this function
4814 // is exited (and the declarator has been parsed).
4815 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004816 }
4817
Douglas Gregor27b4c162010-12-23 22:44:42 +00004818 // C++0x [dcl.fct]p14:
4819 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004820 // of a parameter-declaration-clause without a preceding comma. In
4821 // this case, the ellipsis is parsed as part of the
4822 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004823 // parameter pack that has not been expanded; otherwise, it is parsed
4824 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004825 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004826 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004827 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004828 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004829 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004830 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004831 !Actions.containsUnexpandedParameterPacks(D))) {
4832 SourceLocation EllipsisLoc = ConsumeToken();
4833 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4834 // The ellipsis was put in the wrong place. Recover, and explain to
4835 // the user what they should have done.
4836 ParseDeclarator(D);
4837 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4838 return;
4839 } else
4840 D.setEllipsisLoc(EllipsisLoc);
4841
4842 // The ellipsis can't be followed by a parenthesized declarator. We
4843 // check for that in ParseParenDeclarator, after we have disambiguated
4844 // the l_paren token.
4845 }
4846
Douglas Gregor7861a802009-11-03 01:35:08 +00004847 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4848 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4849 // We found something that indicates the start of an unqualified-id.
4850 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004851 bool AllowConstructorName;
4852 if (D.getDeclSpec().hasTypeSpecifier())
4853 AllowConstructorName = false;
4854 else if (D.getCXXScopeSpec().isSet())
4855 AllowConstructorName =
4856 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004857 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004858 else
4859 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4860
Abramo Bagnara7945c982012-01-27 09:46:47 +00004861 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004862 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4863 /*EnteringContext=*/true,
4864 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004865 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004866 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004867 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004868 D.getName()) ||
4869 // Once we're past the identifier, if the scope was bad, mark the
4870 // whole declarator bad.
4871 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004872 D.SetIdentifier(0, Tok.getLocation());
4873 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004874 } else {
4875 // Parsed the unqualified-id; update range information and move along.
4876 if (D.getSourceRange().getBegin().isInvalid())
4877 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4878 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004879 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004880 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004881 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004882 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004883 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004884 "There's a C++-specific check for tok::identifier above");
4885 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4886 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4887 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004888 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004889 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004890 // A virt-specifier isn't treated as an identifier if it appears after a
4891 // trailing-return-type.
4892 if (D.getContext() != Declarator::TrailingReturnContext ||
4893 !isCXX11VirtSpecifier(Tok)) {
4894 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4895 << FixItHint::CreateRemoval(Tok.getLocation());
4896 D.SetIdentifier(0, Tok.getLocation());
4897 ConsumeToken();
4898 goto PastIdentifier;
4899 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004900 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004901
Douglas Gregor7861a802009-11-03 01:35:08 +00004902 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004903 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004904 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004905 // Example: 'char (*X)' or 'int (*XX)(void)'
4906 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004907
4908 // If the declarator was parenthesized, we entered the declarator
4909 // scope when parsing the parenthesized declarator, then exited
4910 // the scope already. Re-enter the scope, if we need to.
4911 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004912 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004913 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004914 if (!D.isInvalidType() &&
4915 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004916 // Change the declaration context for name lookup, until this function
4917 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004918 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004919 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004920 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004921 // This could be something simple like "int" (in which case the declarator
4922 // portion is empty), if an abstract-declarator is allowed.
4923 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004924
4925 // The grammar for abstract-pack-declarator does not allow grouping parens.
4926 // FIXME: Revisit this once core issue 1488 is resolved.
4927 if (D.hasEllipsis() && D.hasGroupingParens())
4928 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4929 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004930 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004931 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004932 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004933 if (D.getContext() == Declarator::MemberContext)
4934 Diag(Tok, diag::err_expected_member_name_or_semi)
4935 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004936 else if (getLangOpts().CPlusPlus) {
4937 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4938 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004939 else {
4940 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4941 if (Tok.isAtStartOfLine() && Loc.isValid())
4942 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4943 << getLangOpts().CPlusPlus;
4944 else
4945 Diag(Tok, diag::err_expected_unqualified_id)
4946 << getLangOpts().CPlusPlus;
4947 }
Richard Trieu9c672672013-01-26 02:31:38 +00004948 } else
Alp Tokerec543272013-12-24 09:48:30 +00004949 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_paren;
Chris Lattnereec40f92006-08-06 21:55:29 +00004950 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004951 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004952 }
Mike Stump11289f42009-09-09 15:08:12 +00004953
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004954 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004955 assert(D.isPastIdentifier() &&
4956 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004957
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004958 // Don't parse attributes unless we have parsed an unparenthesized name.
4959 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004960 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004961
Chris Lattneracd58a32006-08-06 17:24:14 +00004962 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004963 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004964 // Enter function-declaration scope, limiting any declarators to the
4965 // function prototype scope, including parameter declarators.
4966 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004967 Scope::FunctionPrototypeScope|Scope::DeclScope|
4968 (D.isFunctionDeclaratorAFunctionDeclaration()
4969 ? Scope::FunctionDeclarationScope : 0));
4970
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004971 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4972 // In such a case, check if we actually have a function declarator; if it
4973 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004974 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004975 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4976 // The name of the declarator, if any, is tentatively declared within
4977 // a possible direct initializer.
4978 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4979 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4980 TentativelyDeclaredIdentifiers.pop_back();
4981 if (!IsFunctionDecl)
4982 break;
4983 }
John McCall084e83d2011-03-24 11:26:52 +00004984 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004985 BalancedDelimiterTracker T(*this, tok::l_paren);
4986 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004987 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004988 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004989 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004990 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004991 } else {
4992 break;
4993 }
4994 }
Chad Rosierc1183952012-06-26 22:30:43 +00004995}
Chris Lattneracd58a32006-08-06 17:24:14 +00004996
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004997/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4998/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004999/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005000/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
5001///
5002/// direct-declarator:
5003/// '(' declarator ')'
5004/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005005/// direct-declarator '(' parameter-type-list ')'
5006/// direct-declarator '(' identifier-list[opt] ')'
5007/// [GNU] direct-declarator '(' parameter-forward-declarations
5008/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005009///
5010void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005011 BalancedDelimiterTracker T(*this, tok::l_paren);
5012 T.consumeOpen();
5013
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005014 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00005015
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005016 // Eat any attributes before we look at whether this is a grouping or function
5017 // declarator paren. If this is a grouping paren, the attribute applies to
5018 // the type being built up, for example:
5019 // int (__attribute__(()) *x)(long y)
5020 // If this ends up not being a grouping paren, the attribute applies to the
5021 // first argument, for example:
5022 // int (__attribute__(()) int x)
5023 // In either case, we need to eat any attributes to be able to determine what
5024 // sort of paren this is.
5025 //
John McCall084e83d2011-03-24 11:26:52 +00005026 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005027 bool RequiresArg = false;
5028 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00005029 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005030
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005031 // We require that the argument list (if this is a non-grouping paren) be
5032 // present even if the attribute list was empty.
5033 RequiresArg = true;
5034 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00005035
Steve Naroff44ac7772008-12-25 14:16:32 +00005036 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00005037 ParseMicrosoftTypeAttributes(attrs);
5038
Dawn Perchik335e16b2010-09-03 01:29:35 +00005039 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00005040 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00005041 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005042
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005043 // If we haven't past the identifier yet (or where the identifier would be
5044 // stored, if this is an abstract declarator), then this is probably just
5045 // grouping parens. However, if this could be an abstract-declarator, then
5046 // this could also be the start of function arguments (consider 'void()').
5047 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00005048
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005049 if (!D.mayOmitIdentifier()) {
5050 // If this can't be an abstract-declarator, this *must* be a grouping
5051 // paren, because we haven't seen the identifier yet.
5052 isGrouping = true;
5053 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00005054 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
5055 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00005056 isDeclarationSpecifier() || // 'int(int)' is a function.
5057 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005058 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
5059 // considered to be a type, not a K&R identifier-list.
5060 isGrouping = false;
5061 } else {
5062 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
5063 isGrouping = true;
5064 }
Mike Stump11289f42009-09-09 15:08:12 +00005065
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005066 // If this is a grouping paren, handle:
5067 // direct-declarator: '(' declarator ')'
5068 // direct-declarator: '(' attributes declarator ')'
5069 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005070 SourceLocation EllipsisLoc = D.getEllipsisLoc();
5071 D.setEllipsisLoc(SourceLocation());
5072
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005073 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005074 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00005075 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005076 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005077 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00005078 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005079 T.getCloseLocation()),
5080 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005081
5082 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00005083
5084 // An ellipsis cannot be placed outside parentheses.
5085 if (EllipsisLoc.isValid())
5086 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
5087
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005088 return;
5089 }
Mike Stump11289f42009-09-09 15:08:12 +00005090
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005091 // Okay, if this wasn't a grouping paren, it must be the start of a function
5092 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005093 // identifier (and remember where it would have been), then call into
5094 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005095 D.SetIdentifier(0, Tok.getLocation());
5096
David Blaikie15a430a2011-12-04 05:04:18 +00005097 // Enter function-declaration scope, limiting any declarators to the
5098 // function prototype scope, including parameter declarators.
5099 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005100 Scope::FunctionPrototypeScope | Scope::DeclScope |
5101 (D.isFunctionDeclaratorAFunctionDeclaration()
5102 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00005103 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00005104 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005105}
5106
5107/// ParseFunctionDeclarator - We are after the identifier and have parsed the
5108/// declarator D up to a paren, which indicates that we are parsing function
5109/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00005110///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005111/// If FirstArgAttrs is non-null, then the caller parsed those arguments
5112/// immediately after the open paren - they should be considered to be the
5113/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005114///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005115/// If RequiresArg is true, then the first argument of the function is required
5116/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005117///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005118/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5119/// (C++11) ref-qualifier[opt], exception-specification[opt],
5120/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5121///
5122/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00005123/// dynamic-exception-specification
5124/// noexcept-specification
5125///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005126void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005127 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005128 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00005129 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005130 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00005131 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00005132 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00005133 // lparen is already consumed!
5134 assert(D.isPastIdentifier() && "Should not call before identifier!");
5135
5136 // This should be true when the function has typed arguments.
5137 // Otherwise, it is treated as a K&R-style function.
5138 bool HasProto = false;
5139 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005140 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005141 // Remember where we see an ellipsis, if any.
5142 SourceLocation EllipsisLoc;
5143
5144 DeclSpec DS(AttrFactory);
5145 bool RefQualifierIsLValueRef = true;
5146 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00005147 SourceLocation ConstQualifierLoc;
5148 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005149 ExceptionSpecificationType ESpecType = EST_None;
5150 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005151 SmallVector<ParsedType, 2> DynamicExceptions;
5152 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005153 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005154 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00005155 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005156
James Molloy6f8780b2012-02-29 10:24:19 +00005157 Actions.ActOnStartFunctionDeclarator();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005158 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5159 EndLoc is the end location for the function declarator.
5160 They differ for trailing return types. */
5161 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005162 SourceLocation LParenLoc, RParenLoc;
5163 LParenLoc = Tracker.getOpenLocation();
5164 StartLoc = LParenLoc;
5165
Douglas Gregor9e66af42011-07-05 16:44:18 +00005166 if (isFunctionDeclaratorIdentifierList()) {
5167 if (RequiresArg)
5168 Diag(Tok, diag::err_argument_required_after_attribute);
5169
5170 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5171
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005172 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005173 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005174 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005175 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005176 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005177 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005178 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5179 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005180 else if (RequiresArg)
5181 Diag(Tok, diag::err_argument_required_after_attribute);
5182
David Blaikiebbafb8a2012-03-11 07:00:24 +00005183 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005184
5185 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005186 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005187 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005188 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005189 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005190
David Blaikiebbafb8a2012-03-11 07:00:24 +00005191 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005192 // FIXME: Accept these components in any order, and produce fixits to
5193 // correct the order if the user gets it wrong. Ideally we should deal
5194 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005195
5196 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005197 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5198 /*CXX11AttributesAllowed*/ false,
5199 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005200 if (!DS.getSourceRange().getEnd().isInvalid()) {
5201 EndLoc = DS.getSourceRange().getEnd();
5202 ConstQualifierLoc = DS.getConstSpecLoc();
5203 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5204 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005205
5206 // Parse ref-qualifier[opt].
5207 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005208 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005209 diag::warn_cxx98_compat_ref_qualifier :
5210 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005211
Douglas Gregor9e66af42011-07-05 16:44:18 +00005212 RefQualifierIsLValueRef = Tok.is(tok::amp);
5213 RefQualifierLoc = ConsumeToken();
5214 EndLoc = RefQualifierLoc;
5215 }
5216
Douglas Gregor3024f072012-04-16 07:05:22 +00005217 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005218 // If a declaration declares a member function or member function
5219 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005220 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005221 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005222 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005223 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005224 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005225 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005226 (D.getContext() == Declarator::MemberContext
5227 ? !D.getDeclSpec().isFriendSpecified()
5228 : D.getContext() == Declarator::FileContext &&
5229 D.getCXXScopeSpec().isValid() &&
5230 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005231 Sema::CXXThisScopeRAII ThisScope(Actions,
5232 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005233 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005234 (D.getDeclSpec().isConstexprSpecified() &&
5235 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005236 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005237 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005238
Douglas Gregor9e66af42011-07-05 16:44:18 +00005239 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005240 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005241 DynamicExceptions,
5242 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005243 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005244 if (ESpecType != EST_None)
5245 EndLoc = ESpecRange.getEnd();
5246
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005247 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5248 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005249 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005250
Douglas Gregor9e66af42011-07-05 16:44:18 +00005251 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005252 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005253 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005254 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005255 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5256 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005257 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005258 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005259 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005260 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005261 }
5262 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005263 }
5264
5265 // Remember that we parsed a function type, and remember the attributes.
5266 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005267 IsAmbiguous,
5268 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005269 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005270 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005271 DS.getTypeQualifiers(),
5272 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005273 RefQualifierLoc, ConstQualifierLoc,
5274 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005275 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005276 ESpecType, ESpecRange.getBegin(),
5277 DynamicExceptions.data(),
5278 DynamicExceptionRanges.data(),
5279 DynamicExceptions.size(),
5280 NoexceptExpr.isUsable() ?
5281 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005282 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005283 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005284 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005285
5286 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005287}
5288
5289/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5290/// identifier list form for a K&R-style function: void foo(a,b,c)
5291///
5292/// Note that identifier-lists are only allowed for normal declarators, not for
5293/// abstract-declarators.
5294bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005295 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005296 && Tok.is(tok::identifier)
5297 && !TryAltiVecVectorToken()
5298 // K&R identifier lists can't have typedefs as identifiers, per C99
5299 // 6.7.5.3p11.
5300 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5301 // Identifier lists follow a really simple grammar: the identifiers can
5302 // be followed *only* by a ", identifier" or ")". However, K&R
5303 // identifier lists are really rare in the brave new modern world, and
5304 // it is very common for someone to typo a type in a non-K&R style
5305 // list. If we are presented with something like: "void foo(intptr x,
5306 // float y)", we don't want to start parsing the function declarator as
5307 // though it is a K&R style declarator just because intptr is an
5308 // invalid type.
5309 //
5310 // To handle this, we check to see if the token after the first
5311 // identifier is a "," or ")". Only then do we parse it as an
5312 // identifier list.
5313 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5314}
5315
5316/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5317/// we found a K&R-style identifier list instead of a typed parameter list.
5318///
5319/// After returning, ParamInfo will hold the parsed parameters.
5320///
5321/// identifier-list: [C99 6.7.5]
5322/// identifier
5323/// identifier-list ',' identifier
5324///
5325void Parser::ParseFunctionDeclaratorIdentifierList(
5326 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005327 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005328 // If there was no identifier specified for the declarator, either we are in
5329 // an abstract-declarator, or we are in a parameter declarator which was found
5330 // to be abstract. In abstract-declarators, identifier lists are not valid:
5331 // diagnose this.
5332 if (!D.getIdentifier())
5333 Diag(Tok, diag::ext_ident_list_in_param);
5334
5335 // Maintain an efficient lookup of params we have seen so far.
5336 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5337
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005338 do {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005339 // If this isn't an identifier, report the error and skip until ')'.
5340 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00005341 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00005342 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005343 // Forget we parsed anything.
5344 ParamInfo.clear();
5345 return;
5346 }
5347
5348 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5349
5350 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5351 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5352 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5353
5354 // Verify that the argument identifier has not already been mentioned.
5355 if (!ParamsSoFar.insert(ParmII)) {
5356 Diag(Tok, diag::err_param_redefinition) << ParmII;
5357 } else {
5358 // Remember this identifier in ParamInfo.
5359 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5360 Tok.getLocation(),
5361 0));
5362 }
5363
5364 // Eat the identifier.
5365 ConsumeToken();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005366 // The list continues if we see a comma.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005367 } while (TryConsumeToken(tok::comma));
Douglas Gregor9e66af42011-07-05 16:44:18 +00005368}
5369
5370/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5371/// after the opening parenthesis. This function will not parse a K&R-style
5372/// identifier list.
5373///
Richard Smith2620cd92012-04-11 04:01:28 +00005374/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5375/// caller parsed those arguments immediately after the open paren - they should
5376/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005377///
5378/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5379/// be the location of the ellipsis, if any was parsed.
5380///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005381/// parameter-type-list: [C99 6.7.5]
5382/// parameter-list
5383/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005384/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005385///
5386/// parameter-list: [C99 6.7.5]
5387/// parameter-declaration
5388/// parameter-list ',' parameter-declaration
5389///
5390/// parameter-declaration: [C99 6.7.5]
5391/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005392/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005393/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005394/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005395/// declaration-specifiers abstract-declarator[opt]
5396/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005397/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005398/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005399/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005400///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005401void Parser::ParseParameterDeclarationClause(
5402 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005403 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005404 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005405 SourceLocation &EllipsisLoc) {
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005406 do {
5407 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5408 // before deciding this was a parameter-declaration-clause.
5409 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Chris Lattner371ed4e2008-04-06 06:57:35 +00005410 break;
Mike Stump11289f42009-09-09 15:08:12 +00005411
Chris Lattner371ed4e2008-04-06 06:57:35 +00005412 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005413 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005414 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005415
Richard Smith2620cd92012-04-11 04:01:28 +00005416 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005417 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005418
John McCall53fa7142010-12-24 02:08:15 +00005419 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005420 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005421
5422 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005423
5424 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005425 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005426 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005427 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5428 // too much hassle.
5429 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005430
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005431 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005432
Faisal Vali2b391ab2013-09-26 19:54:12 +00005433
5434 // Parse the declarator. This is "PrototypeContext" or
5435 // "LambdaExprParameterContext", because we must accept either
5436 // 'declarator' or 'abstract-declarator' here.
5437 Declarator ParmDeclarator(DS,
5438 D.getContext() == Declarator::LambdaExprContext ?
5439 Declarator::LambdaExprParameterContext :
5440 Declarator::PrototypeContext);
5441 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005442
5443 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005444 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005445
Chris Lattner371ed4e2008-04-06 06:57:35 +00005446 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005447 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005448
Douglas Gregor4d87df52008-12-16 21:30:33 +00005449 // DefArgToks is used when the parsing of default arguments needs
5450 // to be delayed.
5451 CachedTokens *DefArgToks = 0;
5452
Chris Lattner371ed4e2008-04-06 06:57:35 +00005453 // If no parameter was specified, verify that *something* was specified,
5454 // otherwise we have a missing type and identifier.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005455 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5456 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005457 // Completely missing, emit error.
5458 Diag(DSStart, diag::err_missing_param);
5459 } else {
5460 // Otherwise, we have something. Add it and let semantic analysis try
5461 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005462
Chris Lattner371ed4e2008-04-06 06:57:35 +00005463 // Inform the actions module about the parameter declarator, so it gets
5464 // added to the current scope.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005465 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5466 ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005467 // Parse the default argument, if any. We parse the default
5468 // arguments in all dialects; the semantic analysis in
5469 // ActOnParamDefaultArgument will reject the default argument in
5470 // C.
5471 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005472 SourceLocation EqualLoc = Tok.getLocation();
5473
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005474 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005475 if (D.getContext() == Declarator::MemberContext) {
5476 // If we're inside a class definition, cache the tokens
5477 // corresponding to the default argument. We'll actually parse
5478 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005479 // FIXME: Can we use a smart pointer for Toks?
5480 DefArgToks = new CachedTokens;
5481
Richard Smith1fff95c2013-09-12 23:28:08 +00005482 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005483 delete DefArgToks;
5484 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005485 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005486 } else {
5487 // Mark the end of the default argument so that we know when to
5488 // stop when we parse it later on.
5489 Token DefArgEnd;
5490 DefArgEnd.startToken();
5491 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5492 DefArgEnd.setLocation(Tok.getLocation());
5493 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005494 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005495 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005496 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005497 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005498 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005499 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005500
Chad Rosierc1183952012-06-26 22:30:43 +00005501 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005502 // used.
5503 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005504 Sema::PotentiallyEvaluatedIfUsed,
5505 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005506
Sebastian Redldb63af22012-03-14 15:54:00 +00005507 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005508 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005509 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005510 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005511 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005512 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005513 if (DefArgResult.isInvalid()) {
5514 Actions.ActOnParamDefaultArgumentError(Param);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005515 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00005516 } else {
5517 // Inform the actions module about the default argument
5518 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005519 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005520 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005521 }
5522 }
Mike Stump11289f42009-09-09 15:08:12 +00005523
5524 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005525 ParmDeclarator.getIdentifierLoc(),
5526 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005527 }
5528
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005529 if (TryConsumeToken(tok::ellipsis, EllipsisLoc) &&
5530 !getLangOpts().CPlusPlus) {
5531 // We have ellipsis without a preceding ',', which is ill-formed
5532 // in C. Complain and provide the fix.
5533 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
5534 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005535 break;
5536 }
Mike Stump11289f42009-09-09 15:08:12 +00005537
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005538 // If the next token is a comma, consume it and keep reading arguments.
5539 } while (TryConsumeToken(tok::comma));
Chris Lattner6c940e62008-04-06 06:34:08 +00005540}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005541
Chris Lattnere8074e62006-08-06 18:30:15 +00005542/// [C90] direct-declarator '[' constant-expression[opt] ']'
5543/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5544/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5545/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5546/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005547/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5548/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005549void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005550 if (CheckProhibitedCXX11Attribute())
5551 return;
5552
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005553 BalancedDelimiterTracker T(*this, tok::l_square);
5554 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005555
Chris Lattner84a11622008-12-18 07:27:21 +00005556 // C array syntax has many features, but by-far the most common is [] and [4].
5557 // This code does a fast path to handle some of the most obvious cases.
5558 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005559 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005560 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005561 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005562
Chris Lattner84a11622008-12-18 07:27:21 +00005563 // Remember that we parsed the empty array type.
John McCall084e83d2011-03-24 11:26:52 +00005564 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005565 T.getOpenLocation(),
5566 T.getCloseLocation()),
5567 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005568 return;
5569 } else if (Tok.getKind() == tok::numeric_constant &&
5570 GetLookAheadToken(1).is(tok::r_square)) {
5571 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005572 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005573 ConsumeToken();
5574
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005575 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005576 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005577 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005578
Chris Lattner84a11622008-12-18 07:27:21 +00005579 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005580 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005581 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005582 T.getOpenLocation(),
5583 T.getCloseLocation()),
5584 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005585 return;
5586 }
Mike Stump11289f42009-09-09 15:08:12 +00005587
Chris Lattnere8074e62006-08-06 18:30:15 +00005588 // If valid, this location is the position where we read the 'static' keyword.
5589 SourceLocation StaticLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005590 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005591
Chris Lattnere8074e62006-08-06 18:30:15 +00005592 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005593 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005594 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005595 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005596
Chris Lattnere8074e62006-08-06 18:30:15 +00005597 // If we haven't already read 'static', check to see if there is one after the
5598 // type-qualifier-list.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005599 if (!StaticLoc.isValid())
5600 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005601
Chris Lattnere8074e62006-08-06 18:30:15 +00005602 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005603 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005604 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005605
Chris Lattner521ff2b2008-04-06 05:26:30 +00005606 // Handle the case where we have '[*]' as the array size. However, a leading
5607 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005608 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005609 // infrequent, use of lookahead is not costly here.
5610 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005611 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005612
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005613 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005614 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005615 StaticLoc = SourceLocation(); // Drop the static.
5616 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005617 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005618 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005619 // Note, in C89, this production uses the constant-expr production instead
5620 // of assignment-expr. The only difference is that assignment-expr allows
5621 // things like '=' and '*='. Sema rejects these in C89 mode because they
5622 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005623
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005624 // Parse the constant-expression or assignment-expression now (depending
5625 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005626 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005627 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005628 } else {
5629 EnterExpressionEvaluationContext Unevaluated(Actions,
5630 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005631 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005632 }
Chris Lattner62591722006-08-12 18:40:58 +00005633 }
Mike Stump11289f42009-09-09 15:08:12 +00005634
Chris Lattner62591722006-08-12 18:40:58 +00005635 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005636 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005637 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005638 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00005639 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00005640 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005641 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005642
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005643 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005644
John McCall084e83d2011-03-24 11:26:52 +00005645 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005646 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005647
Chris Lattner84a11622008-12-18 07:27:21 +00005648 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005649 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005650 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005651 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005652 T.getOpenLocation(),
5653 T.getCloseLocation()),
5654 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005655}
5656
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005657/// [GNU] typeof-specifier:
5658/// typeof ( expressions )
5659/// typeof ( type-name )
5660/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005661///
5662void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005663 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005664 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005665 SourceLocation StartLoc = ConsumeToken();
5666
John McCalle8595032010-01-13 20:03:27 +00005667 const bool hasParens = Tok.is(tok::l_paren);
5668
Eli Friedman15681d62012-09-26 04:34:21 +00005669 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5670 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005671
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005672 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005673 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005674 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005675 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5676 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005677 if (hasParens)
5678 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005679
5680 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005681 // FIXME: Not accurate, the range gets one token more than it should.
5682 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005683 else
5684 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005685
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005686 if (isCastExpr) {
5687 if (!CastTy) {
5688 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005689 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005690 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005691
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005692 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005693 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005694 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5695 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005696 DiagID, CastTy))
5697 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005698 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005699 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005700
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005701 // If we get here, the operand to the typeof was an expresion.
5702 if (Operand.isInvalid()) {
5703 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005704 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005705 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005706
Eli Friedmane0afc982012-01-21 01:01:51 +00005707 // We might need to transform the operand if it is potentially evaluated.
5708 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5709 if (Operand.isInvalid()) {
5710 DS.SetTypeSpecError();
5711 return;
5712 }
5713
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005714 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005715 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005716 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5717 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005718 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005719 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005720}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005721
Benjamin Kramere56f3932011-12-23 17:00:35 +00005722/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005723/// _Atomic ( type-name )
5724///
5725void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005726 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5727 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005728
5729 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005730 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005731 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005732 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005733
5734 TypeResult Result = ParseTypeName();
5735 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00005736 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00005737 return;
5738 }
5739
5740 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005741 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005742
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005743 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005744 return;
5745
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005746 DS.setTypeofParensRange(T.getRange());
5747 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005748
5749 const char *PrevSpec = 0;
5750 unsigned DiagID;
5751 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5752 DiagID, Result.release()))
5753 Diag(StartLoc, DiagID) << PrevSpec;
5754}
5755
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005756
5757/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5758/// from TryAltiVecVectorToken.
5759bool Parser::TryAltiVecVectorTokenOutOfLine() {
5760 Token Next = NextToken();
5761 switch (Next.getKind()) {
5762 default: return false;
5763 case tok::kw_short:
5764 case tok::kw_long:
5765 case tok::kw_signed:
5766 case tok::kw_unsigned:
5767 case tok::kw_void:
5768 case tok::kw_char:
5769 case tok::kw_int:
5770 case tok::kw_float:
5771 case tok::kw_double:
5772 case tok::kw_bool:
5773 case tok::kw___pixel:
5774 Tok.setKind(tok::kw___vector);
5775 return true;
5776 case tok::identifier:
5777 if (Next.getIdentifierInfo() == Ident_pixel) {
5778 Tok.setKind(tok::kw___vector);
5779 return true;
5780 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005781 if (Next.getIdentifierInfo() == Ident_bool) {
5782 Tok.setKind(tok::kw___vector);
5783 return true;
5784 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005785 return false;
5786 }
5787}
5788
5789bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5790 const char *&PrevSpec, unsigned &DiagID,
5791 bool &isInvalid) {
5792 if (Tok.getIdentifierInfo() == Ident_vector) {
5793 Token Next = NextToken();
5794 switch (Next.getKind()) {
5795 case tok::kw_short:
5796 case tok::kw_long:
5797 case tok::kw_signed:
5798 case tok::kw_unsigned:
5799 case tok::kw_void:
5800 case tok::kw_char:
5801 case tok::kw_int:
5802 case tok::kw_float:
5803 case tok::kw_double:
5804 case tok::kw_bool:
5805 case tok::kw___pixel:
5806 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5807 return true;
5808 case tok::identifier:
5809 if (Next.getIdentifierInfo() == Ident_pixel) {
5810 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5811 return true;
5812 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005813 if (Next.getIdentifierInfo() == Ident_bool) {
5814 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5815 return true;
5816 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005817 break;
5818 default:
5819 break;
5820 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005821 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005822 DS.isTypeAltiVecVector()) {
5823 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5824 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005825 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5826 DS.isTypeAltiVecVector()) {
5827 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5828 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005829 }
5830 return false;
5831}