blob: 5f02c31fc2459dddd2004cadd412caaed43afe52 [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///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000103/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +0000104/// token lookahead. Comment from gcc: "If they start with an identifier
105/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +0000106/// start with that identifier; otherwise they are an expression list."
107///
Richard Smithb12bf692011-10-17 21:20:17 +0000108/// GCC does not require the ',' between attribs in an attribute-list.
109///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000110/// At the moment, I am not doing 2 token lookahead. I am also unaware of
111/// any attributes that don't work (based on my limited testing). Most
112/// attributes are very simple in practice. Until we find a bug, I don't see
113/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000114
John McCall53fa7142010-12-24 02:08:15 +0000115void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000116 SourceLocation *endLoc,
117 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000118 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000119
Chris Lattner76c72282007-10-09 17:33:22 +0000120 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000121 ConsumeToken();
122 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
123 "attribute")) {
124 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000125 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000126 }
127 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
128 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000129 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000130 }
131 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000132 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
133 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000134 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000135 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
136 ConsumeToken();
137 continue;
138 }
139 // we have an identifier or declaration specifier (const, int, etc.)
140 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
141 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000142
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000143 if (Tok.is(tok::l_paren)) {
144 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000145 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000146 LateParsedAttribute *LA =
147 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
148 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000149
Bill Wendling44426052012-12-20 19:22:21 +0000150 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000151 // with other late-parsed declarations.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000152 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000153 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000154
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000155 // consume everything up to and including the matching right parens
156 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000157
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000158 Token Eof;
159 Eof.startToken();
160 Eof.setLocation(Tok.getLocation());
161 LA->Toks.push_back(Eof);
162 } else {
Michael Han23214e52012-10-03 01:56:22 +0000163 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000164 0, SourceLocation(), AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000165 }
166 } else {
Aaron Ballman00e99962013-08-31 01:11:41 +0000167 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
168 AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000169 }
170 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000171 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000172 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000173 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000174 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
175 SkipUntil(tok::r_paren, false);
176 }
John McCall53fa7142010-12-24 02:08:15 +0000177 if (endLoc)
178 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000179 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000180}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000181
Douglas Gregord2472d42013-05-02 23:25:32 +0000182/// \brief Determine whether the given attribute has all expression arguments.
183static bool attributeHasExprArgs(const IdentifierInfo &II) {
184 return llvm::StringSwitch<bool>(II.getName())
185#include "clang/Parse/AttrExprArgs.inc"
186 .Default(false);
187}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000188
Michael Han23214e52012-10-03 01:56:22 +0000189/// Parse the arguments to a parameterized GNU attribute or
190/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000191void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
192 SourceLocation AttrNameLoc,
193 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000194 SourceLocation *EndLoc,
195 IdentifierInfo *ScopeName,
196 SourceLocation ScopeLoc,
197 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000198
199 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
200
201 // Availability attributes have their own grammar.
202 if (AttrName->isStr("availability")) {
203 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
204 return;
205 }
206 // Thread safety attributes fit into the FIXME case above, so we
207 // just parse the arguments as a list of expressions
208 if (IsThreadSafetyAttribute(AttrName->getName())) {
209 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
210 return;
211 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000212 // Type safety attributes have their own grammar.
213 if (AttrName->isStr("type_tag_for_datatype")) {
214 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
215 return;
216 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000217
218 ConsumeParen(); // ignore the left paren loc for now
219
Richard Smithb12bf692011-10-17 21:20:17 +0000220 bool BuiltinType = false;
Aaron Ballman00e99962013-08-31 01:11:41 +0000221 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000222
Joey Goulyaba589c2013-03-08 09:42:32 +0000223 TypeResult T;
224 SourceRange TypeRange;
225 bool TypeParsed = false;
226
Richard Smithb12bf692011-10-17 21:20:17 +0000227 switch (Tok.getKind()) {
228 case tok::kw_char:
229 case tok::kw_wchar_t:
230 case tok::kw_char16_t:
231 case tok::kw_char32_t:
232 case tok::kw_bool:
233 case tok::kw_short:
234 case tok::kw_int:
235 case tok::kw_long:
236 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000237 case tok::kw___int128:
Richard Smithb12bf692011-10-17 21:20:17 +0000238 case tok::kw_signed:
239 case tok::kw_unsigned:
240 case tok::kw_float:
241 case tok::kw_double:
242 case tok::kw_void:
243 case tok::kw_typeof:
244 // __attribute__(( vec_type_hint(char) ))
Richard Smithb12bf692011-10-17 21:20:17 +0000245 BuiltinType = true;
Joey Goulyaba589c2013-03-08 09:42:32 +0000246 T = ParseTypeName(&TypeRange);
247 TypeParsed = true;
Richard Smithb12bf692011-10-17 21:20:17 +0000248 break;
249
Aaron Ballman00e99962013-08-31 01:11:41 +0000250 case tok::identifier: {
Joey Goulyaba589c2013-03-08 09:42:32 +0000251 if (AttrName->isStr("vec_type_hint")) {
252 T = ParseTypeName(&TypeRange);
253 TypeParsed = true;
254 break;
255 }
Douglas Gregord2472d42013-05-02 23:25:32 +0000256 // If the attribute has all expression arguments, and not a "parameter",
257 // break out to handle it below.
258 if (attributeHasExprArgs(*AttrName))
259 break;
Aaron Ballman00e99962013-08-31 01:11:41 +0000260
261 IdentifierLoc *Param = ::new (Actions.Context) IdentifierLoc;
262 Param->Ident = Tok.getIdentifierInfo();
263 Param->Loc = ConsumeToken();
264 ArgExprs.push_back(Param);
265 } break;
Richard Smithb12bf692011-10-17 21:20:17 +0000266
267 default:
268 break;
269 }
270
Joey Goulyaba589c2013-03-08 09:42:32 +0000271 bool isInvalid = false;
272 bool isParmType = false;
Richard Smithb12bf692011-10-17 21:20:17 +0000273
Joey Goulyaba589c2013-03-08 09:42:32 +0000274 if (!BuiltinType && !AttrName->isStr("vec_type_hint") &&
Aaron Ballman00e99962013-08-31 01:11:41 +0000275 (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
Richard Smithb12bf692011-10-17 21:20:17 +0000276 // Eat the comma.
Aaron Ballman00e99962013-08-31 01:11:41 +0000277 if (!ArgExprs.empty())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000278 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000279
Richard Smithb12bf692011-10-17 21:20:17 +0000280 // Parse the non-empty comma-separated list of expressions.
281 while (1) {
282 ExprResult ArgExpr(ParseAssignmentExpression());
283 if (ArgExpr.isInvalid()) {
284 SkipUntil(tok::r_paren);
285 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000286 }
Richard Smithb12bf692011-10-17 21:20:17 +0000287 ArgExprs.push_back(ArgExpr.release());
288 if (Tok.isNot(tok::comma))
289 break;
290 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000291 }
Richard Smithb12bf692011-10-17 21:20:17 +0000292 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000293 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
294 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
295 tok::greater)) {
Fariborz Jahanian7f733022011-10-18 23:13:50 +0000296 while (Tok.is(tok::identifier)) {
297 ConsumeToken();
298 if (Tok.is(tok::greater))
299 break;
300 if (Tok.is(tok::comma)) {
301 ConsumeToken();
302 continue;
303 }
304 }
305 if (Tok.isNot(tok::greater))
306 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000307 SkipUntil(tok::r_paren, false, true); // skip until ')'
308 }
Joey Goulyaba589c2013-03-08 09:42:32 +0000309 } else if (AttrName->isStr("vec_type_hint")) {
310 if (T.get() && !T.isInvalid())
311 isParmType = true;
312 else {
313 if (Tok.is(tok::identifier))
314 ConsumeToken();
315 if (TypeParsed)
316 isInvalid = true;
317 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000318 }
Richard Smithb12bf692011-10-17 21:20:17 +0000319
320 SourceLocation RParen = Tok.getLocation();
Joey Goulyaba589c2013-03-08 09:42:32 +0000321 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen) &&
322 !isInvalid) {
Michael Han360d2252012-10-04 16:42:52 +0000323 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Joey Goulyaba589c2013-03-08 09:42:32 +0000324 if (isParmType) {
Joey Goulyaba589c2013-03-08 09:42:32 +0000325 Attrs.addNewTypeAttr(AttrName, SourceRange(AttrLoc, RParen), ScopeName,
Aaron Ballman00e99962013-08-31 01:11:41 +0000326 ScopeLoc, T.get(), Syntax);
Joey Goulyaba589c2013-03-08 09:42:32 +0000327 } else {
328 AttributeList *attr = Attrs.addNew(
Aaron Ballman00e99962013-08-31 01:11:41 +0000329 AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
330 ArgExprs.data(), ArgExprs.size(), Syntax);
Joey Goulyaba589c2013-03-08 09:42:32 +0000331 if (BuiltinType &&
332 attr->getKind() == AttributeList::AT_IBOutletCollection)
333 Diag(Tok, diag::err_iboutletcollection_builtintype);
334 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000335 }
336}
337
Chad Rosierc1183952012-06-26 22:30:43 +0000338/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000339/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000340void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000341 SourceLocation AttrNameLoc,
342 ParsedAttributes &Attrs)
343{
344 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000345 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000346 AttrName->getNameStart(), tok::r_paren))
347 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000348
Aaron Ballman478faed2012-06-19 22:09:27 +0000349 ExprResult ArgExpr(ParseConstantExpression());
350 if (ArgExpr.isInvalid()) {
351 T.skipToEnd();
352 return;
353 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000354 ArgsUnion ExprList = ArgExpr.take();
355 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
356 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000357
358 T.consumeClose();
359}
360
Chad Rosierc1183952012-06-26 22:30:43 +0000361/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000362/// arguments.
363bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
364 return llvm::StringSwitch<bool>(Ident->getName())
365 .Case("dllimport", true)
366 .Case("dllexport", true)
367 .Case("noreturn", true)
368 .Case("nothrow", true)
369 .Case("noinline", true)
370 .Case("naked", true)
371 .Case("appdomain", true)
372 .Case("process", true)
373 .Case("jitintrinsic", true)
374 .Case("noalias", true)
375 .Case("restrict", true)
376 .Case("novtable", true)
377 .Case("selectany", true)
378 .Case("thread", true)
Aaron Ballman444eb6e2013-05-04 16:58:37 +0000379 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000380 .Default(false);
381}
382
Chad Rosierc1183952012-06-26 22:30:43 +0000383/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000384/// parameters). Will return false if we properly handled the declspec, or
385/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000386void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000387 SourceLocation Loc,
388 ParsedAttributes &Attrs) {
389 // Try to handle the easy case first -- these declspecs all take a single
390 // parameter as their argument.
391 if (llvm::StringSwitch<bool>(Ident->getName())
392 .Case("uuid", true)
393 .Case("align", true)
394 .Case("allocate", true)
395 .Default(false)) {
396 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
397 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000398 // The deprecated declspec has an optional single argument, so we will
399 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000400 // not.
401 if (Tok.getKind() == tok::l_paren)
402 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
403 else
Aaron Ballman00e99962013-08-31 01:11:41 +0000404 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000405 } else if (Ident->getName() == "property") {
406 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000407 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000408 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000409 if (Tok.isNot(tok::l_paren)) {
410 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
411 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000412 return;
John McCall5e77d762013-04-16 07:28:30 +0000413 }
414 BalancedDelimiterTracker T(*this, tok::l_paren);
415 T.expectAndConsume(diag::err_expected_lparen_after,
416 Ident->getNameStart(), tok::r_paren);
417
418 enum AccessorKind {
419 AK_Invalid = -1,
420 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
421 };
422 IdentifierInfo *AccessorNames[] = { 0, 0 };
423 bool HasInvalidAccessor = false;
424
425 // Parse the accessor specifications.
426 while (true) {
427 // Stop if this doesn't look like an accessor spec.
428 if (!Tok.is(tok::identifier)) {
429 // If the user wrote a completely empty list, use a special diagnostic.
430 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
431 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
432 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
433 break;
434 }
435
436 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
437 break;
438 }
439
440 AccessorKind Kind;
441 SourceLocation KindLoc = Tok.getLocation();
442 StringRef KindStr = Tok.getIdentifierInfo()->getName();
443 if (KindStr == "get") {
444 Kind = AK_Get;
445 } else if (KindStr == "put") {
446 Kind = AK_Put;
447
448 // Recover from the common mistake of using 'set' instead of 'put'.
449 } else if (KindStr == "set") {
450 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
451 << FixItHint::CreateReplacement(KindLoc, "put");
452 Kind = AK_Put;
453
454 // Handle the mistake of forgetting the accessor kind by skipping
455 // this accessor.
456 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
457 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
458 ConsumeToken();
459 HasInvalidAccessor = true;
460 goto next_property_accessor;
461
462 // Otherwise, complain about the unknown accessor kind.
463 } else {
464 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
465 HasInvalidAccessor = true;
466 Kind = AK_Invalid;
467
468 // Try to keep parsing unless it doesn't look like an accessor spec.
469 if (!NextToken().is(tok::equal)) break;
470 }
471
472 // Consume the identifier.
473 ConsumeToken();
474
475 // Consume the '='.
476 if (Tok.is(tok::equal)) {
477 ConsumeToken();
478 } else {
479 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
480 << KindStr;
481 break;
482 }
483
484 // Expect the method name.
485 if (!Tok.is(tok::identifier)) {
486 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
487 break;
488 }
489
490 if (Kind == AK_Invalid) {
491 // Just drop invalid accessors.
492 } else if (AccessorNames[Kind] != NULL) {
493 // Complain about the repeated accessor, ignore it, and keep parsing.
494 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
495 } else {
496 AccessorNames[Kind] = Tok.getIdentifierInfo();
497 }
498 ConsumeToken();
499
500 next_property_accessor:
501 // Keep processing accessors until we run out.
502 if (Tok.is(tok::comma)) {
503 ConsumeAnyToken();
504 continue;
505
506 // If we run into the ')', stop without consuming it.
507 } else if (Tok.is(tok::r_paren)) {
508 break;
509 } else {
510 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
511 break;
512 }
513 }
514
515 // Only add the property attribute if it was well-formed.
516 if (!HasInvalidAccessor) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000517 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000518 AccessorNames[AK_Get], AccessorNames[AK_Put],
519 AttributeList::AS_Declspec);
520 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000521 T.skipToEnd();
522 } else {
523 // We don't recognize this as a valid declspec, but instead of creating the
524 // attribute and allowing sema to warn about it, we will warn here instead.
525 // This is because some attributes have multiple spellings, but we need to
526 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000527 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000528 // both locations.
529 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
530
531 // If there's an open paren, we should eat the open and close parens under
532 // the assumption that this unknown declspec has parameters.
533 BalancedDelimiterTracker T(*this, tok::l_paren);
534 if (!T.consumeOpen())
535 T.skipToEnd();
536 }
537}
538
Eli Friedman06de2b52009-06-08 07:21:15 +0000539/// [MS] decl-specifier:
540/// __declspec ( extended-decl-modifier-seq )
541///
542/// [MS] extended-decl-modifier-seq:
543/// extended-decl-modifier[opt]
544/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000545void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000546 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000547
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000548 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000549 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000550 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000551 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000552 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000553
Chad Rosierc1183952012-06-26 22:30:43 +0000554 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000555 // you can specify multiple attributes per declspec.
556 while (Tok.getKind() != tok::r_paren) {
557 // We expect either a well-known identifier or a generic string. Anything
558 // else is a malformed declspec.
559 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000560 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000561 Tok.getKind() != tok::kw_restrict) {
562 Diag(Tok, diag::err_ms_declspec_type);
563 T.skipToEnd();
564 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000565 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000566
567 IdentifierInfo *AttrName;
568 SourceLocation AttrNameLoc;
569 if (IsString) {
570 SmallString<8> StrBuffer;
571 bool Invalid = false;
572 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
573 if (Invalid) {
574 T.skipToEnd();
575 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000576 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000577 AttrName = PP.getIdentifierInfo(Str);
578 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000579 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000580 AttrName = Tok.getIdentifierInfo();
581 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000582 }
Chad Rosierc1183952012-06-26 22:30:43 +0000583
Aaron Ballman478faed2012-06-19 22:09:27 +0000584 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000585 // If we have a generic string, we will allow it because there is no
586 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000587 // (for instance, SAL declspecs in older versions of MSVC).
588 //
Chad Rosierc1183952012-06-26 22:30:43 +0000589 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000590 // arguments and can be turned into an attribute directly.
Aaron Ballman00e99962013-08-31 01:11:41 +0000591 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
592 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000593 else
594 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000595 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000596 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000597}
598
John McCall53fa7142010-12-24 02:08:15 +0000599void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000600 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000601 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000602 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000603 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballman317a77f2013-05-22 23:25:32 +0000604 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
605 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000606 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
607 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000608 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
609 AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000610 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000611}
612
John McCall53fa7142010-12-24 02:08:15 +0000613void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000614 // Treat these like attributes
615 while (Tok.is(tok::kw___pascal)) {
616 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
617 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000618 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
619 AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000620 }
John McCall53fa7142010-12-24 02:08:15 +0000621}
622
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000623void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
624 // Treat these like attributes
625 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000626 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000627 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000628 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
629 AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000630 }
631}
632
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000633void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000634 // FIXME: The mapping from attribute spelling to semantics should be
635 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000636 SourceLocation Loc = Tok.getLocation();
637 switch(Tok.getKind()) {
638 // OpenCL qualifiers:
639 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000640 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000641 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000642 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000643 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000644 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000645
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000646 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000647 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000648 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000649 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000650 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000651
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000652 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000653 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000654 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000655 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000656 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000657
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000658 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000659 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000660 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000661 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000662 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000663
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000664 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000665 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000666 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000667 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000668 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000669
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000670 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000671 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000672 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000673 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000674 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000675
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000676 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000677 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000678 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000679 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000680 break;
681 default: break;
682 }
683}
684
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000685/// \brief Parse a version number.
686///
687/// version:
688/// simple-integer
689/// simple-integer ',' simple-integer
690/// simple-integer ',' simple-integer ',' simple-integer
691VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
692 Range = Tok.getLocation();
693
694 if (!Tok.is(tok::numeric_constant)) {
695 Diag(Tok, diag::err_expected_version);
696 SkipUntil(tok::comma, tok::r_paren, true, true, true);
697 return VersionTuple();
698 }
699
700 // Parse the major (and possibly minor and subminor) versions, which
701 // are stored in the numeric constant. We utilize a quirk of the
702 // lexer, which is that it handles something like 1.2.3 as a single
703 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000704 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000705 Buffer.resize(Tok.getLength()+1);
706 const char *ThisTokBegin = &Buffer[0];
707
708 // Get the spelling of the token, which eliminates trigraphs, etc.
709 bool Invalid = false;
710 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
711 if (Invalid)
712 return VersionTuple();
713
714 // Parse the major version.
715 unsigned AfterMajor = 0;
716 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000717 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000718 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
719 ++AfterMajor;
720 }
721
722 if (AfterMajor == 0) {
723 Diag(Tok, diag::err_expected_version);
724 SkipUntil(tok::comma, tok::r_paren, true, true, true);
725 return VersionTuple();
726 }
727
728 if (AfterMajor == ActualLength) {
729 ConsumeToken();
730
731 // We only had a single version component.
732 if (Major == 0) {
733 Diag(Tok, diag::err_zero_version);
734 return VersionTuple();
735 }
736
737 return VersionTuple(Major);
738 }
739
740 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
741 Diag(Tok, diag::err_expected_version);
742 SkipUntil(tok::comma, tok::r_paren, true, true, true);
743 return VersionTuple();
744 }
745
746 // Parse the minor version.
747 unsigned AfterMinor = AfterMajor + 1;
748 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000749 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000750 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
751 ++AfterMinor;
752 }
753
754 if (AfterMinor == ActualLength) {
755 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000756
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000757 // We had major.minor.
758 if (Major == 0 && Minor == 0) {
759 Diag(Tok, diag::err_zero_version);
760 return VersionTuple();
761 }
762
Chad Rosierc1183952012-06-26 22:30:43 +0000763 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000764 }
765
766 // If what follows is not a '.', we have a problem.
767 if (ThisTokBegin[AfterMinor] != '.') {
768 Diag(Tok, diag::err_expected_version);
769 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosierc1183952012-06-26 22:30:43 +0000770 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000771 }
772
773 // Parse the subminor version.
774 unsigned AfterSubminor = AfterMinor + 1;
775 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000776 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000777 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
778 ++AfterSubminor;
779 }
780
781 if (AfterSubminor != ActualLength) {
782 Diag(Tok, diag::err_expected_version);
783 SkipUntil(tok::comma, tok::r_paren, true, true, true);
784 return VersionTuple();
785 }
786 ConsumeToken();
787 return VersionTuple(Major, Minor, Subminor);
788}
789
790/// \brief Parse the contents of the "availability" attribute.
791///
792/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000793/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000794///
795/// platform:
796/// identifier
797///
798/// version-arg-list:
799/// version-arg
800/// version-arg ',' version-arg-list
801///
802/// version-arg:
803/// 'introduced' '=' version
804/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000805/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000806/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000807/// opt-message:
808/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000809void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
810 SourceLocation AvailabilityLoc,
811 ParsedAttributes &attrs,
812 SourceLocation *endLoc) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000813 enum { Introduced, Deprecated, Obsoleted, Unknown };
814 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000815 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000816
817 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000818 BalancedDelimiterTracker T(*this, tok::l_paren);
819 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000820 Diag(Tok, diag::err_expected_lparen);
821 return;
822 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000823
824 // Parse the platform name,
825 if (Tok.isNot(tok::identifier)) {
826 Diag(Tok, diag::err_availability_expected_platform);
827 SkipUntil(tok::r_paren);
828 return;
829 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000830
831 IdentifierLoc *Platform = new (Actions.Context) IdentifierLoc;
832 Platform->Ident = Tok.getIdentifierInfo();
833 Platform->Loc = ConsumeToken();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000834
835 // Parse the ',' following the platform name.
836 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
837 return;
838
839 // If we haven't grabbed the pointers for the identifiers
840 // "introduced", "deprecated", and "obsoleted", do so now.
841 if (!Ident_introduced) {
842 Ident_introduced = PP.getIdentifierInfo("introduced");
843 Ident_deprecated = PP.getIdentifierInfo("deprecated");
844 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000845 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000846 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000847 }
848
849 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000850 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000851 do {
852 if (Tok.isNot(tok::identifier)) {
853 Diag(Tok, diag::err_availability_expected_change);
854 SkipUntil(tok::r_paren);
855 return;
856 }
857 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
858 SourceLocation KeywordLoc = ConsumeToken();
859
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000860 if (Keyword == Ident_unavailable) {
861 if (UnavailableLoc.isValid()) {
862 Diag(KeywordLoc, diag::err_availability_redundant)
863 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000864 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000865 UnavailableLoc = KeywordLoc;
866
867 if (Tok.isNot(tok::comma))
868 break;
869
870 ConsumeToken();
871 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000872 }
873
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000874 if (Tok.isNot(tok::equal)) {
875 Diag(Tok, diag::err_expected_equal_after)
876 << Keyword;
877 SkipUntil(tok::r_paren);
878 return;
879 }
880 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000881 if (Keyword == Ident_message) {
882 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000883 Diag(Tok, diag::err_expected_string_literal)
884 << /*Source='availability attribute'*/2;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000885 SkipUntil(tok::r_paren);
886 return;
887 }
888 MessageExpr = ParseStringLiteralExpression();
889 break;
890 }
Chad Rosierc1183952012-06-26 22:30:43 +0000891
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000892 SourceRange VersionRange;
893 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000894
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000895 if (Version.empty()) {
896 SkipUntil(tok::r_paren);
897 return;
898 }
899
900 unsigned Index;
901 if (Keyword == Ident_introduced)
902 Index = Introduced;
903 else if (Keyword == Ident_deprecated)
904 Index = Deprecated;
905 else if (Keyword == Ident_obsoleted)
906 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000907 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000908 Index = Unknown;
909
910 if (Index < Unknown) {
911 if (!Changes[Index].KeywordLoc.isInvalid()) {
912 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000913 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000914 << SourceRange(Changes[Index].KeywordLoc,
915 Changes[Index].VersionRange.getEnd());
916 }
917
918 Changes[Index].KeywordLoc = KeywordLoc;
919 Changes[Index].Version = Version;
920 Changes[Index].VersionRange = VersionRange;
921 } else {
922 Diag(KeywordLoc, diag::err_availability_unknown_change)
923 << Keyword << VersionRange;
924 }
925
926 if (Tok.isNot(tok::comma))
927 break;
928
929 ConsumeToken();
930 } while (true);
931
932 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000933 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000934 return;
935
936 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000937 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000938
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000939 // The 'unavailable' availability cannot be combined with any other
940 // availability changes. Make sure that hasn't happened.
941 if (UnavailableLoc.isValid()) {
942 bool Complained = false;
943 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
944 if (Changes[Index].KeywordLoc.isValid()) {
945 if (!Complained) {
946 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
947 << SourceRange(Changes[Index].KeywordLoc,
948 Changes[Index].VersionRange.getEnd());
949 Complained = true;
950 }
951
952 // Clear out the availability.
953 Changes[Index] = AvailabilityChange();
954 }
955 }
956 }
957
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000958 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000959 attrs.addNew(&Availability,
960 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000961 0, AvailabilityLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +0000962 Platform,
John McCall084e83d2011-03-24 11:26:52 +0000963 Changes[Introduced],
964 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000965 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000966 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000967 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000968}
969
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000970
Bill Wendling44426052012-12-20 19:22:21 +0000971// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000972// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
973
974void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
975
976void Parser::LateParsedClass::ParseLexedAttributes() {
977 Self->ParseLexedAttributes(*Class);
978}
979
980void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000981 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000982}
983
984/// Wrapper class which calls ParseLexedAttribute, after setting up the
985/// scope appropriately.
986void Parser::ParseLexedAttributes(ParsingClass &Class) {
987 // Deal with templates
988 // FIXME: Test cases to make sure this does the right thing for templates.
989 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
990 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
991 HasTemplateScope);
992 if (HasTemplateScope)
993 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
994
Douglas Gregor3024f072012-04-16 07:05:22 +0000995 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000996 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000997 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000998 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
999 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1000
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001001 // Enter the scope of nested classes
1002 if (!AlreadyHasClassScope)
1003 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1004 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001005 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001006 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1007 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1008 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001009 }
Chad Rosierc1183952012-06-26 22:30:43 +00001010
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001011 if (!AlreadyHasClassScope)
1012 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1013 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001014}
1015
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001016
1017/// \brief Parse all attributes in LAs, and attach them to Decl D.
1018void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1019 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001020 assert(LAs.parseSoon() &&
1021 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001022 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001023 if (D)
1024 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001025 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001026 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001027 }
1028 LAs.clear();
1029}
1030
1031
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001032/// \brief Finish parsing an attribute for which parsing was delayed.
1033/// This will be called at the end of parsing a class declaration
1034/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001035/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001036/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001037void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1038 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001039 // Save the current token position.
1040 SourceLocation OrigLoc = Tok.getLocation();
1041
1042 // Append the current token at the end of the new token stream so that it
1043 // doesn't get lost.
1044 LA.Toks.push_back(Tok);
1045 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1046 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001047 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001048
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001049 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +00001050 // FIXME: Do not warn on C++11 attributes, once we start supporting
1051 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001052 Diag(Tok, diag::warn_attribute_on_function_definition)
1053 << LA.AttrName.getName();
1054 }
1055
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001056 ParsedAttributes Attrs(AttrFactory);
1057 SourceLocation endLoc;
1058
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001059 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001060 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001061 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1062 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001063
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001064 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001065 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1066 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001067
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001068 if (LA.Decls.size() == 1) {
1069 // If the Decl is templatized, add template parameters to scope.
1070 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1071 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1072 if (HasTemplateScope)
1073 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001074
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001075 // If the Decl is on a function, add function parameters to the scope.
1076 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1077 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1078 if (HasFunScope)
1079 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001080
Michael Han23214e52012-10-03 01:56:22 +00001081 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001082 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001083
1084 if (HasFunScope) {
1085 Actions.ActOnExitFunctionContext();
1086 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1087 }
1088 if (HasTemplateScope) {
1089 TempScope.Exit();
1090 }
1091 } else {
1092 // If there are multiple decls, then the decl cannot be within the
1093 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001094 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001095 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001096 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001097 } else {
1098 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001099 }
1100
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001101 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1102 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1103 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001104
1105 if (Tok.getLocation() != OrigLoc) {
1106 // Due to a parsing error, we either went over the cached tokens or
1107 // there are still cached tokens left, so we skip the leftover tokens.
1108 // Since this is an uncommon situation that should be avoided, use the
1109 // expensive isBeforeInTranslationUnit call.
1110 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1111 OrigLoc))
1112 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001113 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001114 }
1115}
1116
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001117/// \brief Wrapper around a case statement checking if AttrName is
1118/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001119bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001120 return llvm::StringSwitch<bool>(AttrName)
1121 .Case("guarded_by", true)
1122 .Case("guarded_var", true)
1123 .Case("pt_guarded_by", true)
1124 .Case("pt_guarded_var", true)
1125 .Case("lockable", true)
1126 .Case("scoped_lockable", true)
1127 .Case("no_thread_safety_analysis", true)
1128 .Case("acquired_after", true)
1129 .Case("acquired_before", true)
1130 .Case("exclusive_lock_function", true)
1131 .Case("shared_lock_function", true)
1132 .Case("exclusive_trylock_function", true)
1133 .Case("shared_trylock_function", true)
1134 .Case("unlock_function", true)
1135 .Case("lock_returned", true)
1136 .Case("locks_excluded", true)
1137 .Case("exclusive_locks_required", true)
1138 .Case("shared_locks_required", true)
1139 .Default(false);
1140}
1141
1142/// \brief Parse the contents of thread safety attributes. These
1143/// should always be parsed as an expression list.
1144///
1145/// We need to special case the parsing due to the fact that if the first token
1146/// of the first argument is an identifier, the main parse loop will store
1147/// that token as a "parameter" and the rest of
1148/// the arguments will be added to a list of "arguments". However,
1149/// subsequent tokens in the first argument are lost. We instead parse each
1150/// argument as an expression and add all arguments to the list of "arguments".
1151/// In future, we will take advantage of this special case to also
1152/// deal with some argument scoping issues here (for example, referring to a
1153/// function parameter in the attribute on that function).
1154void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1155 SourceLocation AttrNameLoc,
1156 ParsedAttributes &Attrs,
1157 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001158 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001159
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001160 BalancedDelimiterTracker T(*this, tok::l_paren);
1161 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001162
Aaron Ballman00e99962013-08-31 01:11:41 +00001163 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001164 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001165
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001166 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001167 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001168 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001169 ExprResult ArgExpr(ParseAssignmentExpression());
1170 if (ArgExpr.isInvalid()) {
1171 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001172 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001173 break;
1174 } else {
1175 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001176 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001177 if (Tok.isNot(tok::comma))
1178 break;
1179 ConsumeToken(); // Eat the comma, move to the next argument
1180 }
1181 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001182 if (ArgExprsOk && !T.consumeClose()) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001183 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(),
1184 ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001185 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001186 if (EndLoc)
1187 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001188}
1189
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001190void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1191 SourceLocation AttrNameLoc,
1192 ParsedAttributes &Attrs,
1193 SourceLocation *EndLoc) {
1194 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1195
1196 BalancedDelimiterTracker T(*this, tok::l_paren);
1197 T.consumeOpen();
1198
1199 if (Tok.isNot(tok::identifier)) {
1200 Diag(Tok, diag::err_expected_ident);
1201 T.skipToEnd();
1202 return;
1203 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001204 IdentifierLoc *ArgumentKind = new (Actions.Context) IdentifierLoc;
1205 ArgumentKind->Ident = Tok.getIdentifierInfo();
1206 ArgumentKind->Loc = ConsumeToken();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001207
1208 if (Tok.isNot(tok::comma)) {
1209 Diag(Tok, diag::err_expected_comma);
1210 T.skipToEnd();
1211 return;
1212 }
1213 ConsumeToken();
1214
1215 SourceRange MatchingCTypeRange;
1216 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1217 if (MatchingCType.isInvalid()) {
1218 T.skipToEnd();
1219 return;
1220 }
1221
1222 bool LayoutCompatible = false;
1223 bool MustBeNull = false;
1224 while (Tok.is(tok::comma)) {
1225 ConsumeToken();
1226 if (Tok.isNot(tok::identifier)) {
1227 Diag(Tok, diag::err_expected_ident);
1228 T.skipToEnd();
1229 return;
1230 }
1231 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1232 if (Flag->isStr("layout_compatible"))
1233 LayoutCompatible = true;
1234 else if (Flag->isStr("must_be_null"))
1235 MustBeNull = true;
1236 else {
1237 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1238 T.skipToEnd();
1239 return;
1240 }
1241 ConsumeToken(); // consume flag
1242 }
1243
1244 if (!T.consumeClose()) {
1245 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +00001246 ArgumentKind, MatchingCType.release(),
1247 LayoutCompatible, MustBeNull,
1248 AttributeList::AS_GNU);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001249 }
1250
1251 if (EndLoc)
1252 *EndLoc = T.getCloseLocation();
1253}
1254
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001255/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1256/// of a C++11 attribute-specifier in a location where an attribute is not
1257/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1258/// situation.
1259///
1260/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1261/// this doesn't appear to actually be an attribute-specifier, and the caller
1262/// should try to parse it.
1263bool Parser::DiagnoseProhibitedCXX11Attribute() {
1264 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1265
1266 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1267 case CAK_NotAttributeSpecifier:
1268 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1269 return false;
1270
1271 case CAK_InvalidAttributeSpecifier:
1272 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1273 return false;
1274
1275 case CAK_AttributeSpecifier:
1276 // Parse and discard the attributes.
1277 SourceLocation BeginLoc = ConsumeBracket();
1278 ConsumeBracket();
1279 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1280 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1281 SourceLocation EndLoc = ConsumeBracket();
1282 Diag(BeginLoc, diag::err_attributes_not_allowed)
1283 << SourceRange(BeginLoc, EndLoc);
1284 return true;
1285 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001286 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001287}
1288
Richard Smith98155ad2013-02-20 01:17:14 +00001289/// \brief We have found the opening square brackets of a C++11
1290/// attribute-specifier in a location where an attribute is not permitted, but
1291/// we know where the attributes ought to be written. Parse them anyway, and
1292/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001293void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1294 SourceLocation CorrectLocation) {
1295 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1296 Tok.is(tok::kw_alignas));
1297
1298 // Consume the attributes.
1299 SourceLocation Loc = Tok.getLocation();
1300 ParseCXX11Attributes(Attrs);
1301 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1302
1303 Diag(Loc, diag::err_attributes_not_allowed)
1304 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1305 << FixItHint::CreateRemoval(AttrRange);
1306}
1307
John McCall53fa7142010-12-24 02:08:15 +00001308void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1309 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1310 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001311}
1312
Michael Han64536a62012-11-06 19:34:54 +00001313void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1314 AttributeList *AttrList = attrs.getList();
1315 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001316 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001317 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001318 << AttrList->getName();
1319 AttrList->setInvalid();
1320 }
1321 AttrList = AttrList->getNext();
1322 }
1323}
1324
Chris Lattner53361ac2006-08-10 05:19:57 +00001325/// ParseDeclaration - Parse a full 'declaration', which consists of
1326/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001327/// 'Context' should be a Declarator::TheContext value. This returns the
1328/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001329///
1330/// declaration: [C99 6.7]
1331/// block-declaration ->
1332/// simple-declaration
1333/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001334/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001335/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001336/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001337/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001338/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001339/// others... [FIXME]
1340///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001341Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1342 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001343 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001344 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001345 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001346 // Must temporarily exit the objective-c container scope for
1347 // parsing c none objective-c decls.
1348 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001349
John McCall48871652010-08-21 09:40:31 +00001350 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001351 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001352 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001353 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001354 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001355 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001356 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001357 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001358 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001359 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001360 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001361 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001362 SourceLocation InlineLoc = ConsumeToken();
1363 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1364 break;
1365 }
Chad Rosierc1183952012-06-26 22:30:43 +00001366 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001367 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001368 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001369 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001370 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001371 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001372 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001373 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001374 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001375 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001376 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001377 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001378 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001379 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001380 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001381 default:
John McCall53fa7142010-12-24 02:08:15 +00001382 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001383 }
Chad Rosierc1183952012-06-26 22:30:43 +00001384
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001385 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001386 // single decl, convert it now. Alias declarations can also declare a type;
1387 // include that too if it is present.
1388 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001389}
1390
1391/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1392/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001393/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1394/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001395///[C90/C++]init-declarator-list ';' [TODO]
1396/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001397///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001398/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001399/// attribute-specifier-seq[opt] type-specifier-seq declarator
1400///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001401/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001402/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001403///
1404/// If FRI is non-null, we might be parsing a for-range-declaration instead
1405/// of a simple-declaration. If we find that we are, we also parse the
1406/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001407Parser::DeclGroupPtrTy
1408Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1409 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001410 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001411 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001412 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001413 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001414
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001415 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001416 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001417
Chris Lattner0e894622006-08-13 19:58:17 +00001418 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1419 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001420 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001421 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001422 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001423 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001424 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001425 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001426 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001427 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001428 }
Chad Rosierc1183952012-06-26 22:30:43 +00001429
Richard Smith2386c8b2013-02-22 09:06:26 +00001430 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001431 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001432}
Mike Stump11289f42009-09-09 15:08:12 +00001433
Richard Smith09f76ee2011-10-19 21:33:05 +00001434/// Returns true if this might be the start of a declarator, or a common typo
1435/// for a declarator.
1436bool Parser::MightBeDeclarator(unsigned Context) {
1437 switch (Tok.getKind()) {
1438 case tok::annot_cxxscope:
1439 case tok::annot_template_id:
1440 case tok::caret:
1441 case tok::code_completion:
1442 case tok::coloncolon:
1443 case tok::ellipsis:
1444 case tok::kw___attribute:
1445 case tok::kw_operator:
1446 case tok::l_paren:
1447 case tok::star:
1448 return true;
1449
1450 case tok::amp:
1451 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001452 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001453
Richard Smithc8a79032012-01-09 22:31:44 +00001454 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001455 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001456 NextToken().is(tok::l_square);
1457
1458 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001459 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001460
Richard Smith09f76ee2011-10-19 21:33:05 +00001461 case tok::identifier:
1462 switch (NextToken().getKind()) {
1463 case tok::code_completion:
1464 case tok::coloncolon:
1465 case tok::comma:
1466 case tok::equal:
1467 case tok::equalequal: // Might be a typo for '='.
1468 case tok::kw_alignas:
1469 case tok::kw_asm:
1470 case tok::kw___attribute:
1471 case tok::l_brace:
1472 case tok::l_paren:
1473 case tok::l_square:
1474 case tok::less:
1475 case tok::r_brace:
1476 case tok::r_paren:
1477 case tok::r_square:
1478 case tok::semi:
1479 return true;
1480
1481 case tok::colon:
1482 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001483 // and in block scope it's probably a label. Inside a class definition,
1484 // this is a bit-field.
1485 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001486 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001487
1488 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001489 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001490
1491 default:
1492 return false;
1493 }
1494
1495 default:
1496 return false;
1497 }
1498}
1499
Richard Smithb8caac82012-04-11 20:59:20 +00001500/// Skip until we reach something which seems like a sensible place to pick
1501/// up parsing after a malformed declaration. This will sometimes stop sooner
1502/// than SkipUntil(tok::r_brace) would, but will never stop later.
1503void Parser::SkipMalformedDecl() {
1504 while (true) {
1505 switch (Tok.getKind()) {
1506 case tok::l_brace:
1507 // Skip until matching }, then stop. We've probably skipped over
1508 // a malformed class or function definition or similar.
1509 ConsumeBrace();
1510 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1511 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1512 // This declaration isn't over yet. Keep skipping.
1513 continue;
1514 }
1515 if (Tok.is(tok::semi))
1516 ConsumeToken();
1517 return;
1518
1519 case tok::l_square:
1520 ConsumeBracket();
1521 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1522 continue;
1523
1524 case tok::l_paren:
1525 ConsumeParen();
1526 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1527 continue;
1528
1529 case tok::r_brace:
1530 return;
1531
1532 case tok::semi:
1533 ConsumeToken();
1534 return;
1535
1536 case tok::kw_inline:
1537 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001538 // a good place to pick back up parsing, except in an Objective-C
1539 // @interface context.
1540 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1541 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001542 return;
1543 break;
1544
1545 case tok::kw_namespace:
1546 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001547 // place to pick back up parsing, except in an Objective-C
1548 // @interface context.
1549 if (Tok.isAtStartOfLine() &&
1550 (!ParsingInObjCContainer || CurParsedObjCImpl))
1551 return;
1552 break;
1553
1554 case tok::at:
1555 // @end is very much like } in Objective-C contexts.
1556 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1557 ParsingInObjCContainer)
1558 return;
1559 break;
1560
1561 case tok::minus:
1562 case tok::plus:
1563 // - and + probably start new method declarations in Objective-C contexts.
1564 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001565 return;
1566 break;
1567
1568 case tok::eof:
1569 return;
1570
1571 default:
1572 break;
1573 }
1574
1575 ConsumeAnyToken();
1576 }
1577}
1578
John McCalld5a36322009-11-03 19:26:08 +00001579/// ParseDeclGroup - Having concluded that this is either a function
1580/// definition or a group of object declarations, actually parse the
1581/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001582Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1583 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001584 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001585 SourceLocation *DeclEnd,
1586 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001587 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001588 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001589 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001590
John McCalld5a36322009-11-03 19:26:08 +00001591 // Bail out if the first declarator didn't seem well-formed.
1592 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001593 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001594 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001595 }
Mike Stump11289f42009-09-09 15:08:12 +00001596
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001597 // Save late-parsed attributes for now; they need to be parsed in the
1598 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001599 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1600 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001601 if (D.isFunctionDeclarator())
1602 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1603
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001604 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001605 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001606 // Look at the next token to make sure that this isn't a function
1607 // declaration. We have to check this because __attribute__ might be the
1608 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001609 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001610
Douglas Gregor012efe22013-04-16 16:01:32 +00001611 if (AllowFunctionDefinitions) {
1612 if (isStartOfFunctionDefinition(D)) {
1613 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1614 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001615
Douglas Gregor012efe22013-04-16 16:01:32 +00001616 // Recover by treating the 'typedef' as spurious.
1617 DS.ClearStorageClassSpecs();
1618 }
1619
1620 Decl *TheDecl =
1621 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1622 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001623 }
1624
Douglas Gregor012efe22013-04-16 16:01:32 +00001625 if (isDeclarationSpecifier()) {
1626 // If there is an invalid declaration specifier right after the function
1627 // prototype, then we must be in a missing semicolon case where this isn't
1628 // actually a body. Just fall through into the code that handles it as a
1629 // prototype, and let the top-level code handle the erroneous declspec
1630 // where it would otherwise expect a comma or semicolon.
1631 } else {
1632 Diag(Tok, diag::err_expected_fn_body);
1633 SkipUntil(tok::semi);
1634 return DeclGroupPtrTy();
1635 }
John McCalld5a36322009-11-03 19:26:08 +00001636 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001637 if (Tok.is(tok::l_brace)) {
1638 Diag(Tok, diag::err_function_definition_not_allowed);
1639 SkipUntil(tok::r_brace, true, true);
1640 }
John McCalld5a36322009-11-03 19:26:08 +00001641 }
1642 }
1643
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001644 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001645 return DeclGroupPtrTy();
1646
1647 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1648 // must parse and analyze the for-range-initializer before the declaration is
1649 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001650 //
1651 // Handle the Objective-C for-in loop variable similarly, although we
1652 // don't need to parse the container in advance.
1653 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1654 bool IsForRangeLoop = false;
1655 if (Tok.is(tok::colon)) {
1656 IsForRangeLoop = true;
1657 FRI->ColonLoc = ConsumeToken();
1658 if (Tok.is(tok::l_brace))
1659 FRI->RangeExpr = ParseBraceInitializer();
1660 else
1661 FRI->RangeExpr = ParseExpression();
1662 }
1663
Richard Smith02e85f32011-04-14 22:09:26 +00001664 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001665 if (IsForRangeLoop)
1666 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001667 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001668 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001669 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001670 }
1671
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001672 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001673 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001674 if (LateParsedAttrs.size() > 0)
1675 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001676 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001677 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001678 DeclsInGroup.push_back(FirstDecl);
1679
Richard Smith09f76ee2011-10-19 21:33:05 +00001680 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001681
John McCalld5a36322009-11-03 19:26:08 +00001682 // If we don't have a comma, it is either the end of the list (a ';') or an
1683 // error, bail out.
1684 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001685 SourceLocation CommaLoc = ConsumeToken();
1686
1687 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1688 // This comma was followed by a line-break and something which can't be
1689 // the start of a declarator. The comma was probably a typo for a
1690 // semicolon.
1691 Diag(CommaLoc, diag::err_expected_semi_declaration)
1692 << FixItHint::CreateReplacement(CommaLoc, ";");
1693 ExpectSemi = false;
1694 break;
1695 }
John McCalld5a36322009-11-03 19:26:08 +00001696
1697 // Parse the next declarator.
1698 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001699 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001700
1701 // Accept attributes in an init-declarator. In the first declarator in a
1702 // declaration, these would be part of the declspec. In subsequent
1703 // declarators, they become part of the declarator itself, so that they
1704 // don't apply to declarators after *this* one. Examples:
1705 // short __attribute__((common)) var; -> declspec
1706 // short var __attribute__((common)); -> declarator
1707 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001708 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001709
1710 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001711 if (!D.isInvalidType()) {
1712 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1713 D.complete(ThisDecl);
1714 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001715 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001716 }
John McCalld5a36322009-11-03 19:26:08 +00001717 }
1718
1719 if (DeclEnd)
1720 *DeclEnd = Tok.getLocation();
1721
Richard Smith09f76ee2011-10-19 21:33:05 +00001722 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001723 ExpectAndConsumeSemi(Context == Declarator::FileContext
1724 ? diag::err_invalid_token_after_toplevel_declarator
1725 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001726 // Okay, there was no semicolon and one was expected. If we see a
1727 // declaration specifier, just assume it was missing and continue parsing.
1728 // Otherwise things are very confused and we skip to recover.
1729 if (!isDeclarationSpecifier()) {
1730 SkipUntil(tok::r_brace, true, true);
1731 if (Tok.is(tok::semi))
1732 ConsumeToken();
1733 }
John McCalld5a36322009-11-03 19:26:08 +00001734 }
1735
Rafael Espindolaab417692013-07-09 12:05:01 +00001736 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001737}
1738
Richard Smith02e85f32011-04-14 22:09:26 +00001739/// Parse an optional simple-asm-expr and attributes, and attach them to a
1740/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001741bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001742 // If a simple-asm-expr is present, parse it.
1743 if (Tok.is(tok::kw_asm)) {
1744 SourceLocation Loc;
1745 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1746 if (AsmLabel.isInvalid()) {
1747 SkipUntil(tok::semi, true, true);
1748 return true;
1749 }
1750
1751 D.setAsmLabel(AsmLabel.release());
1752 D.SetRangeEnd(Loc);
1753 }
1754
1755 MaybeParseGNUAttributes(D);
1756 return false;
1757}
1758
Douglas Gregor23996282009-05-12 21:31:51 +00001759/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1760/// declarator'. This method parses the remainder of the declaration
1761/// (including any attributes or initializer, among other things) and
1762/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001763///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001764/// init-declarator: [C99 6.7]
1765/// declarator
1766/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001767/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1768/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001769/// [C++] declarator initializer[opt]
1770///
1771/// [C++] initializer:
1772/// [C++] '=' initializer-clause
1773/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001774/// [C++0x] '=' 'default' [TODO]
1775/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001776/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001777///
1778/// According to the standard grammar, =default and =delete are function
1779/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001780///
John McCall48871652010-08-21 09:40:31 +00001781Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001782 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001783 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001784 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001785
Richard Smith02e85f32011-04-14 22:09:26 +00001786 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1787}
Mike Stump11289f42009-09-09 15:08:12 +00001788
Richard Smith02e85f32011-04-14 22:09:26 +00001789Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1790 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001791 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001792 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001793 switch (TemplateInfo.Kind) {
1794 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001795 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001796 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001797
Douglas Gregor450f00842009-09-25 18:43:00 +00001798 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001799 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001800 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001801 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001802 D);
Larisse Voufo21de36b2013-08-06 03:43:07 +00001803 if (Tok.is(tok::semi) &&
1804 Actions.HandleVariableRedeclaration(ThisDecl, D.getCXXScopeSpec())) {
Douglas Gregor450f00842009-09-25 18:43:00 +00001805 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001806 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001807 }
Larisse Voufo833b05a2013-08-06 07:33:00 +00001808 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001809 // Re-direct this decl to refer to the templated decl so that we can
1810 // initialize it.
1811 ThisDecl = VT->getTemplatedDecl();
1812 break;
1813 }
1814 case ParsedTemplateInfo::ExplicitInstantiation: {
1815 if (Tok.is(tok::semi)) {
1816 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1817 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1818 if (ThisRes.isInvalid()) {
1819 SkipUntil(tok::semi, true, true);
1820 return 0;
1821 }
1822 ThisDecl = ThisRes.get();
1823 } else {
1824 // FIXME: This check should be for a variable template instantiation only.
1825
1826 // Check that this is a valid instantiation
1827 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1828 // If the declarator-id is not a template-id, issue a diagnostic and
1829 // recover by ignoring the 'template' keyword.
1830 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1831 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1832 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1833 } else {
1834 SourceLocation LAngleLoc =
1835 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1836 Diag(D.getIdentifierLoc(),
1837 diag::err_explicit_instantiation_with_definition)
1838 << SourceRange(TemplateInfo.TemplateLoc)
1839 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1840
1841 // Recover as if it were an explicit specialization.
1842 TemplateParameterLists FakedParamLists;
1843 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1844 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1845 LAngleLoc));
1846
1847 ThisDecl =
1848 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1849 }
1850 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001851 break;
1852 }
1853 }
Mike Stump11289f42009-09-09 15:08:12 +00001854
Richard Smith74aeef52013-04-26 16:15:35 +00001855 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001856
Douglas Gregor23996282009-05-12 21:31:51 +00001857 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001858 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001859 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001860 ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001861
Anders Carlsson991285e2010-09-24 21:25:25 +00001862 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001863 if (D.isFunctionDeclarator())
1864 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1865 << 1 /* delete */;
1866 else
1867 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001868 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001869 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001870 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1871 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001872 else
1873 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001874 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001875 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001876 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001877 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001878 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001879
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001880 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001881 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001882 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001883 cutOffParsing();
1884 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001885 }
Chad Rosierc1183952012-06-26 22:30:43 +00001886
John McCalldadc5752010-08-24 06:29:42 +00001887 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001888
David Blaikiebbafb8a2012-03-11 07:00:24 +00001889 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001890 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001891 ExitScope();
1892 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001893
Douglas Gregor23996282009-05-12 21:31:51 +00001894 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001895 SkipUntil(tok::comma, true, true);
1896 Actions.ActOnInitializerError(ThisDecl);
1897 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001898 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1899 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001900 }
1901 } else if (Tok.is(tok::l_paren)) {
1902 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001903 BalancedDelimiterTracker T(*this, tok::l_paren);
1904 T.consumeOpen();
1905
Benjamin Kramerf0623432012-08-23 22:51:59 +00001906 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001907 CommaLocsTy CommaLocs;
1908
David Blaikiebbafb8a2012-03-11 07:00:24 +00001909 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001910 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001911 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001912 }
1913
Douglas Gregor23996282009-05-12 21:31:51 +00001914 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001915 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00001916 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001917
David Blaikiebbafb8a2012-03-11 07:00:24 +00001918 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001919 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001920 ExitScope();
1921 }
Douglas Gregor23996282009-05-12 21:31:51 +00001922 } else {
1923 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001924 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001925
1926 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1927 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001928
David Blaikiebbafb8a2012-03-11 07:00:24 +00001929 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001930 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001931 ExitScope();
1932 }
1933
Sebastian Redla9351792012-02-11 23:51:47 +00001934 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1935 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001936 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001937 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1938 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001939 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001940 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001941 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001942 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001943 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1944
Sebastian Redl3da34892011-06-05 12:23:16 +00001945 if (D.getCXXScopeSpec().isSet()) {
1946 EnterScope(0);
1947 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1948 }
1949
1950 ExprResult Init(ParseBraceInitializer());
1951
1952 if (D.getCXXScopeSpec().isSet()) {
1953 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1954 ExitScope();
1955 }
1956
1957 if (Init.isInvalid()) {
1958 Actions.ActOnInitializerError(ThisDecl);
1959 } else
1960 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1961 /*DirectInit=*/true, TypeContainsAuto);
1962
Douglas Gregor23996282009-05-12 21:31:51 +00001963 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001964 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001965 }
1966
Richard Smithb2bc2e62011-02-21 20:05:19 +00001967 Actions.FinalizeDeclaration(ThisDecl);
1968
Douglas Gregor23996282009-05-12 21:31:51 +00001969 return ThisDecl;
1970}
1971
Chris Lattner1890ac82006-08-13 01:16:23 +00001972/// ParseSpecifierQualifierList
1973/// specifier-qualifier-list:
1974/// type-specifier specifier-qualifier-list[opt]
1975/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001976/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001977///
Richard Smithc5b05522012-03-12 07:56:15 +00001978void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1979 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001980 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1981 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001982 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001983 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001984
Chris Lattner1890ac82006-08-13 01:16:23 +00001985 // Validate declspec for type-name.
1986 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001987 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1988 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001989 Diag(Tok, diag::err_expected_type);
1990 DS.SetTypeSpecError();
1991 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1992 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001993 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001994 if (!DS.hasTypeSpecifier())
1995 DS.SetTypeSpecError();
1996 }
Mike Stump11289f42009-09-09 15:08:12 +00001997
Chris Lattner1b22eed2006-11-28 05:12:07 +00001998 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001999 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00002000 if (DS.getStorageClassSpecLoc().isValid())
2001 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2002 else
Richard Smithb4a9e862013-04-12 22:46:28 +00002003 Diag(DS.getThreadStorageClassSpecLoc(),
2004 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00002005 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002006 }
Mike Stump11289f42009-09-09 15:08:12 +00002007
Chris Lattner1b22eed2006-11-28 05:12:07 +00002008 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002009 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002010 if (DS.isInlineSpecified())
2011 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2012 if (DS.isVirtualSpecified())
2013 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2014 if (DS.isExplicitSpecified())
2015 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002016 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002017 }
Richard Smithc5b05522012-03-12 07:56:15 +00002018
2019 // Issue diagnostic and remove constexpr specfier if present.
2020 if (DS.isConstexprSpecified()) {
2021 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2022 DS.ClearConstexprSpec();
2023 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002024}
Chris Lattner53361ac2006-08-10 05:19:57 +00002025
Chris Lattner6cc055a2009-04-12 20:42:31 +00002026/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2027/// specified token is valid after the identifier in a declarator which
2028/// immediately follows the declspec. For example, these things are valid:
2029///
2030/// int x [ 4]; // direct-declarator
2031/// int x ( int y); // direct-declarator
2032/// int(int x ) // direct-declarator
2033/// int x ; // simple-declaration
2034/// int x = 17; // init-declarator-list
2035/// int x , y; // init-declarator-list
2036/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002037/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002038/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002039///
2040/// This is not, because 'x' does not immediately follow the declspec (though
2041/// ')' happens to be valid anyway).
2042/// int (x)
2043///
2044static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2045 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2046 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002047 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002048}
2049
Chris Lattner20a0c612009-04-14 21:34:55 +00002050
2051/// ParseImplicitInt - This method is called when we have an non-typename
2052/// identifier in a declspec (which normally terminates the decl spec) when
2053/// the declspec has no type specifier. In this case, the declspec is either
2054/// malformed or is "implicit int" (in K&R and C89).
2055///
2056/// This method handles diagnosing this prettily and returns false if the
2057/// declspec is done being processed. If it recovers and thinks there may be
2058/// other pieces of declspec after it, it returns true.
2059///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002060bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002061 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002062 AccessSpecifier AS, DeclSpecContext DSC,
2063 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002064 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002065
Chris Lattner20a0c612009-04-14 21:34:55 +00002066 SourceLocation Loc = Tok.getLocation();
2067 // If we see an identifier that is not a type name, we normally would
2068 // parse it as the identifer being declared. However, when a typename
2069 // is typo'd or the definition is not included, this will incorrectly
2070 // parse the typename as the identifier name and fall over misparsing
2071 // later parts of the diagnostic.
2072 //
2073 // As such, we try to do some look-ahead in cases where this would
2074 // otherwise be an "implicit-int" case to see if this is invalid. For
2075 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2076 // an identifier with implicit int, we'd get a parse error because the
2077 // next token is obviously invalid for a type. Parse these as a case
2078 // with an invalid type specifier.
2079 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002080
Chris Lattner20a0c612009-04-14 21:34:55 +00002081 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002082 // error, do lookahead to try to do better recovery. This never applies
2083 // within a type specifier. Outside of C++, we allow this even if the
2084 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002085 // implicit int as an extension in C99 and C11.
Richard Smith2f07ad52012-05-09 20:55:26 +00002086 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith3b870382013-04-30 22:43:51 +00002087 !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002088 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002089 // If this token is valid for implicit int, e.g. "static x = 4", then
2090 // we just avoid eating the identifier, so it will be parsed as the
2091 // identifier in the declarator.
2092 return false;
2093 }
Mike Stump11289f42009-09-09 15:08:12 +00002094
Richard Smitha952ebb2012-05-15 21:01:51 +00002095 if (getLangOpts().CPlusPlus &&
2096 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2097 // Don't require a type specifier if we have the 'auto' storage class
2098 // specifier in C++98 -- we'll promote it to a type specifier.
2099 return false;
2100 }
2101
Chris Lattner20a0c612009-04-14 21:34:55 +00002102 // Otherwise, if we don't consume this token, we are going to emit an
2103 // error anyway. Try to recover from various common problems. Check
2104 // to see if this was a reference to a tag name without a tag specified.
2105 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002106 //
2107 // C++ doesn't need this, and isTagName doesn't take SS.
2108 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002109 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002110 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002111
Douglas Gregor0be31a22010-07-02 17:43:08 +00002112 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002113 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002114 case DeclSpec::TST_enum:
2115 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2116 case DeclSpec::TST_union:
2117 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2118 case DeclSpec::TST_struct:
2119 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002120 case DeclSpec::TST_interface:
2121 TagName="__interface"; FixitTagName = "__interface ";
2122 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002123 case DeclSpec::TST_class:
2124 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002125 }
Mike Stump11289f42009-09-09 15:08:12 +00002126
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002127 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002128 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2129 LookupResult R(Actions, TokenName, SourceLocation(),
2130 Sema::LookupOrdinaryName);
2131
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002132 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002133 << TokenName << TagName << getLangOpts().CPlusPlus
2134 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2135
2136 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2137 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2138 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002139 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002140 << TokenName << TagName;
2141 }
Mike Stump11289f42009-09-09 15:08:12 +00002142
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002143 // Parse this as a tag as if the missing tag were present.
2144 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002145 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002146 else
Richard Smithc5b05522012-03-12 07:56:15 +00002147 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002148 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002149 return true;
2150 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002151 }
Mike Stump11289f42009-09-09 15:08:12 +00002152
Richard Smithfe904f02012-05-15 21:29:55 +00002153 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002154 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00002155 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2156 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002157 // Look ahead to the next token to try to figure out what this declaration
2158 // was supposed to be.
2159 switch (NextToken().getKind()) {
2160 case tok::comma:
2161 case tok::equal:
2162 case tok::kw_asm:
2163 case tok::l_brace:
2164 case tok::l_square:
2165 case tok::semi:
2166 // This looks like a variable declaration. The type is probably missing.
2167 // We're done parsing decl-specifiers.
2168 return false;
2169
2170 case tok::l_paren: {
2171 // static x(4); // 'x' is not a type
2172 // x(int n); // 'x' is not a type
2173 // x (*p)[]; // 'x' is a type
2174 //
2175 // Since we're in an error case (or the rare 'implicit int in C++' MS
2176 // extension), we can afford to perform a tentative parse to determine
2177 // which case we're in.
2178 TentativeParsingAction PA(*this);
2179 ConsumeToken();
2180 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2181 PA.Revert();
2182 if (TPR == TPResult::False())
2183 return false;
2184 // The identifier is followed by a parenthesized declarator.
2185 // It's supposed to be a type.
2186 break;
2187 }
2188
2189 default:
2190 // This is probably supposed to be a type. This includes cases like:
2191 // int f(itn);
2192 // struct S { unsinged : 4; };
2193 break;
2194 }
2195 }
2196
Chad Rosierc1183952012-06-26 22:30:43 +00002197 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002198 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002199 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002200 IdentifierInfo *II = Tok.getIdentifierInfo();
2201 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002202 // The action emitted a diagnostic, so we don't have to.
2203 if (T) {
2204 // The action has suggested that the type T could be used. Set that as
2205 // the type in the declaration specifiers, consume the would-be type
2206 // name token, and we're done.
2207 const char *PrevSpec;
2208 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002209 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002210 DS.SetRangeEnd(Tok.getLocation());
2211 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002212 // There may be other declaration specifiers after this.
2213 return true;
2214 } else if (II != Tok.getIdentifierInfo()) {
2215 // If no type was suggested, the correction is to a keyword
2216 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002217 // There may be other declaration specifiers after this.
2218 return true;
2219 }
Chad Rosierc1183952012-06-26 22:30:43 +00002220
Douglas Gregor15e56022009-10-13 23:27:22 +00002221 // Fall through; the action had no suggestion for us.
2222 } else {
2223 // The action did not emit a diagnostic, so emit one now.
2224 SourceRange R;
2225 if (SS) R = SS->getRange();
2226 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2227 }
Mike Stump11289f42009-09-09 15:08:12 +00002228
Douglas Gregor15e56022009-10-13 23:27:22 +00002229 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002230 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002231 DS.SetRangeEnd(Tok.getLocation());
2232 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002233
Chris Lattner20a0c612009-04-14 21:34:55 +00002234 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2235 // avoid rippling error messages on subsequent uses of the same type,
2236 // could be useful if #include was forgotten.
2237 return false;
2238}
2239
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002240/// \brief Determine the declaration specifier context from the declarator
2241/// context.
2242///
2243/// \param Context the declarator context, which is one of the
2244/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002245Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002246Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2247 if (Context == Declarator::MemberContext)
2248 return DSC_class;
2249 if (Context == Declarator::FileContext)
2250 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002251 if (Context == Declarator::TrailingReturnContext)
2252 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002253 return DSC_normal;
2254}
2255
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002256/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2257///
2258/// FIXME: Simply returns an alignof() expression if the argument is a
2259/// type. Ideally, the type should be propagated directly into Sema.
2260///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002261/// [C11] type-id
2262/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002263/// [C++0x] type-id ...[opt]
2264/// [C++0x] assignment-expression ...[opt]
2265ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2266 SourceLocation &EllipsisLoc) {
2267 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002268 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002269 SourceLocation TypeLoc = Tok.getLocation();
2270 ParsedType Ty = ParseTypeName().get();
2271 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002272 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2273 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002274 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002275 ER = ParseConstantExpression();
2276
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002277 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002278 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002279
2280 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002281}
2282
2283/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2284/// attribute to Attrs.
2285///
2286/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002287/// [C11] '_Alignas' '(' type-id ')'
2288/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002289/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2290/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002291void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002292 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002293 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2294 "Not an alignment-specifier!");
2295
Richard Smithd11c7a12013-01-29 01:48:07 +00002296 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2297 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002298
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002299 BalancedDelimiterTracker T(*this, tok::l_paren);
2300 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002301 return;
2302
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002303 SourceLocation EllipsisLoc;
2304 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002305 if (ArgExpr.isInvalid()) {
2306 SkipUntil(tok::r_paren);
2307 return;
2308 }
2309
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002310 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002311 if (EndLoc)
2312 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002313
Aaron Ballman00e99962013-08-31 01:11:41 +00002314 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002315 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002316 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2317 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002318}
2319
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002320/// ParseDeclarationSpecifiers
2321/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002322/// storage-class-specifier declaration-specifiers[opt]
2323/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002324/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002325/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002326/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002327/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002328///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002329/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002330/// 'typedef'
2331/// 'extern'
2332/// 'static'
2333/// 'auto'
2334/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002335/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002336/// [C++11] 'thread_local'
2337/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002338/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002339/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002340/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002341/// [C++] 'virtual'
2342/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002343/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002344/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002345/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002346
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002347///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002348void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002349 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002350 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002351 DeclSpecContext DSContext,
2352 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002353 if (DS.getSourceRange().isInvalid()) {
2354 DS.SetRangeStart(Tok.getLocation());
2355 DS.SetRangeEnd(Tok.getLocation());
2356 }
Chad Rosierc1183952012-06-26 22:30:43 +00002357
Douglas Gregordf593fb2011-11-07 17:33:42 +00002358 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002359 bool AttrsLastTime = false;
2360 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002361 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002362 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002363 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002364 unsigned DiagID = 0;
2365
Chris Lattner4d8f8732006-11-28 05:05:08 +00002366 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002367
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002368 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002369 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002370 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002371 if (!AttrsLastTime)
2372 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002373 else {
2374 // Reject C++11 attributes that appertain to decl specifiers as
2375 // we don't support any C++11 attributes that appertain to decl
2376 // specifiers. This also conforms to what g++ 4.8 is doing.
2377 ProhibitCXX11Attributes(attrs);
2378
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002379 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002380 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002381
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002382 // If this is not a declaration specifier token, we're done reading decl
2383 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002384 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002385 return;
Mike Stump11289f42009-09-09 15:08:12 +00002386
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002387 case tok::l_square:
2388 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002389 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002390 goto DoneWithDeclSpec;
2391
2392 ProhibitAttributes(attrs);
2393 // FIXME: It would be good to recover by accepting the attributes,
2394 // but attempting to do that now would cause serious
2395 // madness in terms of diagnostics.
2396 attrs.clear();
2397 attrs.Range = SourceRange();
2398
2399 ParseCXX11Attributes(attrs);
2400 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002401 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002402
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002403 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002404 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002405 if (DS.hasTypeSpecifier()) {
2406 bool AllowNonIdentifiers
2407 = (getCurScope()->getFlags() & (Scope::ControlScope |
2408 Scope::BlockScope |
2409 Scope::TemplateParamScope |
2410 Scope::FunctionPrototypeScope |
2411 Scope::AtCatchScope)) == 0;
2412 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002413 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002414 (DSContext == DSC_class && DS.isFriendSpecified());
2415
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002416 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002417 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002418 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002419 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002420 }
2421
Douglas Gregor80039242011-02-15 20:33:25 +00002422 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2423 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2424 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002425 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002426 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002427 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002428 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002429 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002430 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002431
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002432 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002433 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002434 }
2435
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002436 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002437 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002438 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002439 if (!DS.hasTypeSpecifier())
2440 DS.SetTypeSpecError();
2441 goto DoneWithDeclSpec;
2442 }
John McCall8bc2a702010-03-01 18:20:46 +00002443 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2444 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002445 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002446
2447 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002448 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002449 goto DoneWithDeclSpec;
2450
John McCall9dab4e62009-12-12 11:40:51 +00002451 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002452 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2453 Tok.getAnnotationRange(),
2454 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002455
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002456 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002457 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002458 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002459 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002460 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002461 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002462
2463 // C++ [class.qual]p2:
2464 // In a lookup in which the constructor is an acceptable lookup
2465 // result and the nested-name-specifier nominates a class C:
2466 //
2467 // - if the name specified after the
2468 // nested-name-specifier, when looked up in C, is the
2469 // injected-class-name of C (Clause 9), or
2470 //
2471 // - if the name specified after the nested-name-specifier
2472 // is the same as the identifier or the
2473 // simple-template-id's template-name in the last
2474 // component of the nested-name-specifier,
2475 //
2476 // the name is instead considered to name the constructor of
2477 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002478 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002479 // Thus, if the template-name is actually the constructor
2480 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002481 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002482 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002483 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002484 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002485 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002486 if (isConstructorDeclarator()) {
2487 // The user meant this to be an out-of-line constructor
2488 // definition, but template arguments are not allowed
2489 // there. Just allow this as a constructor; we'll
2490 // complain about it later.
2491 goto DoneWithDeclSpec;
2492 }
2493
2494 // The user meant this to name a type, but it actually names
2495 // a constructor with some extraneous template
2496 // arguments. Complain, then parse it as a type as the user
2497 // intended.
2498 Diag(TemplateId->TemplateNameLoc,
2499 diag::err_out_of_line_template_id_names_constructor)
2500 << TemplateId->Name;
2501 }
2502
John McCall9dab4e62009-12-12 11:40:51 +00002503 DS.getTypeSpecScope() = SS;
2504 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002505 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002506 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002507 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002508 continue;
2509 }
2510
Douglas Gregorc5790df2009-09-28 07:26:33 +00002511 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002512 DS.getTypeSpecScope() = SS;
2513 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002514 if (Tok.getAnnotationValue()) {
2515 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002516 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002517 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002518 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002519 if (isInvalid)
2520 break;
John McCallba7bf592010-08-24 05:47:05 +00002521 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002522 else
2523 DS.SetTypeSpecError();
2524 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2525 ConsumeToken(); // The typename
2526 }
2527
Douglas Gregor167fa622009-03-25 15:40:00 +00002528 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002529 goto DoneWithDeclSpec;
2530
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002531 // If we're in a context where the identifier could be a class name,
2532 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002533 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002534 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002535 &SS)) {
2536 if (isConstructorDeclarator())
2537 goto DoneWithDeclSpec;
2538
2539 // As noted in C++ [class.qual]p2 (cited above), when the name
2540 // of the class is qualified in a context where it could name
2541 // a constructor, its a constructor name. However, we've
2542 // looked at the declarator, and the user probably meant this
2543 // to be a type. Complain that it isn't supposed to be treated
2544 // as a type, then proceed to parse it as a type.
2545 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2546 << Next.getIdentifierInfo();
2547 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002548
John McCallba7bf592010-08-24 05:47:05 +00002549 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2550 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002551 getCurScope(), &SS,
2552 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002553 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002554 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002555
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002556 // If the referenced identifier is not a type, then this declspec is
2557 // erroneous: We already checked about that it has no type specifier, and
2558 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002559 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002560 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002561 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002562 ParsedAttributesWithRange Attrs(AttrFactory);
2563 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2564 if (!Attrs.empty()) {
2565 AttrsLastTime = true;
2566 attrs.takeAllFrom(Attrs);
2567 }
2568 continue;
2569 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002570 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002571 }
Mike Stump11289f42009-09-09 15:08:12 +00002572
John McCall9dab4e62009-12-12 11:40:51 +00002573 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002574 ConsumeToken(); // The C++ scope.
2575
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002576 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002577 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002578 if (isInvalid)
2579 break;
Mike Stump11289f42009-09-09 15:08:12 +00002580
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002581 DS.SetRangeEnd(Tok.getLocation());
2582 ConsumeToken(); // The typename.
2583
2584 continue;
2585 }
Mike Stump11289f42009-09-09 15:08:12 +00002586
Chris Lattnere387d9e2009-01-21 19:48:37 +00002587 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002588 if (Tok.getAnnotationValue()) {
2589 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002590 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002591 DiagID, T);
2592 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002593 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002594
Chris Lattner005fc1b2010-04-05 18:18:31 +00002595 if (isInvalid)
2596 break;
2597
Chris Lattnere387d9e2009-01-21 19:48:37 +00002598 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2599 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002600
Chris Lattnere387d9e2009-01-21 19:48:37 +00002601 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2602 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002603 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002604 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002605 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002606
Chris Lattnere387d9e2009-01-21 19:48:37 +00002607 continue;
2608 }
Mike Stump11289f42009-09-09 15:08:12 +00002609
Douglas Gregor06873092011-04-28 15:48:45 +00002610 case tok::kw___is_signed:
2611 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2612 // typically treats it as a trait. If we see __is_signed as it appears
2613 // in libstdc++, e.g.,
2614 //
2615 // static const bool __is_signed;
2616 //
2617 // then treat __is_signed as an identifier rather than as a keyword.
2618 if (DS.getTypeSpecType() == TST_bool &&
2619 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2620 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2621 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2622 Tok.setKind(tok::identifier);
2623 }
2624
2625 // We're done with the declaration-specifiers.
2626 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002627
Chris Lattner16fac4f2008-07-26 01:18:38 +00002628 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002629 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002630 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002631 // In C++, check to see if this is a scope specifier like foo::bar::, if
2632 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002633 if (getLangOpts().CPlusPlus) {
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002634 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002635 if (!DS.hasTypeSpecifier())
2636 DS.SetTypeSpecError();
2637 goto DoneWithDeclSpec;
2638 }
2639 if (!Tok.is(tok::identifier))
2640 continue;
2641 }
Mike Stump11289f42009-09-09 15:08:12 +00002642
Chris Lattner16fac4f2008-07-26 01:18:38 +00002643 // This identifier can only be a typedef name if we haven't already seen
2644 // a type-specifier. Without this check we misparse:
2645 // typedef int X; struct Y { short X; }; as 'short int'.
2646 if (DS.hasTypeSpecifier())
2647 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002648
John Thompson22334602010-02-05 00:12:22 +00002649 // Check for need to substitute AltiVec keyword tokens.
2650 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2651 break;
2652
Richard Smith3092a3b2012-05-09 18:56:43 +00002653 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2654 // allow the use of a typedef name as a type specifier.
2655 if (DS.isTypeAltiVecVector())
2656 goto DoneWithDeclSpec;
2657
John McCallba7bf592010-08-24 05:47:05 +00002658 ParsedType TypeRep =
2659 Actions.getTypeName(*Tok.getIdentifierInfo(),
2660 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002661
Chris Lattner6cc055a2009-04-12 20:42:31 +00002662 // If this is not a typedef name, don't parse it as part of the declspec,
2663 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002664 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002665 ParsedAttributesWithRange Attrs(AttrFactory);
2666 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2667 if (!Attrs.empty()) {
2668 AttrsLastTime = true;
2669 attrs.takeAllFrom(Attrs);
2670 }
2671 continue;
2672 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002673 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002674 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002675
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002676 // If we're in a context where the identifier could be a class name,
2677 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002678 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002679 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002680 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002681 goto DoneWithDeclSpec;
2682
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002683 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002684 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002685 if (isInvalid)
2686 break;
Mike Stump11289f42009-09-09 15:08:12 +00002687
Chris Lattner16fac4f2008-07-26 01:18:38 +00002688 DS.SetRangeEnd(Tok.getLocation());
2689 ConsumeToken(); // The identifier
2690
2691 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2692 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002693 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002694 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002695 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002696
Steve Naroffcd5e7822008-09-22 10:28:57 +00002697 // Need to support trailing type qualifiers (e.g. "id<p> const").
2698 // If a type specifier follows, it will be diagnosed elsewhere.
2699 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002700 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002701
2702 // type-name
2703 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002704 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002705 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002706 // This template-id does not refer to a type name, so we're
2707 // done with the type-specifiers.
2708 goto DoneWithDeclSpec;
2709 }
2710
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002711 // If we're in a context where the template-id could be a
2712 // constructor name or specialization, check whether this is a
2713 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002714 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002715 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002716 isConstructorDeclarator())
2717 goto DoneWithDeclSpec;
2718
Douglas Gregor7f741122009-02-25 19:37:18 +00002719 // Turn the template-id annotation token into a type annotation
2720 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002721 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002722 continue;
2723 }
2724
Chris Lattnere37e2332006-08-15 04:50:22 +00002725 // GNU attributes support.
2726 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002727 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002728 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002729
2730 // Microsoft declspec support.
2731 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002732 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002733 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002734
Steve Naroff44ac7772008-12-25 14:16:32 +00002735 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002736 case tok::kw___forceinline: {
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002737 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002738 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002739 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002740 // FIXME: This does not work correctly if it is set to be a declspec
2741 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman00e99962013-08-31 01:11:41 +00002742 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2743 AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002744 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002745 }
Eli Friedman53339e02009-06-08 23:27:34 +00002746
Aaron Ballman317a77f2013-05-22 23:25:32 +00002747 case tok::kw___sptr:
2748 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002749 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002750 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002751 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002752 case tok::kw___cdecl:
2753 case tok::kw___stdcall:
2754 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002755 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002756 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002757 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002758 continue;
2759
Dawn Perchik335e16b2010-09-03 01:29:35 +00002760 // Borland single token adornments.
2761 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002762 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002763 continue;
2764
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002765 // OpenCL single token adornments.
2766 case tok::kw___kernel:
2767 ParseOpenCLAttributes(DS.getAttributes());
2768 continue;
2769
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002770 // storage-class-specifier
2771 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002772 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2773 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002774 break;
2775 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002776 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002777 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002778 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2779 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002780 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002781 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002782 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2783 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002784 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002785 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002786 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002787 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002788 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2789 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002790 break;
2791 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002792 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002793 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002794 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2795 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002796 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002797 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002798 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002799 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002800 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2801 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002802 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002803 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2804 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002805 break;
2806 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002807 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2808 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002809 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002810 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002811 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2812 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002813 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002814 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002815 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2816 PrevSpec, DiagID);
2817 break;
2818 case tok::kw_thread_local:
2819 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2820 PrevSpec, DiagID);
2821 break;
2822 case tok::kw__Thread_local:
2823 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2824 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002825 break;
Mike Stump11289f42009-09-09 15:08:12 +00002826
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002827 // function-specifier
2828 case tok::kw_inline:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002829 isInvalid = DS.setFunctionSpecInline(Loc);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002830 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002831 case tok::kw_virtual:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002832 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002833 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002834 case tok::kw_explicit:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002835 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002836 break;
Richard Smith0015f092013-01-17 22:16:11 +00002837 case tok::kw__Noreturn:
2838 if (!getLangOpts().C11)
2839 Diag(Loc, diag::ext_c11_noreturn);
2840 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2841 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002842
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002843 // alignment-specifier
2844 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002845 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002846 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002847 ParseAlignmentSpecifier(DS.getAttributes());
2848 continue;
2849
Anders Carlssoncd8db412009-05-06 04:46:28 +00002850 // friend
2851 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002852 if (DSContext == DSC_class)
2853 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2854 else {
2855 PrevSpec = ""; // not actually used by the diagnostic
2856 DiagID = diag::err_friend_invalid_in_context;
2857 isInvalid = true;
2858 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002859 break;
Mike Stump11289f42009-09-09 15:08:12 +00002860
Douglas Gregor26701a42011-09-09 02:06:17 +00002861 // Modules
2862 case tok::kw___module_private__:
2863 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2864 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002865
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002866 // constexpr
2867 case tok::kw_constexpr:
2868 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2869 break;
2870
Chris Lattnere387d9e2009-01-21 19:48:37 +00002871 // type-specifier
2872 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002873 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2874 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002875 break;
2876 case tok::kw_long:
2877 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002878 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2879 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002880 else
John McCall49bfce42009-08-03 20:12:06 +00002881 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2882 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002883 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002884 case tok::kw___int64:
2885 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2886 DiagID);
2887 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002888 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002889 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2890 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002891 break;
2892 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002893 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2894 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002895 break;
2896 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002897 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2898 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002899 break;
2900 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002901 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2902 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002903 break;
2904 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002905 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2906 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002907 break;
2908 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002909 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2910 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002911 break;
2912 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002913 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2914 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002915 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002916 case tok::kw___int128:
2917 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2918 DiagID);
2919 break;
2920 case tok::kw_half:
2921 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2922 DiagID);
2923 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002924 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002925 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2926 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002927 break;
2928 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002929 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2930 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002931 break;
2932 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002933 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2934 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002935 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002936 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002937 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2938 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002939 break;
2940 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002941 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2942 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002943 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002944 case tok::kw_bool:
2945 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002946 if (Tok.is(tok::kw_bool) &&
2947 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2948 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2949 PrevSpec = ""; // Not used by the diagnostic.
2950 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002951 // For better error recovery.
2952 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002953 isInvalid = true;
2954 } else {
2955 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2956 DiagID);
2957 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002958 break;
2959 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002960 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2961 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002962 break;
2963 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002964 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2965 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002966 break;
2967 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002968 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2969 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002970 break;
John Thompson22334602010-02-05 00:12:22 +00002971 case tok::kw___vector:
2972 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2973 break;
2974 case tok::kw___pixel:
2975 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2976 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00002977 case tok::kw_image1d_t:
2978 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2979 PrevSpec, DiagID);
2980 break;
2981 case tok::kw_image1d_array_t:
2982 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2983 PrevSpec, DiagID);
2984 break;
2985 case tok::kw_image1d_buffer_t:
2986 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2987 PrevSpec, DiagID);
2988 break;
2989 case tok::kw_image2d_t:
2990 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2991 PrevSpec, DiagID);
2992 break;
2993 case tok::kw_image2d_array_t:
2994 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2995 PrevSpec, DiagID);
2996 break;
2997 case tok::kw_image3d_t:
2998 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2999 PrevSpec, DiagID);
3000 break;
Guy Benyei61054192013-02-07 10:55:47 +00003001 case tok::kw_sampler_t:
3002 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
3003 PrevSpec, DiagID);
3004 break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003005 case tok::kw_event_t:
3006 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
3007 PrevSpec, DiagID);
3008 break;
John McCall39439732011-04-09 22:50:59 +00003009 case tok::kw___unknown_anytype:
3010 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3011 PrevSpec, DiagID);
3012 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003013
3014 // class-specifier:
3015 case tok::kw_class:
3016 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003017 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003018 case tok::kw_union: {
3019 tok::TokenKind Kind = Tok.getKind();
3020 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003021
3022 // These are attributes following class specifiers.
3023 // To produce better diagnostic, we parse them when
3024 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003025 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003026 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003027 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003028
3029 // If there are attributes following class specifier,
3030 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003031 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003032 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003033 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003034 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003035 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003036 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003037
3038 // enum-specifier:
3039 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003040 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003041 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003042 continue;
3043
3044 // cv-qualifier:
3045 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003046 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003047 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003048 break;
3049 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003050 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003051 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003052 break;
3053 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003054 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003055 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003056 break;
3057
Douglas Gregor333489b2009-03-27 23:10:48 +00003058 // C++ typename-specifier:
3059 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003060 if (TryAnnotateTypeOrScopeToken()) {
3061 DS.SetTypeSpecError();
3062 goto DoneWithDeclSpec;
3063 }
3064 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003065 continue;
3066 break;
3067
Chris Lattnere387d9e2009-01-21 19:48:37 +00003068 // GNU typeof support.
3069 case tok::kw_typeof:
3070 ParseTypeofSpecifier(DS);
3071 continue;
3072
David Blaikie15a430a2011-12-04 05:04:18 +00003073 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003074 ParseDecltypeSpecifier(DS);
3075 continue;
3076
Alexis Hunt4a257072011-05-19 05:37:45 +00003077 case tok::kw___underlying_type:
3078 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003079 continue;
3080
3081 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003082 // C11 6.7.2.4/4:
3083 // If the _Atomic keyword is immediately followed by a left parenthesis,
3084 // it is interpreted as a type specifier (with a type name), not as a
3085 // type qualifier.
3086 if (NextToken().is(tok::l_paren)) {
3087 ParseAtomicSpecifier(DS);
3088 continue;
3089 }
3090 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3091 getLangOpts());
3092 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003093
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003094 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003095 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003096 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003097 goto DoneWithDeclSpec;
3098 case tok::kw___private:
3099 case tok::kw___global:
3100 case tok::kw___local:
3101 case tok::kw___constant:
3102 case tok::kw___read_only:
3103 case tok::kw___write_only:
3104 case tok::kw___read_write:
3105 ParseOpenCLQualifiers(DS);
3106 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003107
Steve Naroffcfdf6162008-06-05 00:02:44 +00003108 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003109 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003110 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3111 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003112 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003113 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003114
Douglas Gregor3a001f42010-11-19 17:10:50 +00003115 if (!ParseObjCProtocolQualifiers(DS))
3116 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3117 << FixItHint::CreateInsertion(Loc, "id")
3118 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003119
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003120 // Need to support trailing type qualifiers (e.g. "id<p> const").
3121 // If a type specifier follows, it will be diagnosed elsewhere.
3122 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003123 }
John McCall49bfce42009-08-03 20:12:06 +00003124 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003125 if (isInvalid) {
3126 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003127 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003128
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003129 if (DiagID == diag::ext_duplicate_declspec)
3130 Diag(Tok, DiagID)
3131 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3132 else
3133 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003134 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003135
Chris Lattner2e232092008-03-13 06:29:04 +00003136 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003137 if (DiagID != diag::err_bool_redeclaration)
3138 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003139
3140 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003141 }
3142}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003143
Chris Lattner70ae4912007-10-29 04:42:53 +00003144/// ParseStructDeclaration - Parse a struct declaration without the terminating
3145/// semicolon.
3146///
Chris Lattner90a26b02007-01-23 04:38:16 +00003147/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003148/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003149/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003150/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003151/// struct-declarator-list:
3152/// struct-declarator
3153/// struct-declarator-list ',' struct-declarator
3154/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3155/// struct-declarator:
3156/// declarator
3157/// [GNU] declarator attributes[opt]
3158/// declarator[opt] ':' constant-expression
3159/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3160///
Chris Lattnera12405b2008-04-10 06:46:29 +00003161void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003162ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003163
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003164 if (Tok.is(tok::kw___extension__)) {
3165 // __extension__ silences extension warnings in the subexpression.
3166 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003167 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003168 return ParseStructDeclaration(DS, Fields);
3169 }
Mike Stump11289f42009-09-09 15:08:12 +00003170
Steve Naroff97170802007-08-20 22:28:22 +00003171 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003172 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003173
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003174 // If there are no declarators, this is a free-standing declaration
3175 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003176 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003177 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3178 DS);
3179 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003180 return;
3181 }
3182
3183 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003184 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003185 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003186 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003187 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003188 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003189
Bill Wendling44426052012-12-20 19:22:21 +00003190 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003191 if (!FirstDeclarator)
3192 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003193
Steve Naroff97170802007-08-20 22:28:22 +00003194 /// struct-declarator: declarator
3195 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003196 if (Tok.isNot(tok::colon)) {
3197 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3198 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003199 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003200 }
Mike Stump11289f42009-09-09 15:08:12 +00003201
Chris Lattner76c72282007-10-09 17:33:22 +00003202 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00003203 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00003204 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003205 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00003206 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00003207 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003208 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003209 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003210
Steve Naroff97170802007-08-20 22:28:22 +00003211 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003212 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003213
John McCallcfefb6d2009-11-03 02:38:08 +00003214 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003215 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003216
Steve Naroff97170802007-08-20 22:28:22 +00003217 // If we don't have a comma, it is either the end of the list (a ';')
3218 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00003219 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00003220 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003221
Steve Naroff97170802007-08-20 22:28:22 +00003222 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00003223 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003224
John McCallcfefb6d2009-11-03 02:38:08 +00003225 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003226 }
Steve Naroff97170802007-08-20 22:28:22 +00003227}
3228
3229/// ParseStructUnionBody
3230/// struct-contents:
3231/// struct-declaration-list
3232/// [EXT] empty
3233/// [GNU] "struct-declaration-list" without terminatoring ';'
3234/// struct-declaration-list:
3235/// struct-declaration
3236/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003237/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003238///
Chris Lattner1300fb92007-01-23 23:42:53 +00003239void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003240 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003241 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3242 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003243 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003244
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003245 BalancedDelimiterTracker T(*this, tok::l_brace);
3246 if (T.consumeOpen())
3247 return;
Mike Stump11289f42009-09-09 15:08:12 +00003248
Douglas Gregor658b9552009-01-09 22:42:13 +00003249 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003250 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003251
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003252 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003253
Chris Lattner7b9ace62007-01-23 20:11:08 +00003254 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00003255 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003256 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003257
Chris Lattner736ed5d2007-06-09 05:59:07 +00003258 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003259 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003260 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003261 continue;
3262 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003263
Andy Gibbsc804e082013-04-03 09:46:04 +00003264 // Parse _Static_assert declaration.
3265 if (Tok.is(tok::kw__Static_assert)) {
3266 SourceLocation DeclEnd;
3267 ParseStaticAssertDeclaration(DeclEnd);
3268 continue;
3269 }
3270
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003271 if (Tok.is(tok::annot_pragma_pack)) {
3272 HandlePragmaPack();
3273 continue;
3274 }
3275
3276 if (Tok.is(tok::annot_pragma_align)) {
3277 HandlePragmaAlign();
3278 continue;
3279 }
3280
John McCallcfefb6d2009-11-03 02:38:08 +00003281 if (!Tok.is(tok::at)) {
3282 struct CFieldCallback : FieldCallback {
3283 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003284 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003285 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003286
John McCall48871652010-08-21 09:40:31 +00003287 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003288 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003289 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3290
Eli Friedman934dbbf2012-08-08 23:53:27 +00003291 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003292 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003293 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003294 FD.D.getDeclSpec().getSourceRange().getBegin(),
3295 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003296 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003297 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003298 }
John McCallcfefb6d2009-11-03 02:38:08 +00003299 } Callback(*this, TagDecl, FieldDecls);
3300
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003301 // Parse all the comma separated declarators.
3302 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003303 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003304 } else { // Handle @defs
3305 ConsumeToken();
3306 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3307 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00003308 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003309 continue;
3310 }
3311 ConsumeToken();
3312 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3313 if (!Tok.is(tok::identifier)) {
3314 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00003315 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003316 continue;
3317 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003318 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003319 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003320 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003321 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3322 ConsumeToken();
3323 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003324 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003325
Chris Lattner76c72282007-10-09 17:33:22 +00003326 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003327 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003328 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003329 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003330 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003331 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003332 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3333 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00003334 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00003335 // If we stopped at a ';', eat it.
3336 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003337 }
3338 }
Mike Stump11289f42009-09-09 15:08:12 +00003339
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003340 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003341
John McCall084e83d2011-03-24 11:26:52 +00003342 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003343 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003344 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003345
Douglas Gregor0be31a22010-07-02 17:43:08 +00003346 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003347 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003348 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003349 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003350 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003351 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3352 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003353}
3354
Chris Lattner3b561a32006-08-13 00:12:11 +00003355/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003356/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003357/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003358///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003359/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3360/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003361/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3362/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003363/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003364/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003365///
Richard Smith7d137e32012-03-23 03:33:32 +00003366/// [C++11] enum-head '{' enumerator-list[opt] '}'
3367/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003368///
Richard Smith7d137e32012-03-23 03:33:32 +00003369/// enum-head: [C++11]
3370/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3371/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3372/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003373///
Richard Smith7d137e32012-03-23 03:33:32 +00003374/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003375/// 'enum'
3376/// 'enum' 'class'
3377/// 'enum' 'struct'
3378///
Richard Smith7d137e32012-03-23 03:33:32 +00003379/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003380/// ':' type-specifier-seq
3381///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003382/// [C++] elaborated-type-specifier:
3383/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3384///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003385void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003386 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003387 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003388 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003389 if (Tok.is(tok::code_completion)) {
3390 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003391 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003392 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003393 }
John McCallcb432fa2011-07-06 05:58:41 +00003394
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003395 // If attributes exist after tag, parse them.
3396 ParsedAttributesWithRange attrs(AttrFactory);
3397 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003398 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003399
3400 // If declspecs exist after tag, parse them.
3401 while (Tok.is(tok::kw___declspec))
3402 ParseMicrosoftDeclSpec(attrs);
3403
Richard Smith0f8ee222012-01-10 01:33:14 +00003404 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003405 bool IsScopedUsingClassTag = false;
3406
John McCallbeae29a2012-06-23 22:30:04 +00003407 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003408 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3409 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3410 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003411 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003412 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003413
Bill Wendling44426052012-12-20 19:22:21 +00003414 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003415 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003416 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003417
3418 // They are allowed afterwards, though.
3419 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003420 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003421 while (Tok.is(tok::kw___declspec))
3422 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003423 }
Richard Smith7d137e32012-03-23 03:33:32 +00003424
John McCall6347b682012-05-07 06:16:58 +00003425 // C++11 [temp.explicit]p12:
3426 // The usual access controls do not apply to names used to specify
3427 // explicit instantiations.
3428 // We extend this to also cover explicit specializations. Note that
3429 // we don't suppress if this turns out to be an elaborated type
3430 // specifier.
3431 bool shouldDelayDiagsInTag =
3432 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3433 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3434 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003435
Richard Smithbfdb1082012-03-12 08:56:40 +00003436 // Enum definitions should not be parsed in a trailing-return-type.
3437 bool AllowDeclaration = DSC != DSC_trailing;
3438
3439 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003440 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003441 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003442
Abramo Bagnarad7548482010-05-19 21:37:53 +00003443 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003444 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003445 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3446 // if a fixed underlying type is allowed.
3447 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003448
3449 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003450 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003451 return;
3452
3453 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003454 Diag(Tok, diag::err_expected_ident);
3455 if (Tok.isNot(tok::l_brace)) {
3456 // Has no name and is not a definition.
3457 // Skip the rest of this declarator, up until the comma or semicolon.
3458 SkipUntil(tok::comma, true);
3459 return;
3460 }
3461 }
3462 }
Mike Stump11289f42009-09-09 15:08:12 +00003463
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003464 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003465 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003466 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003467 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003468
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003469 // Skip the rest of this declarator, up until the comma or semicolon.
3470 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003471 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003472 }
Mike Stump11289f42009-09-09 15:08:12 +00003473
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003474 // If an identifier is present, consume and remember it.
3475 IdentifierInfo *Name = 0;
3476 SourceLocation NameLoc;
3477 if (Tok.is(tok::identifier)) {
3478 Name = Tok.getIdentifierInfo();
3479 NameLoc = ConsumeToken();
3480 }
Mike Stump11289f42009-09-09 15:08:12 +00003481
Richard Smith0f8ee222012-01-10 01:33:14 +00003482 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003483 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3484 // declaration of a scoped enumeration.
3485 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003486 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003487 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003488 }
3489
John McCall6347b682012-05-07 06:16:58 +00003490 // Okay, end the suppression area. We'll decide whether to emit the
3491 // diagnostics in a second.
3492 if (shouldDelayDiagsInTag)
3493 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003494
Douglas Gregor0bf31402010-10-08 23:50:27 +00003495 TypeResult BaseType;
3496
Douglas Gregord1f69f62010-12-01 17:42:47 +00003497 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003498 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003499 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003500 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003501 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003502 // If we're in class scope, this can either be an enum declaration with
3503 // an underlying type, or a declaration of a bitfield member. We try to
3504 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003505 // (integer literal, sizeof); if it's still ambiguous, we then consider
3506 // anything that's a simple-type-specifier followed by '(' as an
3507 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003508 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003509 EnterExpressionEvaluationContext Unevaluated(Actions,
3510 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003511 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003512 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003513 // bit-field. This is the common case.
3514 if (TPR == TPResult::True())
3515 PossibleBitfield = true;
3516 // If the next token starts a type-specifier-seq, it may be either a
3517 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003518 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003519 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003520 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003521 GetLookAheadToken(2).getKind() == tok::semi) {
3522 // Consume the ':'.
3523 ConsumeToken();
3524 } else {
3525 // We have the start of a type-specifier-seq, so we have to perform
3526 // tentative parsing to determine whether we have an expression or a
3527 // type.
3528 TentativeParsingAction TPA(*this);
3529
3530 // Consume the ':'.
3531 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003532
3533 // If we see a type specifier followed by an open-brace, we have an
3534 // ambiguity between an underlying type and a C++11 braced
3535 // function-style cast. Resolve this by always treating it as an
3536 // underlying type.
3537 // FIXME: The standard is not entirely clear on how to disambiguate in
3538 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003539 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003540 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003541 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003542 // We'll parse this as a bitfield later.
3543 PossibleBitfield = true;
3544 TPA.Revert();
3545 } else {
3546 // We have a type-specifier-seq.
3547 TPA.Commit();
3548 }
3549 }
3550 } else {
3551 // Consume the ':'.
3552 ConsumeToken();
3553 }
3554
3555 if (!PossibleBitfield) {
3556 SourceRange Range;
3557 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003558
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003559 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003560 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003561 } else if (!getLangOpts().ObjC2) {
3562 if (getLangOpts().CPlusPlus)
3563 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3564 else
3565 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3566 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003567 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003568 }
3569
Richard Smith0f8ee222012-01-10 01:33:14 +00003570 // There are four options here. If we have 'friend enum foo;' then this is a
3571 // friend declaration, and cannot have an accompanying definition. If we have
3572 // 'enum foo;', then this is a forward declaration. If we have
3573 // 'enum foo {...' then this is a definition. Otherwise we have something
3574 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003575 //
3576 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3577 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3578 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3579 //
John McCallfaf5fb42010-08-26 23:41:50 +00003580 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003581 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003582 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003583 } else if (Tok.is(tok::l_brace)) {
3584 if (DS.isFriendSpecified()) {
3585 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3586 << SourceRange(DS.getFriendSpecLoc());
3587 ConsumeBrace();
3588 SkipUntil(tok::r_brace);
3589 TUK = Sema::TUK_Friend;
3590 } else {
3591 TUK = Sema::TUK_Definition;
3592 }
Richard Smith369b9f92012-06-25 21:37:02 +00003593 } else if (DSC != DSC_type_specifier &&
3594 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003595 (Tok.isAtStartOfLine() &&
3596 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003597 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3598 if (Tok.isNot(tok::semi)) {
3599 // A semicolon was missing after this declaration. Diagnose and recover.
3600 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3601 "enum");
3602 PP.EnterToken(Tok);
3603 Tok.setKind(tok::semi);
3604 }
John McCall6347b682012-05-07 06:16:58 +00003605 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003606 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003607 }
3608
3609 // If this is an elaborated type specifier, and we delayed
3610 // diagnostics before, just merge them into the current pool.
3611 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3612 diagsFromTag.redelay();
3613 }
Richard Smith7d137e32012-03-23 03:33:32 +00003614
3615 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003616 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003617 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003618 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003619 // Skip the rest of this declarator, up until the comma or semicolon.
3620 Diag(Tok, diag::err_enum_template);
3621 SkipUntil(tok::comma, true);
3622 return;
3623 }
3624
3625 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3626 // Enumerations can't be explicitly instantiated.
3627 DS.SetTypeSpecError();
3628 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3629 return;
3630 }
3631
3632 assert(TemplateInfo.TemplateParams && "no template parameters");
3633 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3634 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003635 }
Chad Rosierc1183952012-06-26 22:30:43 +00003636
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003637 if (TUK == Sema::TUK_Reference)
3638 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003639
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003640 if (!Name && TUK != Sema::TUK_Definition) {
3641 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003642
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003643 // Skip the rest of this declarator, up until the comma or semicolon.
3644 SkipUntil(tok::comma, true);
3645 return;
3646 }
Richard Smith7d137e32012-03-23 03:33:32 +00003647
Douglas Gregord6ab8742009-05-28 23:31:59 +00003648 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003649 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003650 const char *PrevSpec = 0;
3651 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003652 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003653 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003654 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003655 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003656 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003657
Douglas Gregorba41d012010-04-24 16:38:41 +00003658 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003659 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003660 // dependent tag.
3661 if (!Name) {
3662 DS.SetTypeSpecError();
3663 Diag(Tok, diag::err_expected_type_name_after_typename);
3664 return;
3665 }
Chad Rosierc1183952012-06-26 22:30:43 +00003666
Douglas Gregor0be31a22010-07-02 17:43:08 +00003667 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003668 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003669 NameLoc);
3670 if (Type.isInvalid()) {
3671 DS.SetTypeSpecError();
3672 return;
3673 }
Chad Rosierc1183952012-06-26 22:30:43 +00003674
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003675 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3676 NameLoc.isValid() ? NameLoc : StartLoc,
3677 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003678 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003679
Douglas Gregorba41d012010-04-24 16:38:41 +00003680 return;
3681 }
Mike Stump11289f42009-09-09 15:08:12 +00003682
John McCall48871652010-08-21 09:40:31 +00003683 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003684 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003685 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003686 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003687 ConsumeBrace();
3688 SkipUntil(tok::r_brace);
3689 }
Chad Rosierc1183952012-06-26 22:30:43 +00003690
Douglas Gregorba41d012010-04-24 16:38:41 +00003691 DS.SetTypeSpecError();
3692 return;
3693 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003694
Richard Smith369b9f92012-06-25 21:37:02 +00003695 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003696 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003697
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003698 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3699 NameLoc.isValid() ? NameLoc : StartLoc,
3700 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003701 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003702}
3703
Chris Lattnerc1915e22007-01-25 07:29:02 +00003704/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3705/// enumerator-list:
3706/// enumerator
3707/// enumerator-list ',' enumerator
3708/// enumerator:
3709/// enumeration-constant
3710/// enumeration-constant '=' constant-expression
3711/// enumeration-constant:
3712/// identifier
3713///
John McCall48871652010-08-21 09:40:31 +00003714void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003715 // Enter the scope of the enum body and start the definition.
3716 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003717 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003718
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003719 BalancedDelimiterTracker T(*this, tok::l_brace);
3720 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003721
Chris Lattner37256fb2007-08-27 17:24:30 +00003722 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003723 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003724 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003725
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003726 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003727
John McCall48871652010-08-21 09:40:31 +00003728 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003729
Chris Lattnerc1915e22007-01-25 07:29:02 +00003730 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003731 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003732 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3733 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003734
John McCall811a0f52010-10-22 23:36:17 +00003735 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003736 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003737 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003738 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003739 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003740
Chris Lattnerc1915e22007-01-25 07:29:02 +00003741 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003742 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003743 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003744
Chris Lattner76c72282007-10-09 17:33:22 +00003745 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003746 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003747 AssignedVal = ParseConstantExpression();
3748 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003749 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003750 }
Mike Stump11289f42009-09-09 15:08:12 +00003751
Chris Lattnerc1915e22007-01-25 07:29:02 +00003752 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003753 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3754 LastEnumConstDecl,
3755 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003756 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003757 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003758 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003759
Chris Lattner4ef40012007-06-11 01:28:17 +00003760 EnumConstantDecls.push_back(EnumConstDecl);
3761 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003762
Douglas Gregorce66d022010-09-07 14:51:08 +00003763 if (Tok.is(tok::identifier)) {
3764 // We're missing a comma between enumerators.
3765 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003766 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003767 << FixItHint::CreateInsertion(Loc, ", ");
3768 continue;
3769 }
Chad Rosierc1183952012-06-26 22:30:43 +00003770
Chris Lattner76c72282007-10-09 17:33:22 +00003771 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003772 break;
3773 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003774
Richard Smith5d164bc2011-10-15 05:09:34 +00003775 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003776 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003777 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3778 diag::ext_enumerator_list_comma_cxx :
3779 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003780 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003781 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003782 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3783 << FixItHint::CreateRemoval(CommaLoc);
3784 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003785 }
Mike Stump11289f42009-09-09 15:08:12 +00003786
Chris Lattnerc1915e22007-01-25 07:29:02 +00003787 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003788 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003789
Chris Lattnerc1915e22007-01-25 07:29:02 +00003790 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003791 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003792 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003793
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003794 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003795 EnumDecl, EnumConstantDecls,
3796 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003797 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003798
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003799 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003800 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3801 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003802
3803 // The next token must be valid after an enum definition. If not, a ';'
3804 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003805 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3806 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003807 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3808 // Push this token back into the preprocessor and change our current token
3809 // to ';' so that the rest of the code recovers as though there were an
3810 // ';' after the definition.
3811 PP.EnterToken(Tok);
3812 Tok.setKind(tok::semi);
3813 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003814}
Chris Lattner3b561a32006-08-13 00:12:11 +00003815
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003816/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003817/// start of a type-qualifier-list.
3818bool Parser::isTypeQualifier() const {
3819 switch (Tok.getKind()) {
3820 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003821
3822 // type-qualifier only in OpenCL
3823 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003824 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003825
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003826 // type-qualifier
3827 case tok::kw_const:
3828 case tok::kw_volatile:
3829 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003830 case tok::kw___private:
3831 case tok::kw___local:
3832 case tok::kw___global:
3833 case tok::kw___constant:
3834 case tok::kw___read_only:
3835 case tok::kw___read_write:
3836 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003837 return true;
3838 }
3839}
3840
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003841/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3842/// is definitely a type-specifier. Return false if it isn't part of a type
3843/// specifier or if we're not sure.
3844bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3845 switch (Tok.getKind()) {
3846 default: return false;
3847 // type-specifiers
3848 case tok::kw_short:
3849 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003850 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003851 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003852 case tok::kw_signed:
3853 case tok::kw_unsigned:
3854 case tok::kw__Complex:
3855 case tok::kw__Imaginary:
3856 case tok::kw_void:
3857 case tok::kw_char:
3858 case tok::kw_wchar_t:
3859 case tok::kw_char16_t:
3860 case tok::kw_char32_t:
3861 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003862 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003863 case tok::kw_float:
3864 case tok::kw_double:
3865 case tok::kw_bool:
3866 case tok::kw__Bool:
3867 case tok::kw__Decimal32:
3868 case tok::kw__Decimal64:
3869 case tok::kw__Decimal128:
3870 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003871
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003872 // OpenCL specific types:
3873 case tok::kw_image1d_t:
3874 case tok::kw_image1d_array_t:
3875 case tok::kw_image1d_buffer_t:
3876 case tok::kw_image2d_t:
3877 case tok::kw_image2d_array_t:
3878 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003879 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003880 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003881
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003882 // struct-or-union-specifier (C99) or class-specifier (C++)
3883 case tok::kw_class:
3884 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003885 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003886 case tok::kw_union:
3887 // enum-specifier
3888 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003889
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003890 // typedef-name
3891 case tok::annot_typename:
3892 return true;
3893 }
3894}
3895
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003896/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003897/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003898bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003899 switch (Tok.getKind()) {
3900 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003901
Chris Lattner020bab92009-01-04 23:41:41 +00003902 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003903 if (TryAltiVecVectorToken())
3904 return true;
3905 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003906 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003907 // Annotate typenames and C++ scope specifiers. If we get one, just
3908 // recurse to handle whatever we get.
3909 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003910 return true;
3911 if (Tok.is(tok::identifier))
3912 return false;
3913 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003914
Chris Lattner020bab92009-01-04 23:41:41 +00003915 case tok::coloncolon: // ::foo::bar
3916 if (NextToken().is(tok::kw_new) || // ::new
3917 NextToken().is(tok::kw_delete)) // ::delete
3918 return false;
3919
Chris Lattner020bab92009-01-04 23:41:41 +00003920 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003921 return true;
3922 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003923
Chris Lattnere37e2332006-08-15 04:50:22 +00003924 // GNU attributes support.
3925 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003926 // GNU typeof support.
3927 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003928
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003929 // type-specifiers
3930 case tok::kw_short:
3931 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003932 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003933 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003934 case tok::kw_signed:
3935 case tok::kw_unsigned:
3936 case tok::kw__Complex:
3937 case tok::kw__Imaginary:
3938 case tok::kw_void:
3939 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003940 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003941 case tok::kw_char16_t:
3942 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003943 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003944 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003945 case tok::kw_float:
3946 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003947 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003948 case tok::kw__Bool:
3949 case tok::kw__Decimal32:
3950 case tok::kw__Decimal64:
3951 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003952 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003953
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003954 // OpenCL specific types:
3955 case tok::kw_image1d_t:
3956 case tok::kw_image1d_array_t:
3957 case tok::kw_image1d_buffer_t:
3958 case tok::kw_image2d_t:
3959 case tok::kw_image2d_array_t:
3960 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003961 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003962 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003963
Chris Lattner861a2262008-04-13 18:59:07 +00003964 // struct-or-union-specifier (C99) or class-specifier (C++)
3965 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003966 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003967 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003968 case tok::kw_union:
3969 // enum-specifier
3970 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003971
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003972 // type-qualifier
3973 case tok::kw_const:
3974 case tok::kw_volatile:
3975 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003976
John McCallea0a39e2012-11-14 00:49:39 +00003977 // Debugger support.
3978 case tok::kw___unknown_anytype:
3979
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003980 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003981 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003982 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003983
Chris Lattner409bf7d2008-10-20 00:25:30 +00003984 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3985 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003986 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003987
Steve Naroff44ac7772008-12-25 14:16:32 +00003988 case tok::kw___cdecl:
3989 case tok::kw___stdcall:
3990 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003991 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003992 case tok::kw___w64:
3993 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003994 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003995 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003996 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003997
3998 case tok::kw___private:
3999 case tok::kw___local:
4000 case tok::kw___global:
4001 case tok::kw___constant:
4002 case tok::kw___read_only:
4003 case tok::kw___read_write:
4004 case tok::kw___write_only:
4005
Eli Friedman53339e02009-06-08 23:27:34 +00004006 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004007
4008 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004009 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00004010
Richard Smith8e1ac332013-03-28 01:55:44 +00004011 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004012 case tok::kw__Atomic:
4013 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004014 }
4015}
4016
Chris Lattneracd58a32006-08-06 17:24:14 +00004017/// isDeclarationSpecifier() - Return true if the current token is part of a
4018/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004019///
4020/// \param DisambiguatingWithExpression True to indicate that the purpose of
4021/// this check is to disambiguate between an expression and a declaration.
4022bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004023 switch (Tok.getKind()) {
4024 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004025
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004026 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004027 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004028
Chris Lattner020bab92009-01-04 23:41:41 +00004029 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004030 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004031 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004032 return false;
John Thompson22334602010-02-05 00:12:22 +00004033 if (TryAltiVecVectorToken())
4034 return true;
4035 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004036 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004037 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004038 // Annotate typenames and C++ scope specifiers. If we get one, just
4039 // recurse to handle whatever we get.
4040 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004041 return true;
4042 if (Tok.is(tok::identifier))
4043 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004044
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004045 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004046 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004047 // expression is permitted, then this is probably a class message send
4048 // missing the initial '['. In this case, we won't consider this to be
4049 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004050 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004051 isStartOfObjCClassMessageMissingOpenBracket())
4052 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004053
John McCall1f476a12010-02-26 08:45:28 +00004054 return isDeclarationSpecifier();
4055
Chris Lattner020bab92009-01-04 23:41:41 +00004056 case tok::coloncolon: // ::foo::bar
4057 if (NextToken().is(tok::kw_new) || // ::new
4058 NextToken().is(tok::kw_delete)) // ::delete
4059 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004060
Chris Lattner020bab92009-01-04 23:41:41 +00004061 // Annotate typenames and C++ scope specifiers. If we get one, just
4062 // recurse to handle whatever we get.
4063 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004064 return true;
4065 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004066
Chris Lattneracd58a32006-08-06 17:24:14 +00004067 // storage-class-specifier
4068 case tok::kw_typedef:
4069 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004070 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004071 case tok::kw_static:
4072 case tok::kw_auto:
4073 case tok::kw_register:
4074 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004075 case tok::kw_thread_local:
4076 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004077
Douglas Gregor26701a42011-09-09 02:06:17 +00004078 // Modules
4079 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004080
John McCallea0a39e2012-11-14 00:49:39 +00004081 // Debugger support
4082 case tok::kw___unknown_anytype:
4083
Chris Lattneracd58a32006-08-06 17:24:14 +00004084 // type-specifiers
4085 case tok::kw_short:
4086 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004087 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004088 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004089 case tok::kw_signed:
4090 case tok::kw_unsigned:
4091 case tok::kw__Complex:
4092 case tok::kw__Imaginary:
4093 case tok::kw_void:
4094 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004095 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004096 case tok::kw_char16_t:
4097 case tok::kw_char32_t:
4098
Chris Lattneracd58a32006-08-06 17:24:14 +00004099 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004100 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004101 case tok::kw_float:
4102 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004103 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004104 case tok::kw__Bool:
4105 case tok::kw__Decimal32:
4106 case tok::kw__Decimal64:
4107 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004108 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004109
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004110 // OpenCL specific types:
4111 case tok::kw_image1d_t:
4112 case tok::kw_image1d_array_t:
4113 case tok::kw_image1d_buffer_t:
4114 case tok::kw_image2d_t:
4115 case tok::kw_image2d_array_t:
4116 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004117 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004118 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004119
Chris Lattner861a2262008-04-13 18:59:07 +00004120 // struct-or-union-specifier (C99) or class-specifier (C++)
4121 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004122 case tok::kw_struct:
4123 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004124 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004125 // enum-specifier
4126 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004127
Chris Lattneracd58a32006-08-06 17:24:14 +00004128 // type-qualifier
4129 case tok::kw_const:
4130 case tok::kw_volatile:
4131 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004132
Chris Lattneracd58a32006-08-06 17:24:14 +00004133 // function-specifier
4134 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004135 case tok::kw_virtual:
4136 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004137 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004138
Richard Smith1dba27c2013-01-29 09:02:09 +00004139 // alignment-specifier
4140 case tok::kw__Alignas:
4141
Richard Smithd16fe122012-10-25 00:00:53 +00004142 // friend keyword.
4143 case tok::kw_friend:
4144
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004145 // static_assert-declaration
4146 case tok::kw__Static_assert:
4147
Chris Lattner599e47e2007-08-09 17:01:07 +00004148 // GNU typeof support.
4149 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004150
Chris Lattner599e47e2007-08-09 17:01:07 +00004151 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004152 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004153
Richard Smithd16fe122012-10-25 00:00:53 +00004154 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004155 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004156 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004157
Richard Smith8e1ac332013-03-28 01:55:44 +00004158 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004159 case tok::kw__Atomic:
4160 return true;
4161
Chris Lattner8b2ec162008-07-26 03:38:44 +00004162 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4163 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004164 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004165
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004166 // typedef-name
4167 case tok::annot_typename:
4168 return !DisambiguatingWithExpression ||
4169 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004170
Steve Narofff192fab2009-01-06 19:34:12 +00004171 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004172 case tok::kw___cdecl:
4173 case tok::kw___stdcall:
4174 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004175 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004176 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004177 case tok::kw___sptr:
4178 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004179 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004180 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004181 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004182 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004183 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004184
4185 case tok::kw___private:
4186 case tok::kw___local:
4187 case tok::kw___global:
4188 case tok::kw___constant:
4189 case tok::kw___read_only:
4190 case tok::kw___read_write:
4191 case tok::kw___write_only:
4192
Eli Friedman53339e02009-06-08 23:27:34 +00004193 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004194 }
4195}
4196
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004197bool Parser::isConstructorDeclarator() {
4198 TentativeParsingAction TPA(*this);
4199
4200 // Parse the C++ scope specifier.
4201 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004202 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004203 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004204 TPA.Revert();
4205 return false;
4206 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004207
4208 // Parse the constructor name.
4209 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4210 // We already know that we have a constructor name; just consume
4211 // the token.
4212 ConsumeToken();
4213 } else {
4214 TPA.Revert();
4215 return false;
4216 }
4217
Richard Smith43f340f2012-03-27 23:05:05 +00004218 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004219 if (Tok.isNot(tok::l_paren)) {
4220 TPA.Revert();
4221 return false;
4222 }
4223 ConsumeParen();
4224
Richard Smith43f340f2012-03-27 23:05:05 +00004225 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4226 // that we have a constructor.
4227 if (Tok.is(tok::r_paren) ||
4228 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004229 TPA.Revert();
4230 return true;
4231 }
4232
4233 // If we need to, enter the specified scope.
4234 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004235 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004236 DeclScopeObj.EnterDeclaratorScope();
4237
Francois Pichet79f3a872011-01-31 04:54:32 +00004238 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004239 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004240 MaybeParseMicrosoftAttributes(Attrs);
4241
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004242 // Check whether the next token(s) are part of a declaration
4243 // specifier, in which case we have the start of a parameter and,
4244 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004245 bool IsConstructor = false;
4246 if (isDeclarationSpecifier())
4247 IsConstructor = true;
4248 else if (Tok.is(tok::identifier) ||
4249 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4250 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4251 // This might be a parenthesized member name, but is more likely to
4252 // be a constructor declaration with an invalid argument type. Keep
4253 // looking.
4254 if (Tok.is(tok::annot_cxxscope))
4255 ConsumeToken();
4256 ConsumeToken();
4257
4258 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004259 // which must have one of the following syntactic forms (see the
4260 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004261 switch (Tok.getKind()) {
4262 case tok::l_paren:
4263 // C(X ( int));
4264 case tok::l_square:
4265 // C(X [ 5]);
4266 // C(X [ [attribute]]);
4267 case tok::coloncolon:
4268 // C(X :: Y);
4269 // C(X :: *p);
4270 case tok::r_paren:
4271 // C(X )
4272 // Assume this isn't a constructor, rather than assuming it's a
4273 // constructor with an unnamed parameter of an ill-formed type.
4274 break;
4275
4276 default:
4277 IsConstructor = true;
4278 break;
4279 }
4280 }
4281
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004282 TPA.Revert();
4283 return IsConstructor;
4284}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004285
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004286/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004287/// type-qualifier-list: [C99 6.7.5]
4288/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004289/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004290/// [ only if VendorAttributesAllowed=true ]
4291/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004292/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004293/// [ only if VendorAttributesAllowed=true ]
4294/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004295/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004296/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004297///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004298void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4299 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004300 bool CXX11AttributesAllowed,
4301 bool AtomicAllowed) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004302 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004303 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004304 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004305 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004306 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004307 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004308
4309 SourceLocation EndLoc;
4310
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004311 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004312 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004313 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004314 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004315 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004316
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004317 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004318 case tok::code_completion:
4319 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004320 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004321
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004322 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004323 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004324 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004325 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004326 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004327 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004328 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004329 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004330 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004331 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004332 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004333 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004334 case tok::kw__Atomic:
4335 if (!AtomicAllowed)
4336 goto DoneWithTypeQuals;
4337 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4338 getLangOpts());
4339 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004340
4341 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004342 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004343 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004344 goto DoneWithTypeQuals;
4345 case tok::kw___private:
4346 case tok::kw___global:
4347 case tok::kw___local:
4348 case tok::kw___constant:
4349 case tok::kw___read_only:
4350 case tok::kw___write_only:
4351 case tok::kw___read_write:
4352 ParseOpenCLQualifiers(DS);
4353 break;
4354
Aaron Ballman317a77f2013-05-22 23:25:32 +00004355 case tok::kw___sptr:
4356 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004357 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004358 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004359 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004360 case tok::kw___cdecl:
4361 case tok::kw___stdcall:
4362 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004363 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004364 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004365 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004366 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004367 continue;
4368 }
4369 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004370 case tok::kw___pascal:
4371 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004372 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004373 continue;
4374 }
4375 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004376 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004377 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004378 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004379 continue; // do *not* consume the next token!
4380 }
4381 // otherwise, FALL THROUGH!
4382 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004383 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004384 // If this is not a type-qualifier token, we're done reading type
4385 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004386 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004387 if (EndLoc.isValid())
4388 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004389 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004390 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004391
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004392 // If the specifier combination wasn't legal, issue a diagnostic.
4393 if (isInvalid) {
4394 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004395 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004396 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004397 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004398 }
4399}
4400
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004401
4402/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4403///
4404void Parser::ParseDeclarator(Declarator &D) {
4405 /// This implements the 'declarator' production in the C grammar, then checks
4406 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004407 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004408}
4409
Richard Smith0efa75c2012-03-29 01:16:42 +00004410static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4411 if (Kind == tok::star || Kind == tok::caret)
4412 return true;
4413
4414 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4415 if (!Lang.CPlusPlus)
4416 return false;
4417
4418 return Kind == tok::amp || Kind == tok::ampamp;
4419}
4420
Sebastian Redlbd150f42008-11-21 19:14:01 +00004421/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4422/// is parsed by the function passed to it. Pass null, and the direct-declarator
4423/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004424/// ptr-operator production.
4425///
Richard Smith09f76ee2011-10-19 21:33:05 +00004426/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004427/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4428/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004429///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004430/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4431/// [C] pointer[opt] direct-declarator
4432/// [C++] direct-declarator
4433/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004434///
4435/// pointer: [C99 6.7.5]
4436/// '*' type-qualifier-list[opt]
4437/// '*' type-qualifier-list[opt] pointer
4438///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004439/// ptr-operator:
4440/// '*' cv-qualifier-seq[opt]
4441/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004442/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004443/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004444/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004445/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004446void Parser::ParseDeclaratorInternal(Declarator &D,
4447 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004448 if (Diags.hasAllExtensionsSilenced())
4449 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004450
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004451 // C++ member pointers start with a '::' or a nested-name.
4452 // Member pointers get special handling, since there's no place for the
4453 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004454 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004455 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4456 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004457 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4458 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004459 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004460 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004461
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004462 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004463 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004464 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004465 if (D.mayHaveIdentifier())
4466 D.getCXXScopeSpec() = SS;
4467 else
4468 AnnotateScopeToken(SS, true);
4469
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004470 if (DirectDeclParser)
4471 (this->*DirectDeclParser)(D);
4472 return;
4473 }
4474
4475 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004476 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004477 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004478 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004479 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004480
4481 // Recurse to parse whatever is left.
4482 ParseDeclaratorInternal(D, DirectDeclParser);
4483
4484 // Sema will have to catch (syntactically invalid) pointers into global
4485 // scope. It has to catch pointers into namespace scope anyway.
4486 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004487 Loc),
4488 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004489 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004490 return;
4491 }
4492 }
4493
4494 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004495 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004496 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004497 if (DirectDeclParser)
4498 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004499 return;
4500 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004501
Sebastian Redled0f3b02009-03-15 22:02:01 +00004502 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4503 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004504 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004505 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004506
Chris Lattner9eac9312009-03-27 04:18:06 +00004507 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004508 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004509 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004510
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004511 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004512 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004513 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004514
Bill Wendling3708c182007-05-27 10:15:43 +00004515 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004516 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004517 if (Kind == tok::star)
4518 // Remember that we parsed a pointer type, and remember the type-quals.
4519 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004520 DS.getConstSpecLoc(),
4521 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004522 DS.getRestrictSpecLoc()),
4523 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004524 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004525 else
4526 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004527 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004528 Loc),
4529 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004530 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004531 } else {
4532 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004533 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004534
Sebastian Redl3b27be62009-03-23 00:00:23 +00004535 // Complain about rvalue references in C++03, but then go on and build
4536 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004537 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004538 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004539 diag::warn_cxx98_compat_rvalue_reference :
4540 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004541
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004542 // GNU-style and C++11 attributes are allowed here, as is restrict.
4543 ParseTypeQualifierListOpt(DS);
4544 D.ExtendWithDeclSpec(DS);
4545
Bill Wendling93efb222007-06-02 23:28:54 +00004546 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4547 // cv-qualifiers are introduced through the use of a typedef or of a
4548 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004549 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4550 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4551 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004552 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004553 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4554 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004555 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004556 // 'restrict' is permitted as an extension.
4557 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4558 Diag(DS.getAtomicSpecLoc(),
4559 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004560 }
Bill Wendling3708c182007-05-27 10:15:43 +00004561
4562 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004563 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004564
Douglas Gregor66583c52008-11-03 15:51:28 +00004565 if (D.getNumTypeObjects() > 0) {
4566 // C++ [dcl.ref]p4: There shall be no references to references.
4567 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4568 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004569 if (const IdentifierInfo *II = D.getIdentifier())
4570 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4571 << II;
4572 else
4573 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4574 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004575
Sebastian Redlbd150f42008-11-21 19:14:01 +00004576 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004577 // can go ahead and build the (technically ill-formed)
4578 // declarator: reference collapsing will take care of it.
4579 }
4580 }
4581
Richard Smith8e1ac332013-03-28 01:55:44 +00004582 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004583 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004584 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004585 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004586 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004587 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004588}
4589
Richard Smith0efa75c2012-03-29 01:16:42 +00004590static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4591 SourceLocation EllipsisLoc) {
4592 if (EllipsisLoc.isValid()) {
4593 FixItHint Insertion;
4594 if (!D.getEllipsisLoc().isValid()) {
4595 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4596 D.setEllipsisLoc(EllipsisLoc);
4597 }
4598 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4599 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4600 }
4601}
4602
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004603/// ParseDirectDeclarator
4604/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004605/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004606/// '(' declarator ')'
4607/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004608/// [C90] direct-declarator '[' constant-expression[opt] ']'
4609/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4610/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4611/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4612/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004613/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4614/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004615/// direct-declarator '(' parameter-type-list ')'
4616/// direct-declarator '(' identifier-list[opt] ')'
4617/// [GNU] direct-declarator '(' parameter-forward-declarations
4618/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004619/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4620/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004621/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4622/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4623/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004624/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004625/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004626///
4627/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004628/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004629/// '::'[opt] nested-name-specifier[opt] type-name
4630///
4631/// id-expression: [C++ 5.1]
4632/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004633/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004634///
4635/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004636/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004637/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004638/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004639/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004640/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004641///
Richard Smith1453e312012-03-27 01:42:32 +00004642/// Note, any additional constructs added here may need corresponding changes
4643/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004644void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004645 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004646
David Blaikiebbafb8a2012-03-11 07:00:24 +00004647 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004648 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004649 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004650 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4651 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004652 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004653 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004654 }
4655
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004656 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004657 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004658 // Change the declaration context for name lookup, until this function
4659 // is exited (and the declarator has been parsed).
4660 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004661 }
4662
Douglas Gregor27b4c162010-12-23 22:44:42 +00004663 // C++0x [dcl.fct]p14:
4664 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004665 // of a parameter-declaration-clause without a preceding comma. In
4666 // this case, the ellipsis is parsed as part of the
4667 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004668 // parameter pack that has not been expanded; otherwise, it is parsed
4669 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004670 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004671 !((D.getContext() == Declarator::PrototypeContext ||
4672 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004673 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004674 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004675 !Actions.containsUnexpandedParameterPacks(D))) {
4676 SourceLocation EllipsisLoc = ConsumeToken();
4677 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4678 // The ellipsis was put in the wrong place. Recover, and explain to
4679 // the user what they should have done.
4680 ParseDeclarator(D);
4681 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4682 return;
4683 } else
4684 D.setEllipsisLoc(EllipsisLoc);
4685
4686 // The ellipsis can't be followed by a parenthesized declarator. We
4687 // check for that in ParseParenDeclarator, after we have disambiguated
4688 // the l_paren token.
4689 }
4690
Douglas Gregor7861a802009-11-03 01:35:08 +00004691 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4692 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4693 // We found something that indicates the start of an unqualified-id.
4694 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004695 bool AllowConstructorName;
4696 if (D.getDeclSpec().hasTypeSpecifier())
4697 AllowConstructorName = false;
4698 else if (D.getCXXScopeSpec().isSet())
4699 AllowConstructorName =
4700 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004701 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004702 else
4703 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4704
Abramo Bagnara7945c982012-01-27 09:46:47 +00004705 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004706 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4707 /*EnteringContext=*/true,
4708 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004709 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004710 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004711 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004712 D.getName()) ||
4713 // Once we're past the identifier, if the scope was bad, mark the
4714 // whole declarator bad.
4715 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004716 D.SetIdentifier(0, Tok.getLocation());
4717 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004718 } else {
4719 // Parsed the unqualified-id; update range information and move along.
4720 if (D.getSourceRange().getBegin().isInvalid())
4721 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4722 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004723 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004724 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004725 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004726 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004727 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004728 "There's a C++-specific check for tok::identifier above");
4729 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4730 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4731 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004732 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004733 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
4734 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4735 << FixItHint::CreateRemoval(Tok.getLocation());
4736 D.SetIdentifier(0, Tok.getLocation());
4737 ConsumeToken();
4738 goto PastIdentifier;
Douglas Gregor7861a802009-11-03 01:35:08 +00004739 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004740
Douglas Gregor7861a802009-11-03 01:35:08 +00004741 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004742 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004743 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004744 // Example: 'char (*X)' or 'int (*XX)(void)'
4745 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004746
4747 // If the declarator was parenthesized, we entered the declarator
4748 // scope when parsing the parenthesized declarator, then exited
4749 // the scope already. Re-enter the scope, if we need to.
4750 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004751 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004752 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004753 if (!D.isInvalidType() &&
4754 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004755 // Change the declaration context for name lookup, until this function
4756 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004757 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004758 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004759 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004760 // This could be something simple like "int" (in which case the declarator
4761 // portion is empty), if an abstract-declarator is allowed.
4762 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004763
4764 // The grammar for abstract-pack-declarator does not allow grouping parens.
4765 // FIXME: Revisit this once core issue 1488 is resolved.
4766 if (D.hasEllipsis() && D.hasGroupingParens())
4767 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4768 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004769 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004770 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004771 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004772 if (D.getContext() == Declarator::MemberContext)
4773 Diag(Tok, diag::err_expected_member_name_or_semi)
4774 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004775 else if (getLangOpts().CPlusPlus) {
4776 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4777 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4778 else
4779 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4780 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00004781 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004782 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004783 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004784 }
Mike Stump11289f42009-09-09 15:08:12 +00004785
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004786 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004787 assert(D.isPastIdentifier() &&
4788 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004789
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004790 // Don't parse attributes unless we have parsed an unparenthesized name.
4791 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004792 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004793
Chris Lattneracd58a32006-08-06 17:24:14 +00004794 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004795 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004796 // Enter function-declaration scope, limiting any declarators to the
4797 // function prototype scope, including parameter declarators.
4798 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004799 Scope::FunctionPrototypeScope|Scope::DeclScope|
4800 (D.isFunctionDeclaratorAFunctionDeclaration()
4801 ? Scope::FunctionDeclarationScope : 0));
4802
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004803 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4804 // In such a case, check if we actually have a function declarator; if it
4805 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004806 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004807 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4808 // The name of the declarator, if any, is tentatively declared within
4809 // a possible direct initializer.
4810 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4811 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4812 TentativelyDeclaredIdentifiers.pop_back();
4813 if (!IsFunctionDecl)
4814 break;
4815 }
John McCall084e83d2011-03-24 11:26:52 +00004816 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004817 BalancedDelimiterTracker T(*this, tok::l_paren);
4818 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004819 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004820 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004821 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004822 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004823 } else {
4824 break;
4825 }
4826 }
Chad Rosierc1183952012-06-26 22:30:43 +00004827}
Chris Lattneracd58a32006-08-06 17:24:14 +00004828
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004829/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4830/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004831/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004832/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4833///
4834/// direct-declarator:
4835/// '(' declarator ')'
4836/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004837/// direct-declarator '(' parameter-type-list ')'
4838/// direct-declarator '(' identifier-list[opt] ')'
4839/// [GNU] direct-declarator '(' parameter-forward-declarations
4840/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004841///
4842void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004843 BalancedDelimiterTracker T(*this, tok::l_paren);
4844 T.consumeOpen();
4845
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004846 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004847
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004848 // Eat any attributes before we look at whether this is a grouping or function
4849 // declarator paren. If this is a grouping paren, the attribute applies to
4850 // the type being built up, for example:
4851 // int (__attribute__(()) *x)(long y)
4852 // If this ends up not being a grouping paren, the attribute applies to the
4853 // first argument, for example:
4854 // int (__attribute__(()) int x)
4855 // In either case, we need to eat any attributes to be able to determine what
4856 // sort of paren this is.
4857 //
John McCall084e83d2011-03-24 11:26:52 +00004858 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004859 bool RequiresArg = false;
4860 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004861 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004862
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004863 // We require that the argument list (if this is a non-grouping paren) be
4864 // present even if the attribute list was empty.
4865 RequiresArg = true;
4866 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004867
Steve Naroff44ac7772008-12-25 14:16:32 +00004868 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004869 ParseMicrosoftTypeAttributes(attrs);
4870
Dawn Perchik335e16b2010-09-03 01:29:35 +00004871 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004872 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004873 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004874
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004875 // If we haven't past the identifier yet (or where the identifier would be
4876 // stored, if this is an abstract declarator), then this is probably just
4877 // grouping parens. However, if this could be an abstract-declarator, then
4878 // this could also be the start of function arguments (consider 'void()').
4879 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004880
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004881 if (!D.mayOmitIdentifier()) {
4882 // If this can't be an abstract-declarator, this *must* be a grouping
4883 // paren, because we haven't seen the identifier yet.
4884 isGrouping = true;
4885 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004886 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4887 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004888 isDeclarationSpecifier() || // 'int(int)' is a function.
4889 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004890 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4891 // considered to be a type, not a K&R identifier-list.
4892 isGrouping = false;
4893 } else {
4894 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4895 isGrouping = true;
4896 }
Mike Stump11289f42009-09-09 15:08:12 +00004897
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004898 // If this is a grouping paren, handle:
4899 // direct-declarator: '(' declarator ')'
4900 // direct-declarator: '(' attributes declarator ')'
4901 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004902 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4903 D.setEllipsisLoc(SourceLocation());
4904
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004905 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004906 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004907 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004908 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004909 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004910 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004911 T.getCloseLocation()),
4912 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004913
4914 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004915
4916 // An ellipsis cannot be placed outside parentheses.
4917 if (EllipsisLoc.isValid())
4918 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4919
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004920 return;
4921 }
Mike Stump11289f42009-09-09 15:08:12 +00004922
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004923 // Okay, if this wasn't a grouping paren, it must be the start of a function
4924 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004925 // identifier (and remember where it would have been), then call into
4926 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004927 D.SetIdentifier(0, Tok.getLocation());
4928
David Blaikie15a430a2011-12-04 05:04:18 +00004929 // Enter function-declaration scope, limiting any declarators to the
4930 // function prototype scope, including parameter declarators.
4931 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004932 Scope::FunctionPrototypeScope | Scope::DeclScope |
4933 (D.isFunctionDeclaratorAFunctionDeclaration()
4934 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00004935 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004936 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004937}
4938
4939/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4940/// declarator D up to a paren, which indicates that we are parsing function
4941/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004942///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004943/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4944/// immediately after the open paren - they should be considered to be the
4945/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004946///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004947/// If RequiresArg is true, then the first argument of the function is required
4948/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004949///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004950/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4951/// (C++11) ref-qualifier[opt], exception-specification[opt],
4952/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4953///
4954/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004955/// dynamic-exception-specification
4956/// noexcept-specification
4957///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004958void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004959 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004960 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004961 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004962 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004963 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004964 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004965 // lparen is already consumed!
4966 assert(D.isPastIdentifier() && "Should not call before identifier!");
4967
4968 // This should be true when the function has typed arguments.
4969 // Otherwise, it is treated as a K&R-style function.
4970 bool HasProto = false;
4971 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004972 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004973 // Remember where we see an ellipsis, if any.
4974 SourceLocation EllipsisLoc;
4975
4976 DeclSpec DS(AttrFactory);
4977 bool RefQualifierIsLValueRef = true;
4978 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004979 SourceLocation ConstQualifierLoc;
4980 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004981 ExceptionSpecificationType ESpecType = EST_None;
4982 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004983 SmallVector<ParsedType, 2> DynamicExceptions;
4984 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004985 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004986 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004987 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004988
James Molloy6f8780b2012-02-29 10:24:19 +00004989 Actions.ActOnStartFunctionDeclarator();
Manuel Klimek2fdbea22013-08-22 12:12:24 +00004990
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004991 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4992 EndLoc is the end location for the function declarator.
4993 They differ for trailing return types. */
4994 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004995 SourceLocation LParenLoc, RParenLoc;
4996 LParenLoc = Tracker.getOpenLocation();
4997 StartLoc = LParenLoc;
4998
Douglas Gregor9e66af42011-07-05 16:44:18 +00004999 if (isFunctionDeclaratorIdentifierList()) {
5000 if (RequiresArg)
5001 Diag(Tok, diag::err_argument_required_after_attribute);
5002
5003 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5004
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005005 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005006 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005007 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005008 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005009 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005010 if (Tok.isNot(tok::r_paren))
Manuel Klimek2fdbea22013-08-22 12:12:24 +00005011 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005012 else if (RequiresArg)
5013 Diag(Tok, diag::err_argument_required_after_attribute);
5014
David Blaikiebbafb8a2012-03-11 07:00:24 +00005015 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005016
5017 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005018 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005019 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005020 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005021 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005022
David Blaikiebbafb8a2012-03-11 07:00:24 +00005023 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005024 // FIXME: Accept these components in any order, and produce fixits to
5025 // correct the order if the user gets it wrong. Ideally we should deal
5026 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005027
5028 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005029 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5030 /*CXX11AttributesAllowed*/ false,
5031 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005032 if (!DS.getSourceRange().getEnd().isInvalid()) {
5033 EndLoc = DS.getSourceRange().getEnd();
5034 ConstQualifierLoc = DS.getConstSpecLoc();
5035 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5036 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005037
5038 // Parse ref-qualifier[opt].
5039 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005040 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005041 diag::warn_cxx98_compat_ref_qualifier :
5042 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005043
Douglas Gregor9e66af42011-07-05 16:44:18 +00005044 RefQualifierIsLValueRef = Tok.is(tok::amp);
5045 RefQualifierLoc = ConsumeToken();
5046 EndLoc = RefQualifierLoc;
5047 }
5048
Douglas Gregor3024f072012-04-16 07:05:22 +00005049 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005050 // If a declaration declares a member function or member function
5051 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005052 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005053 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005054 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005055 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005056 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005057 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005058 (D.getContext() == Declarator::MemberContext
5059 ? !D.getDeclSpec().isFriendSpecified()
5060 : D.getContext() == Declarator::FileContext &&
5061 D.getCXXScopeSpec().isValid() &&
5062 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005063 Sema::CXXThisScopeRAII ThisScope(Actions,
5064 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005065 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005066 (D.getDeclSpec().isConstexprSpecified() &&
5067 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005068 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005069 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005070
Douglas Gregor9e66af42011-07-05 16:44:18 +00005071 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005072 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005073 DynamicExceptions,
5074 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005075 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005076 if (ESpecType != EST_None)
5077 EndLoc = ESpecRange.getEnd();
5078
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005079 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5080 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005081 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005082
Douglas Gregor9e66af42011-07-05 16:44:18 +00005083 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005084 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005085 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005086 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005087 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5088 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005089 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005090 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005091 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005092 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005093 }
5094 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005095 }
5096
5097 // Remember that we parsed a function type, and remember the attributes.
5098 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005099 IsAmbiguous,
5100 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005101 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005102 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005103 DS.getTypeQualifiers(),
5104 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005105 RefQualifierLoc, ConstQualifierLoc,
5106 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005107 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005108 ESpecType, ESpecRange.getBegin(),
5109 DynamicExceptions.data(),
5110 DynamicExceptionRanges.data(),
5111 DynamicExceptions.size(),
5112 NoexceptExpr.isUsable() ?
5113 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005114 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005115 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005116 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005117
5118 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005119}
5120
5121/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5122/// identifier list form for a K&R-style function: void foo(a,b,c)
5123///
5124/// Note that identifier-lists are only allowed for normal declarators, not for
5125/// abstract-declarators.
5126bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005127 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005128 && Tok.is(tok::identifier)
5129 && !TryAltiVecVectorToken()
5130 // K&R identifier lists can't have typedefs as identifiers, per C99
5131 // 6.7.5.3p11.
5132 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5133 // Identifier lists follow a really simple grammar: the identifiers can
5134 // be followed *only* by a ", identifier" or ")". However, K&R
5135 // identifier lists are really rare in the brave new modern world, and
5136 // it is very common for someone to typo a type in a non-K&R style
5137 // list. If we are presented with something like: "void foo(intptr x,
5138 // float y)", we don't want to start parsing the function declarator as
5139 // though it is a K&R style declarator just because intptr is an
5140 // invalid type.
5141 //
5142 // To handle this, we check to see if the token after the first
5143 // identifier is a "," or ")". Only then do we parse it as an
5144 // identifier list.
5145 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5146}
5147
5148/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5149/// we found a K&R-style identifier list instead of a typed parameter list.
5150///
5151/// After returning, ParamInfo will hold the parsed parameters.
5152///
5153/// identifier-list: [C99 6.7.5]
5154/// identifier
5155/// identifier-list ',' identifier
5156///
5157void Parser::ParseFunctionDeclaratorIdentifierList(
5158 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005159 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005160 // If there was no identifier specified for the declarator, either we are in
5161 // an abstract-declarator, or we are in a parameter declarator which was found
5162 // to be abstract. In abstract-declarators, identifier lists are not valid:
5163 // diagnose this.
5164 if (!D.getIdentifier())
5165 Diag(Tok, diag::ext_ident_list_in_param);
5166
5167 // Maintain an efficient lookup of params we have seen so far.
5168 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5169
5170 while (1) {
5171 // If this isn't an identifier, report the error and skip until ')'.
5172 if (Tok.isNot(tok::identifier)) {
5173 Diag(Tok, diag::err_expected_ident);
5174 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
5175 // Forget we parsed anything.
5176 ParamInfo.clear();
5177 return;
5178 }
5179
5180 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5181
5182 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5183 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5184 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5185
5186 // Verify that the argument identifier has not already been mentioned.
5187 if (!ParamsSoFar.insert(ParmII)) {
5188 Diag(Tok, diag::err_param_redefinition) << ParmII;
5189 } else {
5190 // Remember this identifier in ParamInfo.
5191 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5192 Tok.getLocation(),
5193 0));
5194 }
5195
5196 // Eat the identifier.
5197 ConsumeToken();
5198
5199 // The list continues if we see a comma.
5200 if (Tok.isNot(tok::comma))
5201 break;
5202 ConsumeToken();
5203 }
5204}
5205
5206/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5207/// after the opening parenthesis. This function will not parse a K&R-style
5208/// identifier list.
5209///
Richard Smith2620cd92012-04-11 04:01:28 +00005210/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5211/// caller parsed those arguments immediately after the open paren - they should
5212/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005213///
5214/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5215/// be the location of the ellipsis, if any was parsed.
5216///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005217/// parameter-type-list: [C99 6.7.5]
5218/// parameter-list
5219/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005220/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005221///
5222/// parameter-list: [C99 6.7.5]
5223/// parameter-declaration
5224/// parameter-list ',' parameter-declaration
5225///
5226/// parameter-declaration: [C99 6.7.5]
5227/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005228/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005229/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005230/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005231/// declaration-specifiers abstract-declarator[opt]
5232/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005233/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005234/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005235/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005236///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005237void Parser::ParseParameterDeclarationClause(
5238 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005239 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005240 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005241 SourceLocation &EllipsisLoc) {
Manuel Klimek2fdbea22013-08-22 12:12:24 +00005242
Chris Lattner371ed4e2008-04-06 06:57:35 +00005243 while (1) {
5244 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00005245 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5246 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00005247 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00005248 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00005249 }
Mike Stump11289f42009-09-09 15:08:12 +00005250
Chris Lattner371ed4e2008-04-06 06:57:35 +00005251 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005252 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005253 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005254
Richard Smith2620cd92012-04-11 04:01:28 +00005255 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005256 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005257
John McCall53fa7142010-12-24 02:08:15 +00005258 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005259 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005260
5261 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005262
5263 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005264 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005265 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005266 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5267 // too much hassle.
5268 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005269
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005270 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005271
Manuel Klimek2fdbea22013-08-22 12:12:24 +00005272 // Parse the declarator. This is "PrototypeContext", because we must
5273 // accept either 'declarator' or 'abstract-declarator' here.
5274 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5275 ParseDeclarator(ParmDecl);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005276
5277 // Parse GNU attributes, if present.
Manuel Klimek2fdbea22013-08-22 12:12:24 +00005278 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00005279
Chris Lattner371ed4e2008-04-06 06:57:35 +00005280 // Remember this parsed parameter in ParamInfo.
Manuel Klimek2fdbea22013-08-22 12:12:24 +00005281 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005282
Douglas Gregor4d87df52008-12-16 21:30:33 +00005283 // DefArgToks is used when the parsing of default arguments needs
5284 // to be delayed.
5285 CachedTokens *DefArgToks = 0;
5286
Chris Lattner371ed4e2008-04-06 06:57:35 +00005287 // If no parameter was specified, verify that *something* was specified,
5288 // otherwise we have a missing type and identifier.
Manuel Klimek2fdbea22013-08-22 12:12:24 +00005289 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5290 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005291 // Completely missing, emit error.
5292 Diag(DSStart, diag::err_missing_param);
5293 } else {
5294 // Otherwise, we have something. Add it and let semantic analysis try
5295 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005296
Chris Lattner371ed4e2008-04-06 06:57:35 +00005297 // Inform the actions module about the parameter declarator, so it gets
5298 // added to the current scope.
Manuel Klimek2fdbea22013-08-22 12:12:24 +00005299 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
5300
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005301 // Parse the default argument, if any. We parse the default
5302 // arguments in all dialects; the semantic analysis in
5303 // ActOnParamDefaultArgument will reject the default argument in
5304 // C.
5305 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005306 SourceLocation EqualLoc = Tok.getLocation();
5307
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005308 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005309 if (D.getContext() == Declarator::MemberContext) {
5310 // If we're inside a class definition, cache the tokens
5311 // corresponding to the default argument. We'll actually parse
5312 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005313 // FIXME: Can we use a smart pointer for Toks?
5314 DefArgToks = new CachedTokens;
5315
Mike Stump11289f42009-09-09 15:08:12 +00005316 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00005317 /*StopAtSemi=*/true,
5318 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005319 delete DefArgToks;
5320 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005321 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005322 } else {
5323 // Mark the end of the default argument so that we know when to
5324 // stop when we parse it later on.
5325 Token DefArgEnd;
5326 DefArgEnd.startToken();
5327 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5328 DefArgEnd.setLocation(Tok.getLocation());
5329 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005330 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005331 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005332 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005333 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005334 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005335 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005336
Chad Rosierc1183952012-06-26 22:30:43 +00005337 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005338 // used.
5339 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005340 Sema::PotentiallyEvaluatedIfUsed,
5341 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005342
Sebastian Redldb63af22012-03-14 15:54:00 +00005343 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005344 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005345 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005346 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005347 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005348 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005349 if (DefArgResult.isInvalid()) {
5350 Actions.ActOnParamDefaultArgumentError(Param);
5351 SkipUntil(tok::comma, tok::r_paren, true, true);
5352 } else {
5353 // Inform the actions module about the default argument
5354 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005355 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005356 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005357 }
5358 }
Mike Stump11289f42009-09-09 15:08:12 +00005359
5360 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Manuel Klimek2fdbea22013-08-22 12:12:24 +00005361 ParmDecl.getIdentifierLoc(), Param,
5362 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005363 }
5364
5365 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005366 if (Tok.isNot(tok::comma)) {
5367 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005368 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005369
David Blaikiebbafb8a2012-03-11 07:00:24 +00005370 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005371 // We have ellipsis without a preceding ',', which is ill-formed
5372 // in C. Complain and provide the fix.
5373 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005374 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005375 }
5376 }
Chad Rosierc1183952012-06-26 22:30:43 +00005377
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005378 break;
5379 }
Mike Stump11289f42009-09-09 15:08:12 +00005380
Chris Lattner371ed4e2008-04-06 06:57:35 +00005381 // Consume the comma.
5382 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005383 }
Mike Stump11289f42009-09-09 15:08:12 +00005384
Chris Lattner6c940e62008-04-06 06:34:08 +00005385}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005386
Chris Lattnere8074e62006-08-06 18:30:15 +00005387/// [C90] direct-declarator '[' constant-expression[opt] ']'
5388/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5389/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5390/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5391/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005392/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5393/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005394void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005395 if (CheckProhibitedCXX11Attribute())
5396 return;
5397
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005398 BalancedDelimiterTracker T(*this, tok::l_square);
5399 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005400
Chris Lattner84a11622008-12-18 07:27:21 +00005401 // C array syntax has many features, but by-far the most common is [] and [4].
5402 // This code does a fast path to handle some of the most obvious cases.
5403 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005404 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005405 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005406 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005407
Chris Lattner84a11622008-12-18 07:27:21 +00005408 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005409 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005410 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005411 T.getOpenLocation(),
5412 T.getCloseLocation()),
5413 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005414 return;
5415 } else if (Tok.getKind() == tok::numeric_constant &&
5416 GetLookAheadToken(1).is(tok::r_square)) {
5417 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005418 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005419 ConsumeToken();
5420
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005421 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005422 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005423 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005424
Chris Lattner84a11622008-12-18 07:27:21 +00005425 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005426 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005427 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005428 T.getOpenLocation(),
5429 T.getCloseLocation()),
5430 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005431 return;
5432 }
Mike Stump11289f42009-09-09 15:08:12 +00005433
Chris Lattnere8074e62006-08-06 18:30:15 +00005434 // If valid, this location is the position where we read the 'static' keyword.
5435 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005436 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005437 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005438
Chris Lattnere8074e62006-08-06 18:30:15 +00005439 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005440 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005441 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005442 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005443
Chris Lattnere8074e62006-08-06 18:30:15 +00005444 // If we haven't already read 'static', check to see if there is one after the
5445 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005446 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005447 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005448
Chris Lattnere8074e62006-08-06 18:30:15 +00005449 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005450 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005451 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005452
Chris Lattner521ff2b2008-04-06 05:26:30 +00005453 // Handle the case where we have '[*]' as the array size. However, a leading
5454 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005455 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005456 // infrequent, use of lookahead is not costly here.
5457 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005458 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005459
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005460 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005461 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005462 StaticLoc = SourceLocation(); // Drop the static.
5463 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005464 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005465 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005466 // Note, in C89, this production uses the constant-expr production instead
5467 // of assignment-expr. The only difference is that assignment-expr allows
5468 // things like '=' and '*='. Sema rejects these in C89 mode because they
5469 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005470
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005471 // Parse the constant-expression or assignment-expression now (depending
5472 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005473 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005474 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005475 } else {
5476 EnterExpressionEvaluationContext Unevaluated(Actions,
5477 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005478 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005479 }
Chris Lattner62591722006-08-12 18:40:58 +00005480 }
Mike Stump11289f42009-09-09 15:08:12 +00005481
Chris Lattner62591722006-08-12 18:40:58 +00005482 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005483 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005484 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005485 // If the expression was invalid, skip it.
5486 SkipUntil(tok::r_square);
5487 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005488 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005489
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005490 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005491
John McCall084e83d2011-03-24 11:26:52 +00005492 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005493 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005494
Chris Lattner84a11622008-12-18 07:27:21 +00005495 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005496 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005497 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005498 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005499 T.getOpenLocation(),
5500 T.getCloseLocation()),
5501 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005502}
5503
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005504/// [GNU] typeof-specifier:
5505/// typeof ( expressions )
5506/// typeof ( type-name )
5507/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005508///
5509void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005510 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005511 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005512 SourceLocation StartLoc = ConsumeToken();
5513
John McCalle8595032010-01-13 20:03:27 +00005514 const bool hasParens = Tok.is(tok::l_paren);
5515
Eli Friedman15681d62012-09-26 04:34:21 +00005516 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5517 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005518
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005519 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005520 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005521 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005522 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5523 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005524 if (hasParens)
5525 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005526
5527 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005528 // FIXME: Not accurate, the range gets one token more than it should.
5529 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005530 else
5531 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005532
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005533 if (isCastExpr) {
5534 if (!CastTy) {
5535 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005536 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005537 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005538
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005539 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005540 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005541 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5542 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005543 DiagID, CastTy))
5544 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005545 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005546 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005547
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005548 // If we get here, the operand to the typeof was an expresion.
5549 if (Operand.isInvalid()) {
5550 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005551 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005552 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005553
Eli Friedmane0afc982012-01-21 01:01:51 +00005554 // We might need to transform the operand if it is potentially evaluated.
5555 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5556 if (Operand.isInvalid()) {
5557 DS.SetTypeSpecError();
5558 return;
5559 }
5560
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005561 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005562 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005563 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5564 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005565 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005566 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005567}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005568
Benjamin Kramere56f3932011-12-23 17:00:35 +00005569/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005570/// _Atomic ( type-name )
5571///
5572void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005573 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5574 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005575
5576 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005577 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005578 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005579 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005580
5581 TypeResult Result = ParseTypeName();
5582 if (Result.isInvalid()) {
5583 SkipUntil(tok::r_paren);
5584 return;
5585 }
5586
5587 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005588 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005589
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005590 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005591 return;
5592
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005593 DS.setTypeofParensRange(T.getRange());
5594 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005595
5596 const char *PrevSpec = 0;
5597 unsigned DiagID;
5598 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5599 DiagID, Result.release()))
5600 Diag(StartLoc, DiagID) << PrevSpec;
5601}
5602
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005603
5604/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5605/// from TryAltiVecVectorToken.
5606bool Parser::TryAltiVecVectorTokenOutOfLine() {
5607 Token Next = NextToken();
5608 switch (Next.getKind()) {
5609 default: return false;
5610 case tok::kw_short:
5611 case tok::kw_long:
5612 case tok::kw_signed:
5613 case tok::kw_unsigned:
5614 case tok::kw_void:
5615 case tok::kw_char:
5616 case tok::kw_int:
5617 case tok::kw_float:
5618 case tok::kw_double:
5619 case tok::kw_bool:
5620 case tok::kw___pixel:
5621 Tok.setKind(tok::kw___vector);
5622 return true;
5623 case tok::identifier:
5624 if (Next.getIdentifierInfo() == Ident_pixel) {
5625 Tok.setKind(tok::kw___vector);
5626 return true;
5627 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005628 if (Next.getIdentifierInfo() == Ident_bool) {
5629 Tok.setKind(tok::kw___vector);
5630 return true;
5631 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005632 return false;
5633 }
5634}
5635
5636bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5637 const char *&PrevSpec, unsigned &DiagID,
5638 bool &isInvalid) {
5639 if (Tok.getIdentifierInfo() == Ident_vector) {
5640 Token Next = NextToken();
5641 switch (Next.getKind()) {
5642 case tok::kw_short:
5643 case tok::kw_long:
5644 case tok::kw_signed:
5645 case tok::kw_unsigned:
5646 case tok::kw_void:
5647 case tok::kw_char:
5648 case tok::kw_int:
5649 case tok::kw_float:
5650 case tok::kw_double:
5651 case tok::kw_bool:
5652 case tok::kw___pixel:
5653 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5654 return true;
5655 case tok::identifier:
5656 if (Next.getIdentifierInfo() == Ident_pixel) {
5657 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5658 return true;
5659 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005660 if (Next.getIdentifierInfo() == Ident_bool) {
5661 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5662 return true;
5663 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005664 break;
5665 default:
5666 break;
5667 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005668 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005669 DS.isTypeAltiVecVector()) {
5670 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5671 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005672 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5673 DS.isTypeAltiVecVector()) {
5674 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5675 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005676 }
5677 return false;
5678}