blob: 524754bd9079e7fe5711a3d7aa3665d4c040cb0d [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")) {
127 SkipUntil(tok::r_paren, true); // 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, "(")) {
131 SkipUntil(tok::r_paren, true); // 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 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000174 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Richard Smith66e71682013-10-24 01:07:54 +0000175 SkipUntil(tok::r_paren);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000176 SourceLocation Loc = Tok.getLocation();
Richard Smith66e71682013-10-24 01:07:54 +0000177 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
178 SkipUntil(tok::r_paren);
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
Richard Smith66e71682013-10-24 01:07:54 +0000184/// \brief Determine whether the given attribute has an identifier argument.
185static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
186 StringRef Name = II.getName();
187 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
188 Name = Name.drop_front(2).drop_back(2);
189 return llvm::StringSwitch<bool>(Name)
190#include "clang/Parse/AttrIdentifierArg.inc"
Douglas Gregord2472d42013-05-02 23:25:32 +0000191 .Default(false);
192}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000193
Richard Smithfeefaf52013-09-03 18:01:40 +0000194IdentifierLoc *Parser::ParseIdentifierLoc() {
195 assert(Tok.is(tok::identifier) && "expected an identifier");
196 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
197 Tok.getLocation(),
198 Tok.getIdentifierInfo());
199 ConsumeToken();
200 return IL;
201}
202
Michael Han23214e52012-10-03 01:56:22 +0000203/// Parse the arguments to a parameterized GNU attribute or
204/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000205void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
206 SourceLocation AttrNameLoc,
207 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000208 SourceLocation *EndLoc,
209 IdentifierInfo *ScopeName,
210 SourceLocation ScopeLoc,
211 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000212
213 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
214
Richard Smith66e71682013-10-24 01:07:54 +0000215 AttributeList::Kind AttrKind =
216 AttributeList::getKind(AttrName, ScopeName, AttributeList::AS_GNU);
217
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000218 // Availability attributes have their own grammar.
Richard Smith66e71682013-10-24 01:07:54 +0000219 if (AttrKind == AttributeList::AT_Availability) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000220 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
221 return;
222 }
223 // Thread safety attributes fit into the FIXME case above, so we
224 // just parse the arguments as a list of expressions
225 if (IsThreadSafetyAttribute(AttrName->getName())) {
226 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
227 return;
228 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000229 // Type safety attributes have their own grammar.
Richard Smith66e71682013-10-24 01:07:54 +0000230 if (AttrKind == AttributeList::AT_TypeTagForDatatype) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000231 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
232 return;
233 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000234
Richard Smith66e71682013-10-24 01:07:54 +0000235 // Ignore the left paren location for now.
236 ConsumeParen();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000237
Richard Smithb12bf692011-10-17 21:20:17 +0000238 bool BuiltinType = false;
Aaron Ballman00e99962013-08-31 01:11:41 +0000239 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000240
Joey Goulyaba589c2013-03-08 09:42:32 +0000241 TypeResult T;
242 SourceRange TypeRange;
243 bool TypeParsed = false;
244
Richard Smithb12bf692011-10-17 21:20:17 +0000245 switch (Tok.getKind()) {
246 case tok::kw_char:
247 case tok::kw_wchar_t:
248 case tok::kw_char16_t:
249 case tok::kw_char32_t:
250 case tok::kw_bool:
251 case tok::kw_short:
252 case tok::kw_int:
253 case tok::kw_long:
254 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000255 case tok::kw___int128:
Richard Smithb12bf692011-10-17 21:20:17 +0000256 case tok::kw_signed:
257 case tok::kw_unsigned:
258 case tok::kw_float:
259 case tok::kw_double:
260 case tok::kw_void:
261 case tok::kw_typeof:
262 // __attribute__(( vec_type_hint(char) ))
Richard Smithb12bf692011-10-17 21:20:17 +0000263 BuiltinType = true;
Joey Goulyaba589c2013-03-08 09:42:32 +0000264 T = ParseTypeName(&TypeRange);
265 TypeParsed = true;
Richard Smithb12bf692011-10-17 21:20:17 +0000266 break;
267
Aaron Ballman00e99962013-08-31 01:11:41 +0000268 case tok::identifier: {
Richard Smith66e71682013-10-24 01:07:54 +0000269 if (AttrKind == AttributeList::AT_VecTypeHint) {
Joey Goulyaba589c2013-03-08 09:42:32 +0000270 T = ParseTypeName(&TypeRange);
271 TypeParsed = true;
272 break;
273 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000274
Richard Smith66e71682013-10-24 01:07:54 +0000275 // If this attribute wants an 'identifier' argument, make it so.
276 if (attributeHasIdentifierArg(*AttrName))
277 ArgExprs.push_back(ParseIdentifierLoc());
278
279 // __attribute__((iboutletcollection)) expects an identifier then
280 // some other (ignored) things.
281 if (AttrKind == AttributeList::AT_IBOutletCollection)
282 ArgExprs.push_back(ParseIdentifierLoc());
283
284 // If we don't know how to parse this attribute, but this is the only
285 // token in this argument, assume it's meant to be an identifier.
286 if (AttrKind == AttributeList::UnknownAttribute) {
287 const Token &Next = NextToken();
288 if (Next.is(tok::r_paren) || Next.is(tok::comma))
289 ArgExprs.push_back(ParseIdentifierLoc());
290 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000291 } break;
Richard Smithb12bf692011-10-17 21:20:17 +0000292
293 default:
294 break;
295 }
296
Joey Goulyaba589c2013-03-08 09:42:32 +0000297 bool isInvalid = false;
298 bool isParmType = false;
Richard Smithb12bf692011-10-17 21:20:17 +0000299
Richard Smith66e71682013-10-24 01:07:54 +0000300 if (!BuiltinType && AttrKind != AttributeList::AT_VecTypeHint &&
Aaron Ballman00e99962013-08-31 01:11:41 +0000301 (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
Richard Smithb12bf692011-10-17 21:20:17 +0000302 // Eat the comma.
Aaron Ballman00e99962013-08-31 01:11:41 +0000303 if (!ArgExprs.empty())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000304 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000305
Richard Smithb12bf692011-10-17 21:20:17 +0000306 // Parse the non-empty comma-separated list of expressions.
307 while (1) {
308 ExprResult ArgExpr(ParseAssignmentExpression());
309 if (ArgExpr.isInvalid()) {
310 SkipUntil(tok::r_paren);
311 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000312 }
Richard Smithb12bf692011-10-17 21:20:17 +0000313 ArgExprs.push_back(ArgExpr.release());
314 if (Tok.isNot(tok::comma))
315 break;
316 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000317 }
Richard Smith66e71682013-10-24 01:07:54 +0000318 } else if (Tok.is(tok::less) &&
319 AttrKind == AttributeList::AT_IBOutletCollection) {
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000320 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
321 tok::greater)) {
Fariborz Jahanian7f733022011-10-18 23:13:50 +0000322 while (Tok.is(tok::identifier)) {
323 ConsumeToken();
324 if (Tok.is(tok::greater))
325 break;
326 if (Tok.is(tok::comma)) {
327 ConsumeToken();
328 continue;
329 }
330 }
331 if (Tok.isNot(tok::greater))
332 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000333 SkipUntil(tok::r_paren, false, true); // skip until ')'
334 }
Richard Smith66e71682013-10-24 01:07:54 +0000335 } else if (AttrKind == AttributeList::AT_VecTypeHint) {
Joey Goulyaba589c2013-03-08 09:42:32 +0000336 if (T.get() && !T.isInvalid())
337 isParmType = true;
338 else {
339 if (Tok.is(tok::identifier))
340 ConsumeToken();
341 if (TypeParsed)
342 isInvalid = true;
343 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000344 }
Richard Smithb12bf692011-10-17 21:20:17 +0000345
346 SourceLocation RParen = Tok.getLocation();
Joey Goulyaba589c2013-03-08 09:42:32 +0000347 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen) &&
348 !isInvalid) {
Michael Han360d2252012-10-04 16:42:52 +0000349 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Joey Goulyaba589c2013-03-08 09:42:32 +0000350 if (isParmType) {
Joey Goulyaba589c2013-03-08 09:42:32 +0000351 Attrs.addNewTypeAttr(AttrName, SourceRange(AttrLoc, RParen), ScopeName,
Aaron Ballman00e99962013-08-31 01:11:41 +0000352 ScopeLoc, T.get(), Syntax);
Joey Goulyaba589c2013-03-08 09:42:32 +0000353 } else {
354 AttributeList *attr = Attrs.addNew(
Aaron Ballman00e99962013-08-31 01:11:41 +0000355 AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
356 ArgExprs.data(), ArgExprs.size(), Syntax);
Joey Goulyaba589c2013-03-08 09:42:32 +0000357 if (BuiltinType &&
358 attr->getKind() == AttributeList::AT_IBOutletCollection)
359 Diag(Tok, diag::err_iboutletcollection_builtintype);
360 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000361 }
362}
363
Chad Rosierc1183952012-06-26 22:30:43 +0000364/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000365/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000366void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000367 SourceLocation AttrNameLoc,
368 ParsedAttributes &Attrs)
369{
370 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000371 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000372 AttrName->getNameStart(), tok::r_paren))
373 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000374
Aaron Ballman478faed2012-06-19 22:09:27 +0000375 ExprResult ArgExpr(ParseConstantExpression());
376 if (ArgExpr.isInvalid()) {
377 T.skipToEnd();
378 return;
379 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000380 ArgsUnion ExprList = ArgExpr.take();
381 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
382 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000383
384 T.consumeClose();
385}
386
Chad Rosierc1183952012-06-26 22:30:43 +0000387/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000388/// arguments.
389bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
390 return llvm::StringSwitch<bool>(Ident->getName())
391 .Case("dllimport", true)
392 .Case("dllexport", true)
393 .Case("noreturn", true)
394 .Case("nothrow", true)
395 .Case("noinline", true)
396 .Case("naked", true)
397 .Case("appdomain", true)
398 .Case("process", true)
399 .Case("jitintrinsic", true)
400 .Case("noalias", true)
401 .Case("restrict", true)
402 .Case("novtable", true)
403 .Case("selectany", true)
404 .Case("thread", true)
Aaron Ballman444eb6e2013-05-04 16:58:37 +0000405 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000406 .Default(false);
407}
408
Chad Rosierc1183952012-06-26 22:30:43 +0000409/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000410/// parameters). Will return false if we properly handled the declspec, or
411/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000412void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000413 SourceLocation Loc,
414 ParsedAttributes &Attrs) {
415 // Try to handle the easy case first -- these declspecs all take a single
416 // parameter as their argument.
417 if (llvm::StringSwitch<bool>(Ident->getName())
418 .Case("uuid", true)
419 .Case("align", true)
420 .Case("allocate", true)
421 .Default(false)) {
422 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
423 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000424 // The deprecated declspec has an optional single argument, so we will
425 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000426 // not.
427 if (Tok.getKind() == tok::l_paren)
428 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
429 else
Aaron Ballman00e99962013-08-31 01:11:41 +0000430 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000431 } else if (Ident->getName() == "property") {
432 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000433 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000434 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000435 if (Tok.isNot(tok::l_paren)) {
436 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
437 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000438 return;
John McCall5e77d762013-04-16 07:28:30 +0000439 }
440 BalancedDelimiterTracker T(*this, tok::l_paren);
441 T.expectAndConsume(diag::err_expected_lparen_after,
442 Ident->getNameStart(), tok::r_paren);
443
444 enum AccessorKind {
445 AK_Invalid = -1,
446 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
447 };
448 IdentifierInfo *AccessorNames[] = { 0, 0 };
449 bool HasInvalidAccessor = false;
450
451 // Parse the accessor specifications.
452 while (true) {
453 // Stop if this doesn't look like an accessor spec.
454 if (!Tok.is(tok::identifier)) {
455 // If the user wrote a completely empty list, use a special diagnostic.
456 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
457 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
458 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
459 break;
460 }
461
462 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
463 break;
464 }
465
466 AccessorKind Kind;
467 SourceLocation KindLoc = Tok.getLocation();
468 StringRef KindStr = Tok.getIdentifierInfo()->getName();
469 if (KindStr == "get") {
470 Kind = AK_Get;
471 } else if (KindStr == "put") {
472 Kind = AK_Put;
473
474 // Recover from the common mistake of using 'set' instead of 'put'.
475 } else if (KindStr == "set") {
476 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
477 << FixItHint::CreateReplacement(KindLoc, "put");
478 Kind = AK_Put;
479
480 // Handle the mistake of forgetting the accessor kind by skipping
481 // this accessor.
482 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
483 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
484 ConsumeToken();
485 HasInvalidAccessor = true;
486 goto next_property_accessor;
487
488 // Otherwise, complain about the unknown accessor kind.
489 } else {
490 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
491 HasInvalidAccessor = true;
492 Kind = AK_Invalid;
493
494 // Try to keep parsing unless it doesn't look like an accessor spec.
495 if (!NextToken().is(tok::equal)) break;
496 }
497
498 // Consume the identifier.
499 ConsumeToken();
500
501 // Consume the '='.
502 if (Tok.is(tok::equal)) {
503 ConsumeToken();
504 } else {
505 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
506 << KindStr;
507 break;
508 }
509
510 // Expect the method name.
511 if (!Tok.is(tok::identifier)) {
512 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
513 break;
514 }
515
516 if (Kind == AK_Invalid) {
517 // Just drop invalid accessors.
518 } else if (AccessorNames[Kind] != NULL) {
519 // Complain about the repeated accessor, ignore it, and keep parsing.
520 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
521 } else {
522 AccessorNames[Kind] = Tok.getIdentifierInfo();
523 }
524 ConsumeToken();
525
526 next_property_accessor:
527 // Keep processing accessors until we run out.
528 if (Tok.is(tok::comma)) {
529 ConsumeAnyToken();
530 continue;
531
532 // If we run into the ')', stop without consuming it.
533 } else if (Tok.is(tok::r_paren)) {
534 break;
535 } else {
536 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
537 break;
538 }
539 }
540
541 // Only add the property attribute if it was well-formed.
542 if (!HasInvalidAccessor) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000543 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000544 AccessorNames[AK_Get], AccessorNames[AK_Put],
545 AttributeList::AS_Declspec);
546 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000547 T.skipToEnd();
548 } else {
549 // We don't recognize this as a valid declspec, but instead of creating the
550 // attribute and allowing sema to warn about it, we will warn here instead.
551 // This is because some attributes have multiple spellings, but we need to
552 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000553 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000554 // both locations.
555 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
556
557 // If there's an open paren, we should eat the open and close parens under
558 // the assumption that this unknown declspec has parameters.
559 BalancedDelimiterTracker T(*this, tok::l_paren);
560 if (!T.consumeOpen())
561 T.skipToEnd();
562 }
563}
564
Eli Friedman06de2b52009-06-08 07:21:15 +0000565/// [MS] decl-specifier:
566/// __declspec ( extended-decl-modifier-seq )
567///
568/// [MS] extended-decl-modifier-seq:
569/// extended-decl-modifier[opt]
570/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000571void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000572 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000573
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000574 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000575 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000576 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000577 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000578 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000579
Chad Rosierc1183952012-06-26 22:30:43 +0000580 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000581 // you can specify multiple attributes per declspec.
582 while (Tok.getKind() != tok::r_paren) {
583 // We expect either a well-known identifier or a generic string. Anything
584 // else is a malformed declspec.
585 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000586 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000587 Tok.getKind() != tok::kw_restrict) {
588 Diag(Tok, diag::err_ms_declspec_type);
589 T.skipToEnd();
590 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000591 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000592
593 IdentifierInfo *AttrName;
594 SourceLocation AttrNameLoc;
595 if (IsString) {
596 SmallString<8> StrBuffer;
597 bool Invalid = false;
598 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
599 if (Invalid) {
600 T.skipToEnd();
601 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000602 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000603 AttrName = PP.getIdentifierInfo(Str);
604 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000605 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000606 AttrName = Tok.getIdentifierInfo();
607 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000608 }
Chad Rosierc1183952012-06-26 22:30:43 +0000609
Aaron Ballman478faed2012-06-19 22:09:27 +0000610 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000611 // If we have a generic string, we will allow it because there is no
612 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000613 // (for instance, SAL declspecs in older versions of MSVC).
614 //
Chad Rosierc1183952012-06-26 22:30:43 +0000615 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000616 // arguments and can be turned into an attribute directly.
Aaron Ballman00e99962013-08-31 01:11:41 +0000617 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
618 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000619 else
620 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000621 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000622 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000623}
624
John McCall53fa7142010-12-24 02:08:15 +0000625void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000626 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000627 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000628 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000629 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballman317a77f2013-05-22 23:25:32 +0000630 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
631 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000632 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
633 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000634 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
635 AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000636 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000637}
638
John McCall53fa7142010-12-24 02:08:15 +0000639void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000640 // Treat these like attributes
641 while (Tok.is(tok::kw___pascal)) {
642 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
643 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000644 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
645 AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000646 }
John McCall53fa7142010-12-24 02:08:15 +0000647}
648
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000649void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
650 // Treat these like attributes
651 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000652 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000653 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000654 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
655 AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000656 }
657}
658
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000659void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000660 // FIXME: The mapping from attribute spelling to semantics should be
661 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000662 SourceLocation Loc = Tok.getLocation();
663 switch(Tok.getKind()) {
664 // OpenCL qualifiers:
665 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000666 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000667 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000668 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000669 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000670 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000671
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000672 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000673 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000674 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000675 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000676 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000677
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000678 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000679 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000680 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000681 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000682 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000683
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000684 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000685 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000686 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000687 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000688 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000689
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000690 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000691 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000692 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000693 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000694 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000695
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000696 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000697 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000698 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000699 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000700 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000701
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000702 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000703 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000704 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000705 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000706 break;
707 default: break;
708 }
709}
710
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000711/// \brief Parse a version number.
712///
713/// version:
714/// simple-integer
715/// simple-integer ',' simple-integer
716/// simple-integer ',' simple-integer ',' simple-integer
717VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
718 Range = Tok.getLocation();
719
720 if (!Tok.is(tok::numeric_constant)) {
721 Diag(Tok, diag::err_expected_version);
722 SkipUntil(tok::comma, tok::r_paren, true, true, true);
723 return VersionTuple();
724 }
725
726 // Parse the major (and possibly minor and subminor) versions, which
727 // are stored in the numeric constant. We utilize a quirk of the
728 // lexer, which is that it handles something like 1.2.3 as a single
729 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000730 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000731 Buffer.resize(Tok.getLength()+1);
732 const char *ThisTokBegin = &Buffer[0];
733
734 // Get the spelling of the token, which eliminates trigraphs, etc.
735 bool Invalid = false;
736 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
737 if (Invalid)
738 return VersionTuple();
739
740 // Parse the major version.
741 unsigned AfterMajor = 0;
742 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000743 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000744 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
745 ++AfterMajor;
746 }
747
748 if (AfterMajor == 0) {
749 Diag(Tok, diag::err_expected_version);
750 SkipUntil(tok::comma, tok::r_paren, true, true, true);
751 return VersionTuple();
752 }
753
754 if (AfterMajor == ActualLength) {
755 ConsumeToken();
756
757 // We only had a single version component.
758 if (Major == 0) {
759 Diag(Tok, diag::err_zero_version);
760 return VersionTuple();
761 }
762
763 return VersionTuple(Major);
764 }
765
766 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
767 Diag(Tok, diag::err_expected_version);
768 SkipUntil(tok::comma, tok::r_paren, true, true, true);
769 return VersionTuple();
770 }
771
772 // Parse the minor version.
773 unsigned AfterMinor = AfterMajor + 1;
774 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000775 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000776 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
777 ++AfterMinor;
778 }
779
780 if (AfterMinor == ActualLength) {
781 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000782
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000783 // We had major.minor.
784 if (Major == 0 && Minor == 0) {
785 Diag(Tok, diag::err_zero_version);
786 return VersionTuple();
787 }
788
Chad Rosierc1183952012-06-26 22:30:43 +0000789 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000790 }
791
792 // If what follows is not a '.', we have a problem.
793 if (ThisTokBegin[AfterMinor] != '.') {
794 Diag(Tok, diag::err_expected_version);
795 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosierc1183952012-06-26 22:30:43 +0000796 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000797 }
798
799 // Parse the subminor version.
800 unsigned AfterSubminor = AfterMinor + 1;
801 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000802 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000803 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
804 ++AfterSubminor;
805 }
806
807 if (AfterSubminor != ActualLength) {
808 Diag(Tok, diag::err_expected_version);
809 SkipUntil(tok::comma, tok::r_paren, true, true, true);
810 return VersionTuple();
811 }
812 ConsumeToken();
813 return VersionTuple(Major, Minor, Subminor);
814}
815
816/// \brief Parse the contents of the "availability" attribute.
817///
818/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000819/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000820///
821/// platform:
822/// identifier
823///
824/// version-arg-list:
825/// version-arg
826/// version-arg ',' version-arg-list
827///
828/// version-arg:
829/// 'introduced' '=' version
830/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000831/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000832/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000833/// opt-message:
834/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000835void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
836 SourceLocation AvailabilityLoc,
837 ParsedAttributes &attrs,
838 SourceLocation *endLoc) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000839 enum { Introduced, Deprecated, Obsoleted, Unknown };
840 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000841 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000842
843 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000844 BalancedDelimiterTracker T(*this, tok::l_paren);
845 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000846 Diag(Tok, diag::err_expected_lparen);
847 return;
848 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000849
850 // Parse the platform name,
851 if (Tok.isNot(tok::identifier)) {
852 Diag(Tok, diag::err_availability_expected_platform);
853 SkipUntil(tok::r_paren);
854 return;
855 }
Richard Smithfeefaf52013-09-03 18:01:40 +0000856 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000857
858 // Parse the ',' following the platform name.
859 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
860 return;
861
862 // If we haven't grabbed the pointers for the identifiers
863 // "introduced", "deprecated", and "obsoleted", do so now.
864 if (!Ident_introduced) {
865 Ident_introduced = PP.getIdentifierInfo("introduced");
866 Ident_deprecated = PP.getIdentifierInfo("deprecated");
867 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000868 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000869 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000870 }
871
872 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000873 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000874 do {
875 if (Tok.isNot(tok::identifier)) {
876 Diag(Tok, diag::err_availability_expected_change);
877 SkipUntil(tok::r_paren);
878 return;
879 }
880 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
881 SourceLocation KeywordLoc = ConsumeToken();
882
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000883 if (Keyword == Ident_unavailable) {
884 if (UnavailableLoc.isValid()) {
885 Diag(KeywordLoc, diag::err_availability_redundant)
886 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000887 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000888 UnavailableLoc = KeywordLoc;
889
890 if (Tok.isNot(tok::comma))
891 break;
892
893 ConsumeToken();
894 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000895 }
896
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000897 if (Tok.isNot(tok::equal)) {
898 Diag(Tok, diag::err_expected_equal_after)
899 << Keyword;
900 SkipUntil(tok::r_paren);
901 return;
902 }
903 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000904 if (Keyword == Ident_message) {
Benjamin Kramera9dfa922013-09-13 17:31:48 +0000905 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000906 Diag(Tok, diag::err_expected_string_literal)
907 << /*Source='availability attribute'*/2;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000908 SkipUntil(tok::r_paren);
909 return;
910 }
911 MessageExpr = ParseStringLiteralExpression();
912 break;
913 }
Chad Rosierc1183952012-06-26 22:30:43 +0000914
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000915 SourceRange VersionRange;
916 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000917
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000918 if (Version.empty()) {
919 SkipUntil(tok::r_paren);
920 return;
921 }
922
923 unsigned Index;
924 if (Keyword == Ident_introduced)
925 Index = Introduced;
926 else if (Keyword == Ident_deprecated)
927 Index = Deprecated;
928 else if (Keyword == Ident_obsoleted)
929 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000930 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000931 Index = Unknown;
932
933 if (Index < Unknown) {
934 if (!Changes[Index].KeywordLoc.isInvalid()) {
935 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000936 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000937 << SourceRange(Changes[Index].KeywordLoc,
938 Changes[Index].VersionRange.getEnd());
939 }
940
941 Changes[Index].KeywordLoc = KeywordLoc;
942 Changes[Index].Version = Version;
943 Changes[Index].VersionRange = VersionRange;
944 } else {
945 Diag(KeywordLoc, diag::err_availability_unknown_change)
946 << Keyword << VersionRange;
947 }
948
949 if (Tok.isNot(tok::comma))
950 break;
951
952 ConsumeToken();
953 } while (true);
954
955 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000956 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000957 return;
958
959 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000960 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000961
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000962 // The 'unavailable' availability cannot be combined with any other
963 // availability changes. Make sure that hasn't happened.
964 if (UnavailableLoc.isValid()) {
965 bool Complained = false;
966 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
967 if (Changes[Index].KeywordLoc.isValid()) {
968 if (!Complained) {
969 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
970 << SourceRange(Changes[Index].KeywordLoc,
971 Changes[Index].VersionRange.getEnd());
972 Complained = true;
973 }
974
975 // Clear out the availability.
976 Changes[Index] = AvailabilityChange();
977 }
978 }
979 }
980
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000981 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000982 attrs.addNew(&Availability,
983 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000984 0, AvailabilityLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +0000985 Platform,
John McCall084e83d2011-03-24 11:26:52 +0000986 Changes[Introduced],
987 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000988 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000989 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000990 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000991}
992
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000993
Bill Wendling44426052012-12-20 19:22:21 +0000994// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000995// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
996
997void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
998
999void Parser::LateParsedClass::ParseLexedAttributes() {
1000 Self->ParseLexedAttributes(*Class);
1001}
1002
1003void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001004 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001005}
1006
1007/// Wrapper class which calls ParseLexedAttribute, after setting up the
1008/// scope appropriately.
1009void Parser::ParseLexedAttributes(ParsingClass &Class) {
1010 // Deal with templates
1011 // FIXME: Test cases to make sure this does the right thing for templates.
1012 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
1013 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
1014 HasTemplateScope);
1015 if (HasTemplateScope)
1016 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
1017
Douglas Gregor3024f072012-04-16 07:05:22 +00001018 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001019 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +00001020 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001021 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1022 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1023
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001024 // Enter the scope of nested classes
1025 if (!AlreadyHasClassScope)
1026 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1027 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001028 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001029 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1030 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1031 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001032 }
Chad Rosierc1183952012-06-26 22:30:43 +00001033
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001034 if (!AlreadyHasClassScope)
1035 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1036 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001037}
1038
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001039
1040/// \brief Parse all attributes in LAs, and attach them to Decl D.
1041void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1042 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001043 assert(LAs.parseSoon() &&
1044 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001045 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001046 if (D)
1047 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001048 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001049 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001050 }
1051 LAs.clear();
1052}
1053
1054
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001055/// \brief Finish parsing an attribute for which parsing was delayed.
1056/// This will be called at the end of parsing a class declaration
1057/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001058/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001059/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001060void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1061 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001062 // Save the current token position.
1063 SourceLocation OrigLoc = Tok.getLocation();
1064
1065 // Append the current token at the end of the new token stream so that it
1066 // doesn't get lost.
1067 LA.Toks.push_back(Tok);
1068 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1069 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001070 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001071
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001072 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +00001073 // FIXME: Do not warn on C++11 attributes, once we start supporting
1074 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001075 Diag(Tok, diag::warn_attribute_on_function_definition)
1076 << LA.AttrName.getName();
1077 }
1078
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001079 ParsedAttributes Attrs(AttrFactory);
1080 SourceLocation endLoc;
1081
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001082 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001083 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001084 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1085 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001086
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001087 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001088 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1089 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001090
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001091 if (LA.Decls.size() == 1) {
1092 // If the Decl is templatized, add template parameters to scope.
1093 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1094 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1095 if (HasTemplateScope)
1096 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001097
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001098 // If the Decl is on a function, add function parameters to the scope.
1099 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1100 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1101 if (HasFunScope)
1102 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001103
Michael Han23214e52012-10-03 01:56:22 +00001104 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001105 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001106
1107 if (HasFunScope) {
1108 Actions.ActOnExitFunctionContext();
1109 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1110 }
1111 if (HasTemplateScope) {
1112 TempScope.Exit();
1113 }
1114 } else {
1115 // If there are multiple decls, then the decl cannot be within the
1116 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001117 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001118 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001119 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001120 } else {
1121 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001122 }
1123
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001124 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1125 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1126 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001127
1128 if (Tok.getLocation() != OrigLoc) {
1129 // Due to a parsing error, we either went over the cached tokens or
1130 // there are still cached tokens left, so we skip the leftover tokens.
1131 // Since this is an uncommon situation that should be avoided, use the
1132 // expensive isBeforeInTranslationUnit call.
1133 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1134 OrigLoc))
1135 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001136 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001137 }
1138}
1139
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001140/// \brief Wrapper around a case statement checking if AttrName is
1141/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001142bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001143 return llvm::StringSwitch<bool>(AttrName)
1144 .Case("guarded_by", true)
1145 .Case("guarded_var", true)
1146 .Case("pt_guarded_by", true)
1147 .Case("pt_guarded_var", true)
1148 .Case("lockable", true)
1149 .Case("scoped_lockable", true)
1150 .Case("no_thread_safety_analysis", true)
1151 .Case("acquired_after", true)
1152 .Case("acquired_before", true)
1153 .Case("exclusive_lock_function", true)
1154 .Case("shared_lock_function", true)
1155 .Case("exclusive_trylock_function", true)
1156 .Case("shared_trylock_function", true)
1157 .Case("unlock_function", true)
1158 .Case("lock_returned", true)
1159 .Case("locks_excluded", true)
1160 .Case("exclusive_locks_required", true)
1161 .Case("shared_locks_required", true)
1162 .Default(false);
1163}
1164
1165/// \brief Parse the contents of thread safety attributes. These
1166/// should always be parsed as an expression list.
1167///
1168/// We need to special case the parsing due to the fact that if the first token
1169/// of the first argument is an identifier, the main parse loop will store
1170/// that token as a "parameter" and the rest of
1171/// the arguments will be added to a list of "arguments". However,
1172/// subsequent tokens in the first argument are lost. We instead parse each
1173/// argument as an expression and add all arguments to the list of "arguments".
1174/// In future, we will take advantage of this special case to also
1175/// deal with some argument scoping issues here (for example, referring to a
1176/// function parameter in the attribute on that function).
1177void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1178 SourceLocation AttrNameLoc,
1179 ParsedAttributes &Attrs,
1180 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001181 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001182
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001183 BalancedDelimiterTracker T(*this, tok::l_paren);
1184 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001185
Aaron Ballman00e99962013-08-31 01:11:41 +00001186 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001187 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001188
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001189 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001190 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001191 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001192 ExprResult ArgExpr(ParseAssignmentExpression());
1193 if (ArgExpr.isInvalid()) {
1194 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001195 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001196 break;
1197 } else {
1198 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001199 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001200 if (Tok.isNot(tok::comma))
1201 break;
1202 ConsumeToken(); // Eat the comma, move to the next argument
1203 }
1204 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001205 if (ArgExprsOk && !T.consumeClose()) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001206 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(),
1207 ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001208 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001209 if (EndLoc)
1210 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001211}
1212
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001213void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1214 SourceLocation AttrNameLoc,
1215 ParsedAttributes &Attrs,
1216 SourceLocation *EndLoc) {
1217 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1218
1219 BalancedDelimiterTracker T(*this, tok::l_paren);
1220 T.consumeOpen();
1221
1222 if (Tok.isNot(tok::identifier)) {
1223 Diag(Tok, diag::err_expected_ident);
1224 T.skipToEnd();
1225 return;
1226 }
Richard Smithfeefaf52013-09-03 18:01:40 +00001227 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001228
1229 if (Tok.isNot(tok::comma)) {
1230 Diag(Tok, diag::err_expected_comma);
1231 T.skipToEnd();
1232 return;
1233 }
1234 ConsumeToken();
1235
1236 SourceRange MatchingCTypeRange;
1237 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1238 if (MatchingCType.isInvalid()) {
1239 T.skipToEnd();
1240 return;
1241 }
1242
1243 bool LayoutCompatible = false;
1244 bool MustBeNull = false;
1245 while (Tok.is(tok::comma)) {
1246 ConsumeToken();
1247 if (Tok.isNot(tok::identifier)) {
1248 Diag(Tok, diag::err_expected_ident);
1249 T.skipToEnd();
1250 return;
1251 }
1252 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1253 if (Flag->isStr("layout_compatible"))
1254 LayoutCompatible = true;
1255 else if (Flag->isStr("must_be_null"))
1256 MustBeNull = true;
1257 else {
1258 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1259 T.skipToEnd();
1260 return;
1261 }
1262 ConsumeToken(); // consume flag
1263 }
1264
1265 if (!T.consumeClose()) {
1266 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +00001267 ArgumentKind, MatchingCType.release(),
1268 LayoutCompatible, MustBeNull,
1269 AttributeList::AS_GNU);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001270 }
1271
1272 if (EndLoc)
1273 *EndLoc = T.getCloseLocation();
1274}
1275
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001276/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1277/// of a C++11 attribute-specifier in a location where an attribute is not
1278/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1279/// situation.
1280///
1281/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1282/// this doesn't appear to actually be an attribute-specifier, and the caller
1283/// should try to parse it.
1284bool Parser::DiagnoseProhibitedCXX11Attribute() {
1285 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1286
1287 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1288 case CAK_NotAttributeSpecifier:
1289 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1290 return false;
1291
1292 case CAK_InvalidAttributeSpecifier:
1293 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1294 return false;
1295
1296 case CAK_AttributeSpecifier:
1297 // Parse and discard the attributes.
1298 SourceLocation BeginLoc = ConsumeBracket();
1299 ConsumeBracket();
1300 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1301 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1302 SourceLocation EndLoc = ConsumeBracket();
1303 Diag(BeginLoc, diag::err_attributes_not_allowed)
1304 << SourceRange(BeginLoc, EndLoc);
1305 return true;
1306 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001307 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001308}
1309
Richard Smith98155ad2013-02-20 01:17:14 +00001310/// \brief We have found the opening square brackets of a C++11
1311/// attribute-specifier in a location where an attribute is not permitted, but
1312/// we know where the attributes ought to be written. Parse them anyway, and
1313/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001314void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1315 SourceLocation CorrectLocation) {
1316 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1317 Tok.is(tok::kw_alignas));
1318
1319 // Consume the attributes.
1320 SourceLocation Loc = Tok.getLocation();
1321 ParseCXX11Attributes(Attrs);
1322 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1323
1324 Diag(Loc, diag::err_attributes_not_allowed)
1325 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1326 << FixItHint::CreateRemoval(AttrRange);
1327}
1328
John McCall53fa7142010-12-24 02:08:15 +00001329void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1330 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1331 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001332}
1333
Michael Han64536a62012-11-06 19:34:54 +00001334void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1335 AttributeList *AttrList = attrs.getList();
1336 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001337 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001338 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001339 << AttrList->getName();
1340 AttrList->setInvalid();
1341 }
1342 AttrList = AttrList->getNext();
1343 }
1344}
1345
Chris Lattner53361ac2006-08-10 05:19:57 +00001346/// ParseDeclaration - Parse a full 'declaration', which consists of
1347/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001348/// 'Context' should be a Declarator::TheContext value. This returns the
1349/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001350///
1351/// declaration: [C99 6.7]
1352/// block-declaration ->
1353/// simple-declaration
1354/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001355/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001356/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001357/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001358/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001359/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001360/// others... [FIXME]
1361///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001362Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1363 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001364 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001365 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001366 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001367 // Must temporarily exit the objective-c container scope for
1368 // parsing c none objective-c decls.
1369 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001370
John McCall48871652010-08-21 09:40:31 +00001371 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001372 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001373 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001374 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001375 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001376 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001377 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001378 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001379 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001380 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001381 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001382 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001383 SourceLocation InlineLoc = ConsumeToken();
1384 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1385 break;
1386 }
Chad Rosierc1183952012-06-26 22:30:43 +00001387 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001388 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001389 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001390 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001391 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001392 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001393 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001394 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001395 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001396 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001397 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001398 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001399 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001400 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001401 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001402 default:
John McCall53fa7142010-12-24 02:08:15 +00001403 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001404 }
Chad Rosierc1183952012-06-26 22:30:43 +00001405
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001406 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001407 // single decl, convert it now. Alias declarations can also declare a type;
1408 // include that too if it is present.
1409 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001410}
1411
1412/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1413/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001414/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1415/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001416///[C90/C++]init-declarator-list ';' [TODO]
1417/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001418///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001419/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001420/// attribute-specifier-seq[opt] type-specifier-seq declarator
1421///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001422/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001423/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001424///
1425/// If FRI is non-null, we might be parsing a for-range-declaration instead
1426/// of a simple-declaration. If we find that we are, we also parse the
1427/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001428Parser::DeclGroupPtrTy
1429Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1430 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001431 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001432 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001433 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001434 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001435
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001436 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001437 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001438
Chris Lattner0e894622006-08-13 19:58:17 +00001439 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1440 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001441 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001442 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001443 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001444 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001445 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001446 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001447 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001448 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001449 }
Chad Rosierc1183952012-06-26 22:30:43 +00001450
Richard Smith2386c8b2013-02-22 09:06:26 +00001451 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001452 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001453}
Mike Stump11289f42009-09-09 15:08:12 +00001454
Richard Smith09f76ee2011-10-19 21:33:05 +00001455/// Returns true if this might be the start of a declarator, or a common typo
1456/// for a declarator.
1457bool Parser::MightBeDeclarator(unsigned Context) {
1458 switch (Tok.getKind()) {
1459 case tok::annot_cxxscope:
1460 case tok::annot_template_id:
1461 case tok::caret:
1462 case tok::code_completion:
1463 case tok::coloncolon:
1464 case tok::ellipsis:
1465 case tok::kw___attribute:
1466 case tok::kw_operator:
1467 case tok::l_paren:
1468 case tok::star:
1469 return true;
1470
1471 case tok::amp:
1472 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001473 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001474
Richard Smithc8a79032012-01-09 22:31:44 +00001475 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001476 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001477 NextToken().is(tok::l_square);
1478
1479 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001480 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001481
Richard Smith09f76ee2011-10-19 21:33:05 +00001482 case tok::identifier:
1483 switch (NextToken().getKind()) {
1484 case tok::code_completion:
1485 case tok::coloncolon:
1486 case tok::comma:
1487 case tok::equal:
1488 case tok::equalequal: // Might be a typo for '='.
1489 case tok::kw_alignas:
1490 case tok::kw_asm:
1491 case tok::kw___attribute:
1492 case tok::l_brace:
1493 case tok::l_paren:
1494 case tok::l_square:
1495 case tok::less:
1496 case tok::r_brace:
1497 case tok::r_paren:
1498 case tok::r_square:
1499 case tok::semi:
1500 return true;
1501
1502 case tok::colon:
1503 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001504 // and in block scope it's probably a label. Inside a class definition,
1505 // this is a bit-field.
1506 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001507 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001508
1509 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001510 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001511
1512 default:
1513 return false;
1514 }
1515
1516 default:
1517 return false;
1518 }
1519}
1520
Richard Smithb8caac82012-04-11 20:59:20 +00001521/// Skip until we reach something which seems like a sensible place to pick
1522/// up parsing after a malformed declaration. This will sometimes stop sooner
1523/// than SkipUntil(tok::r_brace) would, but will never stop later.
1524void Parser::SkipMalformedDecl() {
1525 while (true) {
1526 switch (Tok.getKind()) {
1527 case tok::l_brace:
1528 // Skip until matching }, then stop. We've probably skipped over
1529 // a malformed class or function definition or similar.
1530 ConsumeBrace();
1531 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1532 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1533 // This declaration isn't over yet. Keep skipping.
1534 continue;
1535 }
1536 if (Tok.is(tok::semi))
1537 ConsumeToken();
1538 return;
1539
1540 case tok::l_square:
1541 ConsumeBracket();
1542 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1543 continue;
1544
1545 case tok::l_paren:
1546 ConsumeParen();
1547 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1548 continue;
1549
1550 case tok::r_brace:
1551 return;
1552
1553 case tok::semi:
1554 ConsumeToken();
1555 return;
1556
1557 case tok::kw_inline:
1558 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001559 // a good place to pick back up parsing, except in an Objective-C
1560 // @interface context.
1561 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1562 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001563 return;
1564 break;
1565
1566 case tok::kw_namespace:
1567 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001568 // place to pick back up parsing, except in an Objective-C
1569 // @interface context.
1570 if (Tok.isAtStartOfLine() &&
1571 (!ParsingInObjCContainer || CurParsedObjCImpl))
1572 return;
1573 break;
1574
1575 case tok::at:
1576 // @end is very much like } in Objective-C contexts.
1577 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1578 ParsingInObjCContainer)
1579 return;
1580 break;
1581
1582 case tok::minus:
1583 case tok::plus:
1584 // - and + probably start new method declarations in Objective-C contexts.
1585 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001586 return;
1587 break;
1588
1589 case tok::eof:
1590 return;
1591
1592 default:
1593 break;
1594 }
1595
1596 ConsumeAnyToken();
1597 }
1598}
1599
John McCalld5a36322009-11-03 19:26:08 +00001600/// ParseDeclGroup - Having concluded that this is either a function
1601/// definition or a group of object declarations, actually parse the
1602/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001603Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1604 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001605 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001606 SourceLocation *DeclEnd,
1607 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001608 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001609 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001610 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001611
John McCalld5a36322009-11-03 19:26:08 +00001612 // Bail out if the first declarator didn't seem well-formed.
1613 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001614 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001615 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001616 }
Mike Stump11289f42009-09-09 15:08:12 +00001617
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001618 // Save late-parsed attributes for now; they need to be parsed in the
1619 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001620 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1621 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001622 if (D.isFunctionDeclarator())
1623 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1624
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001625 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001626 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001627 // Look at the next token to make sure that this isn't a function
1628 // declaration. We have to check this because __attribute__ might be the
1629 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001630 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001631
Douglas Gregor012efe22013-04-16 16:01:32 +00001632 if (AllowFunctionDefinitions) {
1633 if (isStartOfFunctionDefinition(D)) {
1634 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1635 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001636
Douglas Gregor012efe22013-04-16 16:01:32 +00001637 // Recover by treating the 'typedef' as spurious.
1638 DS.ClearStorageClassSpecs();
1639 }
1640
1641 Decl *TheDecl =
1642 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1643 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001644 }
1645
Douglas Gregor012efe22013-04-16 16:01:32 +00001646 if (isDeclarationSpecifier()) {
1647 // If there is an invalid declaration specifier right after the function
1648 // prototype, then we must be in a missing semicolon case where this isn't
1649 // actually a body. Just fall through into the code that handles it as a
1650 // prototype, and let the top-level code handle the erroneous declspec
1651 // where it would otherwise expect a comma or semicolon.
1652 } else {
1653 Diag(Tok, diag::err_expected_fn_body);
1654 SkipUntil(tok::semi);
1655 return DeclGroupPtrTy();
1656 }
John McCalld5a36322009-11-03 19:26:08 +00001657 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001658 if (Tok.is(tok::l_brace)) {
1659 Diag(Tok, diag::err_function_definition_not_allowed);
1660 SkipUntil(tok::r_brace, true, true);
1661 }
John McCalld5a36322009-11-03 19:26:08 +00001662 }
1663 }
1664
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001665 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001666 return DeclGroupPtrTy();
1667
1668 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1669 // must parse and analyze the for-range-initializer before the declaration is
1670 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001671 //
1672 // Handle the Objective-C for-in loop variable similarly, although we
1673 // don't need to parse the container in advance.
1674 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1675 bool IsForRangeLoop = false;
1676 if (Tok.is(tok::colon)) {
1677 IsForRangeLoop = true;
1678 FRI->ColonLoc = ConsumeToken();
1679 if (Tok.is(tok::l_brace))
1680 FRI->RangeExpr = ParseBraceInitializer();
1681 else
1682 FRI->RangeExpr = ParseExpression();
1683 }
1684
Richard Smith02e85f32011-04-14 22:09:26 +00001685 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001686 if (IsForRangeLoop)
1687 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001688 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001689 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001690 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001691 }
1692
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001693 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001694 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001695 if (LateParsedAttrs.size() > 0)
1696 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001697 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001698 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001699 DeclsInGroup.push_back(FirstDecl);
1700
Richard Smith09f76ee2011-10-19 21:33:05 +00001701 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001702
John McCalld5a36322009-11-03 19:26:08 +00001703 // If we don't have a comma, it is either the end of the list (a ';') or an
1704 // error, bail out.
1705 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001706 SourceLocation CommaLoc = ConsumeToken();
1707
1708 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1709 // This comma was followed by a line-break and something which can't be
1710 // the start of a declarator. The comma was probably a typo for a
1711 // semicolon.
1712 Diag(CommaLoc, diag::err_expected_semi_declaration)
1713 << FixItHint::CreateReplacement(CommaLoc, ";");
1714 ExpectSemi = false;
1715 break;
1716 }
John McCalld5a36322009-11-03 19:26:08 +00001717
1718 // Parse the next declarator.
1719 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001720 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001721
1722 // Accept attributes in an init-declarator. In the first declarator in a
1723 // declaration, these would be part of the declspec. In subsequent
1724 // declarators, they become part of the declarator itself, so that they
1725 // don't apply to declarators after *this* one. Examples:
1726 // short __attribute__((common)) var; -> declspec
1727 // short var __attribute__((common)); -> declarator
1728 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001729 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001730
1731 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001732 if (!D.isInvalidType()) {
1733 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1734 D.complete(ThisDecl);
1735 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001736 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001737 }
John McCalld5a36322009-11-03 19:26:08 +00001738 }
1739
1740 if (DeclEnd)
1741 *DeclEnd = Tok.getLocation();
1742
Richard Smith09f76ee2011-10-19 21:33:05 +00001743 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001744 ExpectAndConsumeSemi(Context == Declarator::FileContext
1745 ? diag::err_invalid_token_after_toplevel_declarator
1746 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001747 // Okay, there was no semicolon and one was expected. If we see a
1748 // declaration specifier, just assume it was missing and continue parsing.
1749 // Otherwise things are very confused and we skip to recover.
1750 if (!isDeclarationSpecifier()) {
1751 SkipUntil(tok::r_brace, true, true);
1752 if (Tok.is(tok::semi))
1753 ConsumeToken();
1754 }
John McCalld5a36322009-11-03 19:26:08 +00001755 }
1756
Rafael Espindolaab417692013-07-09 12:05:01 +00001757 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001758}
1759
Richard Smith02e85f32011-04-14 22:09:26 +00001760/// Parse an optional simple-asm-expr and attributes, and attach them to a
1761/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001762bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001763 // If a simple-asm-expr is present, parse it.
1764 if (Tok.is(tok::kw_asm)) {
1765 SourceLocation Loc;
1766 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1767 if (AsmLabel.isInvalid()) {
1768 SkipUntil(tok::semi, true, true);
1769 return true;
1770 }
1771
1772 D.setAsmLabel(AsmLabel.release());
1773 D.SetRangeEnd(Loc);
1774 }
1775
1776 MaybeParseGNUAttributes(D);
1777 return false;
1778}
1779
Douglas Gregor23996282009-05-12 21:31:51 +00001780/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1781/// declarator'. This method parses the remainder of the declaration
1782/// (including any attributes or initializer, among other things) and
1783/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001784///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001785/// init-declarator: [C99 6.7]
1786/// declarator
1787/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001788/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1789/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001790/// [C++] declarator initializer[opt]
1791///
1792/// [C++] initializer:
1793/// [C++] '=' initializer-clause
1794/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001795/// [C++0x] '=' 'default' [TODO]
1796/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001797/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001798///
1799/// According to the standard grammar, =default and =delete are function
1800/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001801///
John McCall48871652010-08-21 09:40:31 +00001802Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001803 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001804 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001805 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001806
Richard Smith02e85f32011-04-14 22:09:26 +00001807 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1808}
Mike Stump11289f42009-09-09 15:08:12 +00001809
Richard Smith02e85f32011-04-14 22:09:26 +00001810Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1811 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001812 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001813 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001814 switch (TemplateInfo.Kind) {
1815 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001816 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001817 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001818
Douglas Gregor450f00842009-09-25 18:43:00 +00001819 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001820 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001821 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001822 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001823 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00001824 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001825 // Re-direct this decl to refer to the templated decl so that we can
1826 // initialize it.
1827 ThisDecl = VT->getTemplatedDecl();
1828 break;
1829 }
1830 case ParsedTemplateInfo::ExplicitInstantiation: {
1831 if (Tok.is(tok::semi)) {
1832 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1833 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1834 if (ThisRes.isInvalid()) {
1835 SkipUntil(tok::semi, true, true);
1836 return 0;
1837 }
1838 ThisDecl = ThisRes.get();
1839 } else {
1840 // FIXME: This check should be for a variable template instantiation only.
1841
1842 // Check that this is a valid instantiation
1843 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1844 // If the declarator-id is not a template-id, issue a diagnostic and
1845 // recover by ignoring the 'template' keyword.
1846 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1847 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1848 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1849 } else {
1850 SourceLocation LAngleLoc =
1851 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1852 Diag(D.getIdentifierLoc(),
1853 diag::err_explicit_instantiation_with_definition)
1854 << SourceRange(TemplateInfo.TemplateLoc)
1855 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1856
1857 // Recover as if it were an explicit specialization.
1858 TemplateParameterLists FakedParamLists;
1859 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1860 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1861 LAngleLoc));
1862
1863 ThisDecl =
1864 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1865 }
1866 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001867 break;
1868 }
1869 }
Mike Stump11289f42009-09-09 15:08:12 +00001870
Richard Smith74aeef52013-04-26 16:15:35 +00001871 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001872
Douglas Gregor23996282009-05-12 21:31:51 +00001873 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001874 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001875 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001876 ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001877
Anders Carlsson991285e2010-09-24 21:25:25 +00001878 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001879 if (D.isFunctionDeclarator())
1880 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1881 << 1 /* delete */;
1882 else
1883 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001884 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001885 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001886 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1887 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001888 else
1889 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001890 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001891 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001892 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001893 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001894 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001895
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001896 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001897 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001898 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001899 cutOffParsing();
1900 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001901 }
Chad Rosierc1183952012-06-26 22:30:43 +00001902
John McCalldadc5752010-08-24 06:29:42 +00001903 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001904
David Blaikiebbafb8a2012-03-11 07:00:24 +00001905 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001906 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001907 ExitScope();
1908 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001909
Douglas Gregor23996282009-05-12 21:31:51 +00001910 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001911 SkipUntil(tok::comma, true, true);
1912 Actions.ActOnInitializerError(ThisDecl);
1913 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001914 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1915 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001916 }
1917 } else if (Tok.is(tok::l_paren)) {
1918 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001919 BalancedDelimiterTracker T(*this, tok::l_paren);
1920 T.consumeOpen();
1921
Benjamin Kramerf0623432012-08-23 22:51:59 +00001922 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001923 CommaLocsTy CommaLocs;
1924
David Blaikiebbafb8a2012-03-11 07:00:24 +00001925 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001926 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001927 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001928 }
1929
Douglas Gregor23996282009-05-12 21:31:51 +00001930 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001931 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00001932 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001933
David Blaikiebbafb8a2012-03-11 07:00:24 +00001934 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001935 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001936 ExitScope();
1937 }
Douglas Gregor23996282009-05-12 21:31:51 +00001938 } else {
1939 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001940 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001941
1942 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1943 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001944
David Blaikiebbafb8a2012-03-11 07:00:24 +00001945 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001946 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001947 ExitScope();
1948 }
1949
Sebastian Redla9351792012-02-11 23:51:47 +00001950 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1951 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001952 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001953 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1954 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001955 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001956 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001957 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001958 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001959 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1960
Sebastian Redl3da34892011-06-05 12:23:16 +00001961 if (D.getCXXScopeSpec().isSet()) {
1962 EnterScope(0);
1963 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1964 }
1965
1966 ExprResult Init(ParseBraceInitializer());
1967
1968 if (D.getCXXScopeSpec().isSet()) {
1969 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1970 ExitScope();
1971 }
1972
1973 if (Init.isInvalid()) {
1974 Actions.ActOnInitializerError(ThisDecl);
1975 } else
1976 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1977 /*DirectInit=*/true, TypeContainsAuto);
1978
Douglas Gregor23996282009-05-12 21:31:51 +00001979 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001980 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001981 }
1982
Richard Smithb2bc2e62011-02-21 20:05:19 +00001983 Actions.FinalizeDeclaration(ThisDecl);
1984
Douglas Gregor23996282009-05-12 21:31:51 +00001985 return ThisDecl;
1986}
1987
Chris Lattner1890ac82006-08-13 01:16:23 +00001988/// ParseSpecifierQualifierList
1989/// specifier-qualifier-list:
1990/// type-specifier specifier-qualifier-list[opt]
1991/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001992/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001993///
Richard Smithc5b05522012-03-12 07:56:15 +00001994void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1995 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001996 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1997 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001998 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001999 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00002000
Chris Lattner1890ac82006-08-13 01:16:23 +00002001 // Validate declspec for type-name.
2002 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00002003 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
2004 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00002005 Diag(Tok, diag::err_expected_type);
2006 DS.SetTypeSpecError();
2007 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
2008 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002009 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00002010 if (!DS.hasTypeSpecifier())
2011 DS.SetTypeSpecError();
2012 }
Mike Stump11289f42009-09-09 15:08:12 +00002013
Chris Lattner1b22eed2006-11-28 05:12:07 +00002014 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002015 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00002016 if (DS.getStorageClassSpecLoc().isValid())
2017 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2018 else
Richard Smithb4a9e862013-04-12 22:46:28 +00002019 Diag(DS.getThreadStorageClassSpecLoc(),
2020 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00002021 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002022 }
Mike Stump11289f42009-09-09 15:08:12 +00002023
Chris Lattner1b22eed2006-11-28 05:12:07 +00002024 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002025 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002026 if (DS.isInlineSpecified())
2027 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2028 if (DS.isVirtualSpecified())
2029 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2030 if (DS.isExplicitSpecified())
2031 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002032 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002033 }
Richard Smithc5b05522012-03-12 07:56:15 +00002034
2035 // Issue diagnostic and remove constexpr specfier if present.
2036 if (DS.isConstexprSpecified()) {
2037 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2038 DS.ClearConstexprSpec();
2039 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002040}
Chris Lattner53361ac2006-08-10 05:19:57 +00002041
Chris Lattner6cc055a2009-04-12 20:42:31 +00002042/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2043/// specified token is valid after the identifier in a declarator which
2044/// immediately follows the declspec. For example, these things are valid:
2045///
2046/// int x [ 4]; // direct-declarator
2047/// int x ( int y); // direct-declarator
2048/// int(int x ) // direct-declarator
2049/// int x ; // simple-declaration
2050/// int x = 17; // init-declarator-list
2051/// int x , y; // init-declarator-list
2052/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002053/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002054/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002055///
2056/// This is not, because 'x' does not immediately follow the declspec (though
2057/// ')' happens to be valid anyway).
2058/// int (x)
2059///
2060static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2061 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2062 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002063 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002064}
2065
Chris Lattner20a0c612009-04-14 21:34:55 +00002066
2067/// ParseImplicitInt - This method is called when we have an non-typename
2068/// identifier in a declspec (which normally terminates the decl spec) when
2069/// the declspec has no type specifier. In this case, the declspec is either
2070/// malformed or is "implicit int" (in K&R and C89).
2071///
2072/// This method handles diagnosing this prettily and returns false if the
2073/// declspec is done being processed. If it recovers and thinks there may be
2074/// other pieces of declspec after it, it returns true.
2075///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002076bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002077 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002078 AccessSpecifier AS, DeclSpecContext DSC,
2079 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002080 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002081
Chris Lattner20a0c612009-04-14 21:34:55 +00002082 SourceLocation Loc = Tok.getLocation();
2083 // If we see an identifier that is not a type name, we normally would
2084 // parse it as the identifer being declared. However, when a typename
2085 // is typo'd or the definition is not included, this will incorrectly
2086 // parse the typename as the identifier name and fall over misparsing
2087 // later parts of the diagnostic.
2088 //
2089 // As such, we try to do some look-ahead in cases where this would
2090 // otherwise be an "implicit-int" case to see if this is invalid. For
2091 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2092 // an identifier with implicit int, we'd get a parse error because the
2093 // next token is obviously invalid for a type. Parse these as a case
2094 // with an invalid type specifier.
2095 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002096
Chris Lattner20a0c612009-04-14 21:34:55 +00002097 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002098 // error, do lookahead to try to do better recovery. This never applies
2099 // within a type specifier. Outside of C++, we allow this even if the
2100 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002101 // implicit int as an extension in C99 and C11.
Richard Smith2f07ad52012-05-09 20:55:26 +00002102 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith3b870382013-04-30 22:43:51 +00002103 !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002104 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002105 // If this token is valid for implicit int, e.g. "static x = 4", then
2106 // we just avoid eating the identifier, so it will be parsed as the
2107 // identifier in the declarator.
2108 return false;
2109 }
Mike Stump11289f42009-09-09 15:08:12 +00002110
Richard Smitha952ebb2012-05-15 21:01:51 +00002111 if (getLangOpts().CPlusPlus &&
2112 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2113 // Don't require a type specifier if we have the 'auto' storage class
2114 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002115 if (SS)
2116 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002117 return false;
2118 }
2119
Chris Lattner20a0c612009-04-14 21:34:55 +00002120 // Otherwise, if we don't consume this token, we are going to emit an
2121 // error anyway. Try to recover from various common problems. Check
2122 // to see if this was a reference to a tag name without a tag specified.
2123 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002124 //
2125 // C++ doesn't need this, and isTagName doesn't take SS.
2126 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002127 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002128 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002129
Douglas Gregor0be31a22010-07-02 17:43:08 +00002130 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002131 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002132 case DeclSpec::TST_enum:
2133 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2134 case DeclSpec::TST_union:
2135 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2136 case DeclSpec::TST_struct:
2137 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002138 case DeclSpec::TST_interface:
2139 TagName="__interface"; FixitTagName = "__interface ";
2140 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002141 case DeclSpec::TST_class:
2142 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002143 }
Mike Stump11289f42009-09-09 15:08:12 +00002144
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002145 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002146 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2147 LookupResult R(Actions, TokenName, SourceLocation(),
2148 Sema::LookupOrdinaryName);
2149
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002150 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002151 << TokenName << TagName << getLangOpts().CPlusPlus
2152 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2153
2154 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2155 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2156 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002157 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002158 << TokenName << TagName;
2159 }
Mike Stump11289f42009-09-09 15:08:12 +00002160
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002161 // Parse this as a tag as if the missing tag were present.
2162 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002163 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002164 else
Richard Smithc5b05522012-03-12 07:56:15 +00002165 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002166 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002167 return true;
2168 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002169 }
Mike Stump11289f42009-09-09 15:08:12 +00002170
Richard Smithfe904f02012-05-15 21:29:55 +00002171 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002172 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00002173 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2174 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002175 // Look ahead to the next token to try to figure out what this declaration
2176 // was supposed to be.
2177 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002178 case tok::l_paren: {
2179 // static x(4); // 'x' is not a type
2180 // x(int n); // 'x' is not a type
2181 // x (*p)[]; // 'x' is a type
2182 //
2183 // Since we're in an error case (or the rare 'implicit int in C++' MS
2184 // extension), we can afford to perform a tentative parse to determine
2185 // which case we're in.
2186 TentativeParsingAction PA(*this);
2187 ConsumeToken();
2188 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2189 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002190
2191 if (TPR != TPResult::False()) {
2192 // The identifier is followed by a parenthesized declarator.
2193 // It's supposed to be a type.
2194 break;
2195 }
2196
2197 // If we're in a context where we could be declaring a constructor,
2198 // check whether this is a constructor declaration with a bogus name.
2199 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2200 IdentifierInfo *II = Tok.getIdentifierInfo();
2201 if (Actions.isCurrentClassNameTypo(II, SS)) {
2202 Diag(Loc, diag::err_constructor_bad_name)
2203 << Tok.getIdentifierInfo() << II
2204 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2205 Tok.setIdentifierInfo(II);
2206 }
2207 }
2208 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002209 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002210 case tok::comma:
2211 case tok::equal:
2212 case tok::kw_asm:
2213 case tok::l_brace:
2214 case tok::l_square:
2215 case tok::semi:
2216 // This looks like a variable or function declaration. The type is
2217 // probably missing. We're done parsing decl-specifiers.
2218 if (SS)
2219 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2220 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002221
2222 default:
2223 // This is probably supposed to be a type. This includes cases like:
2224 // int f(itn);
2225 // struct S { unsinged : 4; };
2226 break;
2227 }
2228 }
2229
Chad Rosierc1183952012-06-26 22:30:43 +00002230 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002231 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002232 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002233 IdentifierInfo *II = Tok.getIdentifierInfo();
2234 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002235 // The action emitted a diagnostic, so we don't have to.
2236 if (T) {
2237 // The action has suggested that the type T could be used. Set that as
2238 // the type in the declaration specifiers, consume the would-be type
2239 // name token, and we're done.
2240 const char *PrevSpec;
2241 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002242 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002243 DS.SetRangeEnd(Tok.getLocation());
2244 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002245 // There may be other declaration specifiers after this.
2246 return true;
2247 } else if (II != Tok.getIdentifierInfo()) {
2248 // If no type was suggested, the correction is to a keyword
2249 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002250 // There may be other declaration specifiers after this.
2251 return true;
2252 }
Chad Rosierc1183952012-06-26 22:30:43 +00002253
Douglas Gregor15e56022009-10-13 23:27:22 +00002254 // Fall through; the action had no suggestion for us.
2255 } else {
2256 // The action did not emit a diagnostic, so emit one now.
2257 SourceRange R;
2258 if (SS) R = SS->getRange();
2259 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2260 }
Mike Stump11289f42009-09-09 15:08:12 +00002261
Douglas Gregor15e56022009-10-13 23:27:22 +00002262 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002263 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002264 DS.SetRangeEnd(Tok.getLocation());
2265 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002266
Chris Lattner20a0c612009-04-14 21:34:55 +00002267 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2268 // avoid rippling error messages on subsequent uses of the same type,
2269 // could be useful if #include was forgotten.
2270 return false;
2271}
2272
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002273/// \brief Determine the declaration specifier context from the declarator
2274/// context.
2275///
2276/// \param Context the declarator context, which is one of the
2277/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002278Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002279Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2280 if (Context == Declarator::MemberContext)
2281 return DSC_class;
2282 if (Context == Declarator::FileContext)
2283 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002284 if (Context == Declarator::TrailingReturnContext)
2285 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002286 return DSC_normal;
2287}
2288
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002289/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2290///
2291/// FIXME: Simply returns an alignof() expression if the argument is a
2292/// type. Ideally, the type should be propagated directly into Sema.
2293///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002294/// [C11] type-id
2295/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002296/// [C++0x] type-id ...[opt]
2297/// [C++0x] assignment-expression ...[opt]
2298ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2299 SourceLocation &EllipsisLoc) {
2300 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002301 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002302 SourceLocation TypeLoc = Tok.getLocation();
2303 ParsedType Ty = ParseTypeName().get();
2304 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002305 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2306 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002307 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002308 ER = ParseConstantExpression();
2309
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002310 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002311 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002312
2313 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002314}
2315
2316/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2317/// attribute to Attrs.
2318///
2319/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002320/// [C11] '_Alignas' '(' type-id ')'
2321/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002322/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2323/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002324void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002325 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002326 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2327 "Not an alignment-specifier!");
2328
Richard Smithd11c7a12013-01-29 01:48:07 +00002329 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2330 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002331
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002332 BalancedDelimiterTracker T(*this, tok::l_paren);
2333 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002334 return;
2335
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002336 SourceLocation EllipsisLoc;
2337 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002338 if (ArgExpr.isInvalid()) {
2339 SkipUntil(tok::r_paren);
2340 return;
2341 }
2342
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002343 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002344 if (EndLoc)
2345 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002346
Aaron Ballman00e99962013-08-31 01:11:41 +00002347 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002348 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002349 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2350 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002351}
2352
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002353/// ParseDeclarationSpecifiers
2354/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002355/// storage-class-specifier declaration-specifiers[opt]
2356/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002357/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002358/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002359/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002360/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002361///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002362/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002363/// 'typedef'
2364/// 'extern'
2365/// 'static'
2366/// 'auto'
2367/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002368/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002369/// [C++11] 'thread_local'
2370/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002371/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002372/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002373/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002374/// [C++] 'virtual'
2375/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002376/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002377/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002378/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002379
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002380///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002381void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002382 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002383 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002384 DeclSpecContext DSContext,
2385 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002386 if (DS.getSourceRange().isInvalid()) {
2387 DS.SetRangeStart(Tok.getLocation());
2388 DS.SetRangeEnd(Tok.getLocation());
2389 }
Chad Rosierc1183952012-06-26 22:30:43 +00002390
Douglas Gregordf593fb2011-11-07 17:33:42 +00002391 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002392 bool AttrsLastTime = false;
2393 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002394 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002395 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002396 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002397 unsigned DiagID = 0;
2398
Chris Lattner4d8f8732006-11-28 05:05:08 +00002399 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002400
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002401 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002402 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002403 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002404 if (!AttrsLastTime)
2405 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002406 else {
2407 // Reject C++11 attributes that appertain to decl specifiers as
2408 // we don't support any C++11 attributes that appertain to decl
2409 // specifiers. This also conforms to what g++ 4.8 is doing.
2410 ProhibitCXX11Attributes(attrs);
2411
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002412 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002413 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002414
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002415 // If this is not a declaration specifier token, we're done reading decl
2416 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002417 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002418 return;
Mike Stump11289f42009-09-09 15:08:12 +00002419
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002420 case tok::l_square:
2421 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002422 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002423 goto DoneWithDeclSpec;
2424
2425 ProhibitAttributes(attrs);
2426 // FIXME: It would be good to recover by accepting the attributes,
2427 // but attempting to do that now would cause serious
2428 // madness in terms of diagnostics.
2429 attrs.clear();
2430 attrs.Range = SourceRange();
2431
2432 ParseCXX11Attributes(attrs);
2433 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002434 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002435
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002436 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002437 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002438 if (DS.hasTypeSpecifier()) {
2439 bool AllowNonIdentifiers
2440 = (getCurScope()->getFlags() & (Scope::ControlScope |
2441 Scope::BlockScope |
2442 Scope::TemplateParamScope |
2443 Scope::FunctionPrototypeScope |
2444 Scope::AtCatchScope)) == 0;
2445 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002446 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002447 (DSContext == DSC_class && DS.isFriendSpecified());
2448
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002449 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002450 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002451 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002452 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002453 }
2454
Douglas Gregor80039242011-02-15 20:33:25 +00002455 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2456 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2457 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002458 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002459 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002460 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002461 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002462 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002463 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002464
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002465 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002466 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002467 }
2468
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002469 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002470 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002471 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002472 if (!DS.hasTypeSpecifier())
2473 DS.SetTypeSpecError();
2474 goto DoneWithDeclSpec;
2475 }
John McCall8bc2a702010-03-01 18:20:46 +00002476 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2477 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002478 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002479
2480 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002481 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002482 goto DoneWithDeclSpec;
2483
John McCall9dab4e62009-12-12 11:40:51 +00002484 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002485 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2486 Tok.getAnnotationRange(),
2487 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002488
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002489 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002490 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002491 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002492 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002493 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002494 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002495
2496 // C++ [class.qual]p2:
2497 // In a lookup in which the constructor is an acceptable lookup
2498 // result and the nested-name-specifier nominates a class C:
2499 //
2500 // - if the name specified after the
2501 // nested-name-specifier, when looked up in C, is the
2502 // injected-class-name of C (Clause 9), or
2503 //
2504 // - if the name specified after the nested-name-specifier
2505 // is the same as the identifier or the
2506 // simple-template-id's template-name in the last
2507 // component of the nested-name-specifier,
2508 //
2509 // the name is instead considered to name the constructor of
2510 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002511 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002512 // Thus, if the template-name is actually the constructor
2513 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002514 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002515 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002516 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002517 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002518 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002519 if (isConstructorDeclarator()) {
2520 // The user meant this to be an out-of-line constructor
2521 // definition, but template arguments are not allowed
2522 // there. Just allow this as a constructor; we'll
2523 // complain about it later.
2524 goto DoneWithDeclSpec;
2525 }
2526
2527 // The user meant this to name a type, but it actually names
2528 // a constructor with some extraneous template
2529 // arguments. Complain, then parse it as a type as the user
2530 // intended.
2531 Diag(TemplateId->TemplateNameLoc,
2532 diag::err_out_of_line_template_id_names_constructor)
2533 << TemplateId->Name;
2534 }
2535
John McCall9dab4e62009-12-12 11:40:51 +00002536 DS.getTypeSpecScope() = SS;
2537 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002538 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002539 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002540 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002541 continue;
2542 }
2543
Douglas Gregorc5790df2009-09-28 07:26:33 +00002544 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002545 DS.getTypeSpecScope() = SS;
2546 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002547 if (Tok.getAnnotationValue()) {
2548 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002549 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002550 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002551 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002552 if (isInvalid)
2553 break;
John McCallba7bf592010-08-24 05:47:05 +00002554 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002555 else
2556 DS.SetTypeSpecError();
2557 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2558 ConsumeToken(); // The typename
2559 }
2560
Douglas Gregor167fa622009-03-25 15:40:00 +00002561 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002562 goto DoneWithDeclSpec;
2563
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002564 // If we're in a context where the identifier could be a class name,
2565 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002566 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002567 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002568 &SS)) {
2569 if (isConstructorDeclarator())
2570 goto DoneWithDeclSpec;
2571
2572 // As noted in C++ [class.qual]p2 (cited above), when the name
2573 // of the class is qualified in a context where it could name
2574 // a constructor, its a constructor name. However, we've
2575 // looked at the declarator, and the user probably meant this
2576 // to be a type. Complain that it isn't supposed to be treated
2577 // as a type, then proceed to parse it as a type.
2578 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2579 << Next.getIdentifierInfo();
2580 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002581
John McCallba7bf592010-08-24 05:47:05 +00002582 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2583 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002584 getCurScope(), &SS,
2585 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002586 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002587 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002588
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002589 // If the referenced identifier is not a type, then this declspec is
2590 // erroneous: We already checked about that it has no type specifier, and
2591 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002592 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002593 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002594 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002595 ParsedAttributesWithRange Attrs(AttrFactory);
2596 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2597 if (!Attrs.empty()) {
2598 AttrsLastTime = true;
2599 attrs.takeAllFrom(Attrs);
2600 }
2601 continue;
2602 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002603 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002604 }
Mike Stump11289f42009-09-09 15:08:12 +00002605
John McCall9dab4e62009-12-12 11:40:51 +00002606 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002607 ConsumeToken(); // The C++ scope.
2608
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002609 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002610 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002611 if (isInvalid)
2612 break;
Mike Stump11289f42009-09-09 15:08:12 +00002613
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002614 DS.SetRangeEnd(Tok.getLocation());
2615 ConsumeToken(); // The typename.
2616
2617 continue;
2618 }
Mike Stump11289f42009-09-09 15:08:12 +00002619
Chris Lattnere387d9e2009-01-21 19:48:37 +00002620 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002621 if (Tok.getAnnotationValue()) {
2622 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002623 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002624 DiagID, T);
2625 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002626 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002627
Chris Lattner005fc1b2010-04-05 18:18:31 +00002628 if (isInvalid)
2629 break;
2630
Chris Lattnere387d9e2009-01-21 19:48:37 +00002631 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2632 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002633
Chris Lattnere387d9e2009-01-21 19:48:37 +00002634 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2635 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002636 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002637 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002638 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002639
Chris Lattnere387d9e2009-01-21 19:48:37 +00002640 continue;
2641 }
Mike Stump11289f42009-09-09 15:08:12 +00002642
Douglas Gregor06873092011-04-28 15:48:45 +00002643 case tok::kw___is_signed:
2644 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2645 // typically treats it as a trait. If we see __is_signed as it appears
2646 // in libstdc++, e.g.,
2647 //
2648 // static const bool __is_signed;
2649 //
2650 // then treat __is_signed as an identifier rather than as a keyword.
2651 if (DS.getTypeSpecType() == TST_bool &&
2652 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2653 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2654 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2655 Tok.setKind(tok::identifier);
2656 }
2657
2658 // We're done with the declaration-specifiers.
2659 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002660
Chris Lattner16fac4f2008-07-26 01:18:38 +00002661 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002662 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002663 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002664 // In C++, check to see if this is a scope specifier like foo::bar::, if
2665 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002666 if (getLangOpts().CPlusPlus) {
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002667 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002668 if (!DS.hasTypeSpecifier())
2669 DS.SetTypeSpecError();
2670 goto DoneWithDeclSpec;
2671 }
2672 if (!Tok.is(tok::identifier))
2673 continue;
2674 }
Mike Stump11289f42009-09-09 15:08:12 +00002675
Chris Lattner16fac4f2008-07-26 01:18:38 +00002676 // This identifier can only be a typedef name if we haven't already seen
2677 // a type-specifier. Without this check we misparse:
2678 // typedef int X; struct Y { short X; }; as 'short int'.
2679 if (DS.hasTypeSpecifier())
2680 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002681
John Thompson22334602010-02-05 00:12:22 +00002682 // Check for need to substitute AltiVec keyword tokens.
2683 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2684 break;
2685
Richard Smith3092a3b2012-05-09 18:56:43 +00002686 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2687 // allow the use of a typedef name as a type specifier.
2688 if (DS.isTypeAltiVecVector())
2689 goto DoneWithDeclSpec;
2690
John McCallba7bf592010-08-24 05:47:05 +00002691 ParsedType TypeRep =
2692 Actions.getTypeName(*Tok.getIdentifierInfo(),
2693 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002694
Chris Lattner6cc055a2009-04-12 20:42:31 +00002695 // If this is not a typedef name, don't parse it as part of the declspec,
2696 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002697 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002698 ParsedAttributesWithRange Attrs(AttrFactory);
2699 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2700 if (!Attrs.empty()) {
2701 AttrsLastTime = true;
2702 attrs.takeAllFrom(Attrs);
2703 }
2704 continue;
2705 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002706 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002707 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002708
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002709 // If we're in a context where the identifier could be a class name,
2710 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002711 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002712 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002713 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002714 goto DoneWithDeclSpec;
2715
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002716 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002717 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002718 if (isInvalid)
2719 break;
Mike Stump11289f42009-09-09 15:08:12 +00002720
Chris Lattner16fac4f2008-07-26 01:18:38 +00002721 DS.SetRangeEnd(Tok.getLocation());
2722 ConsumeToken(); // The identifier
2723
2724 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2725 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002726 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002727 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002728 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002729
Steve Naroffcd5e7822008-09-22 10:28:57 +00002730 // Need to support trailing type qualifiers (e.g. "id<p> const").
2731 // If a type specifier follows, it will be diagnosed elsewhere.
2732 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002733 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002734
2735 // type-name
2736 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002737 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002738 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002739 // This template-id does not refer to a type name, so we're
2740 // done with the type-specifiers.
2741 goto DoneWithDeclSpec;
2742 }
2743
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002744 // If we're in a context where the template-id could be a
2745 // constructor name or specialization, check whether this is a
2746 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002747 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002748 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002749 isConstructorDeclarator())
2750 goto DoneWithDeclSpec;
2751
Douglas Gregor7f741122009-02-25 19:37:18 +00002752 // Turn the template-id annotation token into a type annotation
2753 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002754 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002755 continue;
2756 }
2757
Chris Lattnere37e2332006-08-15 04:50:22 +00002758 // GNU attributes support.
2759 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002760 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002761 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002762
2763 // Microsoft declspec support.
2764 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002765 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002766 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002767
Steve Naroff44ac7772008-12-25 14:16:32 +00002768 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002769 case tok::kw___forceinline: {
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002770 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002771 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002772 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002773 // FIXME: This does not work correctly if it is set to be a declspec
2774 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman00e99962013-08-31 01:11:41 +00002775 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2776 AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002777 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002778 }
Eli Friedman53339e02009-06-08 23:27:34 +00002779
Aaron Ballman317a77f2013-05-22 23:25:32 +00002780 case tok::kw___sptr:
2781 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002782 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002783 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002784 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002785 case tok::kw___cdecl:
2786 case tok::kw___stdcall:
2787 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002788 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002789 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002790 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002791 continue;
2792
Dawn Perchik335e16b2010-09-03 01:29:35 +00002793 // Borland single token adornments.
2794 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002795 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002796 continue;
2797
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002798 // OpenCL single token adornments.
2799 case tok::kw___kernel:
2800 ParseOpenCLAttributes(DS.getAttributes());
2801 continue;
2802
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002803 // storage-class-specifier
2804 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002805 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2806 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002807 break;
2808 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002809 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002810 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002811 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2812 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002813 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002814 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002815 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2816 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002817 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002818 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002819 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002820 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002821 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2822 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002823 break;
2824 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002825 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002826 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002827 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2828 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002829 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002830 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002831 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002832 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002833 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2834 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002835 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002836 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2837 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002838 break;
2839 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002840 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2841 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002842 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002843 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002844 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2845 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002846 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002847 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002848 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2849 PrevSpec, DiagID);
2850 break;
2851 case tok::kw_thread_local:
2852 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2853 PrevSpec, DiagID);
2854 break;
2855 case tok::kw__Thread_local:
2856 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2857 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002858 break;
Mike Stump11289f42009-09-09 15:08:12 +00002859
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002860 // function-specifier
2861 case tok::kw_inline:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002862 isInvalid = DS.setFunctionSpecInline(Loc);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002863 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002864 case tok::kw_virtual:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002865 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002866 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002867 case tok::kw_explicit:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002868 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002869 break;
Richard Smith0015f092013-01-17 22:16:11 +00002870 case tok::kw__Noreturn:
2871 if (!getLangOpts().C11)
2872 Diag(Loc, diag::ext_c11_noreturn);
2873 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2874 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002875
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002876 // alignment-specifier
2877 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002878 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002879 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002880 ParseAlignmentSpecifier(DS.getAttributes());
2881 continue;
2882
Anders Carlssoncd8db412009-05-06 04:46:28 +00002883 // friend
2884 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002885 if (DSContext == DSC_class)
2886 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2887 else {
2888 PrevSpec = ""; // not actually used by the diagnostic
2889 DiagID = diag::err_friend_invalid_in_context;
2890 isInvalid = true;
2891 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002892 break;
Mike Stump11289f42009-09-09 15:08:12 +00002893
Douglas Gregor26701a42011-09-09 02:06:17 +00002894 // Modules
2895 case tok::kw___module_private__:
2896 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2897 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002898
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002899 // constexpr
2900 case tok::kw_constexpr:
2901 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2902 break;
2903
Chris Lattnere387d9e2009-01-21 19:48:37 +00002904 // type-specifier
2905 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002906 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2907 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002908 break;
2909 case tok::kw_long:
2910 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002911 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2912 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002913 else
John McCall49bfce42009-08-03 20:12:06 +00002914 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2915 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002916 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002917 case tok::kw___int64:
2918 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2919 DiagID);
2920 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002921 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002922 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2923 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002924 break;
2925 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002926 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2927 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002928 break;
2929 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002930 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2931 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002932 break;
2933 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002934 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2935 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002936 break;
2937 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002938 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2939 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002940 break;
2941 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002942 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2943 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002944 break;
2945 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002946 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2947 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002948 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002949 case tok::kw___int128:
2950 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2951 DiagID);
2952 break;
2953 case tok::kw_half:
2954 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2955 DiagID);
2956 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002957 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002958 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2959 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002960 break;
2961 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002962 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2963 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002964 break;
2965 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002966 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2967 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002968 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002969 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002970 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2971 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002972 break;
2973 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002974 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2975 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002976 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002977 case tok::kw_bool:
2978 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002979 if (Tok.is(tok::kw_bool) &&
2980 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2981 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2982 PrevSpec = ""; // Not used by the diagnostic.
2983 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002984 // For better error recovery.
2985 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002986 isInvalid = true;
2987 } else {
2988 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2989 DiagID);
2990 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002991 break;
2992 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002993 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2994 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002995 break;
2996 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002997 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2998 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002999 break;
3000 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003001 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
3002 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003003 break;
John Thompson22334602010-02-05 00:12:22 +00003004 case tok::kw___vector:
3005 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3006 break;
3007 case tok::kw___pixel:
3008 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3009 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003010 case tok::kw_image1d_t:
3011 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
3012 PrevSpec, DiagID);
3013 break;
3014 case tok::kw_image1d_array_t:
3015 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
3016 PrevSpec, DiagID);
3017 break;
3018 case tok::kw_image1d_buffer_t:
3019 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
3020 PrevSpec, DiagID);
3021 break;
3022 case tok::kw_image2d_t:
3023 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
3024 PrevSpec, DiagID);
3025 break;
3026 case tok::kw_image2d_array_t:
3027 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
3028 PrevSpec, DiagID);
3029 break;
3030 case tok::kw_image3d_t:
3031 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
3032 PrevSpec, DiagID);
3033 break;
Guy Benyei61054192013-02-07 10:55:47 +00003034 case tok::kw_sampler_t:
3035 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
3036 PrevSpec, DiagID);
3037 break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003038 case tok::kw_event_t:
3039 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
3040 PrevSpec, DiagID);
3041 break;
John McCall39439732011-04-09 22:50:59 +00003042 case tok::kw___unknown_anytype:
3043 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3044 PrevSpec, DiagID);
3045 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003046
3047 // class-specifier:
3048 case tok::kw_class:
3049 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003050 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003051 case tok::kw_union: {
3052 tok::TokenKind Kind = Tok.getKind();
3053 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003054
3055 // These are attributes following class specifiers.
3056 // To produce better diagnostic, we parse them when
3057 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003058 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003059 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003060 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003061
3062 // If there are attributes following class specifier,
3063 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003064 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003065 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003066 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003067 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003068 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003069 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003070
3071 // enum-specifier:
3072 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003073 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003074 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003075 continue;
3076
3077 // cv-qualifier:
3078 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003079 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003080 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003081 break;
3082 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003083 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003084 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003085 break;
3086 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003087 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003088 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003089 break;
3090
Douglas Gregor333489b2009-03-27 23:10:48 +00003091 // C++ typename-specifier:
3092 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003093 if (TryAnnotateTypeOrScopeToken()) {
3094 DS.SetTypeSpecError();
3095 goto DoneWithDeclSpec;
3096 }
3097 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003098 continue;
3099 break;
3100
Chris Lattnere387d9e2009-01-21 19:48:37 +00003101 // GNU typeof support.
3102 case tok::kw_typeof:
3103 ParseTypeofSpecifier(DS);
3104 continue;
3105
David Blaikie15a430a2011-12-04 05:04:18 +00003106 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003107 ParseDecltypeSpecifier(DS);
3108 continue;
3109
Alexis Hunt4a257072011-05-19 05:37:45 +00003110 case tok::kw___underlying_type:
3111 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003112 continue;
3113
3114 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003115 // C11 6.7.2.4/4:
3116 // If the _Atomic keyword is immediately followed by a left parenthesis,
3117 // it is interpreted as a type specifier (with a type name), not as a
3118 // type qualifier.
3119 if (NextToken().is(tok::l_paren)) {
3120 ParseAtomicSpecifier(DS);
3121 continue;
3122 }
3123 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3124 getLangOpts());
3125 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003126
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003127 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003128 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003129 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003130 goto DoneWithDeclSpec;
3131 case tok::kw___private:
3132 case tok::kw___global:
3133 case tok::kw___local:
3134 case tok::kw___constant:
3135 case tok::kw___read_only:
3136 case tok::kw___write_only:
3137 case tok::kw___read_write:
3138 ParseOpenCLQualifiers(DS);
3139 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003140
Steve Naroffcfdf6162008-06-05 00:02:44 +00003141 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003142 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003143 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3144 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003145 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003146 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003147
Douglas Gregor3a001f42010-11-19 17:10:50 +00003148 if (!ParseObjCProtocolQualifiers(DS))
3149 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3150 << FixItHint::CreateInsertion(Loc, "id")
3151 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003152
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003153 // Need to support trailing type qualifiers (e.g. "id<p> const").
3154 // If a type specifier follows, it will be diagnosed elsewhere.
3155 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003156 }
John McCall49bfce42009-08-03 20:12:06 +00003157 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003158 if (isInvalid) {
3159 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003160 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003161
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003162 if (DiagID == diag::ext_duplicate_declspec)
3163 Diag(Tok, DiagID)
3164 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3165 else
3166 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003167 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003168
Chris Lattner2e232092008-03-13 06:29:04 +00003169 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003170 if (DiagID != diag::err_bool_redeclaration)
3171 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003172
3173 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003174 }
3175}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003176
Chris Lattner70ae4912007-10-29 04:42:53 +00003177/// ParseStructDeclaration - Parse a struct declaration without the terminating
3178/// semicolon.
3179///
Chris Lattner90a26b02007-01-23 04:38:16 +00003180/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003181/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003182/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003183/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003184/// struct-declarator-list:
3185/// struct-declarator
3186/// struct-declarator-list ',' struct-declarator
3187/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3188/// struct-declarator:
3189/// declarator
3190/// [GNU] declarator attributes[opt]
3191/// declarator[opt] ':' constant-expression
3192/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3193///
Chris Lattnera12405b2008-04-10 06:46:29 +00003194void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003195ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003196
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003197 if (Tok.is(tok::kw___extension__)) {
3198 // __extension__ silences extension warnings in the subexpression.
3199 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003200 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003201 return ParseStructDeclaration(DS, Fields);
3202 }
Mike Stump11289f42009-09-09 15:08:12 +00003203
Steve Naroff97170802007-08-20 22:28:22 +00003204 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003205 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003206
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003207 // If there are no declarators, this is a free-standing declaration
3208 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003209 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003210 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3211 DS);
3212 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003213 return;
3214 }
3215
3216 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003217 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003218 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003219 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003220 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003221 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003222
Bill Wendling44426052012-12-20 19:22:21 +00003223 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003224 if (!FirstDeclarator)
3225 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003226
Steve Naroff97170802007-08-20 22:28:22 +00003227 /// struct-declarator: declarator
3228 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003229 if (Tok.isNot(tok::colon)) {
3230 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3231 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003232 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003233 }
Mike Stump11289f42009-09-09 15:08:12 +00003234
Chris Lattner76c72282007-10-09 17:33:22 +00003235 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00003236 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00003237 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003238 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00003239 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00003240 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003241 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003242 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003243
Steve Naroff97170802007-08-20 22:28:22 +00003244 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003245 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003246
John McCallcfefb6d2009-11-03 02:38:08 +00003247 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003248 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003249
Steve Naroff97170802007-08-20 22:28:22 +00003250 // If we don't have a comma, it is either the end of the list (a ';')
3251 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00003252 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00003253 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003254
Steve Naroff97170802007-08-20 22:28:22 +00003255 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00003256 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003257
John McCallcfefb6d2009-11-03 02:38:08 +00003258 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003259 }
Steve Naroff97170802007-08-20 22:28:22 +00003260}
3261
3262/// ParseStructUnionBody
3263/// struct-contents:
3264/// struct-declaration-list
3265/// [EXT] empty
3266/// [GNU] "struct-declaration-list" without terminatoring ';'
3267/// struct-declaration-list:
3268/// struct-declaration
3269/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003270/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003271///
Chris Lattner1300fb92007-01-23 23:42:53 +00003272void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003273 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003274 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3275 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003276 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003277
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003278 BalancedDelimiterTracker T(*this, tok::l_brace);
3279 if (T.consumeOpen())
3280 return;
Mike Stump11289f42009-09-09 15:08:12 +00003281
Douglas Gregor658b9552009-01-09 22:42:13 +00003282 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003283 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003284
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003285 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003286
Chris Lattner7b9ace62007-01-23 20:11:08 +00003287 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00003288 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003289 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003290
Chris Lattner736ed5d2007-06-09 05:59:07 +00003291 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003292 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003293 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003294 continue;
3295 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003296
Andy Gibbsc804e082013-04-03 09:46:04 +00003297 // Parse _Static_assert declaration.
3298 if (Tok.is(tok::kw__Static_assert)) {
3299 SourceLocation DeclEnd;
3300 ParseStaticAssertDeclaration(DeclEnd);
3301 continue;
3302 }
3303
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003304 if (Tok.is(tok::annot_pragma_pack)) {
3305 HandlePragmaPack();
3306 continue;
3307 }
3308
3309 if (Tok.is(tok::annot_pragma_align)) {
3310 HandlePragmaAlign();
3311 continue;
3312 }
3313
John McCallcfefb6d2009-11-03 02:38:08 +00003314 if (!Tok.is(tok::at)) {
3315 struct CFieldCallback : FieldCallback {
3316 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003317 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003318 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003319
John McCall48871652010-08-21 09:40:31 +00003320 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003321 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003322 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3323
Eli Friedman934dbbf2012-08-08 23:53:27 +00003324 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003325 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003326 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003327 FD.D.getDeclSpec().getSourceRange().getBegin(),
3328 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003329 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003330 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003331 }
John McCallcfefb6d2009-11-03 02:38:08 +00003332 } Callback(*this, TagDecl, FieldDecls);
3333
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003334 // Parse all the comma separated declarators.
3335 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003336 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003337 } else { // Handle @defs
3338 ConsumeToken();
3339 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3340 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00003341 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003342 continue;
3343 }
3344 ConsumeToken();
3345 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3346 if (!Tok.is(tok::identifier)) {
3347 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00003348 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003349 continue;
3350 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003351 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003352 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003353 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003354 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3355 ConsumeToken();
3356 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003357 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003358
Chris Lattner76c72282007-10-09 17:33:22 +00003359 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003360 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003361 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003362 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003363 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003364 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003365 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3366 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00003367 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00003368 // If we stopped at a ';', eat it.
3369 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003370 }
3371 }
Mike Stump11289f42009-09-09 15:08:12 +00003372
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003373 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003374
John McCall084e83d2011-03-24 11:26:52 +00003375 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003376 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003377 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003378
Douglas Gregor0be31a22010-07-02 17:43:08 +00003379 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003380 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003381 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003382 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003383 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003384 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3385 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003386}
3387
Chris Lattner3b561a32006-08-13 00:12:11 +00003388/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003389/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003390/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003391///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003392/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3393/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003394/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3395/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003396/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003397/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003398///
Richard Smith7d137e32012-03-23 03:33:32 +00003399/// [C++11] enum-head '{' enumerator-list[opt] '}'
3400/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003401///
Richard Smith7d137e32012-03-23 03:33:32 +00003402/// enum-head: [C++11]
3403/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3404/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3405/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003406///
Richard Smith7d137e32012-03-23 03:33:32 +00003407/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003408/// 'enum'
3409/// 'enum' 'class'
3410/// 'enum' 'struct'
3411///
Richard Smith7d137e32012-03-23 03:33:32 +00003412/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003413/// ':' type-specifier-seq
3414///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003415/// [C++] elaborated-type-specifier:
3416/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3417///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003418void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003419 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003420 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003421 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003422 if (Tok.is(tok::code_completion)) {
3423 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003424 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003425 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003426 }
John McCallcb432fa2011-07-06 05:58:41 +00003427
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003428 // If attributes exist after tag, parse them.
3429 ParsedAttributesWithRange attrs(AttrFactory);
3430 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003431 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003432
3433 // If declspecs exist after tag, parse them.
3434 while (Tok.is(tok::kw___declspec))
3435 ParseMicrosoftDeclSpec(attrs);
3436
Richard Smith0f8ee222012-01-10 01:33:14 +00003437 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003438 bool IsScopedUsingClassTag = false;
3439
John McCallbeae29a2012-06-23 22:30:04 +00003440 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003441 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3442 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3443 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003444 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003445 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003446
Bill Wendling44426052012-12-20 19:22:21 +00003447 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003448 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003449 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003450
3451 // They are allowed afterwards, though.
3452 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003453 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003454 while (Tok.is(tok::kw___declspec))
3455 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003456 }
Richard Smith7d137e32012-03-23 03:33:32 +00003457
John McCall6347b682012-05-07 06:16:58 +00003458 // C++11 [temp.explicit]p12:
3459 // The usual access controls do not apply to names used to specify
3460 // explicit instantiations.
3461 // We extend this to also cover explicit specializations. Note that
3462 // we don't suppress if this turns out to be an elaborated type
3463 // specifier.
3464 bool shouldDelayDiagsInTag =
3465 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3466 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3467 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003468
Richard Smithbfdb1082012-03-12 08:56:40 +00003469 // Enum definitions should not be parsed in a trailing-return-type.
3470 bool AllowDeclaration = DSC != DSC_trailing;
3471
3472 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003473 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003474 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003475
Abramo Bagnarad7548482010-05-19 21:37:53 +00003476 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003477 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003478 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3479 // if a fixed underlying type is allowed.
3480 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003481
3482 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003483 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003484 return;
3485
3486 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003487 Diag(Tok, diag::err_expected_ident);
3488 if (Tok.isNot(tok::l_brace)) {
3489 // Has no name and is not a definition.
3490 // Skip the rest of this declarator, up until the comma or semicolon.
3491 SkipUntil(tok::comma, true);
3492 return;
3493 }
3494 }
3495 }
Mike Stump11289f42009-09-09 15:08:12 +00003496
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003497 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003498 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003499 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003500 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003501
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003502 // Skip the rest of this declarator, up until the comma or semicolon.
3503 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003504 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003505 }
Mike Stump11289f42009-09-09 15:08:12 +00003506
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003507 // If an identifier is present, consume and remember it.
3508 IdentifierInfo *Name = 0;
3509 SourceLocation NameLoc;
3510 if (Tok.is(tok::identifier)) {
3511 Name = Tok.getIdentifierInfo();
3512 NameLoc = ConsumeToken();
3513 }
Mike Stump11289f42009-09-09 15:08:12 +00003514
Richard Smith0f8ee222012-01-10 01:33:14 +00003515 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003516 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3517 // declaration of a scoped enumeration.
3518 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003519 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003520 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003521 }
3522
John McCall6347b682012-05-07 06:16:58 +00003523 // Okay, end the suppression area. We'll decide whether to emit the
3524 // diagnostics in a second.
3525 if (shouldDelayDiagsInTag)
3526 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003527
Douglas Gregor0bf31402010-10-08 23:50:27 +00003528 TypeResult BaseType;
3529
Douglas Gregord1f69f62010-12-01 17:42:47 +00003530 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003531 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003532 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003533 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003534 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003535 // If we're in class scope, this can either be an enum declaration with
3536 // an underlying type, or a declaration of a bitfield member. We try to
3537 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003538 // (integer literal, sizeof); if it's still ambiguous, we then consider
3539 // anything that's a simple-type-specifier followed by '(' as an
3540 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003541 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003542 EnterExpressionEvaluationContext Unevaluated(Actions,
3543 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003544 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003545 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003546 // bit-field. This is the common case.
3547 if (TPR == TPResult::True())
3548 PossibleBitfield = true;
3549 // If the next token starts a type-specifier-seq, it may be either a
3550 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003551 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003552 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003553 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003554 GetLookAheadToken(2).getKind() == tok::semi) {
3555 // Consume the ':'.
3556 ConsumeToken();
3557 } else {
3558 // We have the start of a type-specifier-seq, so we have to perform
3559 // tentative parsing to determine whether we have an expression or a
3560 // type.
3561 TentativeParsingAction TPA(*this);
3562
3563 // Consume the ':'.
3564 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003565
3566 // If we see a type specifier followed by an open-brace, we have an
3567 // ambiguity between an underlying type and a C++11 braced
3568 // function-style cast. Resolve this by always treating it as an
3569 // underlying type.
3570 // FIXME: The standard is not entirely clear on how to disambiguate in
3571 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003572 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003573 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003574 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003575 // We'll parse this as a bitfield later.
3576 PossibleBitfield = true;
3577 TPA.Revert();
3578 } else {
3579 // We have a type-specifier-seq.
3580 TPA.Commit();
3581 }
3582 }
3583 } else {
3584 // Consume the ':'.
3585 ConsumeToken();
3586 }
3587
3588 if (!PossibleBitfield) {
3589 SourceRange Range;
3590 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003591
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003592 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003593 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003594 } else if (!getLangOpts().ObjC2) {
3595 if (getLangOpts().CPlusPlus)
3596 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3597 else
3598 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3599 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003600 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003601 }
3602
Richard Smith0f8ee222012-01-10 01:33:14 +00003603 // There are four options here. If we have 'friend enum foo;' then this is a
3604 // friend declaration, and cannot have an accompanying definition. If we have
3605 // 'enum foo;', then this is a forward declaration. If we have
3606 // 'enum foo {...' then this is a definition. Otherwise we have something
3607 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003608 //
3609 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3610 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3611 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3612 //
John McCallfaf5fb42010-08-26 23:41:50 +00003613 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003614 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003615 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003616 } else if (Tok.is(tok::l_brace)) {
3617 if (DS.isFriendSpecified()) {
3618 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3619 << SourceRange(DS.getFriendSpecLoc());
3620 ConsumeBrace();
3621 SkipUntil(tok::r_brace);
3622 TUK = Sema::TUK_Friend;
3623 } else {
3624 TUK = Sema::TUK_Definition;
3625 }
Richard Smith369b9f92012-06-25 21:37:02 +00003626 } else if (DSC != DSC_type_specifier &&
3627 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003628 (Tok.isAtStartOfLine() &&
3629 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003630 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3631 if (Tok.isNot(tok::semi)) {
3632 // A semicolon was missing after this declaration. Diagnose and recover.
3633 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3634 "enum");
3635 PP.EnterToken(Tok);
3636 Tok.setKind(tok::semi);
3637 }
John McCall6347b682012-05-07 06:16:58 +00003638 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003639 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003640 }
3641
3642 // If this is an elaborated type specifier, and we delayed
3643 // diagnostics before, just merge them into the current pool.
3644 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3645 diagsFromTag.redelay();
3646 }
Richard Smith7d137e32012-03-23 03:33:32 +00003647
3648 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003649 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003650 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003651 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003652 // Skip the rest of this declarator, up until the comma or semicolon.
3653 Diag(Tok, diag::err_enum_template);
3654 SkipUntil(tok::comma, true);
3655 return;
3656 }
3657
3658 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3659 // Enumerations can't be explicitly instantiated.
3660 DS.SetTypeSpecError();
3661 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3662 return;
3663 }
3664
3665 assert(TemplateInfo.TemplateParams && "no template parameters");
3666 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3667 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003668 }
Chad Rosierc1183952012-06-26 22:30:43 +00003669
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003670 if (TUK == Sema::TUK_Reference)
3671 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003672
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003673 if (!Name && TUK != Sema::TUK_Definition) {
3674 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003675
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003676 // Skip the rest of this declarator, up until the comma or semicolon.
3677 SkipUntil(tok::comma, true);
3678 return;
3679 }
Richard Smith7d137e32012-03-23 03:33:32 +00003680
Douglas Gregord6ab8742009-05-28 23:31:59 +00003681 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003682 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003683 const char *PrevSpec = 0;
3684 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003685 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003686 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003687 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003688 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003689 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003690
Douglas Gregorba41d012010-04-24 16:38:41 +00003691 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003692 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003693 // dependent tag.
3694 if (!Name) {
3695 DS.SetTypeSpecError();
3696 Diag(Tok, diag::err_expected_type_name_after_typename);
3697 return;
3698 }
Chad Rosierc1183952012-06-26 22:30:43 +00003699
Douglas Gregor0be31a22010-07-02 17:43:08 +00003700 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003701 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003702 NameLoc);
3703 if (Type.isInvalid()) {
3704 DS.SetTypeSpecError();
3705 return;
3706 }
Chad Rosierc1183952012-06-26 22:30:43 +00003707
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003708 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3709 NameLoc.isValid() ? NameLoc : StartLoc,
3710 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003711 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003712
Douglas Gregorba41d012010-04-24 16:38:41 +00003713 return;
3714 }
Mike Stump11289f42009-09-09 15:08:12 +00003715
John McCall48871652010-08-21 09:40:31 +00003716 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003717 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003718 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003719 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003720 ConsumeBrace();
3721 SkipUntil(tok::r_brace);
3722 }
Chad Rosierc1183952012-06-26 22:30:43 +00003723
Douglas Gregorba41d012010-04-24 16:38:41 +00003724 DS.SetTypeSpecError();
3725 return;
3726 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003727
Richard Smith369b9f92012-06-25 21:37:02 +00003728 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003729 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003730
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003731 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3732 NameLoc.isValid() ? NameLoc : StartLoc,
3733 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003734 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003735}
3736
Chris Lattnerc1915e22007-01-25 07:29:02 +00003737/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3738/// enumerator-list:
3739/// enumerator
3740/// enumerator-list ',' enumerator
3741/// enumerator:
3742/// enumeration-constant
3743/// enumeration-constant '=' constant-expression
3744/// enumeration-constant:
3745/// identifier
3746///
John McCall48871652010-08-21 09:40:31 +00003747void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003748 // Enter the scope of the enum body and start the definition.
3749 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003750 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003751
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003752 BalancedDelimiterTracker T(*this, tok::l_brace);
3753 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003754
Chris Lattner37256fb2007-08-27 17:24:30 +00003755 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003756 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003757 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003758
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003759 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003760
John McCall48871652010-08-21 09:40:31 +00003761 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003762
Chris Lattnerc1915e22007-01-25 07:29:02 +00003763 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003764 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003765 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3766 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003767
John McCall811a0f52010-10-22 23:36:17 +00003768 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003769 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003770 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003771 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003772 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003773
Chris Lattnerc1915e22007-01-25 07:29:02 +00003774 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003775 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003776 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003777
Chris Lattner76c72282007-10-09 17:33:22 +00003778 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003779 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003780 AssignedVal = ParseConstantExpression();
3781 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003782 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003783 }
Mike Stump11289f42009-09-09 15:08:12 +00003784
Chris Lattnerc1915e22007-01-25 07:29:02 +00003785 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003786 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3787 LastEnumConstDecl,
3788 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003789 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003790 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003791 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003792
Chris Lattner4ef40012007-06-11 01:28:17 +00003793 EnumConstantDecls.push_back(EnumConstDecl);
3794 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003795
Douglas Gregorce66d022010-09-07 14:51:08 +00003796 if (Tok.is(tok::identifier)) {
3797 // We're missing a comma between enumerators.
3798 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003799 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003800 << FixItHint::CreateInsertion(Loc, ", ");
3801 continue;
3802 }
Chad Rosierc1183952012-06-26 22:30:43 +00003803
Chris Lattner76c72282007-10-09 17:33:22 +00003804 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003805 break;
3806 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003807
Richard Smith5d164bc2011-10-15 05:09:34 +00003808 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003809 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003810 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3811 diag::ext_enumerator_list_comma_cxx :
3812 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003813 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003814 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003815 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3816 << FixItHint::CreateRemoval(CommaLoc);
3817 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003818 }
Mike Stump11289f42009-09-09 15:08:12 +00003819
Chris Lattnerc1915e22007-01-25 07:29:02 +00003820 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003821 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003822
Chris Lattnerc1915e22007-01-25 07:29:02 +00003823 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003824 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003825 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003826
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003827 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003828 EnumDecl, EnumConstantDecls,
3829 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003830 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003831
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003832 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003833 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3834 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003835
3836 // The next token must be valid after an enum definition. If not, a ';'
3837 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003838 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3839 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003840 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3841 // Push this token back into the preprocessor and change our current token
3842 // to ';' so that the rest of the code recovers as though there were an
3843 // ';' after the definition.
3844 PP.EnterToken(Tok);
3845 Tok.setKind(tok::semi);
3846 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003847}
Chris Lattner3b561a32006-08-13 00:12:11 +00003848
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003849/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003850/// start of a type-qualifier-list.
3851bool Parser::isTypeQualifier() const {
3852 switch (Tok.getKind()) {
3853 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003854
3855 // type-qualifier only in OpenCL
3856 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003857 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003858
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003859 // type-qualifier
3860 case tok::kw_const:
3861 case tok::kw_volatile:
3862 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003863 case tok::kw___private:
3864 case tok::kw___local:
3865 case tok::kw___global:
3866 case tok::kw___constant:
3867 case tok::kw___read_only:
3868 case tok::kw___read_write:
3869 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003870 return true;
3871 }
3872}
3873
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003874/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3875/// is definitely a type-specifier. Return false if it isn't part of a type
3876/// specifier or if we're not sure.
3877bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3878 switch (Tok.getKind()) {
3879 default: return false;
3880 // type-specifiers
3881 case tok::kw_short:
3882 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003883 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003884 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003885 case tok::kw_signed:
3886 case tok::kw_unsigned:
3887 case tok::kw__Complex:
3888 case tok::kw__Imaginary:
3889 case tok::kw_void:
3890 case tok::kw_char:
3891 case tok::kw_wchar_t:
3892 case tok::kw_char16_t:
3893 case tok::kw_char32_t:
3894 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003895 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003896 case tok::kw_float:
3897 case tok::kw_double:
3898 case tok::kw_bool:
3899 case tok::kw__Bool:
3900 case tok::kw__Decimal32:
3901 case tok::kw__Decimal64:
3902 case tok::kw__Decimal128:
3903 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003904
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003905 // OpenCL specific types:
3906 case tok::kw_image1d_t:
3907 case tok::kw_image1d_array_t:
3908 case tok::kw_image1d_buffer_t:
3909 case tok::kw_image2d_t:
3910 case tok::kw_image2d_array_t:
3911 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003912 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003913 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003914
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003915 // struct-or-union-specifier (C99) or class-specifier (C++)
3916 case tok::kw_class:
3917 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003918 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003919 case tok::kw_union:
3920 // enum-specifier
3921 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003922
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003923 // typedef-name
3924 case tok::annot_typename:
3925 return true;
3926 }
3927}
3928
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003929/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003930/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003931bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003932 switch (Tok.getKind()) {
3933 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003934
Chris Lattner020bab92009-01-04 23:41:41 +00003935 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003936 if (TryAltiVecVectorToken())
3937 return true;
3938 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003939 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003940 // Annotate typenames and C++ scope specifiers. If we get one, just
3941 // recurse to handle whatever we get.
3942 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003943 return true;
3944 if (Tok.is(tok::identifier))
3945 return false;
3946 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003947
Chris Lattner020bab92009-01-04 23:41:41 +00003948 case tok::coloncolon: // ::foo::bar
3949 if (NextToken().is(tok::kw_new) || // ::new
3950 NextToken().is(tok::kw_delete)) // ::delete
3951 return false;
3952
Chris Lattner020bab92009-01-04 23:41:41 +00003953 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003954 return true;
3955 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003956
Chris Lattnere37e2332006-08-15 04:50:22 +00003957 // GNU attributes support.
3958 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003959 // GNU typeof support.
3960 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003961
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003962 // type-specifiers
3963 case tok::kw_short:
3964 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003965 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003966 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003967 case tok::kw_signed:
3968 case tok::kw_unsigned:
3969 case tok::kw__Complex:
3970 case tok::kw__Imaginary:
3971 case tok::kw_void:
3972 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003973 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003974 case tok::kw_char16_t:
3975 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003976 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003977 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003978 case tok::kw_float:
3979 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003980 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003981 case tok::kw__Bool:
3982 case tok::kw__Decimal32:
3983 case tok::kw__Decimal64:
3984 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003985 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003986
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003987 // OpenCL specific types:
3988 case tok::kw_image1d_t:
3989 case tok::kw_image1d_array_t:
3990 case tok::kw_image1d_buffer_t:
3991 case tok::kw_image2d_t:
3992 case tok::kw_image2d_array_t:
3993 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003994 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003995 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003996
Chris Lattner861a2262008-04-13 18:59:07 +00003997 // struct-or-union-specifier (C99) or class-specifier (C++)
3998 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003999 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004000 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004001 case tok::kw_union:
4002 // enum-specifier
4003 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004004
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004005 // type-qualifier
4006 case tok::kw_const:
4007 case tok::kw_volatile:
4008 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004009
John McCallea0a39e2012-11-14 00:49:39 +00004010 // Debugger support.
4011 case tok::kw___unknown_anytype:
4012
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004013 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004014 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004015 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004016
Chris Lattner409bf7d2008-10-20 00:25:30 +00004017 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4018 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004019 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004020
Steve Naroff44ac7772008-12-25 14:16:32 +00004021 case tok::kw___cdecl:
4022 case tok::kw___stdcall:
4023 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004024 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004025 case tok::kw___w64:
4026 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004027 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004028 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004029 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004030
4031 case tok::kw___private:
4032 case tok::kw___local:
4033 case tok::kw___global:
4034 case tok::kw___constant:
4035 case tok::kw___read_only:
4036 case tok::kw___read_write:
4037 case tok::kw___write_only:
4038
Eli Friedman53339e02009-06-08 23:27:34 +00004039 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004040
4041 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004042 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00004043
Richard Smith8e1ac332013-03-28 01:55:44 +00004044 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004045 case tok::kw__Atomic:
4046 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004047 }
4048}
4049
Chris Lattneracd58a32006-08-06 17:24:14 +00004050/// isDeclarationSpecifier() - Return true if the current token is part of a
4051/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004052///
4053/// \param DisambiguatingWithExpression True to indicate that the purpose of
4054/// this check is to disambiguate between an expression and a declaration.
4055bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004056 switch (Tok.getKind()) {
4057 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004058
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004059 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004060 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004061
Chris Lattner020bab92009-01-04 23:41:41 +00004062 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004063 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004064 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004065 return false;
John Thompson22334602010-02-05 00:12:22 +00004066 if (TryAltiVecVectorToken())
4067 return true;
4068 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004069 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004070 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004071 // Annotate typenames and C++ scope specifiers. If we get one, just
4072 // recurse to handle whatever we get.
4073 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004074 return true;
4075 if (Tok.is(tok::identifier))
4076 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004077
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004078 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004079 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004080 // expression is permitted, then this is probably a class message send
4081 // missing the initial '['. In this case, we won't consider this to be
4082 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004083 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004084 isStartOfObjCClassMessageMissingOpenBracket())
4085 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004086
John McCall1f476a12010-02-26 08:45:28 +00004087 return isDeclarationSpecifier();
4088
Chris Lattner020bab92009-01-04 23:41:41 +00004089 case tok::coloncolon: // ::foo::bar
4090 if (NextToken().is(tok::kw_new) || // ::new
4091 NextToken().is(tok::kw_delete)) // ::delete
4092 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004093
Chris Lattner020bab92009-01-04 23:41:41 +00004094 // Annotate typenames and C++ scope specifiers. If we get one, just
4095 // recurse to handle whatever we get.
4096 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004097 return true;
4098 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004099
Chris Lattneracd58a32006-08-06 17:24:14 +00004100 // storage-class-specifier
4101 case tok::kw_typedef:
4102 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004103 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004104 case tok::kw_static:
4105 case tok::kw_auto:
4106 case tok::kw_register:
4107 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004108 case tok::kw_thread_local:
4109 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004110
Douglas Gregor26701a42011-09-09 02:06:17 +00004111 // Modules
4112 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004113
John McCallea0a39e2012-11-14 00:49:39 +00004114 // Debugger support
4115 case tok::kw___unknown_anytype:
4116
Chris Lattneracd58a32006-08-06 17:24:14 +00004117 // type-specifiers
4118 case tok::kw_short:
4119 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004120 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004121 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004122 case tok::kw_signed:
4123 case tok::kw_unsigned:
4124 case tok::kw__Complex:
4125 case tok::kw__Imaginary:
4126 case tok::kw_void:
4127 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004128 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004129 case tok::kw_char16_t:
4130 case tok::kw_char32_t:
4131
Chris Lattneracd58a32006-08-06 17:24:14 +00004132 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004133 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004134 case tok::kw_float:
4135 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004136 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004137 case tok::kw__Bool:
4138 case tok::kw__Decimal32:
4139 case tok::kw__Decimal64:
4140 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004141 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004142
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004143 // OpenCL specific types:
4144 case tok::kw_image1d_t:
4145 case tok::kw_image1d_array_t:
4146 case tok::kw_image1d_buffer_t:
4147 case tok::kw_image2d_t:
4148 case tok::kw_image2d_array_t:
4149 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004150 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004151 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004152
Chris Lattner861a2262008-04-13 18:59:07 +00004153 // struct-or-union-specifier (C99) or class-specifier (C++)
4154 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004155 case tok::kw_struct:
4156 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004157 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004158 // enum-specifier
4159 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004160
Chris Lattneracd58a32006-08-06 17:24:14 +00004161 // type-qualifier
4162 case tok::kw_const:
4163 case tok::kw_volatile:
4164 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004165
Chris Lattneracd58a32006-08-06 17:24:14 +00004166 // function-specifier
4167 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004168 case tok::kw_virtual:
4169 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004170 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004171
Richard Smith1dba27c2013-01-29 09:02:09 +00004172 // alignment-specifier
4173 case tok::kw__Alignas:
4174
Richard Smithd16fe122012-10-25 00:00:53 +00004175 // friend keyword.
4176 case tok::kw_friend:
4177
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004178 // static_assert-declaration
4179 case tok::kw__Static_assert:
4180
Chris Lattner599e47e2007-08-09 17:01:07 +00004181 // GNU typeof support.
4182 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004183
Chris Lattner599e47e2007-08-09 17:01:07 +00004184 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004185 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004186
Richard Smithd16fe122012-10-25 00:00:53 +00004187 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004188 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004189 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004190
Richard Smith8e1ac332013-03-28 01:55:44 +00004191 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004192 case tok::kw__Atomic:
4193 return true;
4194
Chris Lattner8b2ec162008-07-26 03:38:44 +00004195 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4196 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004197 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004198
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004199 // typedef-name
4200 case tok::annot_typename:
4201 return !DisambiguatingWithExpression ||
4202 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004203
Steve Narofff192fab2009-01-06 19:34:12 +00004204 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004205 case tok::kw___cdecl:
4206 case tok::kw___stdcall:
4207 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004208 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004209 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004210 case tok::kw___sptr:
4211 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004212 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004213 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004214 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004215 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004216 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004217
4218 case tok::kw___private:
4219 case tok::kw___local:
4220 case tok::kw___global:
4221 case tok::kw___constant:
4222 case tok::kw___read_only:
4223 case tok::kw___read_write:
4224 case tok::kw___write_only:
4225
Eli Friedman53339e02009-06-08 23:27:34 +00004226 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004227 }
4228}
4229
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004230bool Parser::isConstructorDeclarator() {
4231 TentativeParsingAction TPA(*this);
4232
4233 // Parse the C++ scope specifier.
4234 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004235 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004236 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004237 TPA.Revert();
4238 return false;
4239 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004240
4241 // Parse the constructor name.
4242 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4243 // We already know that we have a constructor name; just consume
4244 // the token.
4245 ConsumeToken();
4246 } else {
4247 TPA.Revert();
4248 return false;
4249 }
4250
Richard Smith43f340f2012-03-27 23:05:05 +00004251 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004252 if (Tok.isNot(tok::l_paren)) {
4253 TPA.Revert();
4254 return false;
4255 }
4256 ConsumeParen();
4257
Richard Smith43f340f2012-03-27 23:05:05 +00004258 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4259 // that we have a constructor.
4260 if (Tok.is(tok::r_paren) ||
4261 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004262 TPA.Revert();
4263 return true;
4264 }
4265
Richard Smithf2163662013-09-06 00:12:20 +00004266 // A C++11 attribute here signals that we have a constructor, and is an
4267 // attribute on the first constructor parameter.
4268 if (getLangOpts().CPlusPlus11 &&
4269 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4270 /*OuterMightBeMessageSend*/ true)) {
4271 TPA.Revert();
4272 return true;
4273 }
4274
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004275 // If we need to, enter the specified scope.
4276 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004277 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004278 DeclScopeObj.EnterDeclaratorScope();
4279
Francois Pichet79f3a872011-01-31 04:54:32 +00004280 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004281 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004282 MaybeParseMicrosoftAttributes(Attrs);
4283
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004284 // Check whether the next token(s) are part of a declaration
4285 // specifier, in which case we have the start of a parameter and,
4286 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004287 bool IsConstructor = false;
4288 if (isDeclarationSpecifier())
4289 IsConstructor = true;
4290 else if (Tok.is(tok::identifier) ||
4291 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4292 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4293 // This might be a parenthesized member name, but is more likely to
4294 // be a constructor declaration with an invalid argument type. Keep
4295 // looking.
4296 if (Tok.is(tok::annot_cxxscope))
4297 ConsumeToken();
4298 ConsumeToken();
4299
4300 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004301 // which must have one of the following syntactic forms (see the
4302 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004303 switch (Tok.getKind()) {
4304 case tok::l_paren:
4305 // C(X ( int));
4306 case tok::l_square:
4307 // C(X [ 5]);
4308 // C(X [ [attribute]]);
4309 case tok::coloncolon:
4310 // C(X :: Y);
4311 // C(X :: *p);
4312 case tok::r_paren:
4313 // C(X )
4314 // Assume this isn't a constructor, rather than assuming it's a
4315 // constructor with an unnamed parameter of an ill-formed type.
4316 break;
4317
4318 default:
4319 IsConstructor = true;
4320 break;
4321 }
4322 }
4323
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004324 TPA.Revert();
4325 return IsConstructor;
4326}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004327
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004328/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004329/// type-qualifier-list: [C99 6.7.5]
4330/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004331/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004332/// [ only if VendorAttributesAllowed=true ]
4333/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004334/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004335/// [ only if VendorAttributesAllowed=true ]
4336/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004337/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004338/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004339///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004340void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4341 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004342 bool CXX11AttributesAllowed,
4343 bool AtomicAllowed) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004344 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004345 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004346 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004347 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004348 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004349 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004350
4351 SourceLocation EndLoc;
4352
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004353 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004354 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004355 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004356 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004357 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004358
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004359 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004360 case tok::code_completion:
4361 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004362 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004363
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004364 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004365 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004366 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004367 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004368 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004369 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004370 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004371 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004372 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004373 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004374 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004375 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004376 case tok::kw__Atomic:
4377 if (!AtomicAllowed)
4378 goto DoneWithTypeQuals;
4379 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4380 getLangOpts());
4381 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004382
4383 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004384 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004385 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004386 goto DoneWithTypeQuals;
4387 case tok::kw___private:
4388 case tok::kw___global:
4389 case tok::kw___local:
4390 case tok::kw___constant:
4391 case tok::kw___read_only:
4392 case tok::kw___write_only:
4393 case tok::kw___read_write:
4394 ParseOpenCLQualifiers(DS);
4395 break;
4396
Aaron Ballman317a77f2013-05-22 23:25:32 +00004397 case tok::kw___sptr:
4398 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004399 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004400 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004401 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004402 case tok::kw___cdecl:
4403 case tok::kw___stdcall:
4404 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004405 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004406 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004407 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004408 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004409 continue;
4410 }
4411 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004412 case tok::kw___pascal:
4413 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004414 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004415 continue;
4416 }
4417 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004418 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004419 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004420 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004421 continue; // do *not* consume the next token!
4422 }
4423 // otherwise, FALL THROUGH!
4424 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004425 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004426 // If this is not a type-qualifier token, we're done reading type
4427 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004428 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004429 if (EndLoc.isValid())
4430 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004431 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004432 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004433
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004434 // If the specifier combination wasn't legal, issue a diagnostic.
4435 if (isInvalid) {
4436 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004437 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004438 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004439 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004440 }
4441}
4442
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004443
4444/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4445///
4446void Parser::ParseDeclarator(Declarator &D) {
4447 /// This implements the 'declarator' production in the C grammar, then checks
4448 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004449 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004450}
4451
Richard Smith0efa75c2012-03-29 01:16:42 +00004452static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4453 if (Kind == tok::star || Kind == tok::caret)
4454 return true;
4455
4456 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4457 if (!Lang.CPlusPlus)
4458 return false;
4459
4460 return Kind == tok::amp || Kind == tok::ampamp;
4461}
4462
Sebastian Redlbd150f42008-11-21 19:14:01 +00004463/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4464/// is parsed by the function passed to it. Pass null, and the direct-declarator
4465/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004466/// ptr-operator production.
4467///
Richard Smith09f76ee2011-10-19 21:33:05 +00004468/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004469/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4470/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004471///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004472/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4473/// [C] pointer[opt] direct-declarator
4474/// [C++] direct-declarator
4475/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004476///
4477/// pointer: [C99 6.7.5]
4478/// '*' type-qualifier-list[opt]
4479/// '*' type-qualifier-list[opt] pointer
4480///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004481/// ptr-operator:
4482/// '*' cv-qualifier-seq[opt]
4483/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004484/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004485/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004486/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004487/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004488void Parser::ParseDeclaratorInternal(Declarator &D,
4489 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004490 if (Diags.hasAllExtensionsSilenced())
4491 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004492
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004493 // C++ member pointers start with a '::' or a nested-name.
4494 // Member pointers get special handling, since there's no place for the
4495 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004496 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004497 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4498 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004499 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4500 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004501 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004502 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004503
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004504 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004505 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004506 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004507 if (D.mayHaveIdentifier())
4508 D.getCXXScopeSpec() = SS;
4509 else
4510 AnnotateScopeToken(SS, true);
4511
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004512 if (DirectDeclParser)
4513 (this->*DirectDeclParser)(D);
4514 return;
4515 }
4516
4517 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004518 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004519 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004520 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004521 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004522
4523 // Recurse to parse whatever is left.
4524 ParseDeclaratorInternal(D, DirectDeclParser);
4525
4526 // Sema will have to catch (syntactically invalid) pointers into global
4527 // scope. It has to catch pointers into namespace scope anyway.
4528 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004529 Loc),
4530 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004531 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004532 return;
4533 }
4534 }
4535
4536 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004537 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004538 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004539 if (DirectDeclParser)
4540 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004541 return;
4542 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004543
Sebastian Redled0f3b02009-03-15 22:02:01 +00004544 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4545 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004546 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004547 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004548
Chris Lattner9eac9312009-03-27 04:18:06 +00004549 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004550 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004551 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004552
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004553 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004554 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004555 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004556
Bill Wendling3708c182007-05-27 10:15:43 +00004557 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004558 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004559 if (Kind == tok::star)
4560 // Remember that we parsed a pointer type, and remember the type-quals.
4561 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004562 DS.getConstSpecLoc(),
4563 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004564 DS.getRestrictSpecLoc()),
4565 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004566 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004567 else
4568 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004569 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004570 Loc),
4571 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004572 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004573 } else {
4574 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004575 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004576
Sebastian Redl3b27be62009-03-23 00:00:23 +00004577 // Complain about rvalue references in C++03, but then go on and build
4578 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004579 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004580 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004581 diag::warn_cxx98_compat_rvalue_reference :
4582 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004583
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004584 // GNU-style and C++11 attributes are allowed here, as is restrict.
4585 ParseTypeQualifierListOpt(DS);
4586 D.ExtendWithDeclSpec(DS);
4587
Bill Wendling93efb222007-06-02 23:28:54 +00004588 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4589 // cv-qualifiers are introduced through the use of a typedef or of a
4590 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004591 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4592 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4593 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004594 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004595 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4596 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004597 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004598 // 'restrict' is permitted as an extension.
4599 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4600 Diag(DS.getAtomicSpecLoc(),
4601 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004602 }
Bill Wendling3708c182007-05-27 10:15:43 +00004603
4604 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004605 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004606
Douglas Gregor66583c52008-11-03 15:51:28 +00004607 if (D.getNumTypeObjects() > 0) {
4608 // C++ [dcl.ref]p4: There shall be no references to references.
4609 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4610 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004611 if (const IdentifierInfo *II = D.getIdentifier())
4612 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4613 << II;
4614 else
4615 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4616 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004617
Sebastian Redlbd150f42008-11-21 19:14:01 +00004618 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004619 // can go ahead and build the (technically ill-formed)
4620 // declarator: reference collapsing will take care of it.
4621 }
4622 }
4623
Richard Smith8e1ac332013-03-28 01:55:44 +00004624 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004625 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004626 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004627 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004628 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004629 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004630}
4631
Richard Smith0efa75c2012-03-29 01:16:42 +00004632static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4633 SourceLocation EllipsisLoc) {
4634 if (EllipsisLoc.isValid()) {
4635 FixItHint Insertion;
4636 if (!D.getEllipsisLoc().isValid()) {
4637 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4638 D.setEllipsisLoc(EllipsisLoc);
4639 }
4640 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4641 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4642 }
4643}
4644
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004645/// ParseDirectDeclarator
4646/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004647/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004648/// '(' declarator ')'
4649/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004650/// [C90] direct-declarator '[' constant-expression[opt] ']'
4651/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4652/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4653/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4654/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004655/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4656/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004657/// direct-declarator '(' parameter-type-list ')'
4658/// direct-declarator '(' identifier-list[opt] ')'
4659/// [GNU] direct-declarator '(' parameter-forward-declarations
4660/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004661/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4662/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004663/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4664/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4665/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004666/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004667/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004668///
4669/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004670/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004671/// '::'[opt] nested-name-specifier[opt] type-name
4672///
4673/// id-expression: [C++ 5.1]
4674/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004675/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004676///
4677/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004678/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004679/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004680/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004681/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004682/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004683///
Richard Smith1453e312012-03-27 01:42:32 +00004684/// Note, any additional constructs added here may need corresponding changes
4685/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004686void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004687 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004688
David Blaikiebbafb8a2012-03-11 07:00:24 +00004689 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004690 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004691 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004692 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4693 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004694 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004695 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004696 }
4697
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004698 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004699 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004700 // Change the declaration context for name lookup, until this function
4701 // is exited (and the declarator has been parsed).
4702 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004703 }
4704
Douglas Gregor27b4c162010-12-23 22:44:42 +00004705 // C++0x [dcl.fct]p14:
4706 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004707 // of a parameter-declaration-clause without a preceding comma. In
4708 // this case, the ellipsis is parsed as part of the
4709 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004710 // parameter pack that has not been expanded; otherwise, it is parsed
4711 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004712 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004713 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004714 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004715 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004716 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004717 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004718 !Actions.containsUnexpandedParameterPacks(D))) {
4719 SourceLocation EllipsisLoc = ConsumeToken();
4720 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4721 // The ellipsis was put in the wrong place. Recover, and explain to
4722 // the user what they should have done.
4723 ParseDeclarator(D);
4724 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4725 return;
4726 } else
4727 D.setEllipsisLoc(EllipsisLoc);
4728
4729 // The ellipsis can't be followed by a parenthesized declarator. We
4730 // check for that in ParseParenDeclarator, after we have disambiguated
4731 // the l_paren token.
4732 }
4733
Douglas Gregor7861a802009-11-03 01:35:08 +00004734 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4735 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4736 // We found something that indicates the start of an unqualified-id.
4737 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004738 bool AllowConstructorName;
4739 if (D.getDeclSpec().hasTypeSpecifier())
4740 AllowConstructorName = false;
4741 else if (D.getCXXScopeSpec().isSet())
4742 AllowConstructorName =
4743 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004744 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004745 else
4746 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4747
Abramo Bagnara7945c982012-01-27 09:46:47 +00004748 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004749 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4750 /*EnteringContext=*/true,
4751 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004752 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004753 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004754 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004755 D.getName()) ||
4756 // Once we're past the identifier, if the scope was bad, mark the
4757 // whole declarator bad.
4758 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004759 D.SetIdentifier(0, Tok.getLocation());
4760 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004761 } else {
4762 // Parsed the unqualified-id; update range information and move along.
4763 if (D.getSourceRange().getBegin().isInvalid())
4764 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4765 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004766 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004767 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004768 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004769 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004770 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004771 "There's a C++-specific check for tok::identifier above");
4772 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4773 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4774 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004775 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004776 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004777 // A virt-specifier isn't treated as an identifier if it appears after a
4778 // trailing-return-type.
4779 if (D.getContext() != Declarator::TrailingReturnContext ||
4780 !isCXX11VirtSpecifier(Tok)) {
4781 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4782 << FixItHint::CreateRemoval(Tok.getLocation());
4783 D.SetIdentifier(0, Tok.getLocation());
4784 ConsumeToken();
4785 goto PastIdentifier;
4786 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004787 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004788
Douglas Gregor7861a802009-11-03 01:35:08 +00004789 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004790 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004791 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004792 // Example: 'char (*X)' or 'int (*XX)(void)'
4793 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004794
4795 // If the declarator was parenthesized, we entered the declarator
4796 // scope when parsing the parenthesized declarator, then exited
4797 // the scope already. Re-enter the scope, if we need to.
4798 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004799 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004800 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004801 if (!D.isInvalidType() &&
4802 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004803 // Change the declaration context for name lookup, until this function
4804 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004805 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004806 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004807 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004808 // This could be something simple like "int" (in which case the declarator
4809 // portion is empty), if an abstract-declarator is allowed.
4810 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004811
4812 // The grammar for abstract-pack-declarator does not allow grouping parens.
4813 // FIXME: Revisit this once core issue 1488 is resolved.
4814 if (D.hasEllipsis() && D.hasGroupingParens())
4815 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4816 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004817 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004818 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004819 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004820 if (D.getContext() == Declarator::MemberContext)
4821 Diag(Tok, diag::err_expected_member_name_or_semi)
4822 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004823 else if (getLangOpts().CPlusPlus) {
4824 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4825 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004826 else {
4827 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4828 if (Tok.isAtStartOfLine() && Loc.isValid())
4829 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4830 << getLangOpts().CPlusPlus;
4831 else
4832 Diag(Tok, diag::err_expected_unqualified_id)
4833 << getLangOpts().CPlusPlus;
4834 }
Richard Trieu9c672672013-01-26 02:31:38 +00004835 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00004836 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004837 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004838 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004839 }
Mike Stump11289f42009-09-09 15:08:12 +00004840
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004841 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004842 assert(D.isPastIdentifier() &&
4843 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004844
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004845 // Don't parse attributes unless we have parsed an unparenthesized name.
4846 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004847 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004848
Chris Lattneracd58a32006-08-06 17:24:14 +00004849 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004850 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004851 // Enter function-declaration scope, limiting any declarators to the
4852 // function prototype scope, including parameter declarators.
4853 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004854 Scope::FunctionPrototypeScope|Scope::DeclScope|
4855 (D.isFunctionDeclaratorAFunctionDeclaration()
4856 ? Scope::FunctionDeclarationScope : 0));
4857
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004858 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4859 // In such a case, check if we actually have a function declarator; if it
4860 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004861 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004862 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4863 // The name of the declarator, if any, is tentatively declared within
4864 // a possible direct initializer.
4865 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4866 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4867 TentativelyDeclaredIdentifiers.pop_back();
4868 if (!IsFunctionDecl)
4869 break;
4870 }
John McCall084e83d2011-03-24 11:26:52 +00004871 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004872 BalancedDelimiterTracker T(*this, tok::l_paren);
4873 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004874 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004875 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004876 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004877 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004878 } else {
4879 break;
4880 }
4881 }
Chad Rosierc1183952012-06-26 22:30:43 +00004882}
Chris Lattneracd58a32006-08-06 17:24:14 +00004883
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004884/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4885/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004886/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004887/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4888///
4889/// direct-declarator:
4890/// '(' declarator ')'
4891/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004892/// direct-declarator '(' parameter-type-list ')'
4893/// direct-declarator '(' identifier-list[opt] ')'
4894/// [GNU] direct-declarator '(' parameter-forward-declarations
4895/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004896///
4897void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004898 BalancedDelimiterTracker T(*this, tok::l_paren);
4899 T.consumeOpen();
4900
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004901 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004902
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004903 // Eat any attributes before we look at whether this is a grouping or function
4904 // declarator paren. If this is a grouping paren, the attribute applies to
4905 // the type being built up, for example:
4906 // int (__attribute__(()) *x)(long y)
4907 // If this ends up not being a grouping paren, the attribute applies to the
4908 // first argument, for example:
4909 // int (__attribute__(()) int x)
4910 // In either case, we need to eat any attributes to be able to determine what
4911 // sort of paren this is.
4912 //
John McCall084e83d2011-03-24 11:26:52 +00004913 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004914 bool RequiresArg = false;
4915 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004916 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004917
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004918 // We require that the argument list (if this is a non-grouping paren) be
4919 // present even if the attribute list was empty.
4920 RequiresArg = true;
4921 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004922
Steve Naroff44ac7772008-12-25 14:16:32 +00004923 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004924 ParseMicrosoftTypeAttributes(attrs);
4925
Dawn Perchik335e16b2010-09-03 01:29:35 +00004926 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004927 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004928 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004929
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004930 // If we haven't past the identifier yet (or where the identifier would be
4931 // stored, if this is an abstract declarator), then this is probably just
4932 // grouping parens. However, if this could be an abstract-declarator, then
4933 // this could also be the start of function arguments (consider 'void()').
4934 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004935
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004936 if (!D.mayOmitIdentifier()) {
4937 // If this can't be an abstract-declarator, this *must* be a grouping
4938 // paren, because we haven't seen the identifier yet.
4939 isGrouping = true;
4940 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004941 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4942 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004943 isDeclarationSpecifier() || // 'int(int)' is a function.
4944 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004945 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4946 // considered to be a type, not a K&R identifier-list.
4947 isGrouping = false;
4948 } else {
4949 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4950 isGrouping = true;
4951 }
Mike Stump11289f42009-09-09 15:08:12 +00004952
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004953 // If this is a grouping paren, handle:
4954 // direct-declarator: '(' declarator ')'
4955 // direct-declarator: '(' attributes declarator ')'
4956 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004957 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4958 D.setEllipsisLoc(SourceLocation());
4959
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004960 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004961 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004962 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004963 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004964 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004965 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004966 T.getCloseLocation()),
4967 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004968
4969 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004970
4971 // An ellipsis cannot be placed outside parentheses.
4972 if (EllipsisLoc.isValid())
4973 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4974
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004975 return;
4976 }
Mike Stump11289f42009-09-09 15:08:12 +00004977
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004978 // Okay, if this wasn't a grouping paren, it must be the start of a function
4979 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004980 // identifier (and remember where it would have been), then call into
4981 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004982 D.SetIdentifier(0, Tok.getLocation());
4983
David Blaikie15a430a2011-12-04 05:04:18 +00004984 // Enter function-declaration scope, limiting any declarators to the
4985 // function prototype scope, including parameter declarators.
4986 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004987 Scope::FunctionPrototypeScope | Scope::DeclScope |
4988 (D.isFunctionDeclaratorAFunctionDeclaration()
4989 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00004990 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004991 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004992}
4993
4994/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4995/// declarator D up to a paren, which indicates that we are parsing function
4996/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004997///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004998/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4999/// immediately after the open paren - they should be considered to be the
5000/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005001///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005002/// If RequiresArg is true, then the first argument of the function is required
5003/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005004///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005005/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5006/// (C++11) ref-qualifier[opt], exception-specification[opt],
5007/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5008///
5009/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00005010/// dynamic-exception-specification
5011/// noexcept-specification
5012///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005013void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005014 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005015 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00005016 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005017 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00005018 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00005019 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00005020 // lparen is already consumed!
5021 assert(D.isPastIdentifier() && "Should not call before identifier!");
5022
5023 // This should be true when the function has typed arguments.
5024 // Otherwise, it is treated as a K&R-style function.
5025 bool HasProto = false;
5026 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005027 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005028 // Remember where we see an ellipsis, if any.
5029 SourceLocation EllipsisLoc;
5030
5031 DeclSpec DS(AttrFactory);
5032 bool RefQualifierIsLValueRef = true;
5033 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00005034 SourceLocation ConstQualifierLoc;
5035 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005036 ExceptionSpecificationType ESpecType = EST_None;
5037 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005038 SmallVector<ParsedType, 2> DynamicExceptions;
5039 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005040 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005041 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00005042 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005043
James Molloy6f8780b2012-02-29 10:24:19 +00005044 Actions.ActOnStartFunctionDeclarator();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005045 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5046 EndLoc is the end location for the function declarator.
5047 They differ for trailing return types. */
5048 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005049 SourceLocation LParenLoc, RParenLoc;
5050 LParenLoc = Tracker.getOpenLocation();
5051 StartLoc = LParenLoc;
5052
Douglas Gregor9e66af42011-07-05 16:44:18 +00005053 if (isFunctionDeclaratorIdentifierList()) {
5054 if (RequiresArg)
5055 Diag(Tok, diag::err_argument_required_after_attribute);
5056
5057 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5058
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005059 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005060 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005061 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005062 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005063 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005064 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005065 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5066 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005067 else if (RequiresArg)
5068 Diag(Tok, diag::err_argument_required_after_attribute);
5069
David Blaikiebbafb8a2012-03-11 07:00:24 +00005070 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005071
5072 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005073 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005074 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005075 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005076 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005077
David Blaikiebbafb8a2012-03-11 07:00:24 +00005078 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005079 // FIXME: Accept these components in any order, and produce fixits to
5080 // correct the order if the user gets it wrong. Ideally we should deal
5081 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005082
5083 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005084 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5085 /*CXX11AttributesAllowed*/ false,
5086 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005087 if (!DS.getSourceRange().getEnd().isInvalid()) {
5088 EndLoc = DS.getSourceRange().getEnd();
5089 ConstQualifierLoc = DS.getConstSpecLoc();
5090 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5091 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005092
5093 // Parse ref-qualifier[opt].
5094 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005095 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005096 diag::warn_cxx98_compat_ref_qualifier :
5097 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005098
Douglas Gregor9e66af42011-07-05 16:44:18 +00005099 RefQualifierIsLValueRef = Tok.is(tok::amp);
5100 RefQualifierLoc = ConsumeToken();
5101 EndLoc = RefQualifierLoc;
5102 }
5103
Douglas Gregor3024f072012-04-16 07:05:22 +00005104 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005105 // If a declaration declares a member function or member function
5106 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005107 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005108 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005109 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005110 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005111 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005112 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005113 (D.getContext() == Declarator::MemberContext
5114 ? !D.getDeclSpec().isFriendSpecified()
5115 : D.getContext() == Declarator::FileContext &&
5116 D.getCXXScopeSpec().isValid() &&
5117 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005118 Sema::CXXThisScopeRAII ThisScope(Actions,
5119 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005120 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005121 (D.getDeclSpec().isConstexprSpecified() &&
5122 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005123 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005124 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005125
Douglas Gregor9e66af42011-07-05 16:44:18 +00005126 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005127 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005128 DynamicExceptions,
5129 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005130 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005131 if (ESpecType != EST_None)
5132 EndLoc = ESpecRange.getEnd();
5133
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005134 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5135 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005136 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005137
Douglas Gregor9e66af42011-07-05 16:44:18 +00005138 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005139 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005140 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005141 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005142 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5143 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005144 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005145 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005146 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005147 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005148 }
5149 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005150 }
5151
5152 // Remember that we parsed a function type, and remember the attributes.
5153 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005154 IsAmbiguous,
5155 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005156 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005157 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005158 DS.getTypeQualifiers(),
5159 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005160 RefQualifierLoc, ConstQualifierLoc,
5161 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005162 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005163 ESpecType, ESpecRange.getBegin(),
5164 DynamicExceptions.data(),
5165 DynamicExceptionRanges.data(),
5166 DynamicExceptions.size(),
5167 NoexceptExpr.isUsable() ?
5168 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005169 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005170 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005171 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005172
5173 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005174}
5175
5176/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5177/// identifier list form for a K&R-style function: void foo(a,b,c)
5178///
5179/// Note that identifier-lists are only allowed for normal declarators, not for
5180/// abstract-declarators.
5181bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005182 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005183 && Tok.is(tok::identifier)
5184 && !TryAltiVecVectorToken()
5185 // K&R identifier lists can't have typedefs as identifiers, per C99
5186 // 6.7.5.3p11.
5187 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5188 // Identifier lists follow a really simple grammar: the identifiers can
5189 // be followed *only* by a ", identifier" or ")". However, K&R
5190 // identifier lists are really rare in the brave new modern world, and
5191 // it is very common for someone to typo a type in a non-K&R style
5192 // list. If we are presented with something like: "void foo(intptr x,
5193 // float y)", we don't want to start parsing the function declarator as
5194 // though it is a K&R style declarator just because intptr is an
5195 // invalid type.
5196 //
5197 // To handle this, we check to see if the token after the first
5198 // identifier is a "," or ")". Only then do we parse it as an
5199 // identifier list.
5200 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5201}
5202
5203/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5204/// we found a K&R-style identifier list instead of a typed parameter list.
5205///
5206/// After returning, ParamInfo will hold the parsed parameters.
5207///
5208/// identifier-list: [C99 6.7.5]
5209/// identifier
5210/// identifier-list ',' identifier
5211///
5212void Parser::ParseFunctionDeclaratorIdentifierList(
5213 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005214 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005215 // If there was no identifier specified for the declarator, either we are in
5216 // an abstract-declarator, or we are in a parameter declarator which was found
5217 // to be abstract. In abstract-declarators, identifier lists are not valid:
5218 // diagnose this.
5219 if (!D.getIdentifier())
5220 Diag(Tok, diag::ext_ident_list_in_param);
5221
5222 // Maintain an efficient lookup of params we have seen so far.
5223 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5224
5225 while (1) {
5226 // If this isn't an identifier, report the error and skip until ')'.
5227 if (Tok.isNot(tok::identifier)) {
5228 Diag(Tok, diag::err_expected_ident);
5229 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
5230 // Forget we parsed anything.
5231 ParamInfo.clear();
5232 return;
5233 }
5234
5235 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5236
5237 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5238 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5239 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5240
5241 // Verify that the argument identifier has not already been mentioned.
5242 if (!ParamsSoFar.insert(ParmII)) {
5243 Diag(Tok, diag::err_param_redefinition) << ParmII;
5244 } else {
5245 // Remember this identifier in ParamInfo.
5246 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5247 Tok.getLocation(),
5248 0));
5249 }
5250
5251 // Eat the identifier.
5252 ConsumeToken();
5253
5254 // The list continues if we see a comma.
5255 if (Tok.isNot(tok::comma))
5256 break;
5257 ConsumeToken();
5258 }
5259}
5260
5261/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5262/// after the opening parenthesis. This function will not parse a K&R-style
5263/// identifier list.
5264///
Richard Smith2620cd92012-04-11 04:01:28 +00005265/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5266/// caller parsed those arguments immediately after the open paren - they should
5267/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005268///
5269/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5270/// be the location of the ellipsis, if any was parsed.
5271///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005272/// parameter-type-list: [C99 6.7.5]
5273/// parameter-list
5274/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005275/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005276///
5277/// parameter-list: [C99 6.7.5]
5278/// parameter-declaration
5279/// parameter-list ',' parameter-declaration
5280///
5281/// parameter-declaration: [C99 6.7.5]
5282/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005283/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005284/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005285/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005286/// declaration-specifiers abstract-declarator[opt]
5287/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005288/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005289/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005290/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005291///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005292void Parser::ParseParameterDeclarationClause(
5293 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005294 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005295 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005296 SourceLocation &EllipsisLoc) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005297 while (1) {
5298 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00005299 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5300 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00005301 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00005302 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00005303 }
Mike Stump11289f42009-09-09 15:08:12 +00005304
Chris Lattner371ed4e2008-04-06 06:57:35 +00005305 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005306 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005307 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005308
Richard Smith2620cd92012-04-11 04:01:28 +00005309 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005310 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005311
John McCall53fa7142010-12-24 02:08:15 +00005312 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005313 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005314
5315 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005316
5317 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005318 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005319 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005320 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5321 // too much hassle.
5322 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005323
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005324 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005325
Faisal Vali2b391ab2013-09-26 19:54:12 +00005326
5327 // Parse the declarator. This is "PrototypeContext" or
5328 // "LambdaExprParameterContext", because we must accept either
5329 // 'declarator' or 'abstract-declarator' here.
5330 Declarator ParmDeclarator(DS,
5331 D.getContext() == Declarator::LambdaExprContext ?
5332 Declarator::LambdaExprParameterContext :
5333 Declarator::PrototypeContext);
5334 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005335
5336 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005337 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005338
Chris Lattner371ed4e2008-04-06 06:57:35 +00005339 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005340 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005341
Douglas Gregor4d87df52008-12-16 21:30:33 +00005342 // DefArgToks is used when the parsing of default arguments needs
5343 // to be delayed.
5344 CachedTokens *DefArgToks = 0;
5345
Chris Lattner371ed4e2008-04-06 06:57:35 +00005346 // If no parameter was specified, verify that *something* was specified,
5347 // otherwise we have a missing type and identifier.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005348 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5349 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005350 // Completely missing, emit error.
5351 Diag(DSStart, diag::err_missing_param);
5352 } else {
5353 // Otherwise, we have something. Add it and let semantic analysis try
5354 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005355
Chris Lattner371ed4e2008-04-06 06:57:35 +00005356 // Inform the actions module about the parameter declarator, so it gets
5357 // added to the current scope.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005358 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5359 ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005360 // Parse the default argument, if any. We parse the default
5361 // arguments in all dialects; the semantic analysis in
5362 // ActOnParamDefaultArgument will reject the default argument in
5363 // C.
5364 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005365 SourceLocation EqualLoc = Tok.getLocation();
5366
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005367 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005368 if (D.getContext() == Declarator::MemberContext) {
5369 // If we're inside a class definition, cache the tokens
5370 // corresponding to the default argument. We'll actually parse
5371 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005372 // FIXME: Can we use a smart pointer for Toks?
5373 DefArgToks = new CachedTokens;
5374
Richard Smith1fff95c2013-09-12 23:28:08 +00005375 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005376 delete DefArgToks;
5377 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005378 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005379 } else {
5380 // Mark the end of the default argument so that we know when to
5381 // stop when we parse it later on.
5382 Token DefArgEnd;
5383 DefArgEnd.startToken();
5384 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5385 DefArgEnd.setLocation(Tok.getLocation());
5386 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005387 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005388 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005389 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005390 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005391 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005392 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005393
Chad Rosierc1183952012-06-26 22:30:43 +00005394 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005395 // used.
5396 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005397 Sema::PotentiallyEvaluatedIfUsed,
5398 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005399
Sebastian Redldb63af22012-03-14 15:54:00 +00005400 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005401 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005402 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005403 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005404 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005405 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005406 if (DefArgResult.isInvalid()) {
5407 Actions.ActOnParamDefaultArgumentError(Param);
5408 SkipUntil(tok::comma, tok::r_paren, true, true);
5409 } else {
5410 // Inform the actions module about the default argument
5411 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005412 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005413 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005414 }
5415 }
Mike Stump11289f42009-09-09 15:08:12 +00005416
5417 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005418 ParmDeclarator.getIdentifierLoc(),
5419 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005420 }
5421
5422 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005423 if (Tok.isNot(tok::comma)) {
5424 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005425 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005426
David Blaikiebbafb8a2012-03-11 07:00:24 +00005427 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005428 // We have ellipsis without a preceding ',', which is ill-formed
5429 // in C. Complain and provide the fix.
5430 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005431 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005432 }
5433 }
Chad Rosierc1183952012-06-26 22:30:43 +00005434
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005435 break;
5436 }
Mike Stump11289f42009-09-09 15:08:12 +00005437
Chris Lattner371ed4e2008-04-06 06:57:35 +00005438 // Consume the comma.
5439 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005440 }
Mike Stump11289f42009-09-09 15:08:12 +00005441
Chris Lattner6c940e62008-04-06 06:34:08 +00005442}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005443
Chris Lattnere8074e62006-08-06 18:30:15 +00005444/// [C90] direct-declarator '[' constant-expression[opt] ']'
5445/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5446/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5447/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5448/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005449/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5450/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005451void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005452 if (CheckProhibitedCXX11Attribute())
5453 return;
5454
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005455 BalancedDelimiterTracker T(*this, tok::l_square);
5456 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005457
Chris Lattner84a11622008-12-18 07:27:21 +00005458 // C array syntax has many features, but by-far the most common is [] and [4].
5459 // This code does a fast path to handle some of the most obvious cases.
5460 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005461 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005462 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005463 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005464
Chris Lattner84a11622008-12-18 07:27:21 +00005465 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005466 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005467 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005468 T.getOpenLocation(),
5469 T.getCloseLocation()),
5470 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005471 return;
5472 } else if (Tok.getKind() == tok::numeric_constant &&
5473 GetLookAheadToken(1).is(tok::r_square)) {
5474 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005475 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005476 ConsumeToken();
5477
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005478 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005479 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005480 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005481
Chris Lattner84a11622008-12-18 07:27:21 +00005482 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005483 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005484 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005485 T.getOpenLocation(),
5486 T.getCloseLocation()),
5487 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005488 return;
5489 }
Mike Stump11289f42009-09-09 15:08:12 +00005490
Chris Lattnere8074e62006-08-06 18:30:15 +00005491 // If valid, this location is the position where we read the 'static' keyword.
5492 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005493 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005494 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005495
Chris Lattnere8074e62006-08-06 18:30:15 +00005496 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005497 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005498 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005499 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005500
Chris Lattnere8074e62006-08-06 18:30:15 +00005501 // If we haven't already read 'static', check to see if there is one after the
5502 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005503 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005504 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005505
Chris Lattnere8074e62006-08-06 18:30:15 +00005506 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005507 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005508 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005509
Chris Lattner521ff2b2008-04-06 05:26:30 +00005510 // Handle the case where we have '[*]' as the array size. However, a leading
5511 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005512 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005513 // infrequent, use of lookahead is not costly here.
5514 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005515 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005516
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005517 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005518 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005519 StaticLoc = SourceLocation(); // Drop the static.
5520 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005521 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005522 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005523 // Note, in C89, this production uses the constant-expr production instead
5524 // of assignment-expr. The only difference is that assignment-expr allows
5525 // things like '=' and '*='. Sema rejects these in C89 mode because they
5526 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005527
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005528 // Parse the constant-expression or assignment-expression now (depending
5529 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005530 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005531 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005532 } else {
5533 EnterExpressionEvaluationContext Unevaluated(Actions,
5534 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005535 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005536 }
Chris Lattner62591722006-08-12 18:40:58 +00005537 }
Mike Stump11289f42009-09-09 15:08:12 +00005538
Chris Lattner62591722006-08-12 18:40:58 +00005539 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005540 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005541 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005542 // If the expression was invalid, skip it.
5543 SkipUntil(tok::r_square);
5544 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005545 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005546
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005547 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005548
John McCall084e83d2011-03-24 11:26:52 +00005549 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005550 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005551
Chris Lattner84a11622008-12-18 07:27:21 +00005552 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005553 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005554 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005555 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005556 T.getOpenLocation(),
5557 T.getCloseLocation()),
5558 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005559}
5560
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005561/// [GNU] typeof-specifier:
5562/// typeof ( expressions )
5563/// typeof ( type-name )
5564/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005565///
5566void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005567 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005568 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005569 SourceLocation StartLoc = ConsumeToken();
5570
John McCalle8595032010-01-13 20:03:27 +00005571 const bool hasParens = Tok.is(tok::l_paren);
5572
Eli Friedman15681d62012-09-26 04:34:21 +00005573 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5574 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005575
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005576 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005577 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005578 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005579 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5580 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005581 if (hasParens)
5582 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005583
5584 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005585 // FIXME: Not accurate, the range gets one token more than it should.
5586 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005587 else
5588 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005589
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005590 if (isCastExpr) {
5591 if (!CastTy) {
5592 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005593 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005594 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005595
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005596 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005597 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005598 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5599 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005600 DiagID, CastTy))
5601 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005602 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005603 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005604
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005605 // If we get here, the operand to the typeof was an expresion.
5606 if (Operand.isInvalid()) {
5607 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005608 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005609 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005610
Eli Friedmane0afc982012-01-21 01:01:51 +00005611 // We might need to transform the operand if it is potentially evaluated.
5612 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5613 if (Operand.isInvalid()) {
5614 DS.SetTypeSpecError();
5615 return;
5616 }
5617
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005618 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005619 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005620 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5621 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005622 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005623 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005624}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005625
Benjamin Kramere56f3932011-12-23 17:00:35 +00005626/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005627/// _Atomic ( type-name )
5628///
5629void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005630 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5631 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005632
5633 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005634 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005635 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005636 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005637
5638 TypeResult Result = ParseTypeName();
5639 if (Result.isInvalid()) {
5640 SkipUntil(tok::r_paren);
5641 return;
5642 }
5643
5644 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005645 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005646
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005647 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005648 return;
5649
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005650 DS.setTypeofParensRange(T.getRange());
5651 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005652
5653 const char *PrevSpec = 0;
5654 unsigned DiagID;
5655 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5656 DiagID, Result.release()))
5657 Diag(StartLoc, DiagID) << PrevSpec;
5658}
5659
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005660
5661/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5662/// from TryAltiVecVectorToken.
5663bool Parser::TryAltiVecVectorTokenOutOfLine() {
5664 Token Next = NextToken();
5665 switch (Next.getKind()) {
5666 default: return false;
5667 case tok::kw_short:
5668 case tok::kw_long:
5669 case tok::kw_signed:
5670 case tok::kw_unsigned:
5671 case tok::kw_void:
5672 case tok::kw_char:
5673 case tok::kw_int:
5674 case tok::kw_float:
5675 case tok::kw_double:
5676 case tok::kw_bool:
5677 case tok::kw___pixel:
5678 Tok.setKind(tok::kw___vector);
5679 return true;
5680 case tok::identifier:
5681 if (Next.getIdentifierInfo() == Ident_pixel) {
5682 Tok.setKind(tok::kw___vector);
5683 return true;
5684 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005685 if (Next.getIdentifierInfo() == Ident_bool) {
5686 Tok.setKind(tok::kw___vector);
5687 return true;
5688 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005689 return false;
5690 }
5691}
5692
5693bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5694 const char *&PrevSpec, unsigned &DiagID,
5695 bool &isInvalid) {
5696 if (Tok.getIdentifierInfo() == Ident_vector) {
5697 Token Next = NextToken();
5698 switch (Next.getKind()) {
5699 case tok::kw_short:
5700 case tok::kw_long:
5701 case tok::kw_signed:
5702 case tok::kw_unsigned:
5703 case tok::kw_void:
5704 case tok::kw_char:
5705 case tok::kw_int:
5706 case tok::kw_float:
5707 case tok::kw_double:
5708 case tok::kw_bool:
5709 case tok::kw___pixel:
5710 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5711 return true;
5712 case tok::identifier:
5713 if (Next.getIdentifierInfo() == Ident_pixel) {
5714 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5715 return true;
5716 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005717 if (Next.getIdentifierInfo() == Ident_bool) {
5718 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5719 return true;
5720 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005721 break;
5722 default:
5723 break;
5724 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005725 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005726 DS.isTypeAltiVecVector()) {
5727 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5728 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005729 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5730 DS.isTypeAltiVecVector()) {
5731 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5732 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005733 }
5734 return false;
5735}