blob: 0f480c133536089cc1bc5a9d05f4dee706e00215 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Larisse Voufo725de3e2013-06-21 00:08:46 +000016#include "clang/AST/DeclTemplate.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000017#include "clang/Basic/AddressSpaces.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000018#include "clang/Basic/CharInfo.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000019#include "clang/Basic/OpenCL.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000021#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Sema/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000025#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000026#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000027#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// C99 6.7: Declarations.
32//===----------------------------------------------------------------------===//
33
Chris Lattnerf5fbd792006-08-10 23:56:11 +000034/// ParseTypeName
35/// type-name: [C99 6.7.6]
36/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000037///
38/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000039TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000040 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000041 AccessSpecifier AS,
Richard Smith54ecd982013-02-20 19:22:51 +000042 Decl **OwnedType,
43 ParsedAttributes *Attrs) {
Richard Smith62dad822012-03-15 01:02:11 +000044 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000045 if (DSC == DSC_normal)
46 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000047
Chris Lattnerf5fbd792006-08-10 23:56:11 +000048 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000049 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +000050 if (Attrs)
51 DS.addAttributes(Attrs->getList());
Richard Smithbfdb1082012-03-12 08:56:40 +000052 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000053 if (OwnedType)
54 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000055
Chris Lattnerf5fbd792006-08-10 23:56:11 +000056 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000057 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000058 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000059 if (Range)
60 *Range = DeclaratorInfo.getSourceRange();
61
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000062 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000063 return true;
64
Douglas Gregor0be31a22010-07-02 17:43:08 +000065 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000066}
67
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000068
69/// isAttributeLateParsed - Return true if the attribute has arguments that
70/// require late parsing.
71static bool isAttributeLateParsed(const IdentifierInfo &II) {
72 return llvm::StringSwitch<bool>(II.getName())
73#include "clang/Parse/AttrLateParsed.inc"
74 .Default(false);
75}
76
Alexis Hunt96d5c762009-11-21 08:43:09 +000077/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000078///
79/// [GNU] attributes:
80/// attribute
81/// attributes attribute
82///
83/// [GNU] attribute:
84/// '__attribute__' '(' '(' attribute-list ')' ')'
85///
86/// [GNU] attribute-list:
87/// attrib
88/// attribute_list ',' attrib
89///
90/// [GNU] attrib:
91/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000092/// attrib-name
93/// attrib-name '(' identifier ')'
94/// attrib-name '(' identifier ',' nonempty-expr-list ')'
95/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000096///
Steve Naroff0f2fe172007-06-01 17:11:19 +000097/// [GNU] attrib-name:
98/// identifier
99/// typespec
100/// typequal
101/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +0000102///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000103/// Whether an attribute takes an 'identifier' is determined by the
104/// attrib-name. GCC's behavior here is not worth imitating:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000106/// * In C mode, if the attribute argument list starts with an identifier
107/// followed by a ',' or an ')', and the identifier doesn't resolve to
108/// a type, it is parsed as an identifier. If the attribute actually
109/// wanted an expression, it's out of luck (but it turns out that no
110/// attributes work that way, because C constant expressions are very
111/// limited).
112/// * In C++ mode, if the attribute argument list starts with an identifier,
113/// and the attribute *wants* an identifier, it is parsed as an identifier.
114/// At block scope, any additional tokens between the identifier and the
115/// ',' or ')' are ignored, otherwise they produce a parse error.
Richard Smithb12bf692011-10-17 21:20:17 +0000116///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000117/// We follow the C++ model, but don't allow junk after the identifier.
John McCall53fa7142010-12-24 02:08:15 +0000118void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000119 SourceLocation *endLoc,
120 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000121 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000122
Chris Lattner76c72282007-10-09 17:33:22 +0000123 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000124 ConsumeToken();
125 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
126 "attribute")) {
127 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000128 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000129 }
130 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
131 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000132 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000133 }
134 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000135 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
136 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000137 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000138 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
139 ConsumeToken();
140 continue;
141 }
142 // we have an identifier or declaration specifier (const, int, etc.)
143 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
144 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000145
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000146 if (Tok.is(tok::l_paren)) {
147 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000148 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000149 LateParsedAttribute *LA =
150 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
151 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000152
Bill Wendling44426052012-12-20 19:22:21 +0000153 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000154 // with other late-parsed declarations.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000155 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000156 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000157
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000158 // consume everything up to and including the matching right parens
159 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000160
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000161 Token Eof;
162 Eof.startToken();
163 Eof.setLocation(Tok.getLocation());
164 LA->Toks.push_back(Eof);
165 } else {
Michael Han23214e52012-10-03 01:56:22 +0000166 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000167 0, SourceLocation(), AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000168 }
169 } else {
Aaron Ballman00e99962013-08-31 01:11:41 +0000170 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
171 AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000172 }
173 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000174 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000175 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000176 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000177 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
178 SkipUntil(tok::r_paren, false);
179 }
John McCall53fa7142010-12-24 02:08:15 +0000180 if (endLoc)
181 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000182 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000183}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000184
Douglas Gregord2472d42013-05-02 23:25:32 +0000185/// \brief Determine whether the given attribute has all expression arguments.
186static bool attributeHasExprArgs(const IdentifierInfo &II) {
187 return llvm::StringSwitch<bool>(II.getName())
188#include "clang/Parse/AttrExprArgs.inc"
189 .Default(false);
190}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000191
Richard Smithfeefaf52013-09-03 18:01:40 +0000192IdentifierLoc *Parser::ParseIdentifierLoc() {
193 assert(Tok.is(tok::identifier) && "expected an identifier");
194 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
195 Tok.getLocation(),
196 Tok.getIdentifierInfo());
197 ConsumeToken();
198 return IL;
199}
200
Michael Han23214e52012-10-03 01:56:22 +0000201/// Parse the arguments to a parameterized GNU attribute or
202/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000203void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
204 SourceLocation AttrNameLoc,
205 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000206 SourceLocation *EndLoc,
207 IdentifierInfo *ScopeName,
208 SourceLocation ScopeLoc,
209 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000210
211 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
212
213 // Availability attributes have their own grammar.
214 if (AttrName->isStr("availability")) {
215 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
216 return;
217 }
218 // Thread safety attributes fit into the FIXME case above, so we
219 // just parse the arguments as a list of expressions
220 if (IsThreadSafetyAttribute(AttrName->getName())) {
221 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
222 return;
223 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000224 // Type safety attributes have their own grammar.
225 if (AttrName->isStr("type_tag_for_datatype")) {
226 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
227 return;
228 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000229
230 ConsumeParen(); // ignore the left paren loc for now
231
Richard Smithb12bf692011-10-17 21:20:17 +0000232 bool BuiltinType = false;
Aaron Ballman00e99962013-08-31 01:11:41 +0000233 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000234
Joey Goulyaba589c2013-03-08 09:42:32 +0000235 TypeResult T;
236 SourceRange TypeRange;
237 bool TypeParsed = false;
238
Richard Smithb12bf692011-10-17 21:20:17 +0000239 switch (Tok.getKind()) {
240 case tok::kw_char:
241 case tok::kw_wchar_t:
242 case tok::kw_char16_t:
243 case tok::kw_char32_t:
244 case tok::kw_bool:
245 case tok::kw_short:
246 case tok::kw_int:
247 case tok::kw_long:
248 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000249 case tok::kw___int128:
Richard Smithb12bf692011-10-17 21:20:17 +0000250 case tok::kw_signed:
251 case tok::kw_unsigned:
252 case tok::kw_float:
253 case tok::kw_double:
254 case tok::kw_void:
255 case tok::kw_typeof:
256 // __attribute__(( vec_type_hint(char) ))
Richard Smithb12bf692011-10-17 21:20:17 +0000257 BuiltinType = true;
Joey Goulyaba589c2013-03-08 09:42:32 +0000258 T = ParseTypeName(&TypeRange);
259 TypeParsed = true;
Richard Smithb12bf692011-10-17 21:20:17 +0000260 break;
261
Aaron Ballman00e99962013-08-31 01:11:41 +0000262 case tok::identifier: {
Joey Goulyaba589c2013-03-08 09:42:32 +0000263 if (AttrName->isStr("vec_type_hint")) {
264 T = ParseTypeName(&TypeRange);
265 TypeParsed = true;
266 break;
267 }
Richard Smithf7ca0c02013-09-03 18:57:36 +0000268 // If this attribute doesn't want an 'identifier' argument, then this
269 // argument should be parsed as an expression.
Douglas Gregord2472d42013-05-02 23:25:32 +0000270 if (attributeHasExprArgs(*AttrName))
271 break;
Aaron Ballman00e99962013-08-31 01:11:41 +0000272
Richard Smithfeefaf52013-09-03 18:01:40 +0000273 ArgExprs.push_back(ParseIdentifierLoc());
Aaron Ballman00e99962013-08-31 01:11:41 +0000274 } break;
Richard Smithb12bf692011-10-17 21:20:17 +0000275
276 default:
277 break;
278 }
279
Joey Goulyaba589c2013-03-08 09:42:32 +0000280 bool isInvalid = false;
281 bool isParmType = false;
Richard Smithb12bf692011-10-17 21:20:17 +0000282
Joey Goulyaba589c2013-03-08 09:42:32 +0000283 if (!BuiltinType && !AttrName->isStr("vec_type_hint") &&
Aaron Ballman00e99962013-08-31 01:11:41 +0000284 (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
Richard Smithb12bf692011-10-17 21:20:17 +0000285 // Eat the comma.
Aaron Ballman00e99962013-08-31 01:11:41 +0000286 if (!ArgExprs.empty())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000287 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000288
Richard Smithb12bf692011-10-17 21:20:17 +0000289 // Parse the non-empty comma-separated list of expressions.
290 while (1) {
291 ExprResult ArgExpr(ParseAssignmentExpression());
292 if (ArgExpr.isInvalid()) {
293 SkipUntil(tok::r_paren);
294 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000295 }
Richard Smithb12bf692011-10-17 21:20:17 +0000296 ArgExprs.push_back(ArgExpr.release());
297 if (Tok.isNot(tok::comma))
298 break;
299 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000300 }
Richard Smithb12bf692011-10-17 21:20:17 +0000301 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000302 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
303 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
304 tok::greater)) {
Fariborz Jahanian7f733022011-10-18 23:13:50 +0000305 while (Tok.is(tok::identifier)) {
306 ConsumeToken();
307 if (Tok.is(tok::greater))
308 break;
309 if (Tok.is(tok::comma)) {
310 ConsumeToken();
311 continue;
312 }
313 }
314 if (Tok.isNot(tok::greater))
315 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000316 SkipUntil(tok::r_paren, false, true); // skip until ')'
317 }
Joey Goulyaba589c2013-03-08 09:42:32 +0000318 } else if (AttrName->isStr("vec_type_hint")) {
319 if (T.get() && !T.isInvalid())
320 isParmType = true;
321 else {
322 if (Tok.is(tok::identifier))
323 ConsumeToken();
324 if (TypeParsed)
325 isInvalid = true;
326 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000327 }
Richard Smithb12bf692011-10-17 21:20:17 +0000328
329 SourceLocation RParen = Tok.getLocation();
Joey Goulyaba589c2013-03-08 09:42:32 +0000330 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen) &&
331 !isInvalid) {
Michael Han360d2252012-10-04 16:42:52 +0000332 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Joey Goulyaba589c2013-03-08 09:42:32 +0000333 if (isParmType) {
Joey Goulyaba589c2013-03-08 09:42:32 +0000334 Attrs.addNewTypeAttr(AttrName, SourceRange(AttrLoc, RParen), ScopeName,
Aaron Ballman00e99962013-08-31 01:11:41 +0000335 ScopeLoc, T.get(), Syntax);
Joey Goulyaba589c2013-03-08 09:42:32 +0000336 } else {
337 AttributeList *attr = Attrs.addNew(
Aaron Ballman00e99962013-08-31 01:11:41 +0000338 AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
339 ArgExprs.data(), ArgExprs.size(), Syntax);
Joey Goulyaba589c2013-03-08 09:42:32 +0000340 if (BuiltinType &&
341 attr->getKind() == AttributeList::AT_IBOutletCollection)
342 Diag(Tok, diag::err_iboutletcollection_builtintype);
343 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000344 }
345}
346
Chad Rosierc1183952012-06-26 22:30:43 +0000347/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000348/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000349void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000350 SourceLocation AttrNameLoc,
351 ParsedAttributes &Attrs)
352{
353 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000354 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000355 AttrName->getNameStart(), tok::r_paren))
356 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000357
Aaron Ballman478faed2012-06-19 22:09:27 +0000358 ExprResult ArgExpr(ParseConstantExpression());
359 if (ArgExpr.isInvalid()) {
360 T.skipToEnd();
361 return;
362 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000363 ArgsUnion ExprList = ArgExpr.take();
364 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
365 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000366
367 T.consumeClose();
368}
369
Chad Rosierc1183952012-06-26 22:30:43 +0000370/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000371/// arguments.
372bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
373 return llvm::StringSwitch<bool>(Ident->getName())
374 .Case("dllimport", true)
375 .Case("dllexport", true)
376 .Case("noreturn", true)
377 .Case("nothrow", true)
378 .Case("noinline", true)
379 .Case("naked", true)
380 .Case("appdomain", true)
381 .Case("process", true)
382 .Case("jitintrinsic", true)
383 .Case("noalias", true)
384 .Case("restrict", true)
385 .Case("novtable", true)
386 .Case("selectany", true)
387 .Case("thread", true)
Aaron Ballman444eb6e2013-05-04 16:58:37 +0000388 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000389 .Default(false);
390}
391
Chad Rosierc1183952012-06-26 22:30:43 +0000392/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000393/// parameters). Will return false if we properly handled the declspec, or
394/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000395void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000396 SourceLocation Loc,
397 ParsedAttributes &Attrs) {
398 // Try to handle the easy case first -- these declspecs all take a single
399 // parameter as their argument.
400 if (llvm::StringSwitch<bool>(Ident->getName())
401 .Case("uuid", true)
402 .Case("align", true)
403 .Case("allocate", true)
404 .Default(false)) {
405 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
406 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000407 // The deprecated declspec has an optional single argument, so we will
408 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000409 // not.
410 if (Tok.getKind() == tok::l_paren)
411 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
412 else
Aaron Ballman00e99962013-08-31 01:11:41 +0000413 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000414 } else if (Ident->getName() == "property") {
415 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000416 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000417 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000418 if (Tok.isNot(tok::l_paren)) {
419 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
420 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000421 return;
John McCall5e77d762013-04-16 07:28:30 +0000422 }
423 BalancedDelimiterTracker T(*this, tok::l_paren);
424 T.expectAndConsume(diag::err_expected_lparen_after,
425 Ident->getNameStart(), tok::r_paren);
426
427 enum AccessorKind {
428 AK_Invalid = -1,
429 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
430 };
431 IdentifierInfo *AccessorNames[] = { 0, 0 };
432 bool HasInvalidAccessor = false;
433
434 // Parse the accessor specifications.
435 while (true) {
436 // Stop if this doesn't look like an accessor spec.
437 if (!Tok.is(tok::identifier)) {
438 // If the user wrote a completely empty list, use a special diagnostic.
439 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
440 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
441 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
442 break;
443 }
444
445 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
446 break;
447 }
448
449 AccessorKind Kind;
450 SourceLocation KindLoc = Tok.getLocation();
451 StringRef KindStr = Tok.getIdentifierInfo()->getName();
452 if (KindStr == "get") {
453 Kind = AK_Get;
454 } else if (KindStr == "put") {
455 Kind = AK_Put;
456
457 // Recover from the common mistake of using 'set' instead of 'put'.
458 } else if (KindStr == "set") {
459 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
460 << FixItHint::CreateReplacement(KindLoc, "put");
461 Kind = AK_Put;
462
463 // Handle the mistake of forgetting the accessor kind by skipping
464 // this accessor.
465 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
466 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
467 ConsumeToken();
468 HasInvalidAccessor = true;
469 goto next_property_accessor;
470
471 // Otherwise, complain about the unknown accessor kind.
472 } else {
473 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
474 HasInvalidAccessor = true;
475 Kind = AK_Invalid;
476
477 // Try to keep parsing unless it doesn't look like an accessor spec.
478 if (!NextToken().is(tok::equal)) break;
479 }
480
481 // Consume the identifier.
482 ConsumeToken();
483
484 // Consume the '='.
485 if (Tok.is(tok::equal)) {
486 ConsumeToken();
487 } else {
488 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
489 << KindStr;
490 break;
491 }
492
493 // Expect the method name.
494 if (!Tok.is(tok::identifier)) {
495 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
496 break;
497 }
498
499 if (Kind == AK_Invalid) {
500 // Just drop invalid accessors.
501 } else if (AccessorNames[Kind] != NULL) {
502 // Complain about the repeated accessor, ignore it, and keep parsing.
503 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
504 } else {
505 AccessorNames[Kind] = Tok.getIdentifierInfo();
506 }
507 ConsumeToken();
508
509 next_property_accessor:
510 // Keep processing accessors until we run out.
511 if (Tok.is(tok::comma)) {
512 ConsumeAnyToken();
513 continue;
514
515 // If we run into the ')', stop without consuming it.
516 } else if (Tok.is(tok::r_paren)) {
517 break;
518 } else {
519 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
520 break;
521 }
522 }
523
524 // Only add the property attribute if it was well-formed.
525 if (!HasInvalidAccessor) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000526 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000527 AccessorNames[AK_Get], AccessorNames[AK_Put],
528 AttributeList::AS_Declspec);
529 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000530 T.skipToEnd();
531 } else {
532 // We don't recognize this as a valid declspec, but instead of creating the
533 // attribute and allowing sema to warn about it, we will warn here instead.
534 // This is because some attributes have multiple spellings, but we need to
535 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000536 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000537 // both locations.
538 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
539
540 // If there's an open paren, we should eat the open and close parens under
541 // the assumption that this unknown declspec has parameters.
542 BalancedDelimiterTracker T(*this, tok::l_paren);
543 if (!T.consumeOpen())
544 T.skipToEnd();
545 }
546}
547
Eli Friedman06de2b52009-06-08 07:21:15 +0000548/// [MS] decl-specifier:
549/// __declspec ( extended-decl-modifier-seq )
550///
551/// [MS] extended-decl-modifier-seq:
552/// extended-decl-modifier[opt]
553/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000554void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000555 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000556
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000557 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000558 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000559 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000560 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000561 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000562
Chad Rosierc1183952012-06-26 22:30:43 +0000563 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000564 // you can specify multiple attributes per declspec.
565 while (Tok.getKind() != tok::r_paren) {
566 // We expect either a well-known identifier or a generic string. Anything
567 // else is a malformed declspec.
568 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000569 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000570 Tok.getKind() != tok::kw_restrict) {
571 Diag(Tok, diag::err_ms_declspec_type);
572 T.skipToEnd();
573 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000574 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000575
576 IdentifierInfo *AttrName;
577 SourceLocation AttrNameLoc;
578 if (IsString) {
579 SmallString<8> StrBuffer;
580 bool Invalid = false;
581 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
582 if (Invalid) {
583 T.skipToEnd();
584 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000585 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000586 AttrName = PP.getIdentifierInfo(Str);
587 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000588 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000589 AttrName = Tok.getIdentifierInfo();
590 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000591 }
Chad Rosierc1183952012-06-26 22:30:43 +0000592
Aaron Ballman478faed2012-06-19 22:09:27 +0000593 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000594 // If we have a generic string, we will allow it because there is no
595 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000596 // (for instance, SAL declspecs in older versions of MSVC).
597 //
Chad Rosierc1183952012-06-26 22:30:43 +0000598 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000599 // arguments and can be turned into an attribute directly.
Aaron Ballman00e99962013-08-31 01:11:41 +0000600 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
601 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000602 else
603 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000604 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000605 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000606}
607
John McCall53fa7142010-12-24 02:08:15 +0000608void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000609 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000610 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000611 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000612 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballman317a77f2013-05-22 23:25:32 +0000613 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
614 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000615 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
616 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000617 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
618 AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000619 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000620}
621
John McCall53fa7142010-12-24 02:08:15 +0000622void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000623 // Treat these like attributes
624 while (Tok.is(tok::kw___pascal)) {
625 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
626 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000627 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
628 AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000629 }
John McCall53fa7142010-12-24 02:08:15 +0000630}
631
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000632void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
633 // Treat these like attributes
634 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000635 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000636 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000637 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
638 AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000639 }
640}
641
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000642void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000643 // FIXME: The mapping from attribute spelling to semantics should be
644 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000645 SourceLocation Loc = Tok.getLocation();
646 switch(Tok.getKind()) {
647 // OpenCL qualifiers:
648 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000649 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000650 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000651 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000652 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000653 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000654
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000655 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000656 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000657 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000658 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000659 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000660
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000661 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000662 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000663 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000664 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000665 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000666
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000667 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000668 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000669 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000670 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000671 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000672
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000673 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000674 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000675 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000676 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000677 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000678
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000679 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000680 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000681 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000682 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000683 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000684
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000685 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000686 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000687 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000688 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000689 break;
690 default: break;
691 }
692}
693
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000694/// \brief Parse a version number.
695///
696/// version:
697/// simple-integer
698/// simple-integer ',' simple-integer
699/// simple-integer ',' simple-integer ',' simple-integer
700VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
701 Range = Tok.getLocation();
702
703 if (!Tok.is(tok::numeric_constant)) {
704 Diag(Tok, diag::err_expected_version);
705 SkipUntil(tok::comma, tok::r_paren, true, true, true);
706 return VersionTuple();
707 }
708
709 // Parse the major (and possibly minor and subminor) versions, which
710 // are stored in the numeric constant. We utilize a quirk of the
711 // lexer, which is that it handles something like 1.2.3 as a single
712 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000713 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000714 Buffer.resize(Tok.getLength()+1);
715 const char *ThisTokBegin = &Buffer[0];
716
717 // Get the spelling of the token, which eliminates trigraphs, etc.
718 bool Invalid = false;
719 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
720 if (Invalid)
721 return VersionTuple();
722
723 // Parse the major version.
724 unsigned AfterMajor = 0;
725 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000726 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000727 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
728 ++AfterMajor;
729 }
730
731 if (AfterMajor == 0) {
732 Diag(Tok, diag::err_expected_version);
733 SkipUntil(tok::comma, tok::r_paren, true, true, true);
734 return VersionTuple();
735 }
736
737 if (AfterMajor == ActualLength) {
738 ConsumeToken();
739
740 // We only had a single version component.
741 if (Major == 0) {
742 Diag(Tok, diag::err_zero_version);
743 return VersionTuple();
744 }
745
746 return VersionTuple(Major);
747 }
748
749 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
750 Diag(Tok, diag::err_expected_version);
751 SkipUntil(tok::comma, tok::r_paren, true, true, true);
752 return VersionTuple();
753 }
754
755 // Parse the minor version.
756 unsigned AfterMinor = AfterMajor + 1;
757 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000758 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000759 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
760 ++AfterMinor;
761 }
762
763 if (AfterMinor == ActualLength) {
764 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000765
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000766 // We had major.minor.
767 if (Major == 0 && Minor == 0) {
768 Diag(Tok, diag::err_zero_version);
769 return VersionTuple();
770 }
771
Chad Rosierc1183952012-06-26 22:30:43 +0000772 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000773 }
774
775 // If what follows is not a '.', we have a problem.
776 if (ThisTokBegin[AfterMinor] != '.') {
777 Diag(Tok, diag::err_expected_version);
778 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosierc1183952012-06-26 22:30:43 +0000779 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000780 }
781
782 // Parse the subminor version.
783 unsigned AfterSubminor = AfterMinor + 1;
784 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000785 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000786 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
787 ++AfterSubminor;
788 }
789
790 if (AfterSubminor != ActualLength) {
791 Diag(Tok, diag::err_expected_version);
792 SkipUntil(tok::comma, tok::r_paren, true, true, true);
793 return VersionTuple();
794 }
795 ConsumeToken();
796 return VersionTuple(Major, Minor, Subminor);
797}
798
799/// \brief Parse the contents of the "availability" attribute.
800///
801/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000802/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000803///
804/// platform:
805/// identifier
806///
807/// version-arg-list:
808/// version-arg
809/// version-arg ',' version-arg-list
810///
811/// version-arg:
812/// 'introduced' '=' version
813/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000814/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000815/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000816/// opt-message:
817/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000818void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
819 SourceLocation AvailabilityLoc,
820 ParsedAttributes &attrs,
821 SourceLocation *endLoc) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000822 enum { Introduced, Deprecated, Obsoleted, Unknown };
823 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000824 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000825
826 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000827 BalancedDelimiterTracker T(*this, tok::l_paren);
828 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000829 Diag(Tok, diag::err_expected_lparen);
830 return;
831 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000832
833 // Parse the platform name,
834 if (Tok.isNot(tok::identifier)) {
835 Diag(Tok, diag::err_availability_expected_platform);
836 SkipUntil(tok::r_paren);
837 return;
838 }
Richard Smithfeefaf52013-09-03 18:01:40 +0000839 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000840
841 // Parse the ',' following the platform name.
842 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
843 return;
844
845 // If we haven't grabbed the pointers for the identifiers
846 // "introduced", "deprecated", and "obsoleted", do so now.
847 if (!Ident_introduced) {
848 Ident_introduced = PP.getIdentifierInfo("introduced");
849 Ident_deprecated = PP.getIdentifierInfo("deprecated");
850 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000851 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000852 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000853 }
854
855 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000856 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000857 do {
858 if (Tok.isNot(tok::identifier)) {
859 Diag(Tok, diag::err_availability_expected_change);
860 SkipUntil(tok::r_paren);
861 return;
862 }
863 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
864 SourceLocation KeywordLoc = ConsumeToken();
865
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000866 if (Keyword == Ident_unavailable) {
867 if (UnavailableLoc.isValid()) {
868 Diag(KeywordLoc, diag::err_availability_redundant)
869 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000870 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000871 UnavailableLoc = KeywordLoc;
872
873 if (Tok.isNot(tok::comma))
874 break;
875
876 ConsumeToken();
877 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000878 }
879
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000880 if (Tok.isNot(tok::equal)) {
881 Diag(Tok, diag::err_expected_equal_after)
882 << Keyword;
883 SkipUntil(tok::r_paren);
884 return;
885 }
886 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000887 if (Keyword == Ident_message) {
Benjamin Kramera9dfa922013-09-13 17:31:48 +0000888 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000889 Diag(Tok, diag::err_expected_string_literal)
890 << /*Source='availability attribute'*/2;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000891 SkipUntil(tok::r_paren);
892 return;
893 }
894 MessageExpr = ParseStringLiteralExpression();
895 break;
896 }
Chad Rosierc1183952012-06-26 22:30:43 +0000897
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000898 SourceRange VersionRange;
899 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000900
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000901 if (Version.empty()) {
902 SkipUntil(tok::r_paren);
903 return;
904 }
905
906 unsigned Index;
907 if (Keyword == Ident_introduced)
908 Index = Introduced;
909 else if (Keyword == Ident_deprecated)
910 Index = Deprecated;
911 else if (Keyword == Ident_obsoleted)
912 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000913 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000914 Index = Unknown;
915
916 if (Index < Unknown) {
917 if (!Changes[Index].KeywordLoc.isInvalid()) {
918 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000919 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000920 << SourceRange(Changes[Index].KeywordLoc,
921 Changes[Index].VersionRange.getEnd());
922 }
923
924 Changes[Index].KeywordLoc = KeywordLoc;
925 Changes[Index].Version = Version;
926 Changes[Index].VersionRange = VersionRange;
927 } else {
928 Diag(KeywordLoc, diag::err_availability_unknown_change)
929 << Keyword << VersionRange;
930 }
931
932 if (Tok.isNot(tok::comma))
933 break;
934
935 ConsumeToken();
936 } while (true);
937
938 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000939 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000940 return;
941
942 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000943 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000944
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000945 // The 'unavailable' availability cannot be combined with any other
946 // availability changes. Make sure that hasn't happened.
947 if (UnavailableLoc.isValid()) {
948 bool Complained = false;
949 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
950 if (Changes[Index].KeywordLoc.isValid()) {
951 if (!Complained) {
952 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
953 << SourceRange(Changes[Index].KeywordLoc,
954 Changes[Index].VersionRange.getEnd());
955 Complained = true;
956 }
957
958 // Clear out the availability.
959 Changes[Index] = AvailabilityChange();
960 }
961 }
962 }
963
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000964 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000965 attrs.addNew(&Availability,
966 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000967 0, AvailabilityLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +0000968 Platform,
John McCall084e83d2011-03-24 11:26:52 +0000969 Changes[Introduced],
970 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000971 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000972 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000973 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000974}
975
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000976
Bill Wendling44426052012-12-20 19:22:21 +0000977// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000978// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
979
980void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
981
982void Parser::LateParsedClass::ParseLexedAttributes() {
983 Self->ParseLexedAttributes(*Class);
984}
985
986void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000987 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000988}
989
990/// Wrapper class which calls ParseLexedAttribute, after setting up the
991/// scope appropriately.
992void Parser::ParseLexedAttributes(ParsingClass &Class) {
993 // Deal with templates
994 // FIXME: Test cases to make sure this does the right thing for templates.
995 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
996 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
997 HasTemplateScope);
998 if (HasTemplateScope)
999 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
1000
Douglas Gregor3024f072012-04-16 07:05:22 +00001001 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001002 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +00001003 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001004 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1005 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1006
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001007 // Enter the scope of nested classes
1008 if (!AlreadyHasClassScope)
1009 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1010 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001011 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001012 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1013 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1014 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001015 }
Chad Rosierc1183952012-06-26 22:30:43 +00001016
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001017 if (!AlreadyHasClassScope)
1018 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1019 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001020}
1021
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001022
1023/// \brief Parse all attributes in LAs, and attach them to Decl D.
1024void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1025 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001026 assert(LAs.parseSoon() &&
1027 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001028 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001029 if (D)
1030 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001031 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001032 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001033 }
1034 LAs.clear();
1035}
1036
1037
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001038/// \brief Finish parsing an attribute for which parsing was delayed.
1039/// This will be called at the end of parsing a class declaration
1040/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001041/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001042/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001043void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1044 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001045 // Save the current token position.
1046 SourceLocation OrigLoc = Tok.getLocation();
1047
1048 // Append the current token at the end of the new token stream so that it
1049 // doesn't get lost.
1050 LA.Toks.push_back(Tok);
1051 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1052 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001053 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001054
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001055 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +00001056 // FIXME: Do not warn on C++11 attributes, once we start supporting
1057 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001058 Diag(Tok, diag::warn_attribute_on_function_definition)
1059 << LA.AttrName.getName();
1060 }
1061
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001062 ParsedAttributes Attrs(AttrFactory);
1063 SourceLocation endLoc;
1064
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001065 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001066 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001067 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1068 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001069
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001070 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001071 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1072 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001073
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001074 if (LA.Decls.size() == 1) {
1075 // If the Decl is templatized, add template parameters to scope.
1076 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1077 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1078 if (HasTemplateScope)
1079 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001080
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001081 // If the Decl is on a function, add function parameters to the scope.
1082 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1083 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1084 if (HasFunScope)
1085 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001086
Michael Han23214e52012-10-03 01:56:22 +00001087 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001088 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001089
1090 if (HasFunScope) {
1091 Actions.ActOnExitFunctionContext();
1092 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1093 }
1094 if (HasTemplateScope) {
1095 TempScope.Exit();
1096 }
1097 } else {
1098 // If there are multiple decls, then the decl cannot be within the
1099 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001100 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001101 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001102 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001103 } else {
1104 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001105 }
1106
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001107 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1108 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1109 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001110
1111 if (Tok.getLocation() != OrigLoc) {
1112 // Due to a parsing error, we either went over the cached tokens or
1113 // there are still cached tokens left, so we skip the leftover tokens.
1114 // Since this is an uncommon situation that should be avoided, use the
1115 // expensive isBeforeInTranslationUnit call.
1116 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1117 OrigLoc))
1118 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001119 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001120 }
1121}
1122
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001123/// \brief Wrapper around a case statement checking if AttrName is
1124/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001125bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001126 return llvm::StringSwitch<bool>(AttrName)
1127 .Case("guarded_by", true)
1128 .Case("guarded_var", true)
1129 .Case("pt_guarded_by", true)
1130 .Case("pt_guarded_var", true)
1131 .Case("lockable", true)
1132 .Case("scoped_lockable", true)
1133 .Case("no_thread_safety_analysis", true)
1134 .Case("acquired_after", true)
1135 .Case("acquired_before", true)
1136 .Case("exclusive_lock_function", true)
1137 .Case("shared_lock_function", true)
1138 .Case("exclusive_trylock_function", true)
1139 .Case("shared_trylock_function", true)
1140 .Case("unlock_function", true)
1141 .Case("lock_returned", true)
1142 .Case("locks_excluded", true)
1143 .Case("exclusive_locks_required", true)
1144 .Case("shared_locks_required", true)
1145 .Default(false);
1146}
1147
1148/// \brief Parse the contents of thread safety attributes. These
1149/// should always be parsed as an expression list.
1150///
1151/// We need to special case the parsing due to the fact that if the first token
1152/// of the first argument is an identifier, the main parse loop will store
1153/// that token as a "parameter" and the rest of
1154/// the arguments will be added to a list of "arguments". However,
1155/// subsequent tokens in the first argument are lost. We instead parse each
1156/// argument as an expression and add all arguments to the list of "arguments".
1157/// In future, we will take advantage of this special case to also
1158/// deal with some argument scoping issues here (for example, referring to a
1159/// function parameter in the attribute on that function).
1160void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1161 SourceLocation AttrNameLoc,
1162 ParsedAttributes &Attrs,
1163 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001164 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001165
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001166 BalancedDelimiterTracker T(*this, tok::l_paren);
1167 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001168
Aaron Ballman00e99962013-08-31 01:11:41 +00001169 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001170 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001171
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001172 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001173 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001174 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001175 ExprResult ArgExpr(ParseAssignmentExpression());
1176 if (ArgExpr.isInvalid()) {
1177 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001178 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001179 break;
1180 } else {
1181 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001182 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001183 if (Tok.isNot(tok::comma))
1184 break;
1185 ConsumeToken(); // Eat the comma, move to the next argument
1186 }
1187 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001188 if (ArgExprsOk && !T.consumeClose()) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001189 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(),
1190 ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001191 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001192 if (EndLoc)
1193 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001194}
1195
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001196void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1197 SourceLocation AttrNameLoc,
1198 ParsedAttributes &Attrs,
1199 SourceLocation *EndLoc) {
1200 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1201
1202 BalancedDelimiterTracker T(*this, tok::l_paren);
1203 T.consumeOpen();
1204
1205 if (Tok.isNot(tok::identifier)) {
1206 Diag(Tok, diag::err_expected_ident);
1207 T.skipToEnd();
1208 return;
1209 }
Richard Smithfeefaf52013-09-03 18:01:40 +00001210 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001211
1212 if (Tok.isNot(tok::comma)) {
1213 Diag(Tok, diag::err_expected_comma);
1214 T.skipToEnd();
1215 return;
1216 }
1217 ConsumeToken();
1218
1219 SourceRange MatchingCTypeRange;
1220 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1221 if (MatchingCType.isInvalid()) {
1222 T.skipToEnd();
1223 return;
1224 }
1225
1226 bool LayoutCompatible = false;
1227 bool MustBeNull = false;
1228 while (Tok.is(tok::comma)) {
1229 ConsumeToken();
1230 if (Tok.isNot(tok::identifier)) {
1231 Diag(Tok, diag::err_expected_ident);
1232 T.skipToEnd();
1233 return;
1234 }
1235 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1236 if (Flag->isStr("layout_compatible"))
1237 LayoutCompatible = true;
1238 else if (Flag->isStr("must_be_null"))
1239 MustBeNull = true;
1240 else {
1241 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1242 T.skipToEnd();
1243 return;
1244 }
1245 ConsumeToken(); // consume flag
1246 }
1247
1248 if (!T.consumeClose()) {
1249 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +00001250 ArgumentKind, MatchingCType.release(),
1251 LayoutCompatible, MustBeNull,
1252 AttributeList::AS_GNU);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001253 }
1254
1255 if (EndLoc)
1256 *EndLoc = T.getCloseLocation();
1257}
1258
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001259/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1260/// of a C++11 attribute-specifier in a location where an attribute is not
1261/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1262/// situation.
1263///
1264/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1265/// this doesn't appear to actually be an attribute-specifier, and the caller
1266/// should try to parse it.
1267bool Parser::DiagnoseProhibitedCXX11Attribute() {
1268 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1269
1270 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1271 case CAK_NotAttributeSpecifier:
1272 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1273 return false;
1274
1275 case CAK_InvalidAttributeSpecifier:
1276 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1277 return false;
1278
1279 case CAK_AttributeSpecifier:
1280 // Parse and discard the attributes.
1281 SourceLocation BeginLoc = ConsumeBracket();
1282 ConsumeBracket();
1283 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1284 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1285 SourceLocation EndLoc = ConsumeBracket();
1286 Diag(BeginLoc, diag::err_attributes_not_allowed)
1287 << SourceRange(BeginLoc, EndLoc);
1288 return true;
1289 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001290 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001291}
1292
Richard Smith98155ad2013-02-20 01:17:14 +00001293/// \brief We have found the opening square brackets of a C++11
1294/// attribute-specifier in a location where an attribute is not permitted, but
1295/// we know where the attributes ought to be written. Parse them anyway, and
1296/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001297void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1298 SourceLocation CorrectLocation) {
1299 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1300 Tok.is(tok::kw_alignas));
1301
1302 // Consume the attributes.
1303 SourceLocation Loc = Tok.getLocation();
1304 ParseCXX11Attributes(Attrs);
1305 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1306
1307 Diag(Loc, diag::err_attributes_not_allowed)
1308 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1309 << FixItHint::CreateRemoval(AttrRange);
1310}
1311
John McCall53fa7142010-12-24 02:08:15 +00001312void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1313 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1314 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001315}
1316
Michael Han64536a62012-11-06 19:34:54 +00001317void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1318 AttributeList *AttrList = attrs.getList();
1319 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001320 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001321 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001322 << AttrList->getName();
1323 AttrList->setInvalid();
1324 }
1325 AttrList = AttrList->getNext();
1326 }
1327}
1328
Chris Lattner53361ac2006-08-10 05:19:57 +00001329/// ParseDeclaration - Parse a full 'declaration', which consists of
1330/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001331/// 'Context' should be a Declarator::TheContext value. This returns the
1332/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001333///
1334/// declaration: [C99 6.7]
1335/// block-declaration ->
1336/// simple-declaration
1337/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001338/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001339/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001340/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001341/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001342/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001343/// others... [FIXME]
1344///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001345Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1346 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001347 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001348 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001349 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001350 // Must temporarily exit the objective-c container scope for
1351 // parsing c none objective-c decls.
1352 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001353
John McCall48871652010-08-21 09:40:31 +00001354 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001355 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001356 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001357 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001358 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001359 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001360 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001361 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001362 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001363 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001364 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001365 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001366 SourceLocation InlineLoc = ConsumeToken();
1367 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1368 break;
1369 }
Chad Rosierc1183952012-06-26 22:30:43 +00001370 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001371 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001372 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001373 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001374 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001375 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001376 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001377 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001378 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001379 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001380 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001381 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001382 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001383 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001384 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001385 default:
John McCall53fa7142010-12-24 02:08:15 +00001386 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001387 }
Chad Rosierc1183952012-06-26 22:30:43 +00001388
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001389 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001390 // single decl, convert it now. Alias declarations can also declare a type;
1391 // include that too if it is present.
1392 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001393}
1394
1395/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1396/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001397/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1398/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001399///[C90/C++]init-declarator-list ';' [TODO]
1400/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001401///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001402/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001403/// attribute-specifier-seq[opt] type-specifier-seq declarator
1404///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001405/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001406/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001407///
1408/// If FRI is non-null, we might be parsing a for-range-declaration instead
1409/// of a simple-declaration. If we find that we are, we also parse the
1410/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001411Parser::DeclGroupPtrTy
1412Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1413 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001414 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001415 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001416 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001417 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001418
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001419 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001420 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001421
Chris Lattner0e894622006-08-13 19:58:17 +00001422 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1423 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001424 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001425 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001426 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001427 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001428 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001429 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001430 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001431 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001432 }
Chad Rosierc1183952012-06-26 22:30:43 +00001433
Richard Smith2386c8b2013-02-22 09:06:26 +00001434 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001435 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001436}
Mike Stump11289f42009-09-09 15:08:12 +00001437
Richard Smith09f76ee2011-10-19 21:33:05 +00001438/// Returns true if this might be the start of a declarator, or a common typo
1439/// for a declarator.
1440bool Parser::MightBeDeclarator(unsigned Context) {
1441 switch (Tok.getKind()) {
1442 case tok::annot_cxxscope:
1443 case tok::annot_template_id:
1444 case tok::caret:
1445 case tok::code_completion:
1446 case tok::coloncolon:
1447 case tok::ellipsis:
1448 case tok::kw___attribute:
1449 case tok::kw_operator:
1450 case tok::l_paren:
1451 case tok::star:
1452 return true;
1453
1454 case tok::amp:
1455 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001456 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001457
Richard Smithc8a79032012-01-09 22:31:44 +00001458 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001459 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001460 NextToken().is(tok::l_square);
1461
1462 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001463 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001464
Richard Smith09f76ee2011-10-19 21:33:05 +00001465 case tok::identifier:
1466 switch (NextToken().getKind()) {
1467 case tok::code_completion:
1468 case tok::coloncolon:
1469 case tok::comma:
1470 case tok::equal:
1471 case tok::equalequal: // Might be a typo for '='.
1472 case tok::kw_alignas:
1473 case tok::kw_asm:
1474 case tok::kw___attribute:
1475 case tok::l_brace:
1476 case tok::l_paren:
1477 case tok::l_square:
1478 case tok::less:
1479 case tok::r_brace:
1480 case tok::r_paren:
1481 case tok::r_square:
1482 case tok::semi:
1483 return true;
1484
1485 case tok::colon:
1486 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001487 // and in block scope it's probably a label. Inside a class definition,
1488 // this is a bit-field.
1489 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001490 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001491
1492 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001493 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001494
1495 default:
1496 return false;
1497 }
1498
1499 default:
1500 return false;
1501 }
1502}
1503
Richard Smithb8caac82012-04-11 20:59:20 +00001504/// Skip until we reach something which seems like a sensible place to pick
1505/// up parsing after a malformed declaration. This will sometimes stop sooner
1506/// than SkipUntil(tok::r_brace) would, but will never stop later.
1507void Parser::SkipMalformedDecl() {
1508 while (true) {
1509 switch (Tok.getKind()) {
1510 case tok::l_brace:
1511 // Skip until matching }, then stop. We've probably skipped over
1512 // a malformed class or function definition or similar.
1513 ConsumeBrace();
1514 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1515 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1516 // This declaration isn't over yet. Keep skipping.
1517 continue;
1518 }
1519 if (Tok.is(tok::semi))
1520 ConsumeToken();
1521 return;
1522
1523 case tok::l_square:
1524 ConsumeBracket();
1525 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1526 continue;
1527
1528 case tok::l_paren:
1529 ConsumeParen();
1530 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1531 continue;
1532
1533 case tok::r_brace:
1534 return;
1535
1536 case tok::semi:
1537 ConsumeToken();
1538 return;
1539
1540 case tok::kw_inline:
1541 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001542 // a good place to pick back up parsing, except in an Objective-C
1543 // @interface context.
1544 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1545 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001546 return;
1547 break;
1548
1549 case tok::kw_namespace:
1550 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001551 // place to pick back up parsing, except in an Objective-C
1552 // @interface context.
1553 if (Tok.isAtStartOfLine() &&
1554 (!ParsingInObjCContainer || CurParsedObjCImpl))
1555 return;
1556 break;
1557
1558 case tok::at:
1559 // @end is very much like } in Objective-C contexts.
1560 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1561 ParsingInObjCContainer)
1562 return;
1563 break;
1564
1565 case tok::minus:
1566 case tok::plus:
1567 // - and + probably start new method declarations in Objective-C contexts.
1568 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001569 return;
1570 break;
1571
1572 case tok::eof:
1573 return;
1574
1575 default:
1576 break;
1577 }
1578
1579 ConsumeAnyToken();
1580 }
1581}
1582
John McCalld5a36322009-11-03 19:26:08 +00001583/// ParseDeclGroup - Having concluded that this is either a function
1584/// definition or a group of object declarations, actually parse the
1585/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001586Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1587 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001588 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001589 SourceLocation *DeclEnd,
1590 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001591 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001592 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001593 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001594
John McCalld5a36322009-11-03 19:26:08 +00001595 // Bail out if the first declarator didn't seem well-formed.
1596 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001597 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001598 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001599 }
Mike Stump11289f42009-09-09 15:08:12 +00001600
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001601 // Save late-parsed attributes for now; they need to be parsed in the
1602 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001603 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1604 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001605 if (D.isFunctionDeclarator())
1606 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1607
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001608 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001609 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001610 // Look at the next token to make sure that this isn't a function
1611 // declaration. We have to check this because __attribute__ might be the
1612 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001613 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001614
Douglas Gregor012efe22013-04-16 16:01:32 +00001615 if (AllowFunctionDefinitions) {
1616 if (isStartOfFunctionDefinition(D)) {
1617 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1618 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001619
Douglas Gregor012efe22013-04-16 16:01:32 +00001620 // Recover by treating the 'typedef' as spurious.
1621 DS.ClearStorageClassSpecs();
1622 }
1623
1624 Decl *TheDecl =
1625 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1626 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001627 }
1628
Douglas Gregor012efe22013-04-16 16:01:32 +00001629 if (isDeclarationSpecifier()) {
1630 // If there is an invalid declaration specifier right after the function
1631 // prototype, then we must be in a missing semicolon case where this isn't
1632 // actually a body. Just fall through into the code that handles it as a
1633 // prototype, and let the top-level code handle the erroneous declspec
1634 // where it would otherwise expect a comma or semicolon.
1635 } else {
1636 Diag(Tok, diag::err_expected_fn_body);
1637 SkipUntil(tok::semi);
1638 return DeclGroupPtrTy();
1639 }
John McCalld5a36322009-11-03 19:26:08 +00001640 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001641 if (Tok.is(tok::l_brace)) {
1642 Diag(Tok, diag::err_function_definition_not_allowed);
1643 SkipUntil(tok::r_brace, true, true);
1644 }
John McCalld5a36322009-11-03 19:26:08 +00001645 }
1646 }
1647
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001648 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001649 return DeclGroupPtrTy();
1650
1651 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1652 // must parse and analyze the for-range-initializer before the declaration is
1653 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001654 //
1655 // Handle the Objective-C for-in loop variable similarly, although we
1656 // don't need to parse the container in advance.
1657 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1658 bool IsForRangeLoop = false;
1659 if (Tok.is(tok::colon)) {
1660 IsForRangeLoop = true;
1661 FRI->ColonLoc = ConsumeToken();
1662 if (Tok.is(tok::l_brace))
1663 FRI->RangeExpr = ParseBraceInitializer();
1664 else
1665 FRI->RangeExpr = ParseExpression();
1666 }
1667
Richard Smith02e85f32011-04-14 22:09:26 +00001668 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001669 if (IsForRangeLoop)
1670 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001671 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001672 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001673 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001674 }
1675
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001676 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001677 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001678 if (LateParsedAttrs.size() > 0)
1679 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001680 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001681 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001682 DeclsInGroup.push_back(FirstDecl);
1683
Richard Smith09f76ee2011-10-19 21:33:05 +00001684 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001685
John McCalld5a36322009-11-03 19:26:08 +00001686 // If we don't have a comma, it is either the end of the list (a ';') or an
1687 // error, bail out.
1688 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001689 SourceLocation CommaLoc = ConsumeToken();
1690
1691 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1692 // This comma was followed by a line-break and something which can't be
1693 // the start of a declarator. The comma was probably a typo for a
1694 // semicolon.
1695 Diag(CommaLoc, diag::err_expected_semi_declaration)
1696 << FixItHint::CreateReplacement(CommaLoc, ";");
1697 ExpectSemi = false;
1698 break;
1699 }
John McCalld5a36322009-11-03 19:26:08 +00001700
1701 // Parse the next declarator.
1702 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001703 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001704
1705 // Accept attributes in an init-declarator. In the first declarator in a
1706 // declaration, these would be part of the declspec. In subsequent
1707 // declarators, they become part of the declarator itself, so that they
1708 // don't apply to declarators after *this* one. Examples:
1709 // short __attribute__((common)) var; -> declspec
1710 // short var __attribute__((common)); -> declarator
1711 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001712 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001713
1714 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001715 if (!D.isInvalidType()) {
1716 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1717 D.complete(ThisDecl);
1718 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001719 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001720 }
John McCalld5a36322009-11-03 19:26:08 +00001721 }
1722
1723 if (DeclEnd)
1724 *DeclEnd = Tok.getLocation();
1725
Richard Smith09f76ee2011-10-19 21:33:05 +00001726 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001727 ExpectAndConsumeSemi(Context == Declarator::FileContext
1728 ? diag::err_invalid_token_after_toplevel_declarator
1729 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001730 // Okay, there was no semicolon and one was expected. If we see a
1731 // declaration specifier, just assume it was missing and continue parsing.
1732 // Otherwise things are very confused and we skip to recover.
1733 if (!isDeclarationSpecifier()) {
1734 SkipUntil(tok::r_brace, true, true);
1735 if (Tok.is(tok::semi))
1736 ConsumeToken();
1737 }
John McCalld5a36322009-11-03 19:26:08 +00001738 }
1739
Rafael Espindolaab417692013-07-09 12:05:01 +00001740 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001741}
1742
Richard Smith02e85f32011-04-14 22:09:26 +00001743/// Parse an optional simple-asm-expr and attributes, and attach them to a
1744/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001745bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001746 // If a simple-asm-expr is present, parse it.
1747 if (Tok.is(tok::kw_asm)) {
1748 SourceLocation Loc;
1749 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1750 if (AsmLabel.isInvalid()) {
1751 SkipUntil(tok::semi, true, true);
1752 return true;
1753 }
1754
1755 D.setAsmLabel(AsmLabel.release());
1756 D.SetRangeEnd(Loc);
1757 }
1758
1759 MaybeParseGNUAttributes(D);
1760 return false;
1761}
1762
Douglas Gregor23996282009-05-12 21:31:51 +00001763/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1764/// declarator'. This method parses the remainder of the declaration
1765/// (including any attributes or initializer, among other things) and
1766/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001767///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001768/// init-declarator: [C99 6.7]
1769/// declarator
1770/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001771/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1772/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001773/// [C++] declarator initializer[opt]
1774///
1775/// [C++] initializer:
1776/// [C++] '=' initializer-clause
1777/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001778/// [C++0x] '=' 'default' [TODO]
1779/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001780/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001781///
1782/// According to the standard grammar, =default and =delete are function
1783/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001784///
John McCall48871652010-08-21 09:40:31 +00001785Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001786 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001787 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001788 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001789
Richard Smith02e85f32011-04-14 22:09:26 +00001790 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1791}
Mike Stump11289f42009-09-09 15:08:12 +00001792
Richard Smith02e85f32011-04-14 22:09:26 +00001793Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1794 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001795 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001796 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001797 switch (TemplateInfo.Kind) {
1798 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001799 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001800 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001801
Douglas Gregor450f00842009-09-25 18:43:00 +00001802 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001803 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001804 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001805 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001806 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00001807 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001808 // Re-direct this decl to refer to the templated decl so that we can
1809 // initialize it.
1810 ThisDecl = VT->getTemplatedDecl();
1811 break;
1812 }
1813 case ParsedTemplateInfo::ExplicitInstantiation: {
1814 if (Tok.is(tok::semi)) {
1815 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1816 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1817 if (ThisRes.isInvalid()) {
1818 SkipUntil(tok::semi, true, true);
1819 return 0;
1820 }
1821 ThisDecl = ThisRes.get();
1822 } else {
1823 // FIXME: This check should be for a variable template instantiation only.
1824
1825 // Check that this is a valid instantiation
1826 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1827 // If the declarator-id is not a template-id, issue a diagnostic and
1828 // recover by ignoring the 'template' keyword.
1829 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1830 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1831 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1832 } else {
1833 SourceLocation LAngleLoc =
1834 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1835 Diag(D.getIdentifierLoc(),
1836 diag::err_explicit_instantiation_with_definition)
1837 << SourceRange(TemplateInfo.TemplateLoc)
1838 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1839
1840 // Recover as if it were an explicit specialization.
1841 TemplateParameterLists FakedParamLists;
1842 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1843 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1844 LAngleLoc));
1845
1846 ThisDecl =
1847 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1848 }
1849 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001850 break;
1851 }
1852 }
Mike Stump11289f42009-09-09 15:08:12 +00001853
Richard Smith74aeef52013-04-26 16:15:35 +00001854 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001855
Douglas Gregor23996282009-05-12 21:31:51 +00001856 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001857 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001858 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001859 ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001860
Anders Carlsson991285e2010-09-24 21:25:25 +00001861 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001862 if (D.isFunctionDeclarator())
1863 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1864 << 1 /* delete */;
1865 else
1866 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001867 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001868 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001869 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1870 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001871 else
1872 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001873 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001874 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001875 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001876 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001877 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001878
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001879 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001880 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001881 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001882 cutOffParsing();
1883 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001884 }
Chad Rosierc1183952012-06-26 22:30:43 +00001885
John McCalldadc5752010-08-24 06:29:42 +00001886 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001887
David Blaikiebbafb8a2012-03-11 07:00:24 +00001888 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001889 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001890 ExitScope();
1891 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001892
Douglas Gregor23996282009-05-12 21:31:51 +00001893 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001894 SkipUntil(tok::comma, true, true);
1895 Actions.ActOnInitializerError(ThisDecl);
1896 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001897 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1898 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001899 }
1900 } else if (Tok.is(tok::l_paren)) {
1901 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001902 BalancedDelimiterTracker T(*this, tok::l_paren);
1903 T.consumeOpen();
1904
Benjamin Kramerf0623432012-08-23 22:51:59 +00001905 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001906 CommaLocsTy CommaLocs;
1907
David Blaikiebbafb8a2012-03-11 07:00:24 +00001908 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001909 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001910 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001911 }
1912
Douglas Gregor23996282009-05-12 21:31:51 +00001913 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001914 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00001915 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001916
David Blaikiebbafb8a2012-03-11 07:00:24 +00001917 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001918 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001919 ExitScope();
1920 }
Douglas Gregor23996282009-05-12 21:31:51 +00001921 } else {
1922 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001923 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001924
1925 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1926 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001927
David Blaikiebbafb8a2012-03-11 07:00:24 +00001928 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001929 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001930 ExitScope();
1931 }
1932
Sebastian Redla9351792012-02-11 23:51:47 +00001933 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1934 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001935 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001936 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1937 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001938 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001939 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001940 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001941 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001942 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1943
Sebastian Redl3da34892011-06-05 12:23:16 +00001944 if (D.getCXXScopeSpec().isSet()) {
1945 EnterScope(0);
1946 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1947 }
1948
1949 ExprResult Init(ParseBraceInitializer());
1950
1951 if (D.getCXXScopeSpec().isSet()) {
1952 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1953 ExitScope();
1954 }
1955
1956 if (Init.isInvalid()) {
1957 Actions.ActOnInitializerError(ThisDecl);
1958 } else
1959 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1960 /*DirectInit=*/true, TypeContainsAuto);
1961
Douglas Gregor23996282009-05-12 21:31:51 +00001962 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001963 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001964 }
1965
Richard Smithb2bc2e62011-02-21 20:05:19 +00001966 Actions.FinalizeDeclaration(ThisDecl);
1967
Douglas Gregor23996282009-05-12 21:31:51 +00001968 return ThisDecl;
1969}
1970
Chris Lattner1890ac82006-08-13 01:16:23 +00001971/// ParseSpecifierQualifierList
1972/// specifier-qualifier-list:
1973/// type-specifier specifier-qualifier-list[opt]
1974/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001975/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001976///
Richard Smithc5b05522012-03-12 07:56:15 +00001977void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1978 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001979 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1980 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001981 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001982 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001983
Chris Lattner1890ac82006-08-13 01:16:23 +00001984 // Validate declspec for type-name.
1985 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001986 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1987 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001988 Diag(Tok, diag::err_expected_type);
1989 DS.SetTypeSpecError();
1990 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1991 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001992 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001993 if (!DS.hasTypeSpecifier())
1994 DS.SetTypeSpecError();
1995 }
Mike Stump11289f42009-09-09 15:08:12 +00001996
Chris Lattner1b22eed2006-11-28 05:12:07 +00001997 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001998 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001999 if (DS.getStorageClassSpecLoc().isValid())
2000 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2001 else
Richard Smithb4a9e862013-04-12 22:46:28 +00002002 Diag(DS.getThreadStorageClassSpecLoc(),
2003 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00002004 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002005 }
Mike Stump11289f42009-09-09 15:08:12 +00002006
Chris Lattner1b22eed2006-11-28 05:12:07 +00002007 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002008 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002009 if (DS.isInlineSpecified())
2010 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2011 if (DS.isVirtualSpecified())
2012 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2013 if (DS.isExplicitSpecified())
2014 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002015 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002016 }
Richard Smithc5b05522012-03-12 07:56:15 +00002017
2018 // Issue diagnostic and remove constexpr specfier if present.
2019 if (DS.isConstexprSpecified()) {
2020 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2021 DS.ClearConstexprSpec();
2022 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002023}
Chris Lattner53361ac2006-08-10 05:19:57 +00002024
Chris Lattner6cc055a2009-04-12 20:42:31 +00002025/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2026/// specified token is valid after the identifier in a declarator which
2027/// immediately follows the declspec. For example, these things are valid:
2028///
2029/// int x [ 4]; // direct-declarator
2030/// int x ( int y); // direct-declarator
2031/// int(int x ) // direct-declarator
2032/// int x ; // simple-declaration
2033/// int x = 17; // init-declarator-list
2034/// int x , y; // init-declarator-list
2035/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002036/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002037/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002038///
2039/// This is not, because 'x' does not immediately follow the declspec (though
2040/// ')' happens to be valid anyway).
2041/// int (x)
2042///
2043static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2044 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2045 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002046 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002047}
2048
Chris Lattner20a0c612009-04-14 21:34:55 +00002049
2050/// ParseImplicitInt - This method is called when we have an non-typename
2051/// identifier in a declspec (which normally terminates the decl spec) when
2052/// the declspec has no type specifier. In this case, the declspec is either
2053/// malformed or is "implicit int" (in K&R and C89).
2054///
2055/// This method handles diagnosing this prettily and returns false if the
2056/// declspec is done being processed. If it recovers and thinks there may be
2057/// other pieces of declspec after it, it returns true.
2058///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002059bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002060 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002061 AccessSpecifier AS, DeclSpecContext DSC,
2062 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002063 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002064
Chris Lattner20a0c612009-04-14 21:34:55 +00002065 SourceLocation Loc = Tok.getLocation();
2066 // If we see an identifier that is not a type name, we normally would
2067 // parse it as the identifer being declared. However, when a typename
2068 // is typo'd or the definition is not included, this will incorrectly
2069 // parse the typename as the identifier name and fall over misparsing
2070 // later parts of the diagnostic.
2071 //
2072 // As such, we try to do some look-ahead in cases where this would
2073 // otherwise be an "implicit-int" case to see if this is invalid. For
2074 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2075 // an identifier with implicit int, we'd get a parse error because the
2076 // next token is obviously invalid for a type. Parse these as a case
2077 // with an invalid type specifier.
2078 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002079
Chris Lattner20a0c612009-04-14 21:34:55 +00002080 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002081 // error, do lookahead to try to do better recovery. This never applies
2082 // within a type specifier. Outside of C++, we allow this even if the
2083 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002084 // implicit int as an extension in C99 and C11.
Richard Smith2f07ad52012-05-09 20:55:26 +00002085 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith3b870382013-04-30 22:43:51 +00002086 !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002087 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002088 // If this token is valid for implicit int, e.g. "static x = 4", then
2089 // we just avoid eating the identifier, so it will be parsed as the
2090 // identifier in the declarator.
2091 return false;
2092 }
Mike Stump11289f42009-09-09 15:08:12 +00002093
Richard Smitha952ebb2012-05-15 21:01:51 +00002094 if (getLangOpts().CPlusPlus &&
2095 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2096 // Don't require a type specifier if we have the 'auto' storage class
2097 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002098 if (SS)
2099 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002100 return false;
2101 }
2102
Chris Lattner20a0c612009-04-14 21:34:55 +00002103 // Otherwise, if we don't consume this token, we are going to emit an
2104 // error anyway. Try to recover from various common problems. Check
2105 // to see if this was a reference to a tag name without a tag specified.
2106 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002107 //
2108 // C++ doesn't need this, and isTagName doesn't take SS.
2109 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002110 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002111 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002112
Douglas Gregor0be31a22010-07-02 17:43:08 +00002113 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002114 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002115 case DeclSpec::TST_enum:
2116 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2117 case DeclSpec::TST_union:
2118 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2119 case DeclSpec::TST_struct:
2120 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002121 case DeclSpec::TST_interface:
2122 TagName="__interface"; FixitTagName = "__interface ";
2123 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002124 case DeclSpec::TST_class:
2125 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002126 }
Mike Stump11289f42009-09-09 15:08:12 +00002127
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002128 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002129 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2130 LookupResult R(Actions, TokenName, SourceLocation(),
2131 Sema::LookupOrdinaryName);
2132
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002133 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002134 << TokenName << TagName << getLangOpts().CPlusPlus
2135 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2136
2137 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2138 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2139 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002140 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002141 << TokenName << TagName;
2142 }
Mike Stump11289f42009-09-09 15:08:12 +00002143
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002144 // Parse this as a tag as if the missing tag were present.
2145 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002146 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002147 else
Richard Smithc5b05522012-03-12 07:56:15 +00002148 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002149 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002150 return true;
2151 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002152 }
Mike Stump11289f42009-09-09 15:08:12 +00002153
Richard Smithfe904f02012-05-15 21:29:55 +00002154 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002155 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00002156 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2157 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002158 // Look ahead to the next token to try to figure out what this declaration
2159 // was supposed to be.
2160 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002161 case tok::l_paren: {
2162 // static x(4); // 'x' is not a type
2163 // x(int n); // 'x' is not a type
2164 // x (*p)[]; // 'x' is a type
2165 //
2166 // Since we're in an error case (or the rare 'implicit int in C++' MS
2167 // extension), we can afford to perform a tentative parse to determine
2168 // which case we're in.
2169 TentativeParsingAction PA(*this);
2170 ConsumeToken();
2171 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2172 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002173
2174 if (TPR != TPResult::False()) {
2175 // The identifier is followed by a parenthesized declarator.
2176 // It's supposed to be a type.
2177 break;
2178 }
2179
2180 // If we're in a context where we could be declaring a constructor,
2181 // check whether this is a constructor declaration with a bogus name.
2182 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2183 IdentifierInfo *II = Tok.getIdentifierInfo();
2184 if (Actions.isCurrentClassNameTypo(II, SS)) {
2185 Diag(Loc, diag::err_constructor_bad_name)
2186 << Tok.getIdentifierInfo() << II
2187 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2188 Tok.setIdentifierInfo(II);
2189 }
2190 }
2191 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002192 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002193 case tok::comma:
2194 case tok::equal:
2195 case tok::kw_asm:
2196 case tok::l_brace:
2197 case tok::l_square:
2198 case tok::semi:
2199 // This looks like a variable or function declaration. The type is
2200 // probably missing. We're done parsing decl-specifiers.
2201 if (SS)
2202 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2203 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002204
2205 default:
2206 // This is probably supposed to be a type. This includes cases like:
2207 // int f(itn);
2208 // struct S { unsinged : 4; };
2209 break;
2210 }
2211 }
2212
Chad Rosierc1183952012-06-26 22:30:43 +00002213 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002214 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002215 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002216 IdentifierInfo *II = Tok.getIdentifierInfo();
2217 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002218 // The action emitted a diagnostic, so we don't have to.
2219 if (T) {
2220 // The action has suggested that the type T could be used. Set that as
2221 // the type in the declaration specifiers, consume the would-be type
2222 // name token, and we're done.
2223 const char *PrevSpec;
2224 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002225 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002226 DS.SetRangeEnd(Tok.getLocation());
2227 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002228 // There may be other declaration specifiers after this.
2229 return true;
2230 } else if (II != Tok.getIdentifierInfo()) {
2231 // If no type was suggested, the correction is to a keyword
2232 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002233 // There may be other declaration specifiers after this.
2234 return true;
2235 }
Chad Rosierc1183952012-06-26 22:30:43 +00002236
Douglas Gregor15e56022009-10-13 23:27:22 +00002237 // Fall through; the action had no suggestion for us.
2238 } else {
2239 // The action did not emit a diagnostic, so emit one now.
2240 SourceRange R;
2241 if (SS) R = SS->getRange();
2242 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2243 }
Mike Stump11289f42009-09-09 15:08:12 +00002244
Douglas Gregor15e56022009-10-13 23:27:22 +00002245 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002246 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002247 DS.SetRangeEnd(Tok.getLocation());
2248 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002249
Chris Lattner20a0c612009-04-14 21:34:55 +00002250 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2251 // avoid rippling error messages on subsequent uses of the same type,
2252 // could be useful if #include was forgotten.
2253 return false;
2254}
2255
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002256/// \brief Determine the declaration specifier context from the declarator
2257/// context.
2258///
2259/// \param Context the declarator context, which is one of the
2260/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002261Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002262Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2263 if (Context == Declarator::MemberContext)
2264 return DSC_class;
2265 if (Context == Declarator::FileContext)
2266 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002267 if (Context == Declarator::TrailingReturnContext)
2268 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002269 return DSC_normal;
2270}
2271
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002272/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2273///
2274/// FIXME: Simply returns an alignof() expression if the argument is a
2275/// type. Ideally, the type should be propagated directly into Sema.
2276///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002277/// [C11] type-id
2278/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002279/// [C++0x] type-id ...[opt]
2280/// [C++0x] assignment-expression ...[opt]
2281ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2282 SourceLocation &EllipsisLoc) {
2283 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002284 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002285 SourceLocation TypeLoc = Tok.getLocation();
2286 ParsedType Ty = ParseTypeName().get();
2287 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002288 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2289 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002290 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002291 ER = ParseConstantExpression();
2292
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002293 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002294 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002295
2296 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002297}
2298
2299/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2300/// attribute to Attrs.
2301///
2302/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002303/// [C11] '_Alignas' '(' type-id ')'
2304/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002305/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2306/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002307void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002308 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002309 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2310 "Not an alignment-specifier!");
2311
Richard Smithd11c7a12013-01-29 01:48:07 +00002312 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2313 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002314
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002315 BalancedDelimiterTracker T(*this, tok::l_paren);
2316 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002317 return;
2318
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002319 SourceLocation EllipsisLoc;
2320 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002321 if (ArgExpr.isInvalid()) {
2322 SkipUntil(tok::r_paren);
2323 return;
2324 }
2325
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002326 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002327 if (EndLoc)
2328 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002329
Aaron Ballman00e99962013-08-31 01:11:41 +00002330 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002331 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002332 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2333 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002334}
2335
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002336/// ParseDeclarationSpecifiers
2337/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002338/// storage-class-specifier declaration-specifiers[opt]
2339/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002340/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002341/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002342/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002343/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002344///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002345/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002346/// 'typedef'
2347/// 'extern'
2348/// 'static'
2349/// 'auto'
2350/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002351/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002352/// [C++11] 'thread_local'
2353/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002354/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002355/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002356/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002357/// [C++] 'virtual'
2358/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002359/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002360/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002361/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002362
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002363///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002364void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002365 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002366 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002367 DeclSpecContext DSContext,
2368 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002369 if (DS.getSourceRange().isInvalid()) {
2370 DS.SetRangeStart(Tok.getLocation());
2371 DS.SetRangeEnd(Tok.getLocation());
2372 }
Chad Rosierc1183952012-06-26 22:30:43 +00002373
Douglas Gregordf593fb2011-11-07 17:33:42 +00002374 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002375 bool AttrsLastTime = false;
2376 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002377 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002378 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002379 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002380 unsigned DiagID = 0;
2381
Chris Lattner4d8f8732006-11-28 05:05:08 +00002382 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002383
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002384 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002385 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002386 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002387 if (!AttrsLastTime)
2388 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002389 else {
2390 // Reject C++11 attributes that appertain to decl specifiers as
2391 // we don't support any C++11 attributes that appertain to decl
2392 // specifiers. This also conforms to what g++ 4.8 is doing.
2393 ProhibitCXX11Attributes(attrs);
2394
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002395 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002396 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002397
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002398 // If this is not a declaration specifier token, we're done reading decl
2399 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002400 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002401 return;
Mike Stump11289f42009-09-09 15:08:12 +00002402
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002403 case tok::l_square:
2404 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002405 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002406 goto DoneWithDeclSpec;
2407
2408 ProhibitAttributes(attrs);
2409 // FIXME: It would be good to recover by accepting the attributes,
2410 // but attempting to do that now would cause serious
2411 // madness in terms of diagnostics.
2412 attrs.clear();
2413 attrs.Range = SourceRange();
2414
2415 ParseCXX11Attributes(attrs);
2416 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002417 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002418
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002419 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002420 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002421 if (DS.hasTypeSpecifier()) {
2422 bool AllowNonIdentifiers
2423 = (getCurScope()->getFlags() & (Scope::ControlScope |
2424 Scope::BlockScope |
2425 Scope::TemplateParamScope |
2426 Scope::FunctionPrototypeScope |
2427 Scope::AtCatchScope)) == 0;
2428 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002429 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002430 (DSContext == DSC_class && DS.isFriendSpecified());
2431
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002432 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002433 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002434 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002435 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002436 }
2437
Douglas Gregor80039242011-02-15 20:33:25 +00002438 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2439 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2440 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002441 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002442 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002443 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002444 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002445 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002446 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002447
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002448 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002449 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002450 }
2451
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002452 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002453 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002454 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002455 if (!DS.hasTypeSpecifier())
2456 DS.SetTypeSpecError();
2457 goto DoneWithDeclSpec;
2458 }
John McCall8bc2a702010-03-01 18:20:46 +00002459 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2460 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002461 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002462
2463 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002464 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002465 goto DoneWithDeclSpec;
2466
John McCall9dab4e62009-12-12 11:40:51 +00002467 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002468 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2469 Tok.getAnnotationRange(),
2470 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002471
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002472 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002473 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002474 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002475 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002476 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002477 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002478
2479 // C++ [class.qual]p2:
2480 // In a lookup in which the constructor is an acceptable lookup
2481 // result and the nested-name-specifier nominates a class C:
2482 //
2483 // - if the name specified after the
2484 // nested-name-specifier, when looked up in C, is the
2485 // injected-class-name of C (Clause 9), or
2486 //
2487 // - if the name specified after the nested-name-specifier
2488 // is the same as the identifier or the
2489 // simple-template-id's template-name in the last
2490 // component of the nested-name-specifier,
2491 //
2492 // the name is instead considered to name the constructor of
2493 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002494 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002495 // Thus, if the template-name is actually the constructor
2496 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002497 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002498 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002499 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002500 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002501 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002502 if (isConstructorDeclarator()) {
2503 // The user meant this to be an out-of-line constructor
2504 // definition, but template arguments are not allowed
2505 // there. Just allow this as a constructor; we'll
2506 // complain about it later.
2507 goto DoneWithDeclSpec;
2508 }
2509
2510 // The user meant this to name a type, but it actually names
2511 // a constructor with some extraneous template
2512 // arguments. Complain, then parse it as a type as the user
2513 // intended.
2514 Diag(TemplateId->TemplateNameLoc,
2515 diag::err_out_of_line_template_id_names_constructor)
2516 << TemplateId->Name;
2517 }
2518
John McCall9dab4e62009-12-12 11:40:51 +00002519 DS.getTypeSpecScope() = SS;
2520 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002521 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002522 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002523 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002524 continue;
2525 }
2526
Douglas Gregorc5790df2009-09-28 07:26:33 +00002527 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002528 DS.getTypeSpecScope() = SS;
2529 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002530 if (Tok.getAnnotationValue()) {
2531 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002532 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002533 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002534 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002535 if (isInvalid)
2536 break;
John McCallba7bf592010-08-24 05:47:05 +00002537 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002538 else
2539 DS.SetTypeSpecError();
2540 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2541 ConsumeToken(); // The typename
2542 }
2543
Douglas Gregor167fa622009-03-25 15:40:00 +00002544 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002545 goto DoneWithDeclSpec;
2546
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002547 // If we're in a context where the identifier could be a class name,
2548 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002549 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002550 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002551 &SS)) {
2552 if (isConstructorDeclarator())
2553 goto DoneWithDeclSpec;
2554
2555 // As noted in C++ [class.qual]p2 (cited above), when the name
2556 // of the class is qualified in a context where it could name
2557 // a constructor, its a constructor name. However, we've
2558 // looked at the declarator, and the user probably meant this
2559 // to be a type. Complain that it isn't supposed to be treated
2560 // as a type, then proceed to parse it as a type.
2561 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2562 << Next.getIdentifierInfo();
2563 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002564
John McCallba7bf592010-08-24 05:47:05 +00002565 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2566 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002567 getCurScope(), &SS,
2568 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002569 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002570 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002571
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002572 // If the referenced identifier is not a type, then this declspec is
2573 // erroneous: We already checked about that it has no type specifier, and
2574 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002575 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002576 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002577 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002578 ParsedAttributesWithRange Attrs(AttrFactory);
2579 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2580 if (!Attrs.empty()) {
2581 AttrsLastTime = true;
2582 attrs.takeAllFrom(Attrs);
2583 }
2584 continue;
2585 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002586 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002587 }
Mike Stump11289f42009-09-09 15:08:12 +00002588
John McCall9dab4e62009-12-12 11:40:51 +00002589 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002590 ConsumeToken(); // The C++ scope.
2591
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002592 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002593 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002594 if (isInvalid)
2595 break;
Mike Stump11289f42009-09-09 15:08:12 +00002596
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002597 DS.SetRangeEnd(Tok.getLocation());
2598 ConsumeToken(); // The typename.
2599
2600 continue;
2601 }
Mike Stump11289f42009-09-09 15:08:12 +00002602
Chris Lattnere387d9e2009-01-21 19:48:37 +00002603 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002604 if (Tok.getAnnotationValue()) {
2605 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002606 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002607 DiagID, T);
2608 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002609 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002610
Chris Lattner005fc1b2010-04-05 18:18:31 +00002611 if (isInvalid)
2612 break;
2613
Chris Lattnere387d9e2009-01-21 19:48:37 +00002614 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2615 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002616
Chris Lattnere387d9e2009-01-21 19:48:37 +00002617 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2618 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002619 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002620 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002621 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002622
Chris Lattnere387d9e2009-01-21 19:48:37 +00002623 continue;
2624 }
Mike Stump11289f42009-09-09 15:08:12 +00002625
Douglas Gregor06873092011-04-28 15:48:45 +00002626 case tok::kw___is_signed:
2627 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2628 // typically treats it as a trait. If we see __is_signed as it appears
2629 // in libstdc++, e.g.,
2630 //
2631 // static const bool __is_signed;
2632 //
2633 // then treat __is_signed as an identifier rather than as a keyword.
2634 if (DS.getTypeSpecType() == TST_bool &&
2635 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2636 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2637 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2638 Tok.setKind(tok::identifier);
2639 }
2640
2641 // We're done with the declaration-specifiers.
2642 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002643
Chris Lattner16fac4f2008-07-26 01:18:38 +00002644 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002645 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002646 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002647 // In C++, check to see if this is a scope specifier like foo::bar::, if
2648 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002649 if (getLangOpts().CPlusPlus) {
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002650 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002651 if (!DS.hasTypeSpecifier())
2652 DS.SetTypeSpecError();
2653 goto DoneWithDeclSpec;
2654 }
2655 if (!Tok.is(tok::identifier))
2656 continue;
2657 }
Mike Stump11289f42009-09-09 15:08:12 +00002658
Chris Lattner16fac4f2008-07-26 01:18:38 +00002659 // This identifier can only be a typedef name if we haven't already seen
2660 // a type-specifier. Without this check we misparse:
2661 // typedef int X; struct Y { short X; }; as 'short int'.
2662 if (DS.hasTypeSpecifier())
2663 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002664
John Thompson22334602010-02-05 00:12:22 +00002665 // Check for need to substitute AltiVec keyword tokens.
2666 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2667 break;
2668
Richard Smith3092a3b2012-05-09 18:56:43 +00002669 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2670 // allow the use of a typedef name as a type specifier.
2671 if (DS.isTypeAltiVecVector())
2672 goto DoneWithDeclSpec;
2673
John McCallba7bf592010-08-24 05:47:05 +00002674 ParsedType TypeRep =
2675 Actions.getTypeName(*Tok.getIdentifierInfo(),
2676 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002677
Chris Lattner6cc055a2009-04-12 20:42:31 +00002678 // If this is not a typedef name, don't parse it as part of the declspec,
2679 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002680 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002681 ParsedAttributesWithRange Attrs(AttrFactory);
2682 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2683 if (!Attrs.empty()) {
2684 AttrsLastTime = true;
2685 attrs.takeAllFrom(Attrs);
2686 }
2687 continue;
2688 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002689 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002690 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002691
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002692 // If we're in a context where the identifier could be a class name,
2693 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002694 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002695 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002696 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002697 goto DoneWithDeclSpec;
2698
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002699 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002700 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002701 if (isInvalid)
2702 break;
Mike Stump11289f42009-09-09 15:08:12 +00002703
Chris Lattner16fac4f2008-07-26 01:18:38 +00002704 DS.SetRangeEnd(Tok.getLocation());
2705 ConsumeToken(); // The identifier
2706
2707 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2708 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002709 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002710 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002711 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002712
Steve Naroffcd5e7822008-09-22 10:28:57 +00002713 // Need to support trailing type qualifiers (e.g. "id<p> const").
2714 // If a type specifier follows, it will be diagnosed elsewhere.
2715 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002716 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002717
2718 // type-name
2719 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002720 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002721 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002722 // This template-id does not refer to a type name, so we're
2723 // done with the type-specifiers.
2724 goto DoneWithDeclSpec;
2725 }
2726
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002727 // If we're in a context where the template-id could be a
2728 // constructor name or specialization, check whether this is a
2729 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002730 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002731 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002732 isConstructorDeclarator())
2733 goto DoneWithDeclSpec;
2734
Douglas Gregor7f741122009-02-25 19:37:18 +00002735 // Turn the template-id annotation token into a type annotation
2736 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002737 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002738 continue;
2739 }
2740
Chris Lattnere37e2332006-08-15 04:50:22 +00002741 // GNU attributes support.
2742 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002743 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002744 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002745
2746 // Microsoft declspec support.
2747 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002748 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002749 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002750
Steve Naroff44ac7772008-12-25 14:16:32 +00002751 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002752 case tok::kw___forceinline: {
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002753 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002754 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002755 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002756 // FIXME: This does not work correctly if it is set to be a declspec
2757 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman00e99962013-08-31 01:11:41 +00002758 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2759 AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002760 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002761 }
Eli Friedman53339e02009-06-08 23:27:34 +00002762
Aaron Ballman317a77f2013-05-22 23:25:32 +00002763 case tok::kw___sptr:
2764 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002765 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002766 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002767 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002768 case tok::kw___cdecl:
2769 case tok::kw___stdcall:
2770 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002771 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002772 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002773 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002774 continue;
2775
Dawn Perchik335e16b2010-09-03 01:29:35 +00002776 // Borland single token adornments.
2777 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002778 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002779 continue;
2780
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002781 // OpenCL single token adornments.
2782 case tok::kw___kernel:
2783 ParseOpenCLAttributes(DS.getAttributes());
2784 continue;
2785
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002786 // storage-class-specifier
2787 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002788 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2789 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002790 break;
2791 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002792 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002793 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002794 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2795 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002796 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002797 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002798 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2799 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002800 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002801 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002802 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002803 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002804 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2805 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002806 break;
2807 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002808 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002809 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002810 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2811 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002812 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002813 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002814 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002815 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002816 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2817 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002818 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002819 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2820 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002821 break;
2822 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002823 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2824 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002825 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002826 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002827 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2828 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002829 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002830 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002831 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2832 PrevSpec, DiagID);
2833 break;
2834 case tok::kw_thread_local:
2835 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2836 PrevSpec, DiagID);
2837 break;
2838 case tok::kw__Thread_local:
2839 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2840 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002841 break;
Mike Stump11289f42009-09-09 15:08:12 +00002842
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002843 // function-specifier
2844 case tok::kw_inline:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002845 isInvalid = DS.setFunctionSpecInline(Loc);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002846 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002847 case tok::kw_virtual:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002848 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002849 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002850 case tok::kw_explicit:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002851 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002852 break;
Richard Smith0015f092013-01-17 22:16:11 +00002853 case tok::kw__Noreturn:
2854 if (!getLangOpts().C11)
2855 Diag(Loc, diag::ext_c11_noreturn);
2856 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2857 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002858
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002859 // alignment-specifier
2860 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002861 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002862 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002863 ParseAlignmentSpecifier(DS.getAttributes());
2864 continue;
2865
Anders Carlssoncd8db412009-05-06 04:46:28 +00002866 // friend
2867 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002868 if (DSContext == DSC_class)
2869 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2870 else {
2871 PrevSpec = ""; // not actually used by the diagnostic
2872 DiagID = diag::err_friend_invalid_in_context;
2873 isInvalid = true;
2874 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002875 break;
Mike Stump11289f42009-09-09 15:08:12 +00002876
Douglas Gregor26701a42011-09-09 02:06:17 +00002877 // Modules
2878 case tok::kw___module_private__:
2879 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2880 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002881
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002882 // constexpr
2883 case tok::kw_constexpr:
2884 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2885 break;
2886
Chris Lattnere387d9e2009-01-21 19:48:37 +00002887 // type-specifier
2888 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002889 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2890 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002891 break;
2892 case tok::kw_long:
2893 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002894 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2895 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002896 else
John McCall49bfce42009-08-03 20:12:06 +00002897 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2898 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002899 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002900 case tok::kw___int64:
2901 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2902 DiagID);
2903 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002904 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002905 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2906 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002907 break;
2908 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002909 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2910 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002911 break;
2912 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002913 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2914 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002915 break;
2916 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002917 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2918 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002919 break;
2920 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002921 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2922 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002923 break;
2924 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002925 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2926 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002927 break;
2928 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002929 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2930 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002931 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002932 case tok::kw___int128:
2933 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2934 DiagID);
2935 break;
2936 case tok::kw_half:
2937 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2938 DiagID);
2939 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002940 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002941 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2942 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002943 break;
2944 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002945 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2946 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002947 break;
2948 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002949 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2950 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002951 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002952 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002953 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2954 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002955 break;
2956 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002957 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2958 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002959 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002960 case tok::kw_bool:
2961 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002962 if (Tok.is(tok::kw_bool) &&
2963 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2964 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2965 PrevSpec = ""; // Not used by the diagnostic.
2966 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002967 // For better error recovery.
2968 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002969 isInvalid = true;
2970 } else {
2971 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2972 DiagID);
2973 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002974 break;
2975 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002976 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2977 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002978 break;
2979 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002980 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2981 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002982 break;
2983 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002984 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2985 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002986 break;
John Thompson22334602010-02-05 00:12:22 +00002987 case tok::kw___vector:
2988 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2989 break;
2990 case tok::kw___pixel:
2991 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2992 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00002993 case tok::kw_image1d_t:
2994 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2995 PrevSpec, DiagID);
2996 break;
2997 case tok::kw_image1d_array_t:
2998 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2999 PrevSpec, DiagID);
3000 break;
3001 case tok::kw_image1d_buffer_t:
3002 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
3003 PrevSpec, DiagID);
3004 break;
3005 case tok::kw_image2d_t:
3006 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
3007 PrevSpec, DiagID);
3008 break;
3009 case tok::kw_image2d_array_t:
3010 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
3011 PrevSpec, DiagID);
3012 break;
3013 case tok::kw_image3d_t:
3014 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
3015 PrevSpec, DiagID);
3016 break;
Guy Benyei61054192013-02-07 10:55:47 +00003017 case tok::kw_sampler_t:
3018 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
3019 PrevSpec, DiagID);
3020 break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003021 case tok::kw_event_t:
3022 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
3023 PrevSpec, DiagID);
3024 break;
John McCall39439732011-04-09 22:50:59 +00003025 case tok::kw___unknown_anytype:
3026 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3027 PrevSpec, DiagID);
3028 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003029
3030 // class-specifier:
3031 case tok::kw_class:
3032 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003033 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003034 case tok::kw_union: {
3035 tok::TokenKind Kind = Tok.getKind();
3036 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003037
3038 // These are attributes following class specifiers.
3039 // To produce better diagnostic, we parse them when
3040 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003041 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003042 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003043 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003044
3045 // If there are attributes following class specifier,
3046 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003047 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003048 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003049 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003050 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003051 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003052 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003053
3054 // enum-specifier:
3055 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003056 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003057 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003058 continue;
3059
3060 // cv-qualifier:
3061 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003062 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003063 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003064 break;
3065 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003066 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003067 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003068 break;
3069 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003070 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003071 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003072 break;
3073
Douglas Gregor333489b2009-03-27 23:10:48 +00003074 // C++ typename-specifier:
3075 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003076 if (TryAnnotateTypeOrScopeToken()) {
3077 DS.SetTypeSpecError();
3078 goto DoneWithDeclSpec;
3079 }
3080 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003081 continue;
3082 break;
3083
Chris Lattnere387d9e2009-01-21 19:48:37 +00003084 // GNU typeof support.
3085 case tok::kw_typeof:
3086 ParseTypeofSpecifier(DS);
3087 continue;
3088
David Blaikie15a430a2011-12-04 05:04:18 +00003089 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003090 ParseDecltypeSpecifier(DS);
3091 continue;
3092
Alexis Hunt4a257072011-05-19 05:37:45 +00003093 case tok::kw___underlying_type:
3094 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003095 continue;
3096
3097 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003098 // C11 6.7.2.4/4:
3099 // If the _Atomic keyword is immediately followed by a left parenthesis,
3100 // it is interpreted as a type specifier (with a type name), not as a
3101 // type qualifier.
3102 if (NextToken().is(tok::l_paren)) {
3103 ParseAtomicSpecifier(DS);
3104 continue;
3105 }
3106 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3107 getLangOpts());
3108 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003109
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003110 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003111 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003112 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003113 goto DoneWithDeclSpec;
3114 case tok::kw___private:
3115 case tok::kw___global:
3116 case tok::kw___local:
3117 case tok::kw___constant:
3118 case tok::kw___read_only:
3119 case tok::kw___write_only:
3120 case tok::kw___read_write:
3121 ParseOpenCLQualifiers(DS);
3122 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003123
Steve Naroffcfdf6162008-06-05 00:02:44 +00003124 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003125 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003126 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3127 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003128 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003129 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003130
Douglas Gregor3a001f42010-11-19 17:10:50 +00003131 if (!ParseObjCProtocolQualifiers(DS))
3132 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3133 << FixItHint::CreateInsertion(Loc, "id")
3134 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003135
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003136 // Need to support trailing type qualifiers (e.g. "id<p> const").
3137 // If a type specifier follows, it will be diagnosed elsewhere.
3138 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003139 }
John McCall49bfce42009-08-03 20:12:06 +00003140 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003141 if (isInvalid) {
3142 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003143 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003144
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003145 if (DiagID == diag::ext_duplicate_declspec)
3146 Diag(Tok, DiagID)
3147 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3148 else
3149 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003150 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003151
Chris Lattner2e232092008-03-13 06:29:04 +00003152 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003153 if (DiagID != diag::err_bool_redeclaration)
3154 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003155
3156 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003157 }
3158}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003159
Chris Lattner70ae4912007-10-29 04:42:53 +00003160/// ParseStructDeclaration - Parse a struct declaration without the terminating
3161/// semicolon.
3162///
Chris Lattner90a26b02007-01-23 04:38:16 +00003163/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003164/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003165/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003166/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003167/// struct-declarator-list:
3168/// struct-declarator
3169/// struct-declarator-list ',' struct-declarator
3170/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3171/// struct-declarator:
3172/// declarator
3173/// [GNU] declarator attributes[opt]
3174/// declarator[opt] ':' constant-expression
3175/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3176///
Chris Lattnera12405b2008-04-10 06:46:29 +00003177void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003178ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003179
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003180 if (Tok.is(tok::kw___extension__)) {
3181 // __extension__ silences extension warnings in the subexpression.
3182 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003183 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003184 return ParseStructDeclaration(DS, Fields);
3185 }
Mike Stump11289f42009-09-09 15:08:12 +00003186
Steve Naroff97170802007-08-20 22:28:22 +00003187 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003188 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003189
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003190 // If there are no declarators, this is a free-standing declaration
3191 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003192 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003193 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3194 DS);
3195 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003196 return;
3197 }
3198
3199 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003200 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003201 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003202 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003203 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003204 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003205
Bill Wendling44426052012-12-20 19:22:21 +00003206 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003207 if (!FirstDeclarator)
3208 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003209
Steve Naroff97170802007-08-20 22:28:22 +00003210 /// struct-declarator: declarator
3211 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003212 if (Tok.isNot(tok::colon)) {
3213 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3214 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003215 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003216 }
Mike Stump11289f42009-09-09 15:08:12 +00003217
Chris Lattner76c72282007-10-09 17:33:22 +00003218 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00003219 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00003220 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003221 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00003222 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00003223 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003224 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003225 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003226
Steve Naroff97170802007-08-20 22:28:22 +00003227 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003228 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003229
John McCallcfefb6d2009-11-03 02:38:08 +00003230 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003231 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003232
Steve Naroff97170802007-08-20 22:28:22 +00003233 // If we don't have a comma, it is either the end of the list (a ';')
3234 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00003235 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00003236 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003237
Steve Naroff97170802007-08-20 22:28:22 +00003238 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00003239 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003240
John McCallcfefb6d2009-11-03 02:38:08 +00003241 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003242 }
Steve Naroff97170802007-08-20 22:28:22 +00003243}
3244
3245/// ParseStructUnionBody
3246/// struct-contents:
3247/// struct-declaration-list
3248/// [EXT] empty
3249/// [GNU] "struct-declaration-list" without terminatoring ';'
3250/// struct-declaration-list:
3251/// struct-declaration
3252/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003253/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003254///
Chris Lattner1300fb92007-01-23 23:42:53 +00003255void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003256 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003257 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3258 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003259 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003260
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003261 BalancedDelimiterTracker T(*this, tok::l_brace);
3262 if (T.consumeOpen())
3263 return;
Mike Stump11289f42009-09-09 15:08:12 +00003264
Douglas Gregor658b9552009-01-09 22:42:13 +00003265 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003266 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003267
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003268 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003269
Chris Lattner7b9ace62007-01-23 20:11:08 +00003270 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00003271 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003272 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003273
Chris Lattner736ed5d2007-06-09 05:59:07 +00003274 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003275 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003276 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003277 continue;
3278 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003279
Andy Gibbsc804e082013-04-03 09:46:04 +00003280 // Parse _Static_assert declaration.
3281 if (Tok.is(tok::kw__Static_assert)) {
3282 SourceLocation DeclEnd;
3283 ParseStaticAssertDeclaration(DeclEnd);
3284 continue;
3285 }
3286
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003287 if (Tok.is(tok::annot_pragma_pack)) {
3288 HandlePragmaPack();
3289 continue;
3290 }
3291
3292 if (Tok.is(tok::annot_pragma_align)) {
3293 HandlePragmaAlign();
3294 continue;
3295 }
3296
John McCallcfefb6d2009-11-03 02:38:08 +00003297 if (!Tok.is(tok::at)) {
3298 struct CFieldCallback : FieldCallback {
3299 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003300 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003301 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003302
John McCall48871652010-08-21 09:40:31 +00003303 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003304 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003305 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3306
Eli Friedman934dbbf2012-08-08 23:53:27 +00003307 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003308 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003309 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003310 FD.D.getDeclSpec().getSourceRange().getBegin(),
3311 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003312 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003313 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003314 }
John McCallcfefb6d2009-11-03 02:38:08 +00003315 } Callback(*this, TagDecl, FieldDecls);
3316
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003317 // Parse all the comma separated declarators.
3318 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003319 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003320 } else { // Handle @defs
3321 ConsumeToken();
3322 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3323 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00003324 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003325 continue;
3326 }
3327 ConsumeToken();
3328 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3329 if (!Tok.is(tok::identifier)) {
3330 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00003331 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003332 continue;
3333 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003334 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003335 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003336 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003337 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3338 ConsumeToken();
3339 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003340 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003341
Chris Lattner76c72282007-10-09 17:33:22 +00003342 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003343 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003344 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003345 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003346 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003347 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003348 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3349 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00003350 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00003351 // If we stopped at a ';', eat it.
3352 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003353 }
3354 }
Mike Stump11289f42009-09-09 15:08:12 +00003355
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003356 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003357
John McCall084e83d2011-03-24 11:26:52 +00003358 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003359 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003360 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003361
Douglas Gregor0be31a22010-07-02 17:43:08 +00003362 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003363 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003364 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003365 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003366 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003367 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3368 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003369}
3370
Chris Lattner3b561a32006-08-13 00:12:11 +00003371/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003372/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003373/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003374///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003375/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3376/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003377/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3378/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003379/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003380/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003381///
Richard Smith7d137e32012-03-23 03:33:32 +00003382/// [C++11] enum-head '{' enumerator-list[opt] '}'
3383/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003384///
Richard Smith7d137e32012-03-23 03:33:32 +00003385/// enum-head: [C++11]
3386/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3387/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3388/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003389///
Richard Smith7d137e32012-03-23 03:33:32 +00003390/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003391/// 'enum'
3392/// 'enum' 'class'
3393/// 'enum' 'struct'
3394///
Richard Smith7d137e32012-03-23 03:33:32 +00003395/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003396/// ':' type-specifier-seq
3397///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003398/// [C++] elaborated-type-specifier:
3399/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3400///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003401void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003402 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003403 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003404 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003405 if (Tok.is(tok::code_completion)) {
3406 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003407 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003408 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003409 }
John McCallcb432fa2011-07-06 05:58:41 +00003410
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003411 // If attributes exist after tag, parse them.
3412 ParsedAttributesWithRange attrs(AttrFactory);
3413 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003414 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003415
3416 // If declspecs exist after tag, parse them.
3417 while (Tok.is(tok::kw___declspec))
3418 ParseMicrosoftDeclSpec(attrs);
3419
Richard Smith0f8ee222012-01-10 01:33:14 +00003420 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003421 bool IsScopedUsingClassTag = false;
3422
John McCallbeae29a2012-06-23 22:30:04 +00003423 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003424 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3425 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3426 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003427 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003428 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003429
Bill Wendling44426052012-12-20 19:22:21 +00003430 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003431 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003432 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003433
3434 // They are allowed afterwards, though.
3435 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003436 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003437 while (Tok.is(tok::kw___declspec))
3438 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003439 }
Richard Smith7d137e32012-03-23 03:33:32 +00003440
John McCall6347b682012-05-07 06:16:58 +00003441 // C++11 [temp.explicit]p12:
3442 // The usual access controls do not apply to names used to specify
3443 // explicit instantiations.
3444 // We extend this to also cover explicit specializations. Note that
3445 // we don't suppress if this turns out to be an elaborated type
3446 // specifier.
3447 bool shouldDelayDiagsInTag =
3448 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3449 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3450 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003451
Richard Smithbfdb1082012-03-12 08:56:40 +00003452 // Enum definitions should not be parsed in a trailing-return-type.
3453 bool AllowDeclaration = DSC != DSC_trailing;
3454
3455 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003456 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003457 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003458
Abramo Bagnarad7548482010-05-19 21:37:53 +00003459 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003460 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003461 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3462 // if a fixed underlying type is allowed.
3463 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003464
3465 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003466 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003467 return;
3468
3469 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003470 Diag(Tok, diag::err_expected_ident);
3471 if (Tok.isNot(tok::l_brace)) {
3472 // Has no name and is not a definition.
3473 // Skip the rest of this declarator, up until the comma or semicolon.
3474 SkipUntil(tok::comma, true);
3475 return;
3476 }
3477 }
3478 }
Mike Stump11289f42009-09-09 15:08:12 +00003479
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003480 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003481 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003482 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003483 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003484
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003485 // Skip the rest of this declarator, up until the comma or semicolon.
3486 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003487 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003488 }
Mike Stump11289f42009-09-09 15:08:12 +00003489
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003490 // If an identifier is present, consume and remember it.
3491 IdentifierInfo *Name = 0;
3492 SourceLocation NameLoc;
3493 if (Tok.is(tok::identifier)) {
3494 Name = Tok.getIdentifierInfo();
3495 NameLoc = ConsumeToken();
3496 }
Mike Stump11289f42009-09-09 15:08:12 +00003497
Richard Smith0f8ee222012-01-10 01:33:14 +00003498 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003499 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3500 // declaration of a scoped enumeration.
3501 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003502 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003503 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003504 }
3505
John McCall6347b682012-05-07 06:16:58 +00003506 // Okay, end the suppression area. We'll decide whether to emit the
3507 // diagnostics in a second.
3508 if (shouldDelayDiagsInTag)
3509 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003510
Douglas Gregor0bf31402010-10-08 23:50:27 +00003511 TypeResult BaseType;
3512
Douglas Gregord1f69f62010-12-01 17:42:47 +00003513 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003514 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003515 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003516 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003517 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003518 // If we're in class scope, this can either be an enum declaration with
3519 // an underlying type, or a declaration of a bitfield member. We try to
3520 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003521 // (integer literal, sizeof); if it's still ambiguous, we then consider
3522 // anything that's a simple-type-specifier followed by '(' as an
3523 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003524 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003525 EnterExpressionEvaluationContext Unevaluated(Actions,
3526 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003527 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003528 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003529 // bit-field. This is the common case.
3530 if (TPR == TPResult::True())
3531 PossibleBitfield = true;
3532 // If the next token starts a type-specifier-seq, it may be either a
3533 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003534 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003535 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003536 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003537 GetLookAheadToken(2).getKind() == tok::semi) {
3538 // Consume the ':'.
3539 ConsumeToken();
3540 } else {
3541 // We have the start of a type-specifier-seq, so we have to perform
3542 // tentative parsing to determine whether we have an expression or a
3543 // type.
3544 TentativeParsingAction TPA(*this);
3545
3546 // Consume the ':'.
3547 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003548
3549 // If we see a type specifier followed by an open-brace, we have an
3550 // ambiguity between an underlying type and a C++11 braced
3551 // function-style cast. Resolve this by always treating it as an
3552 // underlying type.
3553 // FIXME: The standard is not entirely clear on how to disambiguate in
3554 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003555 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003556 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003557 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003558 // We'll parse this as a bitfield later.
3559 PossibleBitfield = true;
3560 TPA.Revert();
3561 } else {
3562 // We have a type-specifier-seq.
3563 TPA.Commit();
3564 }
3565 }
3566 } else {
3567 // Consume the ':'.
3568 ConsumeToken();
3569 }
3570
3571 if (!PossibleBitfield) {
3572 SourceRange Range;
3573 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003574
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003575 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003576 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003577 } else if (!getLangOpts().ObjC2) {
3578 if (getLangOpts().CPlusPlus)
3579 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3580 else
3581 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3582 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003583 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003584 }
3585
Richard Smith0f8ee222012-01-10 01:33:14 +00003586 // There are four options here. If we have 'friend enum foo;' then this is a
3587 // friend declaration, and cannot have an accompanying definition. If we have
3588 // 'enum foo;', then this is a forward declaration. If we have
3589 // 'enum foo {...' then this is a definition. Otherwise we have something
3590 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003591 //
3592 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3593 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3594 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3595 //
John McCallfaf5fb42010-08-26 23:41:50 +00003596 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003597 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003598 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003599 } else if (Tok.is(tok::l_brace)) {
3600 if (DS.isFriendSpecified()) {
3601 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3602 << SourceRange(DS.getFriendSpecLoc());
3603 ConsumeBrace();
3604 SkipUntil(tok::r_brace);
3605 TUK = Sema::TUK_Friend;
3606 } else {
3607 TUK = Sema::TUK_Definition;
3608 }
Richard Smith369b9f92012-06-25 21:37:02 +00003609 } else if (DSC != DSC_type_specifier &&
3610 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003611 (Tok.isAtStartOfLine() &&
3612 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003613 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3614 if (Tok.isNot(tok::semi)) {
3615 // A semicolon was missing after this declaration. Diagnose and recover.
3616 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3617 "enum");
3618 PP.EnterToken(Tok);
3619 Tok.setKind(tok::semi);
3620 }
John McCall6347b682012-05-07 06:16:58 +00003621 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003622 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003623 }
3624
3625 // If this is an elaborated type specifier, and we delayed
3626 // diagnostics before, just merge them into the current pool.
3627 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3628 diagsFromTag.redelay();
3629 }
Richard Smith7d137e32012-03-23 03:33:32 +00003630
3631 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003632 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003633 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003634 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003635 // Skip the rest of this declarator, up until the comma or semicolon.
3636 Diag(Tok, diag::err_enum_template);
3637 SkipUntil(tok::comma, true);
3638 return;
3639 }
3640
3641 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3642 // Enumerations can't be explicitly instantiated.
3643 DS.SetTypeSpecError();
3644 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3645 return;
3646 }
3647
3648 assert(TemplateInfo.TemplateParams && "no template parameters");
3649 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3650 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003651 }
Chad Rosierc1183952012-06-26 22:30:43 +00003652
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003653 if (TUK == Sema::TUK_Reference)
3654 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003655
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003656 if (!Name && TUK != Sema::TUK_Definition) {
3657 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003658
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003659 // Skip the rest of this declarator, up until the comma or semicolon.
3660 SkipUntil(tok::comma, true);
3661 return;
3662 }
Richard Smith7d137e32012-03-23 03:33:32 +00003663
Douglas Gregord6ab8742009-05-28 23:31:59 +00003664 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003665 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003666 const char *PrevSpec = 0;
3667 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003668 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003669 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003670 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003671 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003672 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003673
Douglas Gregorba41d012010-04-24 16:38:41 +00003674 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003675 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003676 // dependent tag.
3677 if (!Name) {
3678 DS.SetTypeSpecError();
3679 Diag(Tok, diag::err_expected_type_name_after_typename);
3680 return;
3681 }
Chad Rosierc1183952012-06-26 22:30:43 +00003682
Douglas Gregor0be31a22010-07-02 17:43:08 +00003683 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003684 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003685 NameLoc);
3686 if (Type.isInvalid()) {
3687 DS.SetTypeSpecError();
3688 return;
3689 }
Chad Rosierc1183952012-06-26 22:30:43 +00003690
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003691 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3692 NameLoc.isValid() ? NameLoc : StartLoc,
3693 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003694 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003695
Douglas Gregorba41d012010-04-24 16:38:41 +00003696 return;
3697 }
Mike Stump11289f42009-09-09 15:08:12 +00003698
John McCall48871652010-08-21 09:40:31 +00003699 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003700 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003701 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003702 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003703 ConsumeBrace();
3704 SkipUntil(tok::r_brace);
3705 }
Chad Rosierc1183952012-06-26 22:30:43 +00003706
Douglas Gregorba41d012010-04-24 16:38:41 +00003707 DS.SetTypeSpecError();
3708 return;
3709 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003710
Richard Smith369b9f92012-06-25 21:37:02 +00003711 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003712 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003713
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003714 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3715 NameLoc.isValid() ? NameLoc : StartLoc,
3716 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003717 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003718}
3719
Chris Lattnerc1915e22007-01-25 07:29:02 +00003720/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3721/// enumerator-list:
3722/// enumerator
3723/// enumerator-list ',' enumerator
3724/// enumerator:
3725/// enumeration-constant
3726/// enumeration-constant '=' constant-expression
3727/// enumeration-constant:
3728/// identifier
3729///
John McCall48871652010-08-21 09:40:31 +00003730void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003731 // Enter the scope of the enum body and start the definition.
3732 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003733 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003734
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003735 BalancedDelimiterTracker T(*this, tok::l_brace);
3736 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003737
Chris Lattner37256fb2007-08-27 17:24:30 +00003738 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003739 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003740 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003741
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003742 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003743
John McCall48871652010-08-21 09:40:31 +00003744 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003745
Chris Lattnerc1915e22007-01-25 07:29:02 +00003746 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003747 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003748 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3749 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003750
John McCall811a0f52010-10-22 23:36:17 +00003751 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003752 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003753 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003754 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003755 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003756
Chris Lattnerc1915e22007-01-25 07:29:02 +00003757 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003758 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003759 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003760
Chris Lattner76c72282007-10-09 17:33:22 +00003761 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003762 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003763 AssignedVal = ParseConstantExpression();
3764 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003765 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003766 }
Mike Stump11289f42009-09-09 15:08:12 +00003767
Chris Lattnerc1915e22007-01-25 07:29:02 +00003768 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003769 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3770 LastEnumConstDecl,
3771 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003772 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003773 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003774 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003775
Chris Lattner4ef40012007-06-11 01:28:17 +00003776 EnumConstantDecls.push_back(EnumConstDecl);
3777 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003778
Douglas Gregorce66d022010-09-07 14:51:08 +00003779 if (Tok.is(tok::identifier)) {
3780 // We're missing a comma between enumerators.
3781 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003782 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003783 << FixItHint::CreateInsertion(Loc, ", ");
3784 continue;
3785 }
Chad Rosierc1183952012-06-26 22:30:43 +00003786
Chris Lattner76c72282007-10-09 17:33:22 +00003787 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003788 break;
3789 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003790
Richard Smith5d164bc2011-10-15 05:09:34 +00003791 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003792 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003793 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3794 diag::ext_enumerator_list_comma_cxx :
3795 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003796 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003797 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003798 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3799 << FixItHint::CreateRemoval(CommaLoc);
3800 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003801 }
Mike Stump11289f42009-09-09 15:08:12 +00003802
Chris Lattnerc1915e22007-01-25 07:29:02 +00003803 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003804 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003805
Chris Lattnerc1915e22007-01-25 07:29:02 +00003806 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003807 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003808 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003809
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003810 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003811 EnumDecl, EnumConstantDecls,
3812 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003813 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003814
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003815 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003816 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3817 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003818
3819 // The next token must be valid after an enum definition. If not, a ';'
3820 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003821 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3822 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003823 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3824 // Push this token back into the preprocessor and change our current token
3825 // to ';' so that the rest of the code recovers as though there were an
3826 // ';' after the definition.
3827 PP.EnterToken(Tok);
3828 Tok.setKind(tok::semi);
3829 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003830}
Chris Lattner3b561a32006-08-13 00:12:11 +00003831
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003832/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003833/// start of a type-qualifier-list.
3834bool Parser::isTypeQualifier() const {
3835 switch (Tok.getKind()) {
3836 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003837
3838 // type-qualifier only in OpenCL
3839 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003840 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003841
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003842 // type-qualifier
3843 case tok::kw_const:
3844 case tok::kw_volatile:
3845 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003846 case tok::kw___private:
3847 case tok::kw___local:
3848 case tok::kw___global:
3849 case tok::kw___constant:
3850 case tok::kw___read_only:
3851 case tok::kw___read_write:
3852 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003853 return true;
3854 }
3855}
3856
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003857/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3858/// is definitely a type-specifier. Return false if it isn't part of a type
3859/// specifier or if we're not sure.
3860bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3861 switch (Tok.getKind()) {
3862 default: return false;
3863 // type-specifiers
3864 case tok::kw_short:
3865 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003866 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003867 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003868 case tok::kw_signed:
3869 case tok::kw_unsigned:
3870 case tok::kw__Complex:
3871 case tok::kw__Imaginary:
3872 case tok::kw_void:
3873 case tok::kw_char:
3874 case tok::kw_wchar_t:
3875 case tok::kw_char16_t:
3876 case tok::kw_char32_t:
3877 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003878 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003879 case tok::kw_float:
3880 case tok::kw_double:
3881 case tok::kw_bool:
3882 case tok::kw__Bool:
3883 case tok::kw__Decimal32:
3884 case tok::kw__Decimal64:
3885 case tok::kw__Decimal128:
3886 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003887
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003888 // OpenCL specific types:
3889 case tok::kw_image1d_t:
3890 case tok::kw_image1d_array_t:
3891 case tok::kw_image1d_buffer_t:
3892 case tok::kw_image2d_t:
3893 case tok::kw_image2d_array_t:
3894 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003895 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003896 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003897
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003898 // struct-or-union-specifier (C99) or class-specifier (C++)
3899 case tok::kw_class:
3900 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003901 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003902 case tok::kw_union:
3903 // enum-specifier
3904 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003905
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003906 // typedef-name
3907 case tok::annot_typename:
3908 return true;
3909 }
3910}
3911
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003912/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003913/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003914bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003915 switch (Tok.getKind()) {
3916 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003917
Chris Lattner020bab92009-01-04 23:41:41 +00003918 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003919 if (TryAltiVecVectorToken())
3920 return true;
3921 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003922 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003923 // Annotate typenames and C++ scope specifiers. If we get one, just
3924 // recurse to handle whatever we get.
3925 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003926 return true;
3927 if (Tok.is(tok::identifier))
3928 return false;
3929 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003930
Chris Lattner020bab92009-01-04 23:41:41 +00003931 case tok::coloncolon: // ::foo::bar
3932 if (NextToken().is(tok::kw_new) || // ::new
3933 NextToken().is(tok::kw_delete)) // ::delete
3934 return false;
3935
Chris Lattner020bab92009-01-04 23:41:41 +00003936 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003937 return true;
3938 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003939
Chris Lattnere37e2332006-08-15 04:50:22 +00003940 // GNU attributes support.
3941 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003942 // GNU typeof support.
3943 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003944
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003945 // type-specifiers
3946 case tok::kw_short:
3947 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003948 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003949 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003950 case tok::kw_signed:
3951 case tok::kw_unsigned:
3952 case tok::kw__Complex:
3953 case tok::kw__Imaginary:
3954 case tok::kw_void:
3955 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003956 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003957 case tok::kw_char16_t:
3958 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003959 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003960 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003961 case tok::kw_float:
3962 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003963 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003964 case tok::kw__Bool:
3965 case tok::kw__Decimal32:
3966 case tok::kw__Decimal64:
3967 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003968 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003969
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003970 // OpenCL specific types:
3971 case tok::kw_image1d_t:
3972 case tok::kw_image1d_array_t:
3973 case tok::kw_image1d_buffer_t:
3974 case tok::kw_image2d_t:
3975 case tok::kw_image2d_array_t:
3976 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003977 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003978 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003979
Chris Lattner861a2262008-04-13 18:59:07 +00003980 // struct-or-union-specifier (C99) or class-specifier (C++)
3981 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003982 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003983 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003984 case tok::kw_union:
3985 // enum-specifier
3986 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003987
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003988 // type-qualifier
3989 case tok::kw_const:
3990 case tok::kw_volatile:
3991 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003992
John McCallea0a39e2012-11-14 00:49:39 +00003993 // Debugger support.
3994 case tok::kw___unknown_anytype:
3995
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003996 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003997 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003998 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003999
Chris Lattner409bf7d2008-10-20 00:25:30 +00004000 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4001 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004002 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004003
Steve Naroff44ac7772008-12-25 14:16:32 +00004004 case tok::kw___cdecl:
4005 case tok::kw___stdcall:
4006 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004007 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004008 case tok::kw___w64:
4009 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004010 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004011 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004012 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004013
4014 case tok::kw___private:
4015 case tok::kw___local:
4016 case tok::kw___global:
4017 case tok::kw___constant:
4018 case tok::kw___read_only:
4019 case tok::kw___read_write:
4020 case tok::kw___write_only:
4021
Eli Friedman53339e02009-06-08 23:27:34 +00004022 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004023
4024 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004025 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00004026
Richard Smith8e1ac332013-03-28 01:55:44 +00004027 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004028 case tok::kw__Atomic:
4029 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004030 }
4031}
4032
Chris Lattneracd58a32006-08-06 17:24:14 +00004033/// isDeclarationSpecifier() - Return true if the current token is part of a
4034/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004035///
4036/// \param DisambiguatingWithExpression True to indicate that the purpose of
4037/// this check is to disambiguate between an expression and a declaration.
4038bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004039 switch (Tok.getKind()) {
4040 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004041
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004042 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004043 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004044
Chris Lattner020bab92009-01-04 23:41:41 +00004045 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004046 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004047 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004048 return false;
John Thompson22334602010-02-05 00:12:22 +00004049 if (TryAltiVecVectorToken())
4050 return true;
4051 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004052 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004053 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004054 // Annotate typenames and C++ scope specifiers. If we get one, just
4055 // recurse to handle whatever we get.
4056 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004057 return true;
4058 if (Tok.is(tok::identifier))
4059 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004060
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004061 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004062 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004063 // expression is permitted, then this is probably a class message send
4064 // missing the initial '['. In this case, we won't consider this to be
4065 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004066 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004067 isStartOfObjCClassMessageMissingOpenBracket())
4068 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004069
John McCall1f476a12010-02-26 08:45:28 +00004070 return isDeclarationSpecifier();
4071
Chris Lattner020bab92009-01-04 23:41:41 +00004072 case tok::coloncolon: // ::foo::bar
4073 if (NextToken().is(tok::kw_new) || // ::new
4074 NextToken().is(tok::kw_delete)) // ::delete
4075 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004076
Chris Lattner020bab92009-01-04 23:41:41 +00004077 // Annotate typenames and C++ scope specifiers. If we get one, just
4078 // recurse to handle whatever we get.
4079 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004080 return true;
4081 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004082
Chris Lattneracd58a32006-08-06 17:24:14 +00004083 // storage-class-specifier
4084 case tok::kw_typedef:
4085 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004086 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004087 case tok::kw_static:
4088 case tok::kw_auto:
4089 case tok::kw_register:
4090 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004091 case tok::kw_thread_local:
4092 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004093
Douglas Gregor26701a42011-09-09 02:06:17 +00004094 // Modules
4095 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004096
John McCallea0a39e2012-11-14 00:49:39 +00004097 // Debugger support
4098 case tok::kw___unknown_anytype:
4099
Chris Lattneracd58a32006-08-06 17:24:14 +00004100 // type-specifiers
4101 case tok::kw_short:
4102 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004103 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004104 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004105 case tok::kw_signed:
4106 case tok::kw_unsigned:
4107 case tok::kw__Complex:
4108 case tok::kw__Imaginary:
4109 case tok::kw_void:
4110 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004111 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004112 case tok::kw_char16_t:
4113 case tok::kw_char32_t:
4114
Chris Lattneracd58a32006-08-06 17:24:14 +00004115 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004116 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004117 case tok::kw_float:
4118 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004119 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004120 case tok::kw__Bool:
4121 case tok::kw__Decimal32:
4122 case tok::kw__Decimal64:
4123 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004124 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004125
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004126 // OpenCL specific types:
4127 case tok::kw_image1d_t:
4128 case tok::kw_image1d_array_t:
4129 case tok::kw_image1d_buffer_t:
4130 case tok::kw_image2d_t:
4131 case tok::kw_image2d_array_t:
4132 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004133 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004134 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004135
Chris Lattner861a2262008-04-13 18:59:07 +00004136 // struct-or-union-specifier (C99) or class-specifier (C++)
4137 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004138 case tok::kw_struct:
4139 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004140 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004141 // enum-specifier
4142 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004143
Chris Lattneracd58a32006-08-06 17:24:14 +00004144 // type-qualifier
4145 case tok::kw_const:
4146 case tok::kw_volatile:
4147 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004148
Chris Lattneracd58a32006-08-06 17:24:14 +00004149 // function-specifier
4150 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004151 case tok::kw_virtual:
4152 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004153 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004154
Richard Smith1dba27c2013-01-29 09:02:09 +00004155 // alignment-specifier
4156 case tok::kw__Alignas:
4157
Richard Smithd16fe122012-10-25 00:00:53 +00004158 // friend keyword.
4159 case tok::kw_friend:
4160
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004161 // static_assert-declaration
4162 case tok::kw__Static_assert:
4163
Chris Lattner599e47e2007-08-09 17:01:07 +00004164 // GNU typeof support.
4165 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004166
Chris Lattner599e47e2007-08-09 17:01:07 +00004167 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004168 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004169
Richard Smithd16fe122012-10-25 00:00:53 +00004170 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004171 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004172 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004173
Richard Smith8e1ac332013-03-28 01:55:44 +00004174 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004175 case tok::kw__Atomic:
4176 return true;
4177
Chris Lattner8b2ec162008-07-26 03:38:44 +00004178 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4179 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004180 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004181
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004182 // typedef-name
4183 case tok::annot_typename:
4184 return !DisambiguatingWithExpression ||
4185 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004186
Steve Narofff192fab2009-01-06 19:34:12 +00004187 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004188 case tok::kw___cdecl:
4189 case tok::kw___stdcall:
4190 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004191 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004192 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004193 case tok::kw___sptr:
4194 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004195 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004196 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004197 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004198 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004199 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004200
4201 case tok::kw___private:
4202 case tok::kw___local:
4203 case tok::kw___global:
4204 case tok::kw___constant:
4205 case tok::kw___read_only:
4206 case tok::kw___read_write:
4207 case tok::kw___write_only:
4208
Eli Friedman53339e02009-06-08 23:27:34 +00004209 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004210 }
4211}
4212
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004213bool Parser::isConstructorDeclarator() {
4214 TentativeParsingAction TPA(*this);
4215
4216 // Parse the C++ scope specifier.
4217 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004218 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004219 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004220 TPA.Revert();
4221 return false;
4222 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004223
4224 // Parse the constructor name.
4225 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4226 // We already know that we have a constructor name; just consume
4227 // the token.
4228 ConsumeToken();
4229 } else {
4230 TPA.Revert();
4231 return false;
4232 }
4233
Richard Smith43f340f2012-03-27 23:05:05 +00004234 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004235 if (Tok.isNot(tok::l_paren)) {
4236 TPA.Revert();
4237 return false;
4238 }
4239 ConsumeParen();
4240
Richard Smith43f340f2012-03-27 23:05:05 +00004241 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4242 // that we have a constructor.
4243 if (Tok.is(tok::r_paren) ||
4244 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004245 TPA.Revert();
4246 return true;
4247 }
4248
Richard Smithf2163662013-09-06 00:12:20 +00004249 // A C++11 attribute here signals that we have a constructor, and is an
4250 // attribute on the first constructor parameter.
4251 if (getLangOpts().CPlusPlus11 &&
4252 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4253 /*OuterMightBeMessageSend*/ true)) {
4254 TPA.Revert();
4255 return true;
4256 }
4257
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004258 // If we need to, enter the specified scope.
4259 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004260 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004261 DeclScopeObj.EnterDeclaratorScope();
4262
Francois Pichet79f3a872011-01-31 04:54:32 +00004263 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004264 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004265 MaybeParseMicrosoftAttributes(Attrs);
4266
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004267 // Check whether the next token(s) are part of a declaration
4268 // specifier, in which case we have the start of a parameter and,
4269 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004270 bool IsConstructor = false;
4271 if (isDeclarationSpecifier())
4272 IsConstructor = true;
4273 else if (Tok.is(tok::identifier) ||
4274 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4275 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4276 // This might be a parenthesized member name, but is more likely to
4277 // be a constructor declaration with an invalid argument type. Keep
4278 // looking.
4279 if (Tok.is(tok::annot_cxxscope))
4280 ConsumeToken();
4281 ConsumeToken();
4282
4283 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004284 // which must have one of the following syntactic forms (see the
4285 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004286 switch (Tok.getKind()) {
4287 case tok::l_paren:
4288 // C(X ( int));
4289 case tok::l_square:
4290 // C(X [ 5]);
4291 // C(X [ [attribute]]);
4292 case tok::coloncolon:
4293 // C(X :: Y);
4294 // C(X :: *p);
4295 case tok::r_paren:
4296 // C(X )
4297 // Assume this isn't a constructor, rather than assuming it's a
4298 // constructor with an unnamed parameter of an ill-formed type.
4299 break;
4300
4301 default:
4302 IsConstructor = true;
4303 break;
4304 }
4305 }
4306
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004307 TPA.Revert();
4308 return IsConstructor;
4309}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004310
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004311/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004312/// type-qualifier-list: [C99 6.7.5]
4313/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004314/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004315/// [ only if VendorAttributesAllowed=true ]
4316/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004317/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004318/// [ only if VendorAttributesAllowed=true ]
4319/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004320/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004321/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004322///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004323void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4324 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004325 bool CXX11AttributesAllowed,
4326 bool AtomicAllowed) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004327 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004328 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004329 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004330 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004331 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004332 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004333
4334 SourceLocation EndLoc;
4335
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004336 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004337 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004338 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004339 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004340 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004341
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004342 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004343 case tok::code_completion:
4344 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004345 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004346
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004347 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004348 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004349 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004350 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004351 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004352 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004353 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004354 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004355 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004356 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004357 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004358 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004359 case tok::kw__Atomic:
4360 if (!AtomicAllowed)
4361 goto DoneWithTypeQuals;
4362 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4363 getLangOpts());
4364 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004365
4366 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004367 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004368 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004369 goto DoneWithTypeQuals;
4370 case tok::kw___private:
4371 case tok::kw___global:
4372 case tok::kw___local:
4373 case tok::kw___constant:
4374 case tok::kw___read_only:
4375 case tok::kw___write_only:
4376 case tok::kw___read_write:
4377 ParseOpenCLQualifiers(DS);
4378 break;
4379
Aaron Ballman317a77f2013-05-22 23:25:32 +00004380 case tok::kw___sptr:
4381 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004382 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004383 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004384 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004385 case tok::kw___cdecl:
4386 case tok::kw___stdcall:
4387 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004388 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004389 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004390 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004391 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004392 continue;
4393 }
4394 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004395 case tok::kw___pascal:
4396 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004397 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004398 continue;
4399 }
4400 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004401 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004402 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004403 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004404 continue; // do *not* consume the next token!
4405 }
4406 // otherwise, FALL THROUGH!
4407 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004408 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004409 // If this is not a type-qualifier token, we're done reading type
4410 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004411 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004412 if (EndLoc.isValid())
4413 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004414 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004415 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004416
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004417 // If the specifier combination wasn't legal, issue a diagnostic.
4418 if (isInvalid) {
4419 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004420 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004421 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004422 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004423 }
4424}
4425
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004426
4427/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4428///
4429void Parser::ParseDeclarator(Declarator &D) {
4430 /// This implements the 'declarator' production in the C grammar, then checks
4431 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004432 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004433}
4434
Richard Smith0efa75c2012-03-29 01:16:42 +00004435static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4436 if (Kind == tok::star || Kind == tok::caret)
4437 return true;
4438
4439 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4440 if (!Lang.CPlusPlus)
4441 return false;
4442
4443 return Kind == tok::amp || Kind == tok::ampamp;
4444}
4445
Sebastian Redlbd150f42008-11-21 19:14:01 +00004446/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4447/// is parsed by the function passed to it. Pass null, and the direct-declarator
4448/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004449/// ptr-operator production.
4450///
Richard Smith09f76ee2011-10-19 21:33:05 +00004451/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004452/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4453/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004454///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004455/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4456/// [C] pointer[opt] direct-declarator
4457/// [C++] direct-declarator
4458/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004459///
4460/// pointer: [C99 6.7.5]
4461/// '*' type-qualifier-list[opt]
4462/// '*' type-qualifier-list[opt] pointer
4463///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004464/// ptr-operator:
4465/// '*' cv-qualifier-seq[opt]
4466/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004467/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004468/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004469/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004470/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004471void Parser::ParseDeclaratorInternal(Declarator &D,
4472 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004473 if (Diags.hasAllExtensionsSilenced())
4474 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004475
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004476 // C++ member pointers start with a '::' or a nested-name.
4477 // Member pointers get special handling, since there's no place for the
4478 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004479 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004480 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4481 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004482 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4483 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004484 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004485 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004486
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004487 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004488 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004489 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004490 if (D.mayHaveIdentifier())
4491 D.getCXXScopeSpec() = SS;
4492 else
4493 AnnotateScopeToken(SS, true);
4494
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004495 if (DirectDeclParser)
4496 (this->*DirectDeclParser)(D);
4497 return;
4498 }
4499
4500 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004501 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004502 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004503 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004504 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004505
4506 // Recurse to parse whatever is left.
4507 ParseDeclaratorInternal(D, DirectDeclParser);
4508
4509 // Sema will have to catch (syntactically invalid) pointers into global
4510 // scope. It has to catch pointers into namespace scope anyway.
4511 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004512 Loc),
4513 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004514 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004515 return;
4516 }
4517 }
4518
4519 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004520 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004521 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004522 if (DirectDeclParser)
4523 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004524 return;
4525 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004526
Sebastian Redled0f3b02009-03-15 22:02:01 +00004527 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4528 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004529 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004530 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004531
Chris Lattner9eac9312009-03-27 04:18:06 +00004532 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004533 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004534 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004535
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004536 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004537 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004538 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004539
Bill Wendling3708c182007-05-27 10:15:43 +00004540 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004541 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004542 if (Kind == tok::star)
4543 // Remember that we parsed a pointer type, and remember the type-quals.
4544 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004545 DS.getConstSpecLoc(),
4546 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004547 DS.getRestrictSpecLoc()),
4548 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004549 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004550 else
4551 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004552 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004553 Loc),
4554 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004555 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004556 } else {
4557 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004558 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004559
Sebastian Redl3b27be62009-03-23 00:00:23 +00004560 // Complain about rvalue references in C++03, but then go on and build
4561 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004562 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004563 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004564 diag::warn_cxx98_compat_rvalue_reference :
4565 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004566
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004567 // GNU-style and C++11 attributes are allowed here, as is restrict.
4568 ParseTypeQualifierListOpt(DS);
4569 D.ExtendWithDeclSpec(DS);
4570
Bill Wendling93efb222007-06-02 23:28:54 +00004571 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4572 // cv-qualifiers are introduced through the use of a typedef or of a
4573 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004574 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4575 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4576 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004577 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004578 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4579 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004580 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004581 // 'restrict' is permitted as an extension.
4582 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4583 Diag(DS.getAtomicSpecLoc(),
4584 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004585 }
Bill Wendling3708c182007-05-27 10:15:43 +00004586
4587 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004588 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004589
Douglas Gregor66583c52008-11-03 15:51:28 +00004590 if (D.getNumTypeObjects() > 0) {
4591 // C++ [dcl.ref]p4: There shall be no references to references.
4592 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4593 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004594 if (const IdentifierInfo *II = D.getIdentifier())
4595 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4596 << II;
4597 else
4598 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4599 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004600
Sebastian Redlbd150f42008-11-21 19:14:01 +00004601 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004602 // can go ahead and build the (technically ill-formed)
4603 // declarator: reference collapsing will take care of it.
4604 }
4605 }
4606
Richard Smith8e1ac332013-03-28 01:55:44 +00004607 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004608 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004609 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004610 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004611 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004612 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004613}
4614
Richard Smith0efa75c2012-03-29 01:16:42 +00004615static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4616 SourceLocation EllipsisLoc) {
4617 if (EllipsisLoc.isValid()) {
4618 FixItHint Insertion;
4619 if (!D.getEllipsisLoc().isValid()) {
4620 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4621 D.setEllipsisLoc(EllipsisLoc);
4622 }
4623 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4624 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4625 }
4626}
4627
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004628/// ParseDirectDeclarator
4629/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004630/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004631/// '(' declarator ')'
4632/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004633/// [C90] direct-declarator '[' constant-expression[opt] ']'
4634/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4635/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4636/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4637/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004638/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4639/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004640/// direct-declarator '(' parameter-type-list ')'
4641/// direct-declarator '(' identifier-list[opt] ')'
4642/// [GNU] direct-declarator '(' parameter-forward-declarations
4643/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004644/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4645/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004646/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4647/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4648/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004649/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004650/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004651///
4652/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004653/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004654/// '::'[opt] nested-name-specifier[opt] type-name
4655///
4656/// id-expression: [C++ 5.1]
4657/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004658/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004659///
4660/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004661/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004662/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004663/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004664/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004665/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004666///
Richard Smith1453e312012-03-27 01:42:32 +00004667/// Note, any additional constructs added here may need corresponding changes
4668/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004669void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004670 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004671
David Blaikiebbafb8a2012-03-11 07:00:24 +00004672 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004673 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004674 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004675 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4676 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004677 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004678 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004679 }
4680
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004681 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004682 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004683 // Change the declaration context for name lookup, until this function
4684 // is exited (and the declarator has been parsed).
4685 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004686 }
4687
Douglas Gregor27b4c162010-12-23 22:44:42 +00004688 // C++0x [dcl.fct]p14:
4689 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004690 // of a parameter-declaration-clause without a preceding comma. In
4691 // this case, the ellipsis is parsed as part of the
4692 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004693 // parameter pack that has not been expanded; otherwise, it is parsed
4694 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004695 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004696 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004697 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004698 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004699 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004700 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004701 !Actions.containsUnexpandedParameterPacks(D))) {
4702 SourceLocation EllipsisLoc = ConsumeToken();
4703 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4704 // The ellipsis was put in the wrong place. Recover, and explain to
4705 // the user what they should have done.
4706 ParseDeclarator(D);
4707 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4708 return;
4709 } else
4710 D.setEllipsisLoc(EllipsisLoc);
4711
4712 // The ellipsis can't be followed by a parenthesized declarator. We
4713 // check for that in ParseParenDeclarator, after we have disambiguated
4714 // the l_paren token.
4715 }
4716
Douglas Gregor7861a802009-11-03 01:35:08 +00004717 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4718 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4719 // We found something that indicates the start of an unqualified-id.
4720 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004721 bool AllowConstructorName;
4722 if (D.getDeclSpec().hasTypeSpecifier())
4723 AllowConstructorName = false;
4724 else if (D.getCXXScopeSpec().isSet())
4725 AllowConstructorName =
4726 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004727 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004728 else
4729 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4730
Abramo Bagnara7945c982012-01-27 09:46:47 +00004731 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004732 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4733 /*EnteringContext=*/true,
4734 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004735 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004736 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004737 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004738 D.getName()) ||
4739 // Once we're past the identifier, if the scope was bad, mark the
4740 // whole declarator bad.
4741 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004742 D.SetIdentifier(0, Tok.getLocation());
4743 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004744 } else {
4745 // Parsed the unqualified-id; update range information and move along.
4746 if (D.getSourceRange().getBegin().isInvalid())
4747 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4748 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004749 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004750 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004751 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004752 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004753 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004754 "There's a C++-specific check for tok::identifier above");
4755 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4756 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4757 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004758 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004759 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004760 // A virt-specifier isn't treated as an identifier if it appears after a
4761 // trailing-return-type.
4762 if (D.getContext() != Declarator::TrailingReturnContext ||
4763 !isCXX11VirtSpecifier(Tok)) {
4764 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4765 << FixItHint::CreateRemoval(Tok.getLocation());
4766 D.SetIdentifier(0, Tok.getLocation());
4767 ConsumeToken();
4768 goto PastIdentifier;
4769 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004770 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004771
Douglas Gregor7861a802009-11-03 01:35:08 +00004772 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004773 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004774 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004775 // Example: 'char (*X)' or 'int (*XX)(void)'
4776 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004777
4778 // If the declarator was parenthesized, we entered the declarator
4779 // scope when parsing the parenthesized declarator, then exited
4780 // the scope already. Re-enter the scope, if we need to.
4781 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004782 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004783 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004784 if (!D.isInvalidType() &&
4785 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004786 // Change the declaration context for name lookup, until this function
4787 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004788 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004789 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004790 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004791 // This could be something simple like "int" (in which case the declarator
4792 // portion is empty), if an abstract-declarator is allowed.
4793 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004794
4795 // The grammar for abstract-pack-declarator does not allow grouping parens.
4796 // FIXME: Revisit this once core issue 1488 is resolved.
4797 if (D.hasEllipsis() && D.hasGroupingParens())
4798 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4799 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004800 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004801 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004802 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004803 if (D.getContext() == Declarator::MemberContext)
4804 Diag(Tok, diag::err_expected_member_name_or_semi)
4805 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004806 else if (getLangOpts().CPlusPlus) {
4807 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4808 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004809 else {
4810 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4811 if (Tok.isAtStartOfLine() && Loc.isValid())
4812 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4813 << getLangOpts().CPlusPlus;
4814 else
4815 Diag(Tok, diag::err_expected_unqualified_id)
4816 << getLangOpts().CPlusPlus;
4817 }
Richard Trieu9c672672013-01-26 02:31:38 +00004818 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00004819 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004820 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004821 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004822 }
Mike Stump11289f42009-09-09 15:08:12 +00004823
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004824 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004825 assert(D.isPastIdentifier() &&
4826 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004827
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004828 // Don't parse attributes unless we have parsed an unparenthesized name.
4829 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004830 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004831
Chris Lattneracd58a32006-08-06 17:24:14 +00004832 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004833 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004834 // Enter function-declaration scope, limiting any declarators to the
4835 // function prototype scope, including parameter declarators.
4836 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004837 Scope::FunctionPrototypeScope|Scope::DeclScope|
4838 (D.isFunctionDeclaratorAFunctionDeclaration()
4839 ? Scope::FunctionDeclarationScope : 0));
4840
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004841 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4842 // In such a case, check if we actually have a function declarator; if it
4843 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004844 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004845 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4846 // The name of the declarator, if any, is tentatively declared within
4847 // a possible direct initializer.
4848 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4849 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4850 TentativelyDeclaredIdentifiers.pop_back();
4851 if (!IsFunctionDecl)
4852 break;
4853 }
John McCall084e83d2011-03-24 11:26:52 +00004854 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004855 BalancedDelimiterTracker T(*this, tok::l_paren);
4856 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004857 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004858 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004859 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004860 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004861 } else {
4862 break;
4863 }
4864 }
Chad Rosierc1183952012-06-26 22:30:43 +00004865}
Chris Lattneracd58a32006-08-06 17:24:14 +00004866
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004867/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4868/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004869/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004870/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4871///
4872/// direct-declarator:
4873/// '(' declarator ')'
4874/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004875/// direct-declarator '(' parameter-type-list ')'
4876/// direct-declarator '(' identifier-list[opt] ')'
4877/// [GNU] direct-declarator '(' parameter-forward-declarations
4878/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004879///
4880void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004881 BalancedDelimiterTracker T(*this, tok::l_paren);
4882 T.consumeOpen();
4883
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004884 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004885
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004886 // Eat any attributes before we look at whether this is a grouping or function
4887 // declarator paren. If this is a grouping paren, the attribute applies to
4888 // the type being built up, for example:
4889 // int (__attribute__(()) *x)(long y)
4890 // If this ends up not being a grouping paren, the attribute applies to the
4891 // first argument, for example:
4892 // int (__attribute__(()) int x)
4893 // In either case, we need to eat any attributes to be able to determine what
4894 // sort of paren this is.
4895 //
John McCall084e83d2011-03-24 11:26:52 +00004896 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004897 bool RequiresArg = false;
4898 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004899 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004900
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004901 // We require that the argument list (if this is a non-grouping paren) be
4902 // present even if the attribute list was empty.
4903 RequiresArg = true;
4904 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004905
Steve Naroff44ac7772008-12-25 14:16:32 +00004906 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004907 ParseMicrosoftTypeAttributes(attrs);
4908
Dawn Perchik335e16b2010-09-03 01:29:35 +00004909 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004910 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004911 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004912
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004913 // If we haven't past the identifier yet (or where the identifier would be
4914 // stored, if this is an abstract declarator), then this is probably just
4915 // grouping parens. However, if this could be an abstract-declarator, then
4916 // this could also be the start of function arguments (consider 'void()').
4917 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004918
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004919 if (!D.mayOmitIdentifier()) {
4920 // If this can't be an abstract-declarator, this *must* be a grouping
4921 // paren, because we haven't seen the identifier yet.
4922 isGrouping = true;
4923 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004924 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4925 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004926 isDeclarationSpecifier() || // 'int(int)' is a function.
4927 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004928 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4929 // considered to be a type, not a K&R identifier-list.
4930 isGrouping = false;
4931 } else {
4932 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4933 isGrouping = true;
4934 }
Mike Stump11289f42009-09-09 15:08:12 +00004935
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004936 // If this is a grouping paren, handle:
4937 // direct-declarator: '(' declarator ')'
4938 // direct-declarator: '(' attributes declarator ')'
4939 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004940 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4941 D.setEllipsisLoc(SourceLocation());
4942
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004943 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004944 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004945 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004946 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004947 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004948 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004949 T.getCloseLocation()),
4950 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004951
4952 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004953
4954 // An ellipsis cannot be placed outside parentheses.
4955 if (EllipsisLoc.isValid())
4956 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4957
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004958 return;
4959 }
Mike Stump11289f42009-09-09 15:08:12 +00004960
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004961 // Okay, if this wasn't a grouping paren, it must be the start of a function
4962 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004963 // identifier (and remember where it would have been), then call into
4964 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004965 D.SetIdentifier(0, Tok.getLocation());
4966
David Blaikie15a430a2011-12-04 05:04:18 +00004967 // Enter function-declaration scope, limiting any declarators to the
4968 // function prototype scope, including parameter declarators.
4969 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004970 Scope::FunctionPrototypeScope | Scope::DeclScope |
4971 (D.isFunctionDeclaratorAFunctionDeclaration()
4972 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00004973 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004974 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004975}
4976
4977/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4978/// declarator D up to a paren, which indicates that we are parsing function
4979/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004980///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004981/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4982/// immediately after the open paren - they should be considered to be the
4983/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004984///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004985/// If RequiresArg is true, then the first argument of the function is required
4986/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004987///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004988/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4989/// (C++11) ref-qualifier[opt], exception-specification[opt],
4990/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4991///
4992/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004993/// dynamic-exception-specification
4994/// noexcept-specification
4995///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004996void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004997 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004998 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004999 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005000 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00005001 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00005002 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00005003 // lparen is already consumed!
5004 assert(D.isPastIdentifier() && "Should not call before identifier!");
5005
5006 // This should be true when the function has typed arguments.
5007 // Otherwise, it is treated as a K&R-style function.
5008 bool HasProto = false;
5009 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005010 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005011 // Remember where we see an ellipsis, if any.
5012 SourceLocation EllipsisLoc;
5013
5014 DeclSpec DS(AttrFactory);
5015 bool RefQualifierIsLValueRef = true;
5016 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00005017 SourceLocation ConstQualifierLoc;
5018 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005019 ExceptionSpecificationType ESpecType = EST_None;
5020 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005021 SmallVector<ParsedType, 2> DynamicExceptions;
5022 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005023 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005024 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00005025 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005026
James Molloy6f8780b2012-02-29 10:24:19 +00005027 Actions.ActOnStartFunctionDeclarator();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005028 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5029 EndLoc is the end location for the function declarator.
5030 They differ for trailing return types. */
5031 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005032 SourceLocation LParenLoc, RParenLoc;
5033 LParenLoc = Tracker.getOpenLocation();
5034 StartLoc = LParenLoc;
5035
Douglas Gregor9e66af42011-07-05 16:44:18 +00005036 if (isFunctionDeclaratorIdentifierList()) {
5037 if (RequiresArg)
5038 Diag(Tok, diag::err_argument_required_after_attribute);
5039
5040 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5041
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005042 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005043 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005044 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005045 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005046 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005047 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005048 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5049 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005050 else if (RequiresArg)
5051 Diag(Tok, diag::err_argument_required_after_attribute);
5052
David Blaikiebbafb8a2012-03-11 07:00:24 +00005053 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005054
5055 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005056 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005057 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005058 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005059 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005060
David Blaikiebbafb8a2012-03-11 07:00:24 +00005061 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005062 // FIXME: Accept these components in any order, and produce fixits to
5063 // correct the order if the user gets it wrong. Ideally we should deal
5064 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005065
5066 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005067 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5068 /*CXX11AttributesAllowed*/ false,
5069 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005070 if (!DS.getSourceRange().getEnd().isInvalid()) {
5071 EndLoc = DS.getSourceRange().getEnd();
5072 ConstQualifierLoc = DS.getConstSpecLoc();
5073 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5074 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005075
5076 // Parse ref-qualifier[opt].
5077 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005078 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005079 diag::warn_cxx98_compat_ref_qualifier :
5080 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005081
Douglas Gregor9e66af42011-07-05 16:44:18 +00005082 RefQualifierIsLValueRef = Tok.is(tok::amp);
5083 RefQualifierLoc = ConsumeToken();
5084 EndLoc = RefQualifierLoc;
5085 }
5086
Douglas Gregor3024f072012-04-16 07:05:22 +00005087 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005088 // If a declaration declares a member function or member function
5089 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005090 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005091 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005092 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005093 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005094 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005095 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005096 (D.getContext() == Declarator::MemberContext
5097 ? !D.getDeclSpec().isFriendSpecified()
5098 : D.getContext() == Declarator::FileContext &&
5099 D.getCXXScopeSpec().isValid() &&
5100 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005101 Sema::CXXThisScopeRAII ThisScope(Actions,
5102 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005103 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005104 (D.getDeclSpec().isConstexprSpecified() &&
5105 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005106 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005107 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005108
Douglas Gregor9e66af42011-07-05 16:44:18 +00005109 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005110 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005111 DynamicExceptions,
5112 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005113 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005114 if (ESpecType != EST_None)
5115 EndLoc = ESpecRange.getEnd();
5116
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005117 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5118 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005119 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005120
Douglas Gregor9e66af42011-07-05 16:44:18 +00005121 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005122 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005123 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005124 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005125 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5126 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005127 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005128 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005129 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005130 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005131 }
5132 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005133 }
5134
5135 // Remember that we parsed a function type, and remember the attributes.
5136 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005137 IsAmbiguous,
5138 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005139 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005140 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005141 DS.getTypeQualifiers(),
5142 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005143 RefQualifierLoc, ConstQualifierLoc,
5144 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005145 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005146 ESpecType, ESpecRange.getBegin(),
5147 DynamicExceptions.data(),
5148 DynamicExceptionRanges.data(),
5149 DynamicExceptions.size(),
5150 NoexceptExpr.isUsable() ?
5151 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005152 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005153 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005154 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005155
5156 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005157}
5158
5159/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5160/// identifier list form for a K&R-style function: void foo(a,b,c)
5161///
5162/// Note that identifier-lists are only allowed for normal declarators, not for
5163/// abstract-declarators.
5164bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005165 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005166 && Tok.is(tok::identifier)
5167 && !TryAltiVecVectorToken()
5168 // K&R identifier lists can't have typedefs as identifiers, per C99
5169 // 6.7.5.3p11.
5170 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5171 // Identifier lists follow a really simple grammar: the identifiers can
5172 // be followed *only* by a ", identifier" or ")". However, K&R
5173 // identifier lists are really rare in the brave new modern world, and
5174 // it is very common for someone to typo a type in a non-K&R style
5175 // list. If we are presented with something like: "void foo(intptr x,
5176 // float y)", we don't want to start parsing the function declarator as
5177 // though it is a K&R style declarator just because intptr is an
5178 // invalid type.
5179 //
5180 // To handle this, we check to see if the token after the first
5181 // identifier is a "," or ")". Only then do we parse it as an
5182 // identifier list.
5183 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5184}
5185
5186/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5187/// we found a K&R-style identifier list instead of a typed parameter list.
5188///
5189/// After returning, ParamInfo will hold the parsed parameters.
5190///
5191/// identifier-list: [C99 6.7.5]
5192/// identifier
5193/// identifier-list ',' identifier
5194///
5195void Parser::ParseFunctionDeclaratorIdentifierList(
5196 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005197 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005198 // If there was no identifier specified for the declarator, either we are in
5199 // an abstract-declarator, or we are in a parameter declarator which was found
5200 // to be abstract. In abstract-declarators, identifier lists are not valid:
5201 // diagnose this.
5202 if (!D.getIdentifier())
5203 Diag(Tok, diag::ext_ident_list_in_param);
5204
5205 // Maintain an efficient lookup of params we have seen so far.
5206 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5207
5208 while (1) {
5209 // If this isn't an identifier, report the error and skip until ')'.
5210 if (Tok.isNot(tok::identifier)) {
5211 Diag(Tok, diag::err_expected_ident);
5212 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
5213 // Forget we parsed anything.
5214 ParamInfo.clear();
5215 return;
5216 }
5217
5218 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5219
5220 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5221 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5222 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5223
5224 // Verify that the argument identifier has not already been mentioned.
5225 if (!ParamsSoFar.insert(ParmII)) {
5226 Diag(Tok, diag::err_param_redefinition) << ParmII;
5227 } else {
5228 // Remember this identifier in ParamInfo.
5229 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5230 Tok.getLocation(),
5231 0));
5232 }
5233
5234 // Eat the identifier.
5235 ConsumeToken();
5236
5237 // The list continues if we see a comma.
5238 if (Tok.isNot(tok::comma))
5239 break;
5240 ConsumeToken();
5241 }
5242}
5243
5244/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5245/// after the opening parenthesis. This function will not parse a K&R-style
5246/// identifier list.
5247///
Richard Smith2620cd92012-04-11 04:01:28 +00005248/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5249/// caller parsed those arguments immediately after the open paren - they should
5250/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005251///
5252/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5253/// be the location of the ellipsis, if any was parsed.
5254///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005255/// parameter-type-list: [C99 6.7.5]
5256/// parameter-list
5257/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005258/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005259///
5260/// parameter-list: [C99 6.7.5]
5261/// parameter-declaration
5262/// parameter-list ',' parameter-declaration
5263///
5264/// parameter-declaration: [C99 6.7.5]
5265/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005266/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005267/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005268/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005269/// declaration-specifiers abstract-declarator[opt]
5270/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005271/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005272/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005273/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005274///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005275void Parser::ParseParameterDeclarationClause(
5276 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005277 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005278 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005279 SourceLocation &EllipsisLoc) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005280 while (1) {
5281 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00005282 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5283 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00005284 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00005285 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00005286 }
Mike Stump11289f42009-09-09 15:08:12 +00005287
Chris Lattner371ed4e2008-04-06 06:57:35 +00005288 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005289 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005290 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005291
Richard Smith2620cd92012-04-11 04:01:28 +00005292 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005293 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005294
John McCall53fa7142010-12-24 02:08:15 +00005295 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005296 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005297
5298 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005299
5300 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005301 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005302 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005303 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5304 // too much hassle.
5305 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005306
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005307 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005308
Faisal Vali2b391ab2013-09-26 19:54:12 +00005309
5310 // Parse the declarator. This is "PrototypeContext" or
5311 // "LambdaExprParameterContext", because we must accept either
5312 // 'declarator' or 'abstract-declarator' here.
5313 Declarator ParmDeclarator(DS,
5314 D.getContext() == Declarator::LambdaExprContext ?
5315 Declarator::LambdaExprParameterContext :
5316 Declarator::PrototypeContext);
5317 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005318
5319 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005320 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005321
Chris Lattner371ed4e2008-04-06 06:57:35 +00005322 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005323 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005324
Douglas Gregor4d87df52008-12-16 21:30:33 +00005325 // DefArgToks is used when the parsing of default arguments needs
5326 // to be delayed.
5327 CachedTokens *DefArgToks = 0;
5328
Chris Lattner371ed4e2008-04-06 06:57:35 +00005329 // If no parameter was specified, verify that *something* was specified,
5330 // otherwise we have a missing type and identifier.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005331 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5332 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005333 // Completely missing, emit error.
5334 Diag(DSStart, diag::err_missing_param);
5335 } else {
5336 // Otherwise, we have something. Add it and let semantic analysis try
5337 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005338
Chris Lattner371ed4e2008-04-06 06:57:35 +00005339 // Inform the actions module about the parameter declarator, so it gets
5340 // added to the current scope.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005341 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5342 ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005343 // Parse the default argument, if any. We parse the default
5344 // arguments in all dialects; the semantic analysis in
5345 // ActOnParamDefaultArgument will reject the default argument in
5346 // C.
5347 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005348 SourceLocation EqualLoc = Tok.getLocation();
5349
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005350 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005351 if (D.getContext() == Declarator::MemberContext) {
5352 // If we're inside a class definition, cache the tokens
5353 // corresponding to the default argument. We'll actually parse
5354 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005355 // FIXME: Can we use a smart pointer for Toks?
5356 DefArgToks = new CachedTokens;
5357
Richard Smith1fff95c2013-09-12 23:28:08 +00005358 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005359 delete DefArgToks;
5360 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005361 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005362 } else {
5363 // Mark the end of the default argument so that we know when to
5364 // stop when we parse it later on.
5365 Token DefArgEnd;
5366 DefArgEnd.startToken();
5367 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5368 DefArgEnd.setLocation(Tok.getLocation());
5369 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005370 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005371 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005372 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005373 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005374 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005375 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005376
Chad Rosierc1183952012-06-26 22:30:43 +00005377 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005378 // used.
5379 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005380 Sema::PotentiallyEvaluatedIfUsed,
5381 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005382
Sebastian Redldb63af22012-03-14 15:54:00 +00005383 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005384 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005385 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005386 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005387 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005388 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005389 if (DefArgResult.isInvalid()) {
5390 Actions.ActOnParamDefaultArgumentError(Param);
5391 SkipUntil(tok::comma, tok::r_paren, true, true);
5392 } else {
5393 // Inform the actions module about the default argument
5394 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005395 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005396 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005397 }
5398 }
Mike Stump11289f42009-09-09 15:08:12 +00005399
5400 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005401 ParmDeclarator.getIdentifierLoc(),
5402 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005403 }
5404
5405 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005406 if (Tok.isNot(tok::comma)) {
5407 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005408 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005409
David Blaikiebbafb8a2012-03-11 07:00:24 +00005410 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005411 // We have ellipsis without a preceding ',', which is ill-formed
5412 // in C. Complain and provide the fix.
5413 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005414 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005415 }
5416 }
Chad Rosierc1183952012-06-26 22:30:43 +00005417
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005418 break;
5419 }
Mike Stump11289f42009-09-09 15:08:12 +00005420
Chris Lattner371ed4e2008-04-06 06:57:35 +00005421 // Consume the comma.
5422 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005423 }
Mike Stump11289f42009-09-09 15:08:12 +00005424
Chris Lattner6c940e62008-04-06 06:34:08 +00005425}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005426
Chris Lattnere8074e62006-08-06 18:30:15 +00005427/// [C90] direct-declarator '[' constant-expression[opt] ']'
5428/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5429/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5430/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5431/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005432/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5433/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005434void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005435 if (CheckProhibitedCXX11Attribute())
5436 return;
5437
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005438 BalancedDelimiterTracker T(*this, tok::l_square);
5439 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005440
Chris Lattner84a11622008-12-18 07:27:21 +00005441 // C array syntax has many features, but by-far the most common is [] and [4].
5442 // This code does a fast path to handle some of the most obvious cases.
5443 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005444 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005445 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005446 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005447
Chris Lattner84a11622008-12-18 07:27:21 +00005448 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005449 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005450 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005451 T.getOpenLocation(),
5452 T.getCloseLocation()),
5453 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005454 return;
5455 } else if (Tok.getKind() == tok::numeric_constant &&
5456 GetLookAheadToken(1).is(tok::r_square)) {
5457 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005458 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005459 ConsumeToken();
5460
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005461 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005462 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005463 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005464
Chris Lattner84a11622008-12-18 07:27:21 +00005465 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005466 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005467 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005468 T.getOpenLocation(),
5469 T.getCloseLocation()),
5470 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005471 return;
5472 }
Mike Stump11289f42009-09-09 15:08:12 +00005473
Chris Lattnere8074e62006-08-06 18:30:15 +00005474 // If valid, this location is the position where we read the 'static' keyword.
5475 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005476 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005477 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005478
Chris Lattnere8074e62006-08-06 18:30:15 +00005479 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005480 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005481 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005482 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005483
Chris Lattnere8074e62006-08-06 18:30:15 +00005484 // If we haven't already read 'static', check to see if there is one after the
5485 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005486 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005487 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005488
Chris Lattnere8074e62006-08-06 18:30:15 +00005489 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005490 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005491 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005492
Chris Lattner521ff2b2008-04-06 05:26:30 +00005493 // Handle the case where we have '[*]' as the array size. However, a leading
5494 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005495 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005496 // infrequent, use of lookahead is not costly here.
5497 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005498 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005499
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005500 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005501 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005502 StaticLoc = SourceLocation(); // Drop the static.
5503 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005504 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005505 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005506 // Note, in C89, this production uses the constant-expr production instead
5507 // of assignment-expr. The only difference is that assignment-expr allows
5508 // things like '=' and '*='. Sema rejects these in C89 mode because they
5509 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005510
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005511 // Parse the constant-expression or assignment-expression now (depending
5512 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005513 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005514 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005515 } else {
5516 EnterExpressionEvaluationContext Unevaluated(Actions,
5517 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005518 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005519 }
Chris Lattner62591722006-08-12 18:40:58 +00005520 }
Mike Stump11289f42009-09-09 15:08:12 +00005521
Chris Lattner62591722006-08-12 18:40:58 +00005522 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005523 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005524 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005525 // If the expression was invalid, skip it.
5526 SkipUntil(tok::r_square);
5527 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005528 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005529
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005530 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005531
John McCall084e83d2011-03-24 11:26:52 +00005532 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005533 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005534
Chris Lattner84a11622008-12-18 07:27:21 +00005535 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005536 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005537 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005538 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005539 T.getOpenLocation(),
5540 T.getCloseLocation()),
5541 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005542}
5543
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005544/// [GNU] typeof-specifier:
5545/// typeof ( expressions )
5546/// typeof ( type-name )
5547/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005548///
5549void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005550 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005551 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005552 SourceLocation StartLoc = ConsumeToken();
5553
John McCalle8595032010-01-13 20:03:27 +00005554 const bool hasParens = Tok.is(tok::l_paren);
5555
Eli Friedman15681d62012-09-26 04:34:21 +00005556 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5557 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005558
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005559 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005560 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005561 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005562 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5563 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005564 if (hasParens)
5565 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005566
5567 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005568 // FIXME: Not accurate, the range gets one token more than it should.
5569 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005570 else
5571 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005572
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005573 if (isCastExpr) {
5574 if (!CastTy) {
5575 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005576 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005577 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005578
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005579 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005580 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005581 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5582 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005583 DiagID, CastTy))
5584 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005585 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005586 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005587
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005588 // If we get here, the operand to the typeof was an expresion.
5589 if (Operand.isInvalid()) {
5590 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005591 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005592 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005593
Eli Friedmane0afc982012-01-21 01:01:51 +00005594 // We might need to transform the operand if it is potentially evaluated.
5595 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5596 if (Operand.isInvalid()) {
5597 DS.SetTypeSpecError();
5598 return;
5599 }
5600
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005601 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005602 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005603 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5604 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005605 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005606 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005607}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005608
Benjamin Kramere56f3932011-12-23 17:00:35 +00005609/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005610/// _Atomic ( type-name )
5611///
5612void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005613 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5614 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005615
5616 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005617 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005618 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005619 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005620
5621 TypeResult Result = ParseTypeName();
5622 if (Result.isInvalid()) {
5623 SkipUntil(tok::r_paren);
5624 return;
5625 }
5626
5627 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005628 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005629
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005630 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005631 return;
5632
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005633 DS.setTypeofParensRange(T.getRange());
5634 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005635
5636 const char *PrevSpec = 0;
5637 unsigned DiagID;
5638 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5639 DiagID, Result.release()))
5640 Diag(StartLoc, DiagID) << PrevSpec;
5641}
5642
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005643
5644/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5645/// from TryAltiVecVectorToken.
5646bool Parser::TryAltiVecVectorTokenOutOfLine() {
5647 Token Next = NextToken();
5648 switch (Next.getKind()) {
5649 default: return false;
5650 case tok::kw_short:
5651 case tok::kw_long:
5652 case tok::kw_signed:
5653 case tok::kw_unsigned:
5654 case tok::kw_void:
5655 case tok::kw_char:
5656 case tok::kw_int:
5657 case tok::kw_float:
5658 case tok::kw_double:
5659 case tok::kw_bool:
5660 case tok::kw___pixel:
5661 Tok.setKind(tok::kw___vector);
5662 return true;
5663 case tok::identifier:
5664 if (Next.getIdentifierInfo() == Ident_pixel) {
5665 Tok.setKind(tok::kw___vector);
5666 return true;
5667 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005668 if (Next.getIdentifierInfo() == Ident_bool) {
5669 Tok.setKind(tok::kw___vector);
5670 return true;
5671 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005672 return false;
5673 }
5674}
5675
5676bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5677 const char *&PrevSpec, unsigned &DiagID,
5678 bool &isInvalid) {
5679 if (Tok.getIdentifierInfo() == Ident_vector) {
5680 Token Next = NextToken();
5681 switch (Next.getKind()) {
5682 case tok::kw_short:
5683 case tok::kw_long:
5684 case tok::kw_signed:
5685 case tok::kw_unsigned:
5686 case tok::kw_void:
5687 case tok::kw_char:
5688 case tok::kw_int:
5689 case tok::kw_float:
5690 case tok::kw_double:
5691 case tok::kw_bool:
5692 case tok::kw___pixel:
5693 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5694 return true;
5695 case tok::identifier:
5696 if (Next.getIdentifierInfo() == Ident_pixel) {
5697 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5698 return true;
5699 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005700 if (Next.getIdentifierInfo() == Ident_bool) {
5701 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5702 return true;
5703 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005704 break;
5705 default:
5706 break;
5707 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005708 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005709 DS.isTypeAltiVecVector()) {
5710 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5711 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005712 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5713 DS.isTypeAltiVecVector()) {
5714 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5715 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005716 }
5717 return false;
5718}