blob: 74769bf731987d3620002624c8e4f5b1aac77a92 [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"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000016#include "clang/Basic/AddressSpaces.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000017#include "clang/Basic/OpenCL.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000019#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Sema/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000023#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000025#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000026using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// C99 6.7: Declarations.
30//===----------------------------------------------------------------------===//
31
Chris Lattnerf5fbd792006-08-10 23:56:11 +000032/// ParseTypeName
33/// type-name: [C99 6.7.6]
34/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000035///
36/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000037TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000038 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000039 AccessSpecifier AS,
40 Decl **OwnedType) {
Richard Smith62dad822012-03-15 01:02:11 +000041 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000042 if (DSC == DSC_normal)
43 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000044
Chris Lattnerf5fbd792006-08-10 23:56:11 +000045 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000046 DeclSpec DS(AttrFactory);
Richard Smithbfdb1082012-03-12 08:56:40 +000047 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000048 if (OwnedType)
49 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000050
Chris Lattnerf5fbd792006-08-10 23:56:11 +000051 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000052 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000053 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000054 if (Range)
55 *Range = DeclaratorInfo.getSourceRange();
56
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000057 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000058 return true;
59
Douglas Gregor0be31a22010-07-02 17:43:08 +000060 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000061}
62
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000063
64/// isAttributeLateParsed - Return true if the attribute has arguments that
65/// require late parsing.
66static bool isAttributeLateParsed(const IdentifierInfo &II) {
67 return llvm::StringSwitch<bool>(II.getName())
68#include "clang/Parse/AttrLateParsed.inc"
69 .Default(false);
70}
71
Alexis Hunt96d5c762009-11-21 08:43:09 +000072/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000073///
74/// [GNU] attributes:
75/// attribute
76/// attributes attribute
77///
78/// [GNU] attribute:
79/// '__attribute__' '(' '(' attribute-list ')' ')'
80///
81/// [GNU] attribute-list:
82/// attrib
83/// attribute_list ',' attrib
84///
85/// [GNU] attrib:
86/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000087/// attrib-name
88/// attrib-name '(' identifier ')'
89/// attrib-name '(' identifier ',' nonempty-expr-list ')'
90/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000091///
Steve Naroff0f2fe172007-06-01 17:11:19 +000092/// [GNU] attrib-name:
93/// identifier
94/// typespec
95/// typequal
96/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000097///
Steve Naroff0f2fe172007-06-01 17:11:19 +000098/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000099/// token lookahead. Comment from gcc: "If they start with an identifier
100/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +0000101/// start with that identifier; otherwise they are an expression list."
102///
Richard Smithb12bf692011-10-17 21:20:17 +0000103/// GCC does not require the ',' between attribs in an attribute-list.
104///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105/// At the moment, I am not doing 2 token lookahead. I am also unaware of
106/// any attributes that don't work (based on my limited testing). Most
107/// attributes are very simple in practice. Until we find a bug, I don't see
108/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000109
John McCall53fa7142010-12-24 02:08:15 +0000110void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000111 SourceLocation *endLoc,
112 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000113 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000114
Chris Lattner76c72282007-10-09 17:33:22 +0000115 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000116 ConsumeToken();
117 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
118 "attribute")) {
119 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000120 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000121 }
122 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
123 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000124 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000125 }
126 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000127 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
128 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000129 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000130 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
131 ConsumeToken();
132 continue;
133 }
134 // we have an identifier or declaration specifier (const, int, etc.)
135 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
136 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000137
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000138 if (Tok.is(tok::l_paren)) {
139 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000140 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000141 LateParsedAttribute *LA =
142 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
143 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000144
Bill Wendling44426052012-12-20 19:22:21 +0000145 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000146 // with other late-parsed declarations.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000147 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000148 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000149
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000150 // consume everything up to and including the matching right parens
151 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000152
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000153 Token Eof;
154 Eof.startToken();
155 Eof.setLocation(Tok.getLocation());
156 LA->Toks.push_back(Eof);
157 } else {
Michael Han23214e52012-10-03 01:56:22 +0000158 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000159 0, SourceLocation(), AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000160 }
161 } else {
John McCall084e83d2011-03-24 11:26:52 +0000162 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000163 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000164 }
165 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000166 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000167 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000168 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000169 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
170 SkipUntil(tok::r_paren, false);
171 }
John McCall53fa7142010-12-24 02:08:15 +0000172 if (endLoc)
173 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000174 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000175}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000176
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000177
Michael Han23214e52012-10-03 01:56:22 +0000178/// Parse the arguments to a parameterized GNU attribute or
179/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000180void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
181 SourceLocation AttrNameLoc,
182 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000183 SourceLocation *EndLoc,
184 IdentifierInfo *ScopeName,
185 SourceLocation ScopeLoc,
186 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000187
188 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
189
190 // Availability attributes have their own grammar.
191 if (AttrName->isStr("availability")) {
192 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
193 return;
194 }
195 // Thread safety attributes fit into the FIXME case above, so we
196 // just parse the arguments as a list of expressions
197 if (IsThreadSafetyAttribute(AttrName->getName())) {
198 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
199 return;
200 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000201 // Type safety attributes have their own grammar.
202 if (AttrName->isStr("type_tag_for_datatype")) {
203 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
204 return;
205 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000206
207 ConsumeParen(); // ignore the left paren loc for now
208
Richard Smithb12bf692011-10-17 21:20:17 +0000209 IdentifierInfo *ParmName = 0;
210 SourceLocation ParmLoc;
211 bool BuiltinType = false;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000212
Richard Smithb12bf692011-10-17 21:20:17 +0000213 switch (Tok.getKind()) {
214 case tok::kw_char:
215 case tok::kw_wchar_t:
216 case tok::kw_char16_t:
217 case tok::kw_char32_t:
218 case tok::kw_bool:
219 case tok::kw_short:
220 case tok::kw_int:
221 case tok::kw_long:
222 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000223 case tok::kw___int128:
Richard Smithb12bf692011-10-17 21:20:17 +0000224 case tok::kw_signed:
225 case tok::kw_unsigned:
226 case tok::kw_float:
227 case tok::kw_double:
228 case tok::kw_void:
229 case tok::kw_typeof:
230 // __attribute__(( vec_type_hint(char) ))
231 // FIXME: Don't just discard the builtin type token.
232 ConsumeToken();
233 BuiltinType = true;
234 break;
235
236 case tok::identifier:
237 ParmName = Tok.getIdentifierInfo();
238 ParmLoc = ConsumeToken();
239 break;
240
241 default:
242 break;
243 }
244
Benjamin Kramerf0623432012-08-23 22:51:59 +0000245 ExprVector ArgExprs;
Richard Smithb12bf692011-10-17 21:20:17 +0000246
247 if (!BuiltinType &&
248 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
249 // Eat the comma.
250 if (ParmLoc.isValid())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000251 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000252
Richard Smithb12bf692011-10-17 21:20:17 +0000253 // Parse the non-empty comma-separated list of expressions.
254 while (1) {
255 ExprResult ArgExpr(ParseAssignmentExpression());
256 if (ArgExpr.isInvalid()) {
257 SkipUntil(tok::r_paren);
258 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000259 }
Richard Smithb12bf692011-10-17 21:20:17 +0000260 ArgExprs.push_back(ArgExpr.release());
261 if (Tok.isNot(tok::comma))
262 break;
263 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000264 }
Richard Smithb12bf692011-10-17 21:20:17 +0000265 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000266 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
267 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
268 tok::greater)) {
Fariborz Jahanian7f733022011-10-18 23:13:50 +0000269 while (Tok.is(tok::identifier)) {
270 ConsumeToken();
271 if (Tok.is(tok::greater))
272 break;
273 if (Tok.is(tok::comma)) {
274 ConsumeToken();
275 continue;
276 }
277 }
278 if (Tok.isNot(tok::greater))
279 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000280 SkipUntil(tok::r_paren, false, true); // skip until ')'
281 }
282 }
Richard Smithb12bf692011-10-17 21:20:17 +0000283
284 SourceLocation RParen = Tok.getLocation();
285 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han360d2252012-10-04 16:42:52 +0000286 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithb12bf692011-10-17 21:20:17 +0000287 AttributeList *attr =
Michael Han360d2252012-10-04 16:42:52 +0000288 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen),
Michael Han23214e52012-10-03 01:56:22 +0000289 ScopeName, ScopeLoc, ParmName, ParmLoc,
290 ArgExprs.data(), ArgExprs.size(), Syntax);
Alexis Hunt3bc72c12012-06-19 23:57:03 +0000291 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithb12bf692011-10-17 21:20:17 +0000292 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000293 }
294}
295
Chad Rosierc1183952012-06-26 22:30:43 +0000296/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000297/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000298void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000299 SourceLocation AttrNameLoc,
300 ParsedAttributes &Attrs)
301{
302 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000303 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000304 AttrName->getNameStart(), tok::r_paren))
305 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000306
Aaron Ballman478faed2012-06-19 22:09:27 +0000307 ExprResult ArgExpr(ParseConstantExpression());
308 if (ArgExpr.isInvalid()) {
309 T.skipToEnd();
310 return;
311 }
312 Expr *ExprList = ArgExpr.take();
Chad Rosierc1183952012-06-26 22:30:43 +0000313 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000314 &ExprList, 1, AttributeList::AS_Declspec);
315
316 T.consumeClose();
317}
318
Chad Rosierc1183952012-06-26 22:30:43 +0000319/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000320/// arguments.
321bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
322 return llvm::StringSwitch<bool>(Ident->getName())
323 .Case("dllimport", true)
324 .Case("dllexport", true)
325 .Case("noreturn", true)
326 .Case("nothrow", true)
327 .Case("noinline", true)
328 .Case("naked", true)
329 .Case("appdomain", true)
330 .Case("process", true)
331 .Case("jitintrinsic", true)
332 .Case("noalias", true)
333 .Case("restrict", true)
334 .Case("novtable", true)
335 .Case("selectany", true)
336 .Case("thread", true)
337 .Default(false);
338}
339
Chad Rosierc1183952012-06-26 22:30:43 +0000340/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000341/// parameters). Will return false if we properly handled the declspec, or
342/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000343void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000344 SourceLocation Loc,
345 ParsedAttributes &Attrs) {
346 // Try to handle the easy case first -- these declspecs all take a single
347 // parameter as their argument.
348 if (llvm::StringSwitch<bool>(Ident->getName())
349 .Case("uuid", true)
350 .Case("align", true)
351 .Case("allocate", true)
352 .Default(false)) {
353 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
354 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000355 // The deprecated declspec has an optional single argument, so we will
356 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000357 // not.
358 if (Tok.getKind() == tok::l_paren)
359 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
360 else
Chad Rosierc1183952012-06-26 22:30:43 +0000361 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000362 AttributeList::AS_Declspec);
363 } else if (Ident->getName() == "property") {
364 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000365 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000366 // must be named get or put.
367 //
Chad Rosierc1183952012-06-26 22:30:43 +0000368 // For right now, we will just skip to the closing right paren of the
Aaron Ballman478faed2012-06-19 22:09:27 +0000369 // property expression.
370 //
371 // FIXME: we should deal with __declspec(property) at some point because it
372 // is used in the platform SDK headers for the Parallel Patterns Library
373 // and ATL.
374 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000375 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000376 Ident->getNameStart(), tok::r_paren))
377 return;
378 T.skipToEnd();
379 } else {
380 // We don't recognize this as a valid declspec, but instead of creating the
381 // attribute and allowing sema to warn about it, we will warn here instead.
382 // This is because some attributes have multiple spellings, but we need to
383 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000384 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000385 // both locations.
386 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
387
388 // If there's an open paren, we should eat the open and close parens under
389 // the assumption that this unknown declspec has parameters.
390 BalancedDelimiterTracker T(*this, tok::l_paren);
391 if (!T.consumeOpen())
392 T.skipToEnd();
393 }
394}
395
Eli Friedman06de2b52009-06-08 07:21:15 +0000396/// [MS] decl-specifier:
397/// __declspec ( extended-decl-modifier-seq )
398///
399/// [MS] extended-decl-modifier-seq:
400/// extended-decl-modifier[opt]
401/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000402void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000403 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000404
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000405 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000406 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000407 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000408 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000409 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000410
Chad Rosierc1183952012-06-26 22:30:43 +0000411 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000412 // you can specify multiple attributes per declspec.
413 while (Tok.getKind() != tok::r_paren) {
414 // We expect either a well-known identifier or a generic string. Anything
415 // else is a malformed declspec.
416 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000417 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000418 Tok.getKind() != tok::kw_restrict) {
419 Diag(Tok, diag::err_ms_declspec_type);
420 T.skipToEnd();
421 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000422 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000423
424 IdentifierInfo *AttrName;
425 SourceLocation AttrNameLoc;
426 if (IsString) {
427 SmallString<8> StrBuffer;
428 bool Invalid = false;
429 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
430 if (Invalid) {
431 T.skipToEnd();
432 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000433 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000434 AttrName = PP.getIdentifierInfo(Str);
435 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000436 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000437 AttrName = Tok.getIdentifierInfo();
438 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000439 }
Chad Rosierc1183952012-06-26 22:30:43 +0000440
Aaron Ballman478faed2012-06-19 22:09:27 +0000441 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000442 // If we have a generic string, we will allow it because there is no
443 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000444 // (for instance, SAL declspecs in older versions of MSVC).
445 //
Chad Rosierc1183952012-06-26 22:30:43 +0000446 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000447 // arguments and can be turned into an attribute directly.
Chad Rosierc1183952012-06-26 22:30:43 +0000448 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000449 0, 0, AttributeList::AS_Declspec);
450 else
451 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000452 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000453 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000454}
455
John McCall53fa7142010-12-24 02:08:15 +0000456void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000457 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000458 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000459 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000460 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Chad Rosier6c0da112012-12-21 21:27:13 +0000461 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000462 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
463 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000464 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000465 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Eli Friedman53339e02009-06-08 23:27:34 +0000466 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000467}
468
John McCall53fa7142010-12-24 02:08:15 +0000469void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000470 // Treat these like attributes
471 while (Tok.is(tok::kw___pascal)) {
472 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
473 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000474 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000475 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000476 }
John McCall53fa7142010-12-24 02:08:15 +0000477}
478
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000479void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
480 // Treat these like attributes
481 while (Tok.is(tok::kw___kernel)) {
482 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000483 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
484 AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000485 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000486 }
487}
488
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000489void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
490 SourceLocation Loc = Tok.getLocation();
491 switch(Tok.getKind()) {
492 // OpenCL qualifiers:
493 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000494 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000495 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000496 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000497 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000498 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000499
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000500 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000501 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000502 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000503 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000504 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000505
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000506 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000507 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000508 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000509 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000510 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000511
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000512 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000513 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000514 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000515 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000516 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000517
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000518 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000519 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000520 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000521 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000522 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000523
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000524 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000525 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000526 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000527 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000528 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000529
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000530 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000531 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000532 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000533 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000534 break;
535 default: break;
536 }
537}
538
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000539/// \brief Parse a version number.
540///
541/// version:
542/// simple-integer
543/// simple-integer ',' simple-integer
544/// simple-integer ',' simple-integer ',' simple-integer
545VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
546 Range = Tok.getLocation();
547
548 if (!Tok.is(tok::numeric_constant)) {
549 Diag(Tok, diag::err_expected_version);
550 SkipUntil(tok::comma, tok::r_paren, true, true, true);
551 return VersionTuple();
552 }
553
554 // Parse the major (and possibly minor and subminor) versions, which
555 // are stored in the numeric constant. We utilize a quirk of the
556 // lexer, which is that it handles something like 1.2.3 as a single
557 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000558 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000559 Buffer.resize(Tok.getLength()+1);
560 const char *ThisTokBegin = &Buffer[0];
561
562 // Get the spelling of the token, which eliminates trigraphs, etc.
563 bool Invalid = false;
564 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
565 if (Invalid)
566 return VersionTuple();
567
568 // Parse the major version.
569 unsigned AfterMajor = 0;
570 unsigned Major = 0;
571 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
572 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
573 ++AfterMajor;
574 }
575
576 if (AfterMajor == 0) {
577 Diag(Tok, diag::err_expected_version);
578 SkipUntil(tok::comma, tok::r_paren, true, true, true);
579 return VersionTuple();
580 }
581
582 if (AfterMajor == ActualLength) {
583 ConsumeToken();
584
585 // We only had a single version component.
586 if (Major == 0) {
587 Diag(Tok, diag::err_zero_version);
588 return VersionTuple();
589 }
590
591 return VersionTuple(Major);
592 }
593
594 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
595 Diag(Tok, diag::err_expected_version);
596 SkipUntil(tok::comma, tok::r_paren, true, true, true);
597 return VersionTuple();
598 }
599
600 // Parse the minor version.
601 unsigned AfterMinor = AfterMajor + 1;
602 unsigned Minor = 0;
603 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
604 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
605 ++AfterMinor;
606 }
607
608 if (AfterMinor == ActualLength) {
609 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000610
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000611 // We had major.minor.
612 if (Major == 0 && Minor == 0) {
613 Diag(Tok, diag::err_zero_version);
614 return VersionTuple();
615 }
616
Chad Rosierc1183952012-06-26 22:30:43 +0000617 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000618 }
619
620 // If what follows is not a '.', we have a problem.
621 if (ThisTokBegin[AfterMinor] != '.') {
622 Diag(Tok, diag::err_expected_version);
623 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosierc1183952012-06-26 22:30:43 +0000624 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000625 }
626
627 // Parse the subminor version.
628 unsigned AfterSubminor = AfterMinor + 1;
629 unsigned Subminor = 0;
630 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
631 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
632 ++AfterSubminor;
633 }
634
635 if (AfterSubminor != ActualLength) {
636 Diag(Tok, diag::err_expected_version);
637 SkipUntil(tok::comma, tok::r_paren, true, true, true);
638 return VersionTuple();
639 }
640 ConsumeToken();
641 return VersionTuple(Major, Minor, Subminor);
642}
643
644/// \brief Parse the contents of the "availability" attribute.
645///
646/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000647/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000648///
649/// platform:
650/// identifier
651///
652/// version-arg-list:
653/// version-arg
654/// version-arg ',' version-arg-list
655///
656/// version-arg:
657/// 'introduced' '=' version
658/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000659/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000660/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000661/// opt-message:
662/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000663void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
664 SourceLocation AvailabilityLoc,
665 ParsedAttributes &attrs,
666 SourceLocation *endLoc) {
667 SourceLocation PlatformLoc;
668 IdentifierInfo *Platform = 0;
669
670 enum { Introduced, Deprecated, Obsoleted, Unknown };
671 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000672 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000673
674 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000675 BalancedDelimiterTracker T(*this, tok::l_paren);
676 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000677 Diag(Tok, diag::err_expected_lparen);
678 return;
679 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000680
681 // Parse the platform name,
682 if (Tok.isNot(tok::identifier)) {
683 Diag(Tok, diag::err_availability_expected_platform);
684 SkipUntil(tok::r_paren);
685 return;
686 }
687 Platform = Tok.getIdentifierInfo();
688 PlatformLoc = ConsumeToken();
689
690 // Parse the ',' following the platform name.
691 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
692 return;
693
694 // If we haven't grabbed the pointers for the identifiers
695 // "introduced", "deprecated", and "obsoleted", do so now.
696 if (!Ident_introduced) {
697 Ident_introduced = PP.getIdentifierInfo("introduced");
698 Ident_deprecated = PP.getIdentifierInfo("deprecated");
699 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000700 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000701 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000702 }
703
704 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000705 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000706 do {
707 if (Tok.isNot(tok::identifier)) {
708 Diag(Tok, diag::err_availability_expected_change);
709 SkipUntil(tok::r_paren);
710 return;
711 }
712 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
713 SourceLocation KeywordLoc = ConsumeToken();
714
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000715 if (Keyword == Ident_unavailable) {
716 if (UnavailableLoc.isValid()) {
717 Diag(KeywordLoc, diag::err_availability_redundant)
718 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000719 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000720 UnavailableLoc = KeywordLoc;
721
722 if (Tok.isNot(tok::comma))
723 break;
724
725 ConsumeToken();
726 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000727 }
728
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000729 if (Tok.isNot(tok::equal)) {
730 Diag(Tok, diag::err_expected_equal_after)
731 << Keyword;
732 SkipUntil(tok::r_paren);
733 return;
734 }
735 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000736 if (Keyword == Ident_message) {
737 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000738 Diag(Tok, diag::err_expected_string_literal)
739 << /*Source='availability attribute'*/2;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000740 SkipUntil(tok::r_paren);
741 return;
742 }
743 MessageExpr = ParseStringLiteralExpression();
744 break;
745 }
Chad Rosierc1183952012-06-26 22:30:43 +0000746
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000747 SourceRange VersionRange;
748 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000749
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000750 if (Version.empty()) {
751 SkipUntil(tok::r_paren);
752 return;
753 }
754
755 unsigned Index;
756 if (Keyword == Ident_introduced)
757 Index = Introduced;
758 else if (Keyword == Ident_deprecated)
759 Index = Deprecated;
760 else if (Keyword == Ident_obsoleted)
761 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000762 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000763 Index = Unknown;
764
765 if (Index < Unknown) {
766 if (!Changes[Index].KeywordLoc.isInvalid()) {
767 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000768 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000769 << SourceRange(Changes[Index].KeywordLoc,
770 Changes[Index].VersionRange.getEnd());
771 }
772
773 Changes[Index].KeywordLoc = KeywordLoc;
774 Changes[Index].Version = Version;
775 Changes[Index].VersionRange = VersionRange;
776 } else {
777 Diag(KeywordLoc, diag::err_availability_unknown_change)
778 << Keyword << VersionRange;
779 }
780
781 if (Tok.isNot(tok::comma))
782 break;
783
784 ConsumeToken();
785 } while (true);
786
787 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000788 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000789 return;
790
791 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000792 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000793
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000794 // The 'unavailable' availability cannot be combined with any other
795 // availability changes. Make sure that hasn't happened.
796 if (UnavailableLoc.isValid()) {
797 bool Complained = false;
798 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
799 if (Changes[Index].KeywordLoc.isValid()) {
800 if (!Complained) {
801 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
802 << SourceRange(Changes[Index].KeywordLoc,
803 Changes[Index].VersionRange.getEnd());
804 Complained = true;
805 }
806
807 // Clear out the availability.
808 Changes[Index] = AvailabilityChange();
809 }
810 }
811 }
812
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000813 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000814 attrs.addNew(&Availability,
815 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000816 0, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000817 Platform, PlatformLoc,
818 Changes[Introduced],
819 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000820 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000821 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000822 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000823}
824
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000825
Bill Wendling44426052012-12-20 19:22:21 +0000826// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000827// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
828
829void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
830
831void Parser::LateParsedClass::ParseLexedAttributes() {
832 Self->ParseLexedAttributes(*Class);
833}
834
835void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000836 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000837}
838
839/// Wrapper class which calls ParseLexedAttribute, after setting up the
840/// scope appropriately.
841void Parser::ParseLexedAttributes(ParsingClass &Class) {
842 // Deal with templates
843 // FIXME: Test cases to make sure this does the right thing for templates.
844 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
845 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
846 HasTemplateScope);
847 if (HasTemplateScope)
848 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
849
Douglas Gregor3024f072012-04-16 07:05:22 +0000850 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000851 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000852 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000853 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
854 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
855
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000856 // Enter the scope of nested classes
857 if (!AlreadyHasClassScope)
858 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
859 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +0000860 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +0000861 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
862 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
863 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000864 }
Chad Rosierc1183952012-06-26 22:30:43 +0000865
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000866 if (!AlreadyHasClassScope)
867 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
868 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000869}
870
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000871
872/// \brief Parse all attributes in LAs, and attach them to Decl D.
873void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
874 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000875 assert(LAs.parseSoon() &&
876 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000877 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +0000878 if (D)
879 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000880 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +0000881 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000882 }
883 LAs.clear();
884}
885
886
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000887/// \brief Finish parsing an attribute for which parsing was delayed.
888/// This will be called at the end of parsing a class declaration
889/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +0000890/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000891/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000892void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
893 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000894 // Save the current token position.
895 SourceLocation OrigLoc = Tok.getLocation();
896
897 // Append the current token at the end of the new token stream so that it
898 // doesn't get lost.
899 LA.Toks.push_back(Tok);
900 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
901 // Consume the previously pushed token.
902 ConsumeAnyToken();
903
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000904 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +0000905 // FIXME: Do not warn on C++11 attributes, once we start supporting
906 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000907 Diag(Tok, diag::warn_attribute_on_function_definition)
908 << LA.AttrName.getName();
909 }
910
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000911 ParsedAttributes Attrs(AttrFactory);
912 SourceLocation endLoc;
913
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000914 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000915 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000916 NamedDecl *ND = dyn_cast<NamedDecl>(D);
917 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000918
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000919 // Allow 'this' within late-parsed attributes.
920 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
921 /*TypeQuals=*/0,
922 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000923
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000924 if (LA.Decls.size() == 1) {
925 // If the Decl is templatized, add template parameters to scope.
926 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
927 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
928 if (HasTemplateScope)
929 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000930
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000931 // If the Decl is on a function, add function parameters to the scope.
932 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
933 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
934 if (HasFunScope)
935 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000936
Michael Han23214e52012-10-03 01:56:22 +0000937 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000938 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000939
940 if (HasFunScope) {
941 Actions.ActOnExitFunctionContext();
942 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
943 }
944 if (HasTemplateScope) {
945 TempScope.Exit();
946 }
947 } else {
948 // If there are multiple decls, then the decl cannot be within the
949 // function scope.
Michael Han23214e52012-10-03 01:56:22 +0000950 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000951 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000952 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +0000953 } else {
954 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000955 }
956
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000957 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
958 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
959 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000960
961 if (Tok.getLocation() != OrigLoc) {
962 // Due to a parsing error, we either went over the cached tokens or
963 // there are still cached tokens left, so we skip the leftover tokens.
964 // Since this is an uncommon situation that should be avoided, use the
965 // expensive isBeforeInTranslationUnit call.
966 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
967 OrigLoc))
968 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +0000969 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000970 }
971}
972
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000973/// \brief Wrapper around a case statement checking if AttrName is
974/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000975bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000976 return llvm::StringSwitch<bool>(AttrName)
977 .Case("guarded_by", true)
978 .Case("guarded_var", true)
979 .Case("pt_guarded_by", true)
980 .Case("pt_guarded_var", true)
981 .Case("lockable", true)
982 .Case("scoped_lockable", true)
983 .Case("no_thread_safety_analysis", true)
984 .Case("acquired_after", true)
985 .Case("acquired_before", true)
986 .Case("exclusive_lock_function", true)
987 .Case("shared_lock_function", true)
988 .Case("exclusive_trylock_function", true)
989 .Case("shared_trylock_function", true)
990 .Case("unlock_function", true)
991 .Case("lock_returned", true)
992 .Case("locks_excluded", true)
993 .Case("exclusive_locks_required", true)
994 .Case("shared_locks_required", true)
995 .Default(false);
996}
997
998/// \brief Parse the contents of thread safety attributes. These
999/// should always be parsed as an expression list.
1000///
1001/// We need to special case the parsing due to the fact that if the first token
1002/// of the first argument is an identifier, the main parse loop will store
1003/// that token as a "parameter" and the rest of
1004/// the arguments will be added to a list of "arguments". However,
1005/// subsequent tokens in the first argument are lost. We instead parse each
1006/// argument as an expression and add all arguments to the list of "arguments".
1007/// In future, we will take advantage of this special case to also
1008/// deal with some argument scoping issues here (for example, referring to a
1009/// function parameter in the attribute on that function).
1010void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1011 SourceLocation AttrNameLoc,
1012 ParsedAttributes &Attrs,
1013 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001014 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001015
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001016 BalancedDelimiterTracker T(*this, tok::l_paren);
1017 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001018
Benjamin Kramerf0623432012-08-23 22:51:59 +00001019 ExprVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001020 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001021
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001022 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001023 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001024 ExprResult ArgExpr(ParseAssignmentExpression());
1025 if (ArgExpr.isInvalid()) {
1026 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001027 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001028 break;
1029 } else {
1030 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001031 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001032 if (Tok.isNot(tok::comma))
1033 break;
1034 ConsumeToken(); // Eat the comma, move to the next argument
1035 }
1036 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001037 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001038 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramerf0623432012-08-23 22:51:59 +00001039 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001040 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001041 if (EndLoc)
1042 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001043}
1044
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001045void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1046 SourceLocation AttrNameLoc,
1047 ParsedAttributes &Attrs,
1048 SourceLocation *EndLoc) {
1049 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1050
1051 BalancedDelimiterTracker T(*this, tok::l_paren);
1052 T.consumeOpen();
1053
1054 if (Tok.isNot(tok::identifier)) {
1055 Diag(Tok, diag::err_expected_ident);
1056 T.skipToEnd();
1057 return;
1058 }
1059 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1060 SourceLocation ArgumentKindLoc = ConsumeToken();
1061
1062 if (Tok.isNot(tok::comma)) {
1063 Diag(Tok, diag::err_expected_comma);
1064 T.skipToEnd();
1065 return;
1066 }
1067 ConsumeToken();
1068
1069 SourceRange MatchingCTypeRange;
1070 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1071 if (MatchingCType.isInvalid()) {
1072 T.skipToEnd();
1073 return;
1074 }
1075
1076 bool LayoutCompatible = false;
1077 bool MustBeNull = false;
1078 while (Tok.is(tok::comma)) {
1079 ConsumeToken();
1080 if (Tok.isNot(tok::identifier)) {
1081 Diag(Tok, diag::err_expected_ident);
1082 T.skipToEnd();
1083 return;
1084 }
1085 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1086 if (Flag->isStr("layout_compatible"))
1087 LayoutCompatible = true;
1088 else if (Flag->isStr("must_be_null"))
1089 MustBeNull = true;
1090 else {
1091 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1092 T.skipToEnd();
1093 return;
1094 }
1095 ConsumeToken(); // consume flag
1096 }
1097
1098 if (!T.consumeClose()) {
1099 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1100 ArgumentKind, ArgumentKindLoc,
1101 MatchingCType.release(), LayoutCompatible,
1102 MustBeNull, AttributeList::AS_GNU);
1103 }
1104
1105 if (EndLoc)
1106 *EndLoc = T.getCloseLocation();
1107}
1108
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001109/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1110/// of a C++11 attribute-specifier in a location where an attribute is not
1111/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1112/// situation.
1113///
1114/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1115/// this doesn't appear to actually be an attribute-specifier, and the caller
1116/// should try to parse it.
1117bool Parser::DiagnoseProhibitedCXX11Attribute() {
1118 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1119
1120 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1121 case CAK_NotAttributeSpecifier:
1122 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1123 return false;
1124
1125 case CAK_InvalidAttributeSpecifier:
1126 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1127 return false;
1128
1129 case CAK_AttributeSpecifier:
1130 // Parse and discard the attributes.
1131 SourceLocation BeginLoc = ConsumeBracket();
1132 ConsumeBracket();
1133 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1134 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1135 SourceLocation EndLoc = ConsumeBracket();
1136 Diag(BeginLoc, diag::err_attributes_not_allowed)
1137 << SourceRange(BeginLoc, EndLoc);
1138 return true;
1139 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001140 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001141}
1142
John McCall53fa7142010-12-24 02:08:15 +00001143void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1144 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1145 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001146}
1147
Michael Han64536a62012-11-06 19:34:54 +00001148void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1149 AttributeList *AttrList = attrs.getList();
1150 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001151 if (AttrList->isCXX11Attribute()) {
Michael Han64536a62012-11-06 19:34:54 +00001152 Diag(AttrList->getLoc(), diag::warn_attribute_no_decl)
1153 << AttrList->getName();
1154 AttrList->setInvalid();
1155 }
1156 AttrList = AttrList->getNext();
1157 }
1158}
1159
Chris Lattner53361ac2006-08-10 05:19:57 +00001160/// ParseDeclaration - Parse a full 'declaration', which consists of
1161/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001162/// 'Context' should be a Declarator::TheContext value. This returns the
1163/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001164///
1165/// declaration: [C99 6.7]
1166/// block-declaration ->
1167/// simple-declaration
1168/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001169/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001170/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001171/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001172/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001173/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001174/// others... [FIXME]
1175///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001176Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1177 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001178 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001179 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001180 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001181 // Must temporarily exit the objective-c container scope for
1182 // parsing c none objective-c decls.
1183 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001184
John McCall48871652010-08-21 09:40:31 +00001185 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001186 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001187 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001188 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001189 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001190 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001191 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001192 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001193 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001194 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001195 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001196 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001197 SourceLocation InlineLoc = ConsumeToken();
1198 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1199 break;
1200 }
Chad Rosierc1183952012-06-26 22:30:43 +00001201 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001202 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001203 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001204 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001205 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001206 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001207 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001208 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001209 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001210 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001211 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001212 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001213 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001214 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001215 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001216 default:
John McCall53fa7142010-12-24 02:08:15 +00001217 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001218 }
Chad Rosierc1183952012-06-26 22:30:43 +00001219
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001220 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001221 // single decl, convert it now. Alias declarations can also declare a type;
1222 // include that too if it is present.
1223 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001224}
1225
1226/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1227/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001228/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1229/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001230///[C90/C++]init-declarator-list ';' [TODO]
1231/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001232///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001233/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001234/// attribute-specifier-seq[opt] type-specifier-seq declarator
1235///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001236/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001237/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001238///
1239/// If FRI is non-null, we might be parsing a for-range-declaration instead
1240/// of a simple-declaration. If we find that we are, we also parse the
1241/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001242Parser::DeclGroupPtrTy
1243Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1244 SourceLocation &DeclEnd,
1245 ParsedAttributesWithRange &attrs,
1246 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001247 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001248 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +00001249 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001250
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001251 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001252 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001253
Chris Lattner0e894622006-08-13 19:58:17 +00001254 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1255 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001256 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001257 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001258 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001259 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001260 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001261 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001262 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001263 }
Chad Rosierc1183952012-06-26 22:30:43 +00001264
1265 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001266}
Mike Stump11289f42009-09-09 15:08:12 +00001267
Richard Smith09f76ee2011-10-19 21:33:05 +00001268/// Returns true if this might be the start of a declarator, or a common typo
1269/// for a declarator.
1270bool Parser::MightBeDeclarator(unsigned Context) {
1271 switch (Tok.getKind()) {
1272 case tok::annot_cxxscope:
1273 case tok::annot_template_id:
1274 case tok::caret:
1275 case tok::code_completion:
1276 case tok::coloncolon:
1277 case tok::ellipsis:
1278 case tok::kw___attribute:
1279 case tok::kw_operator:
1280 case tok::l_paren:
1281 case tok::star:
1282 return true;
1283
1284 case tok::amp:
1285 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001286 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001287
Richard Smithc8a79032012-01-09 22:31:44 +00001288 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001289 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001290 NextToken().is(tok::l_square);
1291
1292 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001293 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001294
Richard Smith09f76ee2011-10-19 21:33:05 +00001295 case tok::identifier:
1296 switch (NextToken().getKind()) {
1297 case tok::code_completion:
1298 case tok::coloncolon:
1299 case tok::comma:
1300 case tok::equal:
1301 case tok::equalequal: // Might be a typo for '='.
1302 case tok::kw_alignas:
1303 case tok::kw_asm:
1304 case tok::kw___attribute:
1305 case tok::l_brace:
1306 case tok::l_paren:
1307 case tok::l_square:
1308 case tok::less:
1309 case tok::r_brace:
1310 case tok::r_paren:
1311 case tok::r_square:
1312 case tok::semi:
1313 return true;
1314
1315 case tok::colon:
1316 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001317 // and in block scope it's probably a label. Inside a class definition,
1318 // this is a bit-field.
1319 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001320 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001321
1322 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001323 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001324
1325 default:
1326 return false;
1327 }
1328
1329 default:
1330 return false;
1331 }
1332}
1333
Richard Smithb8caac82012-04-11 20:59:20 +00001334/// Skip until we reach something which seems like a sensible place to pick
1335/// up parsing after a malformed declaration. This will sometimes stop sooner
1336/// than SkipUntil(tok::r_brace) would, but will never stop later.
1337void Parser::SkipMalformedDecl() {
1338 while (true) {
1339 switch (Tok.getKind()) {
1340 case tok::l_brace:
1341 // Skip until matching }, then stop. We've probably skipped over
1342 // a malformed class or function definition or similar.
1343 ConsumeBrace();
1344 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1345 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1346 // This declaration isn't over yet. Keep skipping.
1347 continue;
1348 }
1349 if (Tok.is(tok::semi))
1350 ConsumeToken();
1351 return;
1352
1353 case tok::l_square:
1354 ConsumeBracket();
1355 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1356 continue;
1357
1358 case tok::l_paren:
1359 ConsumeParen();
1360 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1361 continue;
1362
1363 case tok::r_brace:
1364 return;
1365
1366 case tok::semi:
1367 ConsumeToken();
1368 return;
1369
1370 case tok::kw_inline:
1371 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001372 // a good place to pick back up parsing, except in an Objective-C
1373 // @interface context.
1374 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1375 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001376 return;
1377 break;
1378
1379 case tok::kw_namespace:
1380 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001381 // place to pick back up parsing, except in an Objective-C
1382 // @interface context.
1383 if (Tok.isAtStartOfLine() &&
1384 (!ParsingInObjCContainer || CurParsedObjCImpl))
1385 return;
1386 break;
1387
1388 case tok::at:
1389 // @end is very much like } in Objective-C contexts.
1390 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1391 ParsingInObjCContainer)
1392 return;
1393 break;
1394
1395 case tok::minus:
1396 case tok::plus:
1397 // - and + probably start new method declarations in Objective-C contexts.
1398 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001399 return;
1400 break;
1401
1402 case tok::eof:
1403 return;
1404
1405 default:
1406 break;
1407 }
1408
1409 ConsumeAnyToken();
1410 }
1411}
1412
John McCalld5a36322009-11-03 19:26:08 +00001413/// ParseDeclGroup - Having concluded that this is either a function
1414/// definition or a group of object declarations, actually parse the
1415/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001416Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1417 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001418 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001419 SourceLocation *DeclEnd,
1420 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001421 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001422 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001423 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001424
John McCalld5a36322009-11-03 19:26:08 +00001425 // Bail out if the first declarator didn't seem well-formed.
1426 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001427 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001428 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001429 }
Mike Stump11289f42009-09-09 15:08:12 +00001430
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001431 // Save late-parsed attributes for now; they need to be parsed in the
1432 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001433 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1434 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001435 if (D.isFunctionDeclarator())
1436 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1437
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001438 // Check to see if we have a function *definition* which must have a body.
1439 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1440 // Look at the next token to make sure that this isn't a function
1441 // declaration. We have to check this because __attribute__ might be the
1442 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001443 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001444
Chris Lattner13901342010-07-11 22:42:07 +00001445 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +00001446 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1447 Diag(Tok, diag::err_function_declared_typedef);
1448
1449 // Recover by treating the 'typedef' as spurious.
1450 DS.ClearStorageClassSpecs();
1451 }
1452
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001453 Decl *TheDecl =
1454 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld5a36322009-11-03 19:26:08 +00001455 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +00001456 }
Chad Rosierc1183952012-06-26 22:30:43 +00001457
Chris Lattner13901342010-07-11 22:42:07 +00001458 if (isDeclarationSpecifier()) {
1459 // If there is an invalid declaration specifier right after the function
1460 // prototype, then we must be in a missing semicolon case where this isn't
1461 // actually a body. Just fall through into the code that handles it as a
1462 // prototype, and let the top-level code handle the erroneous declspec
1463 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +00001464 } else {
1465 Diag(Tok, diag::err_expected_fn_body);
1466 SkipUntil(tok::semi);
1467 return DeclGroupPtrTy();
1468 }
1469 }
1470
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001471 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001472 return DeclGroupPtrTy();
1473
1474 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1475 // must parse and analyze the for-range-initializer before the declaration is
1476 // analyzed.
1477 if (FRI && Tok.is(tok::colon)) {
1478 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +00001479 if (Tok.is(tok::l_brace))
1480 FRI->RangeExpr = ParseBraceInitializer();
1481 else
1482 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +00001483 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1484 Actions.ActOnCXXForRangeDecl(ThisDecl);
1485 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001486 D.complete(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001487 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1488 }
1489
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001490 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001491 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001492 if (LateParsedAttrs.size() > 0)
1493 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001494 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001495 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001496 DeclsInGroup.push_back(FirstDecl);
1497
Richard Smith09f76ee2011-10-19 21:33:05 +00001498 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001499
John McCalld5a36322009-11-03 19:26:08 +00001500 // If we don't have a comma, it is either the end of the list (a ';') or an
1501 // error, bail out.
1502 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001503 SourceLocation CommaLoc = ConsumeToken();
1504
1505 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1506 // This comma was followed by a line-break and something which can't be
1507 // the start of a declarator. The comma was probably a typo for a
1508 // semicolon.
1509 Diag(CommaLoc, diag::err_expected_semi_declaration)
1510 << FixItHint::CreateReplacement(CommaLoc, ";");
1511 ExpectSemi = false;
1512 break;
1513 }
John McCalld5a36322009-11-03 19:26:08 +00001514
1515 // Parse the next declarator.
1516 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001517 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001518
1519 // Accept attributes in an init-declarator. In the first declarator in a
1520 // declaration, these would be part of the declspec. In subsequent
1521 // declarators, they become part of the declarator itself, so that they
1522 // don't apply to declarators after *this* one. Examples:
1523 // short __attribute__((common)) var; -> declspec
1524 // short var __attribute__((common)); -> declarator
1525 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001526 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001527
1528 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001529 if (!D.isInvalidType()) {
1530 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1531 D.complete(ThisDecl);
1532 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001533 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001534 }
John McCalld5a36322009-11-03 19:26:08 +00001535 }
1536
1537 if (DeclEnd)
1538 *DeclEnd = Tok.getLocation();
1539
Richard Smith09f76ee2011-10-19 21:33:05 +00001540 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001541 ExpectAndConsumeSemi(Context == Declarator::FileContext
1542 ? diag::err_invalid_token_after_toplevel_declarator
1543 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001544 // Okay, there was no semicolon and one was expected. If we see a
1545 // declaration specifier, just assume it was missing and continue parsing.
1546 // Otherwise things are very confused and we skip to recover.
1547 if (!isDeclarationSpecifier()) {
1548 SkipUntil(tok::r_brace, true, true);
1549 if (Tok.is(tok::semi))
1550 ConsumeToken();
1551 }
John McCalld5a36322009-11-03 19:26:08 +00001552 }
1553
Douglas Gregor0be31a22010-07-02 17:43:08 +00001554 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001555 DeclsInGroup.data(),
1556 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001557}
1558
Richard Smith02e85f32011-04-14 22:09:26 +00001559/// Parse an optional simple-asm-expr and attributes, and attach them to a
1560/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001561bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001562 // If a simple-asm-expr is present, parse it.
1563 if (Tok.is(tok::kw_asm)) {
1564 SourceLocation Loc;
1565 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1566 if (AsmLabel.isInvalid()) {
1567 SkipUntil(tok::semi, true, true);
1568 return true;
1569 }
1570
1571 D.setAsmLabel(AsmLabel.release());
1572 D.SetRangeEnd(Loc);
1573 }
1574
1575 MaybeParseGNUAttributes(D);
1576 return false;
1577}
1578
Douglas Gregor23996282009-05-12 21:31:51 +00001579/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1580/// declarator'. This method parses the remainder of the declaration
1581/// (including any attributes or initializer, among other things) and
1582/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001583///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001584/// init-declarator: [C99 6.7]
1585/// declarator
1586/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001587/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1588/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001589/// [C++] declarator initializer[opt]
1590///
1591/// [C++] initializer:
1592/// [C++] '=' initializer-clause
1593/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001594/// [C++0x] '=' 'default' [TODO]
1595/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001596/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001597///
1598/// According to the standard grammar, =default and =delete are function
1599/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001600///
John McCall48871652010-08-21 09:40:31 +00001601Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001602 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001603 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001604 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001605
Richard Smith02e85f32011-04-14 22:09:26 +00001606 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1607}
Mike Stump11289f42009-09-09 15:08:12 +00001608
Richard Smith02e85f32011-04-14 22:09:26 +00001609Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1610 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001611 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001612 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001613 switch (TemplateInfo.Kind) {
1614 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001615 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001616 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001617
Douglas Gregor450f00842009-09-25 18:43:00 +00001618 case ParsedTemplateInfo::Template:
1619 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001620 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001621 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001622 D);
1623 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001624
Douglas Gregor450f00842009-09-25 18:43:00 +00001625 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosierc1183952012-06-26 22:30:43 +00001626 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001627 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001628 TemplateInfo.ExternLoc,
1629 TemplateInfo.TemplateLoc,
1630 D);
1631 if (ThisRes.isInvalid()) {
1632 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001633 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001634 }
Chad Rosierc1183952012-06-26 22:30:43 +00001635
Douglas Gregor450f00842009-09-25 18:43:00 +00001636 ThisDecl = ThisRes.get();
1637 break;
1638 }
1639 }
Mike Stump11289f42009-09-09 15:08:12 +00001640
Richard Smith30482bc2011-02-20 03:19:35 +00001641 bool TypeContainsAuto =
1642 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1643
Douglas Gregor23996282009-05-12 21:31:51 +00001644 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001645 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001646 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001647 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001648 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001649 if (D.isFunctionDeclarator())
1650 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1651 << 1 /* delete */;
1652 else
1653 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001654 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001655 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001656 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1657 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001658 else
1659 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001660 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001661 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001662 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001663 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001664 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001665
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001666 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001667 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001668 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001669 cutOffParsing();
1670 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001671 }
Chad Rosierc1183952012-06-26 22:30:43 +00001672
John McCalldadc5752010-08-24 06:29:42 +00001673 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001674
David Blaikiebbafb8a2012-03-11 07:00:24 +00001675 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001676 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001677 ExitScope();
1678 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001679
Douglas Gregor23996282009-05-12 21:31:51 +00001680 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001681 SkipUntil(tok::comma, true, true);
1682 Actions.ActOnInitializerError(ThisDecl);
1683 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001684 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1685 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001686 }
1687 } else if (Tok.is(tok::l_paren)) {
1688 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001689 BalancedDelimiterTracker T(*this, tok::l_paren);
1690 T.consumeOpen();
1691
Benjamin Kramerf0623432012-08-23 22:51:59 +00001692 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001693 CommaLocsTy CommaLocs;
1694
David Blaikiebbafb8a2012-03-11 07:00:24 +00001695 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001696 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001697 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001698 }
1699
Douglas Gregor23996282009-05-12 21:31:51 +00001700 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001701 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00001702 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001703
David Blaikiebbafb8a2012-03-11 07:00:24 +00001704 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001705 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001706 ExitScope();
1707 }
Douglas Gregor23996282009-05-12 21:31:51 +00001708 } else {
1709 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001710 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001711
1712 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1713 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001714
David Blaikiebbafb8a2012-03-11 07:00:24 +00001715 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001716 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001717 ExitScope();
1718 }
1719
Sebastian Redla9351792012-02-11 23:51:47 +00001720 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1721 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001722 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001723 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1724 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001725 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001726 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001727 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001728 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001729 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1730
Sebastian Redl3da34892011-06-05 12:23:16 +00001731 if (D.getCXXScopeSpec().isSet()) {
1732 EnterScope(0);
1733 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1734 }
1735
1736 ExprResult Init(ParseBraceInitializer());
1737
1738 if (D.getCXXScopeSpec().isSet()) {
1739 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1740 ExitScope();
1741 }
1742
1743 if (Init.isInvalid()) {
1744 Actions.ActOnInitializerError(ThisDecl);
1745 } else
1746 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1747 /*DirectInit=*/true, TypeContainsAuto);
1748
Douglas Gregor23996282009-05-12 21:31:51 +00001749 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001750 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001751 }
1752
Richard Smithb2bc2e62011-02-21 20:05:19 +00001753 Actions.FinalizeDeclaration(ThisDecl);
1754
Douglas Gregor23996282009-05-12 21:31:51 +00001755 return ThisDecl;
1756}
1757
Chris Lattner1890ac82006-08-13 01:16:23 +00001758/// ParseSpecifierQualifierList
1759/// specifier-qualifier-list:
1760/// type-specifier specifier-qualifier-list[opt]
1761/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001762/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001763///
Richard Smithc5b05522012-03-12 07:56:15 +00001764void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1765 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001766 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1767 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001768 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001769 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001770
Chris Lattner1890ac82006-08-13 01:16:23 +00001771 // Validate declspec for type-name.
1772 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001773 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1774 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001775 Diag(Tok, diag::err_expected_type);
1776 DS.SetTypeSpecError();
1777 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1778 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001779 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001780 if (!DS.hasTypeSpecifier())
1781 DS.SetTypeSpecError();
1782 }
Mike Stump11289f42009-09-09 15:08:12 +00001783
Chris Lattner1b22eed2006-11-28 05:12:07 +00001784 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001785 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001786 if (DS.getStorageClassSpecLoc().isValid())
1787 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1788 else
1789 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001790 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001791 }
Mike Stump11289f42009-09-09 15:08:12 +00001792
Chris Lattner1b22eed2006-11-28 05:12:07 +00001793 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001794 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001795 if (DS.isInlineSpecified())
1796 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1797 if (DS.isVirtualSpecified())
1798 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1799 if (DS.isExplicitSpecified())
1800 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001801 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001802 }
Richard Smithc5b05522012-03-12 07:56:15 +00001803
1804 // Issue diagnostic and remove constexpr specfier if present.
1805 if (DS.isConstexprSpecified()) {
1806 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1807 DS.ClearConstexprSpec();
1808 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001809}
Chris Lattner53361ac2006-08-10 05:19:57 +00001810
Chris Lattner6cc055a2009-04-12 20:42:31 +00001811/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1812/// specified token is valid after the identifier in a declarator which
1813/// immediately follows the declspec. For example, these things are valid:
1814///
1815/// int x [ 4]; // direct-declarator
1816/// int x ( int y); // direct-declarator
1817/// int(int x ) // direct-declarator
1818/// int x ; // simple-declaration
1819/// int x = 17; // init-declarator-list
1820/// int x , y; // init-declarator-list
1821/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001822/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001823/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001824///
1825/// This is not, because 'x' does not immediately follow the declspec (though
1826/// ')' happens to be valid anyway).
1827/// int (x)
1828///
1829static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1830 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1831 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001832 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001833}
1834
Chris Lattner20a0c612009-04-14 21:34:55 +00001835
1836/// ParseImplicitInt - This method is called when we have an non-typename
1837/// identifier in a declspec (which normally terminates the decl spec) when
1838/// the declspec has no type specifier. In this case, the declspec is either
1839/// malformed or is "implicit int" (in K&R and C89).
1840///
1841/// This method handles diagnosing this prettily and returns false if the
1842/// declspec is done being processed. If it recovers and thinks there may be
1843/// other pieces of declspec after it, it returns true.
1844///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001845bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001846 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00001847 AccessSpecifier AS, DeclSpecContext DSC,
1848 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001849 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001850
Chris Lattner20a0c612009-04-14 21:34:55 +00001851 SourceLocation Loc = Tok.getLocation();
1852 // If we see an identifier that is not a type name, we normally would
1853 // parse it as the identifer being declared. However, when a typename
1854 // is typo'd or the definition is not included, this will incorrectly
1855 // parse the typename as the identifier name and fall over misparsing
1856 // later parts of the diagnostic.
1857 //
1858 // As such, we try to do some look-ahead in cases where this would
1859 // otherwise be an "implicit-int" case to see if this is invalid. For
1860 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1861 // an identifier with implicit int, we'd get a parse error because the
1862 // next token is obviously invalid for a type. Parse these as a case
1863 // with an invalid type specifier.
1864 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001865
Chris Lattner20a0c612009-04-14 21:34:55 +00001866 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00001867 // error, do lookahead to try to do better recovery. This never applies
1868 // within a type specifier. Outside of C++, we allow this even if the
1869 // language doesn't "officially" support implicit int -- we support
1870 // implicit int as an extension in C99 and C11. Allegedly, MS also
1871 // supports implicit int in C++ mode.
Richard Smith2f07ad52012-05-09 20:55:26 +00001872 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smitha952ebb2012-05-15 21:01:51 +00001873 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smithc5b05522012-03-12 07:56:15 +00001874 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001875 // If this token is valid for implicit int, e.g. "static x = 4", then
1876 // we just avoid eating the identifier, so it will be parsed as the
1877 // identifier in the declarator.
1878 return false;
1879 }
Mike Stump11289f42009-09-09 15:08:12 +00001880
Richard Smitha952ebb2012-05-15 21:01:51 +00001881 if (getLangOpts().CPlusPlus &&
1882 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1883 // Don't require a type specifier if we have the 'auto' storage class
1884 // specifier in C++98 -- we'll promote it to a type specifier.
1885 return false;
1886 }
1887
Chris Lattner20a0c612009-04-14 21:34:55 +00001888 // Otherwise, if we don't consume this token, we are going to emit an
1889 // error anyway. Try to recover from various common problems. Check
1890 // to see if this was a reference to a tag name without a tag specified.
1891 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001892 //
1893 // C++ doesn't need this, and isTagName doesn't take SS.
1894 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001895 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001896 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001897
Douglas Gregor0be31a22010-07-02 17:43:08 +00001898 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001899 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001900 case DeclSpec::TST_enum:
1901 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1902 case DeclSpec::TST_union:
1903 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1904 case DeclSpec::TST_struct:
1905 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00001906 case DeclSpec::TST_interface:
1907 TagName="__interface"; FixitTagName = "__interface ";
1908 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001909 case DeclSpec::TST_class:
1910 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001911 }
Mike Stump11289f42009-09-09 15:08:12 +00001912
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001913 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001914 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1915 LookupResult R(Actions, TokenName, SourceLocation(),
1916 Sema::LookupOrdinaryName);
1917
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001918 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001919 << TokenName << TagName << getLangOpts().CPlusPlus
1920 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1921
1922 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1923 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1924 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00001925 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001926 << TokenName << TagName;
1927 }
Mike Stump11289f42009-09-09 15:08:12 +00001928
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001929 // Parse this as a tag as if the missing tag were present.
1930 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00001931 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001932 else
Richard Smithc5b05522012-03-12 07:56:15 +00001933 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00001934 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001935 return true;
1936 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001937 }
Mike Stump11289f42009-09-09 15:08:12 +00001938
Richard Smithfe904f02012-05-15 21:29:55 +00001939 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00001940 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00001941 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1942 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00001943 // Look ahead to the next token to try to figure out what this declaration
1944 // was supposed to be.
1945 switch (NextToken().getKind()) {
1946 case tok::comma:
1947 case tok::equal:
1948 case tok::kw_asm:
1949 case tok::l_brace:
1950 case tok::l_square:
1951 case tok::semi:
1952 // This looks like a variable declaration. The type is probably missing.
1953 // We're done parsing decl-specifiers.
1954 return false;
1955
1956 case tok::l_paren: {
1957 // static x(4); // 'x' is not a type
1958 // x(int n); // 'x' is not a type
1959 // x (*p)[]; // 'x' is a type
1960 //
1961 // Since we're in an error case (or the rare 'implicit int in C++' MS
1962 // extension), we can afford to perform a tentative parse to determine
1963 // which case we're in.
1964 TentativeParsingAction PA(*this);
1965 ConsumeToken();
1966 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1967 PA.Revert();
1968 if (TPR == TPResult::False())
1969 return false;
1970 // The identifier is followed by a parenthesized declarator.
1971 // It's supposed to be a type.
1972 break;
1973 }
1974
1975 default:
1976 // This is probably supposed to be a type. This includes cases like:
1977 // int f(itn);
1978 // struct S { unsinged : 4; };
1979 break;
1980 }
1981 }
1982
Chad Rosierc1183952012-06-26 22:30:43 +00001983 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00001984 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001985 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001986 IdentifierInfo *II = Tok.getIdentifierInfo();
1987 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001988 // The action emitted a diagnostic, so we don't have to.
1989 if (T) {
1990 // The action has suggested that the type T could be used. Set that as
1991 // the type in the declaration specifiers, consume the would-be type
1992 // name token, and we're done.
1993 const char *PrevSpec;
1994 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001995 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001996 DS.SetRangeEnd(Tok.getLocation());
1997 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001998 // There may be other declaration specifiers after this.
1999 return true;
2000 } else if (II != Tok.getIdentifierInfo()) {
2001 // If no type was suggested, the correction is to a keyword
2002 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002003 // There may be other declaration specifiers after this.
2004 return true;
2005 }
Chad Rosierc1183952012-06-26 22:30:43 +00002006
Douglas Gregor15e56022009-10-13 23:27:22 +00002007 // Fall through; the action had no suggestion for us.
2008 } else {
2009 // The action did not emit a diagnostic, so emit one now.
2010 SourceRange R;
2011 if (SS) R = SS->getRange();
2012 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2013 }
Mike Stump11289f42009-09-09 15:08:12 +00002014
Douglas Gregor15e56022009-10-13 23:27:22 +00002015 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002016 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002017 DS.SetRangeEnd(Tok.getLocation());
2018 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002019
Chris Lattner20a0c612009-04-14 21:34:55 +00002020 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2021 // avoid rippling error messages on subsequent uses of the same type,
2022 // could be useful if #include was forgotten.
2023 return false;
2024}
2025
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002026/// \brief Determine the declaration specifier context from the declarator
2027/// context.
2028///
2029/// \param Context the declarator context, which is one of the
2030/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002031Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002032Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2033 if (Context == Declarator::MemberContext)
2034 return DSC_class;
2035 if (Context == Declarator::FileContext)
2036 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002037 if (Context == Declarator::TrailingReturnContext)
2038 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002039 return DSC_normal;
2040}
2041
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002042/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2043///
2044/// FIXME: Simply returns an alignof() expression if the argument is a
2045/// type. Ideally, the type should be propagated directly into Sema.
2046///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002047/// [C11] type-id
2048/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002049/// [C++0x] type-id ...[opt]
2050/// [C++0x] assignment-expression ...[opt]
2051ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2052 SourceLocation &EllipsisLoc) {
2053 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002054 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002055 SourceLocation TypeLoc = Tok.getLocation();
2056 ParsedType Ty = ParseTypeName().get();
2057 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002058 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2059 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002060 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002061 ER = ParseConstantExpression();
2062
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002063 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002064 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002065
2066 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002067}
2068
2069/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2070/// attribute to Attrs.
2071///
2072/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002073/// [C11] '_Alignas' '(' type-id ')'
2074/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002075/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
2076/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002077void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2078 SourceLocation *endLoc) {
2079 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2080 "Not an alignment-specifier!");
2081
2082 SourceLocation KWLoc = Tok.getLocation();
2083 ConsumeToken();
2084
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002085 BalancedDelimiterTracker T(*this, tok::l_paren);
2086 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002087 return;
2088
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002089 SourceLocation EllipsisLoc;
2090 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002091 if (ArgExpr.isInvalid()) {
2092 SkipUntil(tok::r_paren);
2093 return;
2094 }
2095
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002096 T.consumeClose();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002097 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002098 *endLoc = T.getCloseLocation();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002099
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002100 // FIXME: Handle pack-expansions here.
2101 if (EllipsisLoc.isValid()) {
2102 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2103 return;
2104 }
2105
Benjamin Kramerf0623432012-08-23 22:51:59 +00002106 ExprVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002107 ArgExprs.push_back(ArgExpr.release());
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002108 // FIXME: This should not be GNU, but we since the attribute used is
2109 // based on the spelling, and there is no true spelling for
2110 // C++11 attributes, this isn't accepted.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002111 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002112 0, T.getOpenLocation(), ArgExprs.data(), 1,
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002113 AttributeList::AS_GNU);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002114}
2115
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002116/// ParseDeclarationSpecifiers
2117/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002118/// storage-class-specifier declaration-specifiers[opt]
2119/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002120/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002121/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002122/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002123/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002124///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002125/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002126/// 'typedef'
2127/// 'extern'
2128/// 'static'
2129/// 'auto'
2130/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002131/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002132/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002133/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002134/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002135/// [C++] 'virtual'
2136/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002137/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002138/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002139/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002140
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002141///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002142void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002143 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002144 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002145 DeclSpecContext DSContext,
2146 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002147 if (DS.getSourceRange().isInvalid()) {
2148 DS.SetRangeStart(Tok.getLocation());
2149 DS.SetRangeEnd(Tok.getLocation());
2150 }
Chad Rosierc1183952012-06-26 22:30:43 +00002151
Douglas Gregordf593fb2011-11-07 17:33:42 +00002152 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002153 bool AttrsLastTime = false;
2154 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002155 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002156 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002157 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002158 unsigned DiagID = 0;
2159
Chris Lattner4d8f8732006-11-28 05:05:08 +00002160 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002161
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002162 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002163 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002164 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002165 if (!AttrsLastTime)
2166 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002167 else {
2168 // Reject C++11 attributes that appertain to decl specifiers as
2169 // we don't support any C++11 attributes that appertain to decl
2170 // specifiers. This also conforms to what g++ 4.8 is doing.
2171 ProhibitCXX11Attributes(attrs);
2172
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002173 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002174 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002175
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002176 // If this is not a declaration specifier token, we're done reading decl
2177 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002178 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002179 return;
Mike Stump11289f42009-09-09 15:08:12 +00002180
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002181 case tok::l_square:
2182 case tok::kw_alignas:
2183 if (!isCXX11AttributeSpecifier())
2184 goto DoneWithDeclSpec;
2185
2186 ProhibitAttributes(attrs);
2187 // FIXME: It would be good to recover by accepting the attributes,
2188 // but attempting to do that now would cause serious
2189 // madness in terms of diagnostics.
2190 attrs.clear();
2191 attrs.Range = SourceRange();
2192
2193 ParseCXX11Attributes(attrs);
2194 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002195 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002196
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002197 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002198 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002199 if (DS.hasTypeSpecifier()) {
2200 bool AllowNonIdentifiers
2201 = (getCurScope()->getFlags() & (Scope::ControlScope |
2202 Scope::BlockScope |
2203 Scope::TemplateParamScope |
2204 Scope::FunctionPrototypeScope |
2205 Scope::AtCatchScope)) == 0;
2206 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002207 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002208 (DSContext == DSC_class && DS.isFriendSpecified());
2209
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002210 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002211 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002212 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002213 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002214 }
2215
Douglas Gregor80039242011-02-15 20:33:25 +00002216 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2217 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2218 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002219 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002220 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002221 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002222 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002223 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002224 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002225
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002226 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002227 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002228 }
2229
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002230 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002231 // C++ scope specifier. Annotate and loop, or bail out on error.
2232 if (TryAnnotateCXXScopeToken(true)) {
2233 if (!DS.hasTypeSpecifier())
2234 DS.SetTypeSpecError();
2235 goto DoneWithDeclSpec;
2236 }
John McCall8bc2a702010-03-01 18:20:46 +00002237 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2238 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002239 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002240
2241 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002242 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002243 goto DoneWithDeclSpec;
2244
John McCall9dab4e62009-12-12 11:40:51 +00002245 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002246 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2247 Tok.getAnnotationRange(),
2248 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002249
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002250 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002251 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002252 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002253 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002254 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002255 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002256
2257 // C++ [class.qual]p2:
2258 // In a lookup in which the constructor is an acceptable lookup
2259 // result and the nested-name-specifier nominates a class C:
2260 //
2261 // - if the name specified after the
2262 // nested-name-specifier, when looked up in C, is the
2263 // injected-class-name of C (Clause 9), or
2264 //
2265 // - if the name specified after the nested-name-specifier
2266 // is the same as the identifier or the
2267 // simple-template-id's template-name in the last
2268 // component of the nested-name-specifier,
2269 //
2270 // the name is instead considered to name the constructor of
2271 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002272 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002273 // Thus, if the template-name is actually the constructor
2274 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002275 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002276 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCall84821e72010-04-13 06:39:49 +00002277 if ((DSContext == DSC_top_level ||
2278 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2279 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002280 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002281 if (isConstructorDeclarator()) {
2282 // The user meant this to be an out-of-line constructor
2283 // definition, but template arguments are not allowed
2284 // there. Just allow this as a constructor; we'll
2285 // complain about it later.
2286 goto DoneWithDeclSpec;
2287 }
2288
2289 // The user meant this to name a type, but it actually names
2290 // a constructor with some extraneous template
2291 // arguments. Complain, then parse it as a type as the user
2292 // intended.
2293 Diag(TemplateId->TemplateNameLoc,
2294 diag::err_out_of_line_template_id_names_constructor)
2295 << TemplateId->Name;
2296 }
2297
John McCall9dab4e62009-12-12 11:40:51 +00002298 DS.getTypeSpecScope() = SS;
2299 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002300 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002301 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002302 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002303 continue;
2304 }
2305
Douglas Gregorc5790df2009-09-28 07:26:33 +00002306 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002307 DS.getTypeSpecScope() = SS;
2308 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002309 if (Tok.getAnnotationValue()) {
2310 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002311 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002312 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002313 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002314 if (isInvalid)
2315 break;
John McCallba7bf592010-08-24 05:47:05 +00002316 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002317 else
2318 DS.SetTypeSpecError();
2319 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2320 ConsumeToken(); // The typename
2321 }
2322
Douglas Gregor167fa622009-03-25 15:40:00 +00002323 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002324 goto DoneWithDeclSpec;
2325
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002326 // If we're in a context where the identifier could be a class name,
2327 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00002328 if ((DSContext == DSC_top_level ||
2329 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002330 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002331 &SS)) {
2332 if (isConstructorDeclarator())
2333 goto DoneWithDeclSpec;
2334
2335 // As noted in C++ [class.qual]p2 (cited above), when the name
2336 // of the class is qualified in a context where it could name
2337 // a constructor, its a constructor name. However, we've
2338 // looked at the declarator, and the user probably meant this
2339 // to be a type. Complain that it isn't supposed to be treated
2340 // as a type, then proceed to parse it as a type.
2341 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2342 << Next.getIdentifierInfo();
2343 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002344
John McCallba7bf592010-08-24 05:47:05 +00002345 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2346 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002347 getCurScope(), &SS,
2348 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002349 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002350 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002351
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002352 // If the referenced identifier is not a type, then this declspec is
2353 // erroneous: We already checked about that it has no type specifier, and
2354 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002355 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002356 if (TypeRep == 0) {
2357 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002358 ParsedAttributesWithRange Attrs(AttrFactory);
2359 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2360 if (!Attrs.empty()) {
2361 AttrsLastTime = true;
2362 attrs.takeAllFrom(Attrs);
2363 }
2364 continue;
2365 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002366 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002367 }
Mike Stump11289f42009-09-09 15:08:12 +00002368
John McCall9dab4e62009-12-12 11:40:51 +00002369 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002370 ConsumeToken(); // The C++ scope.
2371
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002372 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002373 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002374 if (isInvalid)
2375 break;
Mike Stump11289f42009-09-09 15:08:12 +00002376
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002377 DS.SetRangeEnd(Tok.getLocation());
2378 ConsumeToken(); // The typename.
2379
2380 continue;
2381 }
Mike Stump11289f42009-09-09 15:08:12 +00002382
Chris Lattnere387d9e2009-01-21 19:48:37 +00002383 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002384 if (Tok.getAnnotationValue()) {
2385 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002386 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002387 DiagID, T);
2388 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002389 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002390
Chris Lattner005fc1b2010-04-05 18:18:31 +00002391 if (isInvalid)
2392 break;
2393
Chris Lattnere387d9e2009-01-21 19:48:37 +00002394 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2395 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002396
Chris Lattnere387d9e2009-01-21 19:48:37 +00002397 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2398 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002399 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002400 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002401 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002402
Chris Lattnere387d9e2009-01-21 19:48:37 +00002403 continue;
2404 }
Mike Stump11289f42009-09-09 15:08:12 +00002405
Douglas Gregor06873092011-04-28 15:48:45 +00002406 case tok::kw___is_signed:
2407 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2408 // typically treats it as a trait. If we see __is_signed as it appears
2409 // in libstdc++, e.g.,
2410 //
2411 // static const bool __is_signed;
2412 //
2413 // then treat __is_signed as an identifier rather than as a keyword.
2414 if (DS.getTypeSpecType() == TST_bool &&
2415 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2416 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2417 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2418 Tok.setKind(tok::identifier);
2419 }
2420
2421 // We're done with the declaration-specifiers.
2422 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002423
Chris Lattner16fac4f2008-07-26 01:18:38 +00002424 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002425 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002426 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002427 // In C++, check to see if this is a scope specifier like foo::bar::, if
2428 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002429 if (getLangOpts().CPlusPlus) {
John McCall1f476a12010-02-26 08:45:28 +00002430 if (TryAnnotateCXXScopeToken(true)) {
2431 if (!DS.hasTypeSpecifier())
2432 DS.SetTypeSpecError();
2433 goto DoneWithDeclSpec;
2434 }
2435 if (!Tok.is(tok::identifier))
2436 continue;
2437 }
Mike Stump11289f42009-09-09 15:08:12 +00002438
Chris Lattner16fac4f2008-07-26 01:18:38 +00002439 // This identifier can only be a typedef name if we haven't already seen
2440 // a type-specifier. Without this check we misparse:
2441 // typedef int X; struct Y { short X; }; as 'short int'.
2442 if (DS.hasTypeSpecifier())
2443 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002444
John Thompson22334602010-02-05 00:12:22 +00002445 // Check for need to substitute AltiVec keyword tokens.
2446 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2447 break;
2448
Richard Smith3092a3b2012-05-09 18:56:43 +00002449 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2450 // allow the use of a typedef name as a type specifier.
2451 if (DS.isTypeAltiVecVector())
2452 goto DoneWithDeclSpec;
2453
John McCallba7bf592010-08-24 05:47:05 +00002454 ParsedType TypeRep =
2455 Actions.getTypeName(*Tok.getIdentifierInfo(),
2456 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002457
Chris Lattner6cc055a2009-04-12 20:42:31 +00002458 // If this is not a typedef name, don't parse it as part of the declspec,
2459 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002460 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002461 ParsedAttributesWithRange Attrs(AttrFactory);
2462 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2463 if (!Attrs.empty()) {
2464 AttrsLastTime = true;
2465 attrs.takeAllFrom(Attrs);
2466 }
2467 continue;
2468 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002469 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002470 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002471
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002472 // If we're in a context where the identifier could be a class name,
2473 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002474 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002475 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002476 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002477 goto DoneWithDeclSpec;
2478
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002479 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002480 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002481 if (isInvalid)
2482 break;
Mike Stump11289f42009-09-09 15:08:12 +00002483
Chris Lattner16fac4f2008-07-26 01:18:38 +00002484 DS.SetRangeEnd(Tok.getLocation());
2485 ConsumeToken(); // The identifier
2486
2487 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2488 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002489 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002490 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002491 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002492
Steve Naroffcd5e7822008-09-22 10:28:57 +00002493 // Need to support trailing type qualifiers (e.g. "id<p> const").
2494 // If a type specifier follows, it will be diagnosed elsewhere.
2495 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002496 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002497
2498 // type-name
2499 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002500 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002501 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002502 // This template-id does not refer to a type name, so we're
2503 // done with the type-specifiers.
2504 goto DoneWithDeclSpec;
2505 }
2506
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002507 // If we're in a context where the template-id could be a
2508 // constructor name or specialization, check whether this is a
2509 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002510 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002511 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002512 isConstructorDeclarator())
2513 goto DoneWithDeclSpec;
2514
Douglas Gregor7f741122009-02-25 19:37:18 +00002515 // Turn the template-id annotation token into a type annotation
2516 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002517 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002518 continue;
2519 }
2520
Chris Lattnere37e2332006-08-15 04:50:22 +00002521 // GNU attributes support.
2522 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002523 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002524 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002525
2526 // Microsoft declspec support.
2527 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002528 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002529 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002530
Steve Naroff44ac7772008-12-25 14:16:32 +00002531 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002532 case tok::kw___forceinline: {
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002533 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002534 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002535 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002536 // FIXME: This does not work correctly if it is set to be a declspec
2537 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002538 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002539 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002540 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002541 }
Eli Friedman53339e02009-06-08 23:27:34 +00002542
2543 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002544 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002545 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002546 case tok::kw___cdecl:
2547 case tok::kw___stdcall:
2548 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002549 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002550 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002551 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002552 continue;
2553
Dawn Perchik335e16b2010-09-03 01:29:35 +00002554 // Borland single token adornments.
2555 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002556 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002557 continue;
2558
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002559 // OpenCL single token adornments.
2560 case tok::kw___kernel:
2561 ParseOpenCLAttributes(DS.getAttributes());
2562 continue;
2563
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002564 // storage-class-specifier
2565 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002566 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2567 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002568 break;
2569 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00002570 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002571 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002572 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2573 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002574 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002575 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002576 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2577 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002578 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002579 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00002580 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002581 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002582 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2583 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002584 break;
2585 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002586 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002587 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002588 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2589 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002590 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002591 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002592 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002593 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002594 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2595 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002596 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002597 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2598 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002599 break;
2600 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002601 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2602 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002603 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002604 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002605 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2606 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002607 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002608 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00002609 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002610 break;
Mike Stump11289f42009-09-09 15:08:12 +00002611
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002612 // function-specifier
2613 case tok::kw_inline:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002614 isInvalid = DS.setFunctionSpecInline(Loc);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002615 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002616 case tok::kw_virtual:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002617 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002618 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002619 case tok::kw_explicit:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002620 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002621 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002622
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002623 // alignment-specifier
2624 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002625 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002626 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002627 ParseAlignmentSpecifier(DS.getAttributes());
2628 continue;
2629
Anders Carlssoncd8db412009-05-06 04:46:28 +00002630 // friend
2631 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002632 if (DSContext == DSC_class)
2633 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2634 else {
2635 PrevSpec = ""; // not actually used by the diagnostic
2636 DiagID = diag::err_friend_invalid_in_context;
2637 isInvalid = true;
2638 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002639 break;
Mike Stump11289f42009-09-09 15:08:12 +00002640
Douglas Gregor26701a42011-09-09 02:06:17 +00002641 // Modules
2642 case tok::kw___module_private__:
2643 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2644 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002645
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002646 // constexpr
2647 case tok::kw_constexpr:
2648 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2649 break;
2650
Chris Lattnere387d9e2009-01-21 19:48:37 +00002651 // type-specifier
2652 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002653 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2654 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002655 break;
2656 case tok::kw_long:
2657 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002658 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2659 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002660 else
John McCall49bfce42009-08-03 20:12:06 +00002661 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2662 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002663 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002664 case tok::kw___int64:
2665 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2666 DiagID);
2667 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002668 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002669 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2670 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002671 break;
2672 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002673 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2674 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002675 break;
2676 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002677 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2678 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002679 break;
2680 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002681 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2682 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002683 break;
2684 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002685 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2686 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002687 break;
2688 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002689 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2690 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002691 break;
2692 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002693 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2694 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002695 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002696 case tok::kw___int128:
2697 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2698 DiagID);
2699 break;
2700 case tok::kw_half:
2701 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2702 DiagID);
2703 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002704 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002705 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2706 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002707 break;
2708 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002709 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2710 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002711 break;
2712 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002713 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2714 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002715 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002716 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002717 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2718 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002719 break;
2720 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002721 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2722 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002723 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002724 case tok::kw_bool:
2725 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002726 if (Tok.is(tok::kw_bool) &&
2727 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2728 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2729 PrevSpec = ""; // Not used by the diagnostic.
2730 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002731 // For better error recovery.
2732 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002733 isInvalid = true;
2734 } else {
2735 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2736 DiagID);
2737 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002738 break;
2739 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002740 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2741 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002742 break;
2743 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002744 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2745 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002746 break;
2747 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2749 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002750 break;
John Thompson22334602010-02-05 00:12:22 +00002751 case tok::kw___vector:
2752 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2753 break;
2754 case tok::kw___pixel:
2755 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2756 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00002757 case tok::kw_image1d_t:
2758 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2759 PrevSpec, DiagID);
2760 break;
2761 case tok::kw_image1d_array_t:
2762 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2763 PrevSpec, DiagID);
2764 break;
2765 case tok::kw_image1d_buffer_t:
2766 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2767 PrevSpec, DiagID);
2768 break;
2769 case tok::kw_image2d_t:
2770 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2771 PrevSpec, DiagID);
2772 break;
2773 case tok::kw_image2d_array_t:
2774 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2775 PrevSpec, DiagID);
2776 break;
2777 case tok::kw_image3d_t:
2778 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2779 PrevSpec, DiagID);
2780 break;
John McCall39439732011-04-09 22:50:59 +00002781 case tok::kw___unknown_anytype:
2782 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2783 PrevSpec, DiagID);
2784 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002785
2786 // class-specifier:
2787 case tok::kw_class:
2788 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00002789 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002790 case tok::kw_union: {
2791 tok::TokenKind Kind = Tok.getKind();
2792 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00002793
2794 // These are attributes following class specifiers.
2795 // To produce better diagnostic, we parse them when
2796 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00002797 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00002798 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00002799 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00002800
2801 // If there are attributes following class specifier,
2802 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00002803 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00002804 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00002805 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00002806 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002807 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002808 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002809
2810 // enum-specifier:
2811 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002812 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002813 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002814 continue;
2815
2816 // cv-qualifier:
2817 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002818 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002819 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002820 break;
2821 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002822 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002823 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002824 break;
2825 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002826 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002827 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002828 break;
2829
Douglas Gregor333489b2009-03-27 23:10:48 +00002830 // C++ typename-specifier:
2831 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00002832 if (TryAnnotateTypeOrScopeToken()) {
2833 DS.SetTypeSpecError();
2834 goto DoneWithDeclSpec;
2835 }
2836 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00002837 continue;
2838 break;
2839
Chris Lattnere387d9e2009-01-21 19:48:37 +00002840 // GNU typeof support.
2841 case tok::kw_typeof:
2842 ParseTypeofSpecifier(DS);
2843 continue;
2844
David Blaikie15a430a2011-12-04 05:04:18 +00002845 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00002846 ParseDecltypeSpecifier(DS);
2847 continue;
2848
Alexis Hunt4a257072011-05-19 05:37:45 +00002849 case tok::kw___underlying_type:
2850 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00002851 continue;
2852
2853 case tok::kw__Atomic:
2854 ParseAtomicSpecifier(DS);
2855 continue;
Alexis Hunt4a257072011-05-19 05:37:45 +00002856
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002857 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00002858 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002859 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002860 goto DoneWithDeclSpec;
2861 case tok::kw___private:
2862 case tok::kw___global:
2863 case tok::kw___local:
2864 case tok::kw___constant:
2865 case tok::kw___read_only:
2866 case tok::kw___write_only:
2867 case tok::kw___read_write:
2868 ParseOpenCLQualifiers(DS);
2869 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002870
Steve Naroffcfdf6162008-06-05 00:02:44 +00002871 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002872 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00002873 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2874 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002875 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00002876 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002877
Douglas Gregor3a001f42010-11-19 17:10:50 +00002878 if (!ParseObjCProtocolQualifiers(DS))
2879 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2880 << FixItHint::CreateInsertion(Loc, "id")
2881 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00002882
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002883 // Need to support trailing type qualifiers (e.g. "id<p> const").
2884 // If a type specifier follows, it will be diagnosed elsewhere.
2885 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002886 }
John McCall49bfce42009-08-03 20:12:06 +00002887 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002888 if (isInvalid) {
2889 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002890 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00002891
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002892 if (DiagID == diag::ext_duplicate_declspec)
2893 Diag(Tok, DiagID)
2894 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2895 else
2896 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002897 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002898
Chris Lattner2e232092008-03-13 06:29:04 +00002899 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002900 if (DiagID != diag::err_bool_redeclaration)
2901 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002902
2903 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002904 }
2905}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002906
Chris Lattner70ae4912007-10-29 04:42:53 +00002907/// ParseStructDeclaration - Parse a struct declaration without the terminating
2908/// semicolon.
2909///
Chris Lattner90a26b02007-01-23 04:38:16 +00002910/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002911/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002912/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002913/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002914/// struct-declarator-list:
2915/// struct-declarator
2916/// struct-declarator-list ',' struct-declarator
2917/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2918/// struct-declarator:
2919/// declarator
2920/// [GNU] declarator attributes[opt]
2921/// declarator[opt] ':' constant-expression
2922/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2923///
Chris Lattnera12405b2008-04-10 06:46:29 +00002924void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002925ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00002926
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002927 if (Tok.is(tok::kw___extension__)) {
2928 // __extension__ silences extension warnings in the subexpression.
2929 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002930 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002931 return ParseStructDeclaration(DS, Fields);
2932 }
Mike Stump11289f42009-09-09 15:08:12 +00002933
Steve Naroff97170802007-08-20 22:28:22 +00002934 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002935 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002936
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002937 // If there are no declarators, this is a free-standing declaration
2938 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002939 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002940 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2941 DS);
2942 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00002943 return;
2944 }
2945
2946 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002947 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00002948 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00002949 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002950 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00002951 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00002952
Bill Wendling44426052012-12-20 19:22:21 +00002953 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002954 if (!FirstDeclarator)
2955 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002956
Steve Naroff97170802007-08-20 22:28:22 +00002957 /// struct-declarator: declarator
2958 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002959 if (Tok.isNot(tok::colon)) {
2960 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2961 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002962 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002963 }
Mike Stump11289f42009-09-09 15:08:12 +00002964
Chris Lattner76c72282007-10-09 17:33:22 +00002965 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002966 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002967 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002968 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002969 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002970 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002971 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002972 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002973
Steve Naroff97170802007-08-20 22:28:22 +00002974 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002975 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002976
John McCallcfefb6d2009-11-03 02:38:08 +00002977 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00002978 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00002979
Steve Naroff97170802007-08-20 22:28:22 +00002980 // If we don't have a comma, it is either the end of the list (a ';')
2981 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002982 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002983 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002984
Steve Naroff97170802007-08-20 22:28:22 +00002985 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00002986 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002987
John McCallcfefb6d2009-11-03 02:38:08 +00002988 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002989 }
Steve Naroff97170802007-08-20 22:28:22 +00002990}
2991
2992/// ParseStructUnionBody
2993/// struct-contents:
2994/// struct-declaration-list
2995/// [EXT] empty
2996/// [GNU] "struct-declaration-list" without terminatoring ';'
2997/// struct-declaration-list:
2998/// struct-declaration
2999/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003000/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003001///
Chris Lattner1300fb92007-01-23 23:42:53 +00003002void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003003 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003004 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3005 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00003006
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003007 BalancedDelimiterTracker T(*this, tok::l_brace);
3008 if (T.consumeOpen())
3009 return;
Mike Stump11289f42009-09-09 15:08:12 +00003010
Douglas Gregor658b9552009-01-09 22:42:13 +00003011 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003012 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003013
Chris Lattner7b9ace62007-01-23 20:11:08 +00003014 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
3015 // C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003016 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithe4345902011-12-29 21:57:33 +00003017 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3018 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3019 }
Chris Lattner7b9ace62007-01-23 20:11:08 +00003020
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003021 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003022
Chris Lattner7b9ace62007-01-23 20:11:08 +00003023 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00003024 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003025 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003026
Chris Lattner736ed5d2007-06-09 05:59:07 +00003027 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003028 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003029 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003030 continue;
3031 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003032
John McCallcfefb6d2009-11-03 02:38:08 +00003033 if (!Tok.is(tok::at)) {
3034 struct CFieldCallback : FieldCallback {
3035 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003036 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003037 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003038
John McCall48871652010-08-21 09:40:31 +00003039 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003040 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003041 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3042
Eli Friedman934dbbf2012-08-08 23:53:27 +00003043 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003044 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003045 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003046 FD.D.getDeclSpec().getSourceRange().getBegin(),
3047 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003048 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003049 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003050 }
John McCallcfefb6d2009-11-03 02:38:08 +00003051 } Callback(*this, TagDecl, FieldDecls);
3052
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003053 // Parse all the comma separated declarators.
3054 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003055 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003056 } else { // Handle @defs
3057 ConsumeToken();
3058 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3059 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00003060 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003061 continue;
3062 }
3063 ConsumeToken();
3064 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3065 if (!Tok.is(tok::identifier)) {
3066 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00003067 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003068 continue;
3069 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003070 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003071 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003072 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003073 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3074 ConsumeToken();
3075 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003076 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003077
Chris Lattner76c72282007-10-09 17:33:22 +00003078 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003079 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003080 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003081 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003082 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003083 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003084 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3085 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00003086 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00003087 // If we stopped at a ';', eat it.
3088 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003089 }
3090 }
Mike Stump11289f42009-09-09 15:08:12 +00003091
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003092 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003093
John McCall084e83d2011-03-24 11:26:52 +00003094 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003095 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003096 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003097
Douglas Gregor0be31a22010-07-02 17:43:08 +00003098 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003099 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003100 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003101 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003102 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003103 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3104 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003105}
3106
Chris Lattner3b561a32006-08-13 00:12:11 +00003107/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003108/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003109/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003110///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003111/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3112/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003113/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3114/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003115/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003116/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003117///
Richard Smith7d137e32012-03-23 03:33:32 +00003118/// [C++11] enum-head '{' enumerator-list[opt] '}'
3119/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003120///
Richard Smith7d137e32012-03-23 03:33:32 +00003121/// enum-head: [C++11]
3122/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3123/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3124/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003125///
Richard Smith7d137e32012-03-23 03:33:32 +00003126/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003127/// 'enum'
3128/// 'enum' 'class'
3129/// 'enum' 'struct'
3130///
Richard Smith7d137e32012-03-23 03:33:32 +00003131/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003132/// ':' type-specifier-seq
3133///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003134/// [C++] elaborated-type-specifier:
3135/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3136///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003137void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003138 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003139 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003140 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003141 if (Tok.is(tok::code_completion)) {
3142 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003143 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003144 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003145 }
John McCallcb432fa2011-07-06 05:58:41 +00003146
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003147 // If attributes exist after tag, parse them.
3148 ParsedAttributesWithRange attrs(AttrFactory);
3149 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003150 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003151
3152 // If declspecs exist after tag, parse them.
3153 while (Tok.is(tok::kw___declspec))
3154 ParseMicrosoftDeclSpec(attrs);
3155
Richard Smith0f8ee222012-01-10 01:33:14 +00003156 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003157 bool IsScopedUsingClassTag = false;
3158
John McCallbeae29a2012-06-23 22:30:04 +00003159 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003160 if (getLangOpts().CPlusPlus11 &&
John McCallcb432fa2011-07-06 05:58:41 +00003161 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003162 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003163 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003164 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003165
Bill Wendling44426052012-12-20 19:22:21 +00003166 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003167 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003168 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003169
3170 // They are allowed afterwards, though.
3171 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003172 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003173 while (Tok.is(tok::kw___declspec))
3174 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003175 }
Richard Smith7d137e32012-03-23 03:33:32 +00003176
John McCall6347b682012-05-07 06:16:58 +00003177 // C++11 [temp.explicit]p12:
3178 // The usual access controls do not apply to names used to specify
3179 // explicit instantiations.
3180 // We extend this to also cover explicit specializations. Note that
3181 // we don't suppress if this turns out to be an elaborated type
3182 // specifier.
3183 bool shouldDelayDiagsInTag =
3184 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3185 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3186 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003187
Richard Smithbfdb1082012-03-12 08:56:40 +00003188 // Enum definitions should not be parsed in a trailing-return-type.
3189 bool AllowDeclaration = DSC != DSC_trailing;
3190
3191 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003192 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003193 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003194
Abramo Bagnarad7548482010-05-19 21:37:53 +00003195 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003196 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003197 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3198 // if a fixed underlying type is allowed.
3199 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003200
3201 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003202 /*EnteringContext=*/false))
John McCall1f476a12010-02-26 08:45:28 +00003203 return;
3204
3205 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003206 Diag(Tok, diag::err_expected_ident);
3207 if (Tok.isNot(tok::l_brace)) {
3208 // Has no name and is not a definition.
3209 // Skip the rest of this declarator, up until the comma or semicolon.
3210 SkipUntil(tok::comma, true);
3211 return;
3212 }
3213 }
3214 }
Mike Stump11289f42009-09-09 15:08:12 +00003215
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003216 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003217 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003218 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003219 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003220
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003221 // Skip the rest of this declarator, up until the comma or semicolon.
3222 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003223 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003224 }
Mike Stump11289f42009-09-09 15:08:12 +00003225
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003226 // If an identifier is present, consume and remember it.
3227 IdentifierInfo *Name = 0;
3228 SourceLocation NameLoc;
3229 if (Tok.is(tok::identifier)) {
3230 Name = Tok.getIdentifierInfo();
3231 NameLoc = ConsumeToken();
3232 }
Mike Stump11289f42009-09-09 15:08:12 +00003233
Richard Smith0f8ee222012-01-10 01:33:14 +00003234 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003235 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3236 // declaration of a scoped enumeration.
3237 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003238 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003239 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003240 }
3241
John McCall6347b682012-05-07 06:16:58 +00003242 // Okay, end the suppression area. We'll decide whether to emit the
3243 // diagnostics in a second.
3244 if (shouldDelayDiagsInTag)
3245 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003246
Douglas Gregor0bf31402010-10-08 23:50:27 +00003247 TypeResult BaseType;
3248
Douglas Gregord1f69f62010-12-01 17:42:47 +00003249 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003250 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003251 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003252 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003253 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003254 // If we're in class scope, this can either be an enum declaration with
3255 // an underlying type, or a declaration of a bitfield member. We try to
3256 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003257 // (integer literal, sizeof); if it's still ambiguous, we then consider
3258 // anything that's a simple-type-specifier followed by '(' as an
3259 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003260 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003261 EnterExpressionEvaluationContext Unevaluated(Actions,
3262 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003263 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003264 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003265 // bit-field. This is the common case.
3266 if (TPR == TPResult::True())
3267 PossibleBitfield = true;
3268 // If the next token starts a type-specifier-seq, it may be either a
3269 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003270 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003271 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003272 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003273 GetLookAheadToken(2).getKind() == tok::semi) {
3274 // Consume the ':'.
3275 ConsumeToken();
3276 } else {
3277 // We have the start of a type-specifier-seq, so we have to perform
3278 // tentative parsing to determine whether we have an expression or a
3279 // type.
3280 TentativeParsingAction TPA(*this);
3281
3282 // Consume the ':'.
3283 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003284
3285 // If we see a type specifier followed by an open-brace, we have an
3286 // ambiguity between an underlying type and a C++11 braced
3287 // function-style cast. Resolve this by always treating it as an
3288 // underlying type.
3289 // FIXME: The standard is not entirely clear on how to disambiguate in
3290 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003291 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003292 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003293 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003294 // We'll parse this as a bitfield later.
3295 PossibleBitfield = true;
3296 TPA.Revert();
3297 } else {
3298 // We have a type-specifier-seq.
3299 TPA.Commit();
3300 }
3301 }
3302 } else {
3303 // Consume the ':'.
3304 ConsumeToken();
3305 }
3306
3307 if (!PossibleBitfield) {
3308 SourceRange Range;
3309 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003310
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003311 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003312 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003313 } else if (!getLangOpts().ObjC2) {
3314 if (getLangOpts().CPlusPlus)
3315 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3316 else
3317 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3318 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003319 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003320 }
3321
Richard Smith0f8ee222012-01-10 01:33:14 +00003322 // There are four options here. If we have 'friend enum foo;' then this is a
3323 // friend declaration, and cannot have an accompanying definition. If we have
3324 // 'enum foo;', then this is a forward declaration. If we have
3325 // 'enum foo {...' then this is a definition. Otherwise we have something
3326 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003327 //
3328 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3329 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3330 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3331 //
John McCallfaf5fb42010-08-26 23:41:50 +00003332 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003333 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003334 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003335 } else if (Tok.is(tok::l_brace)) {
3336 if (DS.isFriendSpecified()) {
3337 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3338 << SourceRange(DS.getFriendSpecLoc());
3339 ConsumeBrace();
3340 SkipUntil(tok::r_brace);
3341 TUK = Sema::TUK_Friend;
3342 } else {
3343 TUK = Sema::TUK_Definition;
3344 }
Richard Smith369b9f92012-06-25 21:37:02 +00003345 } else if (DSC != DSC_type_specifier &&
3346 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003347 (Tok.isAtStartOfLine() &&
3348 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003349 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3350 if (Tok.isNot(tok::semi)) {
3351 // A semicolon was missing after this declaration. Diagnose and recover.
3352 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3353 "enum");
3354 PP.EnterToken(Tok);
3355 Tok.setKind(tok::semi);
3356 }
John McCall6347b682012-05-07 06:16:58 +00003357 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003358 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003359 }
3360
3361 // If this is an elaborated type specifier, and we delayed
3362 // diagnostics before, just merge them into the current pool.
3363 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3364 diagsFromTag.redelay();
3365 }
Richard Smith7d137e32012-03-23 03:33:32 +00003366
3367 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003368 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003369 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003370 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003371 // Skip the rest of this declarator, up until the comma or semicolon.
3372 Diag(Tok, diag::err_enum_template);
3373 SkipUntil(tok::comma, true);
3374 return;
3375 }
3376
3377 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3378 // Enumerations can't be explicitly instantiated.
3379 DS.SetTypeSpecError();
3380 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3381 return;
3382 }
3383
3384 assert(TemplateInfo.TemplateParams && "no template parameters");
3385 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3386 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003387 }
Chad Rosierc1183952012-06-26 22:30:43 +00003388
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003389 if (TUK == Sema::TUK_Reference)
3390 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003391
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003392 if (!Name && TUK != Sema::TUK_Definition) {
3393 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003394
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003395 // Skip the rest of this declarator, up until the comma or semicolon.
3396 SkipUntil(tok::comma, true);
3397 return;
3398 }
Richard Smith7d137e32012-03-23 03:33:32 +00003399
Douglas Gregord6ab8742009-05-28 23:31:59 +00003400 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003401 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003402 const char *PrevSpec = 0;
3403 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003404 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003405 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003406 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003407 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003408 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003409
Douglas Gregorba41d012010-04-24 16:38:41 +00003410 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003411 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003412 // dependent tag.
3413 if (!Name) {
3414 DS.SetTypeSpecError();
3415 Diag(Tok, diag::err_expected_type_name_after_typename);
3416 return;
3417 }
Chad Rosierc1183952012-06-26 22:30:43 +00003418
Douglas Gregor0be31a22010-07-02 17:43:08 +00003419 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003420 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003421 NameLoc);
3422 if (Type.isInvalid()) {
3423 DS.SetTypeSpecError();
3424 return;
3425 }
Chad Rosierc1183952012-06-26 22:30:43 +00003426
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003427 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3428 NameLoc.isValid() ? NameLoc : StartLoc,
3429 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003430 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003431
Douglas Gregorba41d012010-04-24 16:38:41 +00003432 return;
3433 }
Mike Stump11289f42009-09-09 15:08:12 +00003434
John McCall48871652010-08-21 09:40:31 +00003435 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003436 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003437 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003438 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003439 ConsumeBrace();
3440 SkipUntil(tok::r_brace);
3441 }
Chad Rosierc1183952012-06-26 22:30:43 +00003442
Douglas Gregorba41d012010-04-24 16:38:41 +00003443 DS.SetTypeSpecError();
3444 return;
3445 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003446
Richard Smith369b9f92012-06-25 21:37:02 +00003447 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003448 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003449
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003450 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3451 NameLoc.isValid() ? NameLoc : StartLoc,
3452 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003453 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003454}
3455
Chris Lattnerc1915e22007-01-25 07:29:02 +00003456/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3457/// enumerator-list:
3458/// enumerator
3459/// enumerator-list ',' enumerator
3460/// enumerator:
3461/// enumeration-constant
3462/// enumeration-constant '=' constant-expression
3463/// enumeration-constant:
3464/// identifier
3465///
John McCall48871652010-08-21 09:40:31 +00003466void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003467 // Enter the scope of the enum body and start the definition.
3468 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003469 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003470
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003471 BalancedDelimiterTracker T(*this, tok::l_brace);
3472 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003473
Chris Lattner37256fb2007-08-27 17:24:30 +00003474 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003475 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003476 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003477
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003478 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003479
John McCall48871652010-08-21 09:40:31 +00003480 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003481
Chris Lattnerc1915e22007-01-25 07:29:02 +00003482 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003483 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003484 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3485 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003486
John McCall811a0f52010-10-22 23:36:17 +00003487 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003488 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003489 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003490 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003491 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003492
Chris Lattnerc1915e22007-01-25 07:29:02 +00003493 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003494 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003495 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003496
Chris Lattner76c72282007-10-09 17:33:22 +00003497 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003498 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003499 AssignedVal = ParseConstantExpression();
3500 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003501 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003502 }
Mike Stump11289f42009-09-09 15:08:12 +00003503
Chris Lattnerc1915e22007-01-25 07:29:02 +00003504 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003505 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3506 LastEnumConstDecl,
3507 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003508 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003509 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003510 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003511
Chris Lattner4ef40012007-06-11 01:28:17 +00003512 EnumConstantDecls.push_back(EnumConstDecl);
3513 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003514
Douglas Gregorce66d022010-09-07 14:51:08 +00003515 if (Tok.is(tok::identifier)) {
3516 // We're missing a comma between enumerators.
3517 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003518 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003519 << FixItHint::CreateInsertion(Loc, ", ");
3520 continue;
3521 }
Chad Rosierc1183952012-06-26 22:30:43 +00003522
Chris Lattner76c72282007-10-09 17:33:22 +00003523 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003524 break;
3525 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003526
Richard Smith5d164bc2011-10-15 05:09:34 +00003527 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003528 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003529 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3530 diag::ext_enumerator_list_comma_cxx :
3531 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003532 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003533 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003534 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3535 << FixItHint::CreateRemoval(CommaLoc);
3536 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003537 }
Mike Stump11289f42009-09-09 15:08:12 +00003538
Chris Lattnerc1915e22007-01-25 07:29:02 +00003539 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003540 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003541
Chris Lattnerc1915e22007-01-25 07:29:02 +00003542 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003543 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003544 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003545
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003546 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3547 EnumDecl, EnumConstantDecls.data(),
3548 EnumConstantDecls.size(), getCurScope(),
3549 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003550
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003551 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003552 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3553 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003554
3555 // The next token must be valid after an enum definition. If not, a ';'
3556 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003557 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3558 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003559 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3560 // Push this token back into the preprocessor and change our current token
3561 // to ';' so that the rest of the code recovers as though there were an
3562 // ';' after the definition.
3563 PP.EnterToken(Tok);
3564 Tok.setKind(tok::semi);
3565 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003566}
Chris Lattner3b561a32006-08-13 00:12:11 +00003567
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003568/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003569/// start of a type-qualifier-list.
3570bool Parser::isTypeQualifier() const {
3571 switch (Tok.getKind()) {
3572 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003573
3574 // type-qualifier only in OpenCL
3575 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003576 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003577
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003578 // type-qualifier
3579 case tok::kw_const:
3580 case tok::kw_volatile:
3581 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003582 case tok::kw___private:
3583 case tok::kw___local:
3584 case tok::kw___global:
3585 case tok::kw___constant:
3586 case tok::kw___read_only:
3587 case tok::kw___read_write:
3588 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003589 return true;
3590 }
3591}
3592
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003593/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3594/// is definitely a type-specifier. Return false if it isn't part of a type
3595/// specifier or if we're not sure.
3596bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3597 switch (Tok.getKind()) {
3598 default: return false;
3599 // type-specifiers
3600 case tok::kw_short:
3601 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003602 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003603 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003604 case tok::kw_signed:
3605 case tok::kw_unsigned:
3606 case tok::kw__Complex:
3607 case tok::kw__Imaginary:
3608 case tok::kw_void:
3609 case tok::kw_char:
3610 case tok::kw_wchar_t:
3611 case tok::kw_char16_t:
3612 case tok::kw_char32_t:
3613 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003614 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003615 case tok::kw_float:
3616 case tok::kw_double:
3617 case tok::kw_bool:
3618 case tok::kw__Bool:
3619 case tok::kw__Decimal32:
3620 case tok::kw__Decimal64:
3621 case tok::kw__Decimal128:
3622 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003623
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003624 // OpenCL specific types:
3625 case tok::kw_image1d_t:
3626 case tok::kw_image1d_array_t:
3627 case tok::kw_image1d_buffer_t:
3628 case tok::kw_image2d_t:
3629 case tok::kw_image2d_array_t:
3630 case tok::kw_image3d_t:
3631
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003632 // struct-or-union-specifier (C99) or class-specifier (C++)
3633 case tok::kw_class:
3634 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003635 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003636 case tok::kw_union:
3637 // enum-specifier
3638 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003639
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003640 // typedef-name
3641 case tok::annot_typename:
3642 return true;
3643 }
3644}
3645
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003646/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003647/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003648bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003649 switch (Tok.getKind()) {
3650 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003651
Chris Lattner020bab92009-01-04 23:41:41 +00003652 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003653 if (TryAltiVecVectorToken())
3654 return true;
3655 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003656 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003657 // Annotate typenames and C++ scope specifiers. If we get one, just
3658 // recurse to handle whatever we get.
3659 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003660 return true;
3661 if (Tok.is(tok::identifier))
3662 return false;
3663 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003664
Chris Lattner020bab92009-01-04 23:41:41 +00003665 case tok::coloncolon: // ::foo::bar
3666 if (NextToken().is(tok::kw_new) || // ::new
3667 NextToken().is(tok::kw_delete)) // ::delete
3668 return false;
3669
Chris Lattner020bab92009-01-04 23:41:41 +00003670 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003671 return true;
3672 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003673
Chris Lattnere37e2332006-08-15 04:50:22 +00003674 // GNU attributes support.
3675 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003676 // GNU typeof support.
3677 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003678
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003679 // type-specifiers
3680 case tok::kw_short:
3681 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003682 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003683 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003684 case tok::kw_signed:
3685 case tok::kw_unsigned:
3686 case tok::kw__Complex:
3687 case tok::kw__Imaginary:
3688 case tok::kw_void:
3689 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003690 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003691 case tok::kw_char16_t:
3692 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003693 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003694 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003695 case tok::kw_float:
3696 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003697 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003698 case tok::kw__Bool:
3699 case tok::kw__Decimal32:
3700 case tok::kw__Decimal64:
3701 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003702 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003703
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003704 // OpenCL specific types:
3705 case tok::kw_image1d_t:
3706 case tok::kw_image1d_array_t:
3707 case tok::kw_image1d_buffer_t:
3708 case tok::kw_image2d_t:
3709 case tok::kw_image2d_array_t:
3710 case tok::kw_image3d_t:
3711
Chris Lattner861a2262008-04-13 18:59:07 +00003712 // struct-or-union-specifier (C99) or class-specifier (C++)
3713 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003714 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003715 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003716 case tok::kw_union:
3717 // enum-specifier
3718 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003719
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003720 // type-qualifier
3721 case tok::kw_const:
3722 case tok::kw_volatile:
3723 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003724
John McCallea0a39e2012-11-14 00:49:39 +00003725 // Debugger support.
3726 case tok::kw___unknown_anytype:
3727
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003728 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003729 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003730 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003731
Chris Lattner409bf7d2008-10-20 00:25:30 +00003732 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3733 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003734 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003735
Steve Naroff44ac7772008-12-25 14:16:32 +00003736 case tok::kw___cdecl:
3737 case tok::kw___stdcall:
3738 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003739 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003740 case tok::kw___w64:
3741 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003742 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003743 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003744 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003745
3746 case tok::kw___private:
3747 case tok::kw___local:
3748 case tok::kw___global:
3749 case tok::kw___constant:
3750 case tok::kw___read_only:
3751 case tok::kw___read_write:
3752 case tok::kw___write_only:
3753
Eli Friedman53339e02009-06-08 23:27:34 +00003754 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003755
3756 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003757 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003758
Benjamin Kramere56f3932011-12-23 17:00:35 +00003759 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003760 case tok::kw__Atomic:
3761 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003762 }
3763}
3764
Chris Lattneracd58a32006-08-06 17:24:14 +00003765/// isDeclarationSpecifier() - Return true if the current token is part of a
3766/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003767///
3768/// \param DisambiguatingWithExpression True to indicate that the purpose of
3769/// this check is to disambiguate between an expression and a declaration.
3770bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003771 switch (Tok.getKind()) {
3772 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003773
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003774 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003775 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003776
Chris Lattner020bab92009-01-04 23:41:41 +00003777 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003778 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003779 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00003780 return false;
John Thompson22334602010-02-05 00:12:22 +00003781 if (TryAltiVecVectorToken())
3782 return true;
3783 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00003784 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00003785 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003786 // Annotate typenames and C++ scope specifiers. If we get one, just
3787 // recurse to handle whatever we get.
3788 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003789 return true;
3790 if (Tok.is(tok::identifier))
3791 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003792
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003793 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00003794 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003795 // expression is permitted, then this is probably a class message send
3796 // missing the initial '['. In this case, we won't consider this to be
3797 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00003798 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003799 isStartOfObjCClassMessageMissingOpenBracket())
3800 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003801
John McCall1f476a12010-02-26 08:45:28 +00003802 return isDeclarationSpecifier();
3803
Chris Lattner020bab92009-01-04 23:41:41 +00003804 case tok::coloncolon: // ::foo::bar
3805 if (NextToken().is(tok::kw_new) || // ::new
3806 NextToken().is(tok::kw_delete)) // ::delete
3807 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003808
Chris Lattner020bab92009-01-04 23:41:41 +00003809 // Annotate typenames and C++ scope specifiers. If we get one, just
3810 // recurse to handle whatever we get.
3811 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003812 return true;
3813 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003814
Chris Lattneracd58a32006-08-06 17:24:14 +00003815 // storage-class-specifier
3816 case tok::kw_typedef:
3817 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003818 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003819 case tok::kw_static:
3820 case tok::kw_auto:
3821 case tok::kw_register:
3822 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003823
Douglas Gregor26701a42011-09-09 02:06:17 +00003824 // Modules
3825 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00003826
John McCallea0a39e2012-11-14 00:49:39 +00003827 // Debugger support
3828 case tok::kw___unknown_anytype:
3829
Chris Lattneracd58a32006-08-06 17:24:14 +00003830 // type-specifiers
3831 case tok::kw_short:
3832 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003833 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003834 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00003835 case tok::kw_signed:
3836 case tok::kw_unsigned:
3837 case tok::kw__Complex:
3838 case tok::kw__Imaginary:
3839 case tok::kw_void:
3840 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003841 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003842 case tok::kw_char16_t:
3843 case tok::kw_char32_t:
3844
Chris Lattneracd58a32006-08-06 17:24:14 +00003845 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003846 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00003847 case tok::kw_float:
3848 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003849 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003850 case tok::kw__Bool:
3851 case tok::kw__Decimal32:
3852 case tok::kw__Decimal64:
3853 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003854 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003855
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003856 // OpenCL specific types:
3857 case tok::kw_image1d_t:
3858 case tok::kw_image1d_array_t:
3859 case tok::kw_image1d_buffer_t:
3860 case tok::kw_image2d_t:
3861 case tok::kw_image2d_array_t:
3862 case tok::kw_image3d_t:
3863
Chris Lattner861a2262008-04-13 18:59:07 +00003864 // struct-or-union-specifier (C99) or class-specifier (C++)
3865 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003866 case tok::kw_struct:
3867 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00003868 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00003869 // enum-specifier
3870 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003871
Chris Lattneracd58a32006-08-06 17:24:14 +00003872 // type-qualifier
3873 case tok::kw_const:
3874 case tok::kw_volatile:
3875 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003876
Chris Lattneracd58a32006-08-06 17:24:14 +00003877 // function-specifier
3878 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003879 case tok::kw_virtual:
3880 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003881
Richard Smithd16fe122012-10-25 00:00:53 +00003882 // friend keyword.
3883 case tok::kw_friend:
3884
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003885 // static_assert-declaration
3886 case tok::kw__Static_assert:
3887
Chris Lattner599e47e2007-08-09 17:01:07 +00003888 // GNU typeof support.
3889 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003890
Chris Lattner599e47e2007-08-09 17:01:07 +00003891 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003892 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00003893
Richard Smithd16fe122012-10-25 00:00:53 +00003894 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00003895 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00003896 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00003897
Benjamin Kramere56f3932011-12-23 17:00:35 +00003898 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003899 case tok::kw__Atomic:
3900 return true;
3901
Chris Lattner8b2ec162008-07-26 03:38:44 +00003902 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3903 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003904 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003905
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003906 // typedef-name
3907 case tok::annot_typename:
3908 return !DisambiguatingWithExpression ||
3909 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00003910
Steve Narofff192fab2009-01-06 19:34:12 +00003911 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003912 case tok::kw___cdecl:
3913 case tok::kw___stdcall:
3914 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003915 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003916 case tok::kw___w64:
3917 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003918 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003919 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003920 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003921 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003922
3923 case tok::kw___private:
3924 case tok::kw___local:
3925 case tok::kw___global:
3926 case tok::kw___constant:
3927 case tok::kw___read_only:
3928 case tok::kw___read_write:
3929 case tok::kw___write_only:
3930
Eli Friedman53339e02009-06-08 23:27:34 +00003931 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003932 }
3933}
3934
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003935bool Parser::isConstructorDeclarator() {
3936 TentativeParsingAction TPA(*this);
3937
3938 // Parse the C++ scope specifier.
3939 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00003940 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003941 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00003942 TPA.Revert();
3943 return false;
3944 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003945
3946 // Parse the constructor name.
3947 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3948 // We already know that we have a constructor name; just consume
3949 // the token.
3950 ConsumeToken();
3951 } else {
3952 TPA.Revert();
3953 return false;
3954 }
3955
Richard Smith43f340f2012-03-27 23:05:05 +00003956 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003957 if (Tok.isNot(tok::l_paren)) {
3958 TPA.Revert();
3959 return false;
3960 }
3961 ConsumeParen();
3962
Richard Smith43f340f2012-03-27 23:05:05 +00003963 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3964 // that we have a constructor.
3965 if (Tok.is(tok::r_paren) ||
3966 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003967 TPA.Revert();
3968 return true;
3969 }
3970
3971 // If we need to, enter the specified scope.
3972 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003973 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003974 DeclScopeObj.EnterDeclaratorScope();
3975
Francois Pichet79f3a872011-01-31 04:54:32 +00003976 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003977 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003978 MaybeParseMicrosoftAttributes(Attrs);
3979
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003980 // Check whether the next token(s) are part of a declaration
3981 // specifier, in which case we have the start of a parameter and,
3982 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00003983 bool IsConstructor = false;
3984 if (isDeclarationSpecifier())
3985 IsConstructor = true;
3986 else if (Tok.is(tok::identifier) ||
3987 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3988 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3989 // This might be a parenthesized member name, but is more likely to
3990 // be a constructor declaration with an invalid argument type. Keep
3991 // looking.
3992 if (Tok.is(tok::annot_cxxscope))
3993 ConsumeToken();
3994 ConsumeToken();
3995
3996 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00003997 // which must have one of the following syntactic forms (see the
3998 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00003999 switch (Tok.getKind()) {
4000 case tok::l_paren:
4001 // C(X ( int));
4002 case tok::l_square:
4003 // C(X [ 5]);
4004 // C(X [ [attribute]]);
4005 case tok::coloncolon:
4006 // C(X :: Y);
4007 // C(X :: *p);
4008 case tok::r_paren:
4009 // C(X )
4010 // Assume this isn't a constructor, rather than assuming it's a
4011 // constructor with an unnamed parameter of an ill-formed type.
4012 break;
4013
4014 default:
4015 IsConstructor = true;
4016 break;
4017 }
4018 }
4019
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004020 TPA.Revert();
4021 return IsConstructor;
4022}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004023
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004024/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004025/// type-qualifier-list: [C99 6.7.5]
4026/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004027/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004028/// [ only if VendorAttributesAllowed=true ]
4029/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004030/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004031/// [ only if VendorAttributesAllowed=true ]
4032/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004033/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004034/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004035///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004036void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4037 bool VendorAttributesAllowed,
Richard Smith3dff2512012-04-10 03:25:07 +00004038 bool CXX11AttributesAllowed) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004039 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004040 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004041 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004042 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004043 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004044 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004045
4046 SourceLocation EndLoc;
4047
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004048 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004049 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004050 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004051 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004052 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004053
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004054 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004055 case tok::code_completion:
4056 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004057 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004058
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004059 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004060 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004061 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004062 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004063 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004064 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004065 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004066 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004067 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004068 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004069 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004070 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004071
4072 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004073 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004074 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004075 goto DoneWithTypeQuals;
4076 case tok::kw___private:
4077 case tok::kw___global:
4078 case tok::kw___local:
4079 case tok::kw___constant:
4080 case tok::kw___read_only:
4081 case tok::kw___write_only:
4082 case tok::kw___read_write:
4083 ParseOpenCLQualifiers(DS);
4084 break;
4085
Eli Friedman53339e02009-06-08 23:27:34 +00004086 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004087 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004088 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004089 case tok::kw___cdecl:
4090 case tok::kw___stdcall:
4091 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004092 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004093 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004094 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004095 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004096 continue;
4097 }
4098 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004099 case tok::kw___pascal:
4100 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004101 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004102 continue;
4103 }
4104 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004105 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004106 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004107 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004108 continue; // do *not* consume the next token!
4109 }
4110 // otherwise, FALL THROUGH!
4111 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004112 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004113 // If this is not a type-qualifier token, we're done reading type
4114 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004115 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004116 if (EndLoc.isValid())
4117 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004118 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004119 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004120
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004121 // If the specifier combination wasn't legal, issue a diagnostic.
4122 if (isInvalid) {
4123 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004124 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004125 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004126 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004127 }
4128}
4129
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004130
4131/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4132///
4133void Parser::ParseDeclarator(Declarator &D) {
4134 /// This implements the 'declarator' production in the C grammar, then checks
4135 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004136 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004137}
4138
Richard Smith0efa75c2012-03-29 01:16:42 +00004139static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4140 if (Kind == tok::star || Kind == tok::caret)
4141 return true;
4142
4143 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4144 if (!Lang.CPlusPlus)
4145 return false;
4146
4147 return Kind == tok::amp || Kind == tok::ampamp;
4148}
4149
Sebastian Redlbd150f42008-11-21 19:14:01 +00004150/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4151/// is parsed by the function passed to it. Pass null, and the direct-declarator
4152/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004153/// ptr-operator production.
4154///
Richard Smith09f76ee2011-10-19 21:33:05 +00004155/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004156/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4157/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004158///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004159/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4160/// [C] pointer[opt] direct-declarator
4161/// [C++] direct-declarator
4162/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004163///
4164/// pointer: [C99 6.7.5]
4165/// '*' type-qualifier-list[opt]
4166/// '*' type-qualifier-list[opt] pointer
4167///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004168/// ptr-operator:
4169/// '*' cv-qualifier-seq[opt]
4170/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004171/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004172/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004173/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004174/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004175void Parser::ParseDeclaratorInternal(Declarator &D,
4176 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004177 if (Diags.hasAllExtensionsSilenced())
4178 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004179
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004180 // C++ member pointers start with a '::' or a nested-name.
4181 // Member pointers get special handling, since there's no place for the
4182 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004183 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004184 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4185 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004186 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4187 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004188 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004189 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004190
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004191 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004192 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004193 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004194 if (D.mayHaveIdentifier())
4195 D.getCXXScopeSpec() = SS;
4196 else
4197 AnnotateScopeToken(SS, true);
4198
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004199 if (DirectDeclParser)
4200 (this->*DirectDeclParser)(D);
4201 return;
4202 }
4203
4204 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004205 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004206 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004207 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004208 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004209
4210 // Recurse to parse whatever is left.
4211 ParseDeclaratorInternal(D, DirectDeclParser);
4212
4213 // Sema will have to catch (syntactically invalid) pointers into global
4214 // scope. It has to catch pointers into namespace scope anyway.
4215 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004216 Loc),
4217 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004218 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004219 return;
4220 }
4221 }
4222
4223 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004224 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004225 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004226 if (DirectDeclParser)
4227 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004228 return;
4229 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004230
Sebastian Redled0f3b02009-03-15 22:02:01 +00004231 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4232 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004233 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004234 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004235
Chris Lattner9eac9312009-03-27 04:18:06 +00004236 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004237 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004238 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004239
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004240 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004241 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004242 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004243
Bill Wendling3708c182007-05-27 10:15:43 +00004244 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004245 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004246 if (Kind == tok::star)
4247 // Remember that we parsed a pointer type, and remember the type-quals.
4248 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004249 DS.getConstSpecLoc(),
4250 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004251 DS.getRestrictSpecLoc()),
4252 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004253 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004254 else
4255 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004256 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004257 Loc),
4258 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004259 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004260 } else {
4261 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004262 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004263
Sebastian Redl3b27be62009-03-23 00:00:23 +00004264 // Complain about rvalue references in C++03, but then go on and build
4265 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004266 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004267 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004268 diag::warn_cxx98_compat_rvalue_reference :
4269 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004270
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004271 // GNU-style and C++11 attributes are allowed here, as is restrict.
4272 ParseTypeQualifierListOpt(DS);
4273 D.ExtendWithDeclSpec(DS);
4274
Bill Wendling93efb222007-06-02 23:28:54 +00004275 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4276 // cv-qualifiers are introduced through the use of a typedef or of a
4277 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004278 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4279 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4280 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004281 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004282 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4283 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004284 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00004285 }
Bill Wendling3708c182007-05-27 10:15:43 +00004286
4287 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004288 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004289
Douglas Gregor66583c52008-11-03 15:51:28 +00004290 if (D.getNumTypeObjects() > 0) {
4291 // C++ [dcl.ref]p4: There shall be no references to references.
4292 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4293 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004294 if (const IdentifierInfo *II = D.getIdentifier())
4295 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4296 << II;
4297 else
4298 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4299 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004300
Sebastian Redlbd150f42008-11-21 19:14:01 +00004301 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004302 // can go ahead and build the (technically ill-formed)
4303 // declarator: reference collapsing will take care of it.
4304 }
4305 }
4306
Bill Wendling3708c182007-05-27 10:15:43 +00004307 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00004308 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004309 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004310 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004311 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004312 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004313}
4314
Richard Smith0efa75c2012-03-29 01:16:42 +00004315static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4316 SourceLocation EllipsisLoc) {
4317 if (EllipsisLoc.isValid()) {
4318 FixItHint Insertion;
4319 if (!D.getEllipsisLoc().isValid()) {
4320 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4321 D.setEllipsisLoc(EllipsisLoc);
4322 }
4323 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4324 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4325 }
4326}
4327
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004328/// ParseDirectDeclarator
4329/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004330/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004331/// '(' declarator ')'
4332/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004333/// [C90] direct-declarator '[' constant-expression[opt] ']'
4334/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4335/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4336/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4337/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004338/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4339/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004340/// direct-declarator '(' parameter-type-list ')'
4341/// direct-declarator '(' identifier-list[opt] ')'
4342/// [GNU] direct-declarator '(' parameter-forward-declarations
4343/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004344/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4345/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004346/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4347/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4348/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004349/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004350/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004351///
4352/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004353/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004354/// '::'[opt] nested-name-specifier[opt] type-name
4355///
4356/// id-expression: [C++ 5.1]
4357/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004358/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004359///
4360/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004361/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004362/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004363/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004364/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004365/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004366///
Richard Smith1453e312012-03-27 01:42:32 +00004367/// Note, any additional constructs added here may need corresponding changes
4368/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004369void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004370 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004371
David Blaikiebbafb8a2012-03-11 07:00:24 +00004372 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004373 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004374 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004375 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4376 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004377 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004378 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004379 }
4380
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004381 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004382 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004383 // Change the declaration context for name lookup, until this function
4384 // is exited (and the declarator has been parsed).
4385 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004386 }
4387
Douglas Gregor27b4c162010-12-23 22:44:42 +00004388 // C++0x [dcl.fct]p14:
4389 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004390 // of a parameter-declaration-clause without a preceding comma. In
4391 // this case, the ellipsis is parsed as part of the
4392 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004393 // parameter pack that has not been expanded; otherwise, it is parsed
4394 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004395 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004396 !((D.getContext() == Declarator::PrototypeContext ||
4397 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004398 NextToken().is(tok::r_paren) &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004399 !Actions.containsUnexpandedParameterPacks(D))) {
4400 SourceLocation EllipsisLoc = ConsumeToken();
4401 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4402 // The ellipsis was put in the wrong place. Recover, and explain to
4403 // the user what they should have done.
4404 ParseDeclarator(D);
4405 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4406 return;
4407 } else
4408 D.setEllipsisLoc(EllipsisLoc);
4409
4410 // The ellipsis can't be followed by a parenthesized declarator. We
4411 // check for that in ParseParenDeclarator, after we have disambiguated
4412 // the l_paren token.
4413 }
4414
Douglas Gregor7861a802009-11-03 01:35:08 +00004415 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4416 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4417 // We found something that indicates the start of an unqualified-id.
4418 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004419 bool AllowConstructorName;
4420 if (D.getDeclSpec().hasTypeSpecifier())
4421 AllowConstructorName = false;
4422 else if (D.getCXXScopeSpec().isSet())
4423 AllowConstructorName =
4424 (D.getContext() == Declarator::FileContext ||
4425 (D.getContext() == Declarator::MemberContext &&
4426 D.getDeclSpec().isFriendSpecified()));
4427 else
4428 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4429
Abramo Bagnara7945c982012-01-27 09:46:47 +00004430 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004431 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4432 /*EnteringContext=*/true,
4433 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004434 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004435 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004436 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004437 D.getName()) ||
4438 // Once we're past the identifier, if the scope was bad, mark the
4439 // whole declarator bad.
4440 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004441 D.SetIdentifier(0, Tok.getLocation());
4442 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004443 } else {
4444 // Parsed the unqualified-id; update range information and move along.
4445 if (D.getSourceRange().getBegin().isInvalid())
4446 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4447 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004448 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004449 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004450 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004451 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004452 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004453 "There's a C++-specific check for tok::identifier above");
4454 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4455 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4456 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004457 goto PastIdentifier;
4458 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004459
Douglas Gregor7861a802009-11-03 01:35:08 +00004460 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004461 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004462 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004463 // Example: 'char (*X)' or 'int (*XX)(void)'
4464 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004465
4466 // If the declarator was parenthesized, we entered the declarator
4467 // scope when parsing the parenthesized declarator, then exited
4468 // the scope already. Re-enter the scope, if we need to.
4469 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004470 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004471 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004472 if (!D.isInvalidType() &&
4473 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004474 // Change the declaration context for name lookup, until this function
4475 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004476 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004477 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004478 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004479 // This could be something simple like "int" (in which case the declarator
4480 // portion is empty), if an abstract-declarator is allowed.
4481 D.SetIdentifier(0, Tok.getLocation());
4482 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004483 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004484 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004485 if (D.getContext() == Declarator::MemberContext)
4486 Diag(Tok, diag::err_expected_member_name_or_semi)
4487 << D.getDeclSpec().getSourceRange();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004488 else if (getLangOpts().CPlusPlus)
4489 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004490 else
Chris Lattner6d29c102008-11-18 07:48:38 +00004491 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004492 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004493 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004494 }
Mike Stump11289f42009-09-09 15:08:12 +00004495
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004496 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004497 assert(D.isPastIdentifier() &&
4498 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004499
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004500 // Don't parse attributes unless we have parsed an unparenthesized name.
4501 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004502 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004503
Chris Lattneracd58a32006-08-06 17:24:14 +00004504 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004505 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004506 // Enter function-declaration scope, limiting any declarators to the
4507 // function prototype scope, including parameter declarators.
4508 ParseScope PrototypeScope(this,
4509 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004510 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4511 // In such a case, check if we actually have a function declarator; if it
4512 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004513 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004514 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4515 // The name of the declarator, if any, is tentatively declared within
4516 // a possible direct initializer.
4517 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4518 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4519 TentativelyDeclaredIdentifiers.pop_back();
4520 if (!IsFunctionDecl)
4521 break;
4522 }
John McCall084e83d2011-03-24 11:26:52 +00004523 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004524 BalancedDelimiterTracker T(*this, tok::l_paren);
4525 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004526 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004527 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004528 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004529 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004530 } else {
4531 break;
4532 }
4533 }
Chad Rosierc1183952012-06-26 22:30:43 +00004534}
Chris Lattneracd58a32006-08-06 17:24:14 +00004535
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004536/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4537/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004538/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004539/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4540///
4541/// direct-declarator:
4542/// '(' declarator ')'
4543/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004544/// direct-declarator '(' parameter-type-list ')'
4545/// direct-declarator '(' identifier-list[opt] ')'
4546/// [GNU] direct-declarator '(' parameter-forward-declarations
4547/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004548///
4549void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004550 BalancedDelimiterTracker T(*this, tok::l_paren);
4551 T.consumeOpen();
4552
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004553 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004554
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004555 // Eat any attributes before we look at whether this is a grouping or function
4556 // declarator paren. If this is a grouping paren, the attribute applies to
4557 // the type being built up, for example:
4558 // int (__attribute__(()) *x)(long y)
4559 // If this ends up not being a grouping paren, the attribute applies to the
4560 // first argument, for example:
4561 // int (__attribute__(()) int x)
4562 // In either case, we need to eat any attributes to be able to determine what
4563 // sort of paren this is.
4564 //
John McCall084e83d2011-03-24 11:26:52 +00004565 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004566 bool RequiresArg = false;
4567 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004568 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004569
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004570 // We require that the argument list (if this is a non-grouping paren) be
4571 // present even if the attribute list was empty.
4572 RequiresArg = true;
4573 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004574
Steve Naroff44ac7772008-12-25 14:16:32 +00004575 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004576 ParseMicrosoftTypeAttributes(attrs);
4577
Dawn Perchik335e16b2010-09-03 01:29:35 +00004578 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004579 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004580 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004581
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004582 // If we haven't past the identifier yet (or where the identifier would be
4583 // stored, if this is an abstract declarator), then this is probably just
4584 // grouping parens. However, if this could be an abstract-declarator, then
4585 // this could also be the start of function arguments (consider 'void()').
4586 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004587
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004588 if (!D.mayOmitIdentifier()) {
4589 // If this can't be an abstract-declarator, this *must* be a grouping
4590 // paren, because we haven't seen the identifier yet.
4591 isGrouping = true;
4592 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004593 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4594 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004595 isDeclarationSpecifier() || // 'int(int)' is a function.
4596 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004597 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4598 // considered to be a type, not a K&R identifier-list.
4599 isGrouping = false;
4600 } else {
4601 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4602 isGrouping = true;
4603 }
Mike Stump11289f42009-09-09 15:08:12 +00004604
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004605 // If this is a grouping paren, handle:
4606 // direct-declarator: '(' declarator ')'
4607 // direct-declarator: '(' attributes declarator ')'
4608 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004609 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4610 D.setEllipsisLoc(SourceLocation());
4611
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004612 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004613 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004614 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004615 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004616 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004617 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004618 T.getCloseLocation()),
4619 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004620
4621 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004622
4623 // An ellipsis cannot be placed outside parentheses.
4624 if (EllipsisLoc.isValid())
4625 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4626
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004627 return;
4628 }
Mike Stump11289f42009-09-09 15:08:12 +00004629
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004630 // Okay, if this wasn't a grouping paren, it must be the start of a function
4631 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004632 // identifier (and remember where it would have been), then call into
4633 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004634 D.SetIdentifier(0, Tok.getLocation());
4635
David Blaikie15a430a2011-12-04 05:04:18 +00004636 // Enter function-declaration scope, limiting any declarators to the
4637 // function prototype scope, including parameter declarators.
4638 ParseScope PrototypeScope(this,
4639 Scope::FunctionPrototypeScope|Scope::DeclScope);
Richard Smith943c4402012-07-30 21:30:52 +00004640 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004641 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004642}
4643
4644/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4645/// declarator D up to a paren, which indicates that we are parsing function
4646/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004647///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004648/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4649/// immediately after the open paren - they should be considered to be the
4650/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004651///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004652/// If RequiresArg is true, then the first argument of the function is required
4653/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004654///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004655/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4656/// (C++11) ref-qualifier[opt], exception-specification[opt],
4657/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4658///
4659/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004660/// dynamic-exception-specification
4661/// noexcept-specification
4662///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004663void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004664 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004665 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004666 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004667 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004668 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004669 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004670 // lparen is already consumed!
4671 assert(D.isPastIdentifier() && "Should not call before identifier!");
4672
4673 // This should be true when the function has typed arguments.
4674 // Otherwise, it is treated as a K&R-style function.
4675 bool HasProto = false;
4676 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004677 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004678 // Remember where we see an ellipsis, if any.
4679 SourceLocation EllipsisLoc;
4680
4681 DeclSpec DS(AttrFactory);
4682 bool RefQualifierIsLValueRef = true;
4683 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004684 SourceLocation ConstQualifierLoc;
4685 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004686 ExceptionSpecificationType ESpecType = EST_None;
4687 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004688 SmallVector<ParsedType, 2> DynamicExceptions;
4689 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004690 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004691 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004692 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004693
James Molloy6f8780b2012-02-29 10:24:19 +00004694 Actions.ActOnStartFunctionDeclarator();
4695
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004696 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4697 EndLoc is the end location for the function declarator.
4698 They differ for trailing return types. */
4699 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004700 SourceLocation LParenLoc, RParenLoc;
4701 LParenLoc = Tracker.getOpenLocation();
4702 StartLoc = LParenLoc;
4703
Douglas Gregor9e66af42011-07-05 16:44:18 +00004704 if (isFunctionDeclaratorIdentifierList()) {
4705 if (RequiresArg)
4706 Diag(Tok, diag::err_argument_required_after_attribute);
4707
4708 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4709
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004710 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004711 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004712 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004713 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004714 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004715 if (Tok.isNot(tok::r_paren))
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004716 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004717 else if (RequiresArg)
4718 Diag(Tok, diag::err_argument_required_after_attribute);
4719
David Blaikiebbafb8a2012-03-11 07:00:24 +00004720 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004721
4722 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004723 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004724 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004725 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004726 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004727
David Blaikiebbafb8a2012-03-11 07:00:24 +00004728 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004729 // FIXME: Accept these components in any order, and produce fixits to
4730 // correct the order if the user gets it wrong. Ideally we should deal
4731 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004732
4733 // Parse cv-qualifier-seq[opt].
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004734 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4735 if (!DS.getSourceRange().getEnd().isInvalid()) {
4736 EndLoc = DS.getSourceRange().getEnd();
4737 ConstQualifierLoc = DS.getConstSpecLoc();
4738 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4739 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004740
4741 // Parse ref-qualifier[opt].
4742 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004743 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004744 diag::warn_cxx98_compat_ref_qualifier :
4745 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004746
Douglas Gregor9e66af42011-07-05 16:44:18 +00004747 RefQualifierIsLValueRef = Tok.is(tok::amp);
4748 RefQualifierLoc = ConsumeToken();
4749 EndLoc = RefQualifierLoc;
4750 }
4751
Douglas Gregor3024f072012-04-16 07:05:22 +00004752 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00004753 // If a declaration declares a member function or member function
4754 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00004755 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00004756 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00004757 // declarator.
Chad Rosierc1183952012-06-26 22:30:43 +00004758 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004759 getLangOpts().CPlusPlus11 &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004760 (D.getContext() == Declarator::MemberContext ||
4761 (D.getContext() == Declarator::FileContext &&
Chad Rosierc1183952012-06-26 22:30:43 +00004762 D.getCXXScopeSpec().isValid() &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004763 Actions.CurContext->isRecord()));
4764 Sema::CXXThisScopeRAII ThisScope(Actions,
4765 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00004766 DS.getTypeQualifiers() |
4767 (D.getDeclSpec().isConstexprSpecified()
4768 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00004769 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00004770
Douglas Gregor9e66af42011-07-05 16:44:18 +00004771 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00004772 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00004773 DynamicExceptions,
4774 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00004775 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004776 if (ESpecType != EST_None)
4777 EndLoc = ESpecRange.getEnd();
4778
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004779 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4780 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00004781 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004782
Douglas Gregor9e66af42011-07-05 16:44:18 +00004783 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004784 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004785 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00004786 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004787 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4788 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004789 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004790 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00004791 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004792 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004793 }
4794 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004795 }
4796
4797 // Remember that we parsed a function type, and remember the attributes.
4798 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004799 IsAmbiguous,
4800 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004801 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004802 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004803 DS.getTypeQualifiers(),
4804 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00004805 RefQualifierLoc, ConstQualifierLoc,
4806 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00004807 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00004808 ESpecType, ESpecRange.getBegin(),
4809 DynamicExceptions.data(),
4810 DynamicExceptionRanges.data(),
4811 DynamicExceptions.size(),
4812 NoexceptExpr.isUsable() ?
4813 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004814 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004815 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004816 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00004817
4818 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004819}
4820
4821/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4822/// identifier list form for a K&R-style function: void foo(a,b,c)
4823///
4824/// Note that identifier-lists are only allowed for normal declarators, not for
4825/// abstract-declarators.
4826bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004827 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00004828 && Tok.is(tok::identifier)
4829 && !TryAltiVecVectorToken()
4830 // K&R identifier lists can't have typedefs as identifiers, per C99
4831 // 6.7.5.3p11.
4832 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4833 // Identifier lists follow a really simple grammar: the identifiers can
4834 // be followed *only* by a ", identifier" or ")". However, K&R
4835 // identifier lists are really rare in the brave new modern world, and
4836 // it is very common for someone to typo a type in a non-K&R style
4837 // list. If we are presented with something like: "void foo(intptr x,
4838 // float y)", we don't want to start parsing the function declarator as
4839 // though it is a K&R style declarator just because intptr is an
4840 // invalid type.
4841 //
4842 // To handle this, we check to see if the token after the first
4843 // identifier is a "," or ")". Only then do we parse it as an
4844 // identifier list.
4845 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4846}
4847
4848/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4849/// we found a K&R-style identifier list instead of a typed parameter list.
4850///
4851/// After returning, ParamInfo will hold the parsed parameters.
4852///
4853/// identifier-list: [C99 6.7.5]
4854/// identifier
4855/// identifier-list ',' identifier
4856///
4857void Parser::ParseFunctionDeclaratorIdentifierList(
4858 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004859 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004860 // If there was no identifier specified for the declarator, either we are in
4861 // an abstract-declarator, or we are in a parameter declarator which was found
4862 // to be abstract. In abstract-declarators, identifier lists are not valid:
4863 // diagnose this.
4864 if (!D.getIdentifier())
4865 Diag(Tok, diag::ext_ident_list_in_param);
4866
4867 // Maintain an efficient lookup of params we have seen so far.
4868 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4869
4870 while (1) {
4871 // If this isn't an identifier, report the error and skip until ')'.
4872 if (Tok.isNot(tok::identifier)) {
4873 Diag(Tok, diag::err_expected_ident);
4874 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4875 // Forget we parsed anything.
4876 ParamInfo.clear();
4877 return;
4878 }
4879
4880 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4881
4882 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4883 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4884 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4885
4886 // Verify that the argument identifier has not already been mentioned.
4887 if (!ParamsSoFar.insert(ParmII)) {
4888 Diag(Tok, diag::err_param_redefinition) << ParmII;
4889 } else {
4890 // Remember this identifier in ParamInfo.
4891 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4892 Tok.getLocation(),
4893 0));
4894 }
4895
4896 // Eat the identifier.
4897 ConsumeToken();
4898
4899 // The list continues if we see a comma.
4900 if (Tok.isNot(tok::comma))
4901 break;
4902 ConsumeToken();
4903 }
4904}
4905
4906/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4907/// after the opening parenthesis. This function will not parse a K&R-style
4908/// identifier list.
4909///
Richard Smith2620cd92012-04-11 04:01:28 +00004910/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4911/// caller parsed those arguments immediately after the open paren - they should
4912/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004913///
4914/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4915/// be the location of the ellipsis, if any was parsed.
4916///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004917/// parameter-type-list: [C99 6.7.5]
4918/// parameter-list
4919/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004920/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004921///
4922/// parameter-list: [C99 6.7.5]
4923/// parameter-declaration
4924/// parameter-list ',' parameter-declaration
4925///
4926/// parameter-declaration: [C99 6.7.5]
4927/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004928/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00004929/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00004930/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00004931/// declaration-specifiers abstract-declarator[opt]
4932/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00004933/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004934/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00004935/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004936///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004937void Parser::ParseParameterDeclarationClause(
4938 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00004939 ParsedAttributes &FirstArgAttrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004940 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004941 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00004942
Chris Lattner371ed4e2008-04-06 06:57:35 +00004943 while (1) {
4944 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00004945 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4946 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00004947 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00004948 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00004949 }
Mike Stump11289f42009-09-09 15:08:12 +00004950
Chris Lattner371ed4e2008-04-06 06:57:35 +00004951 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00004952 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00004953 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004954
Richard Smith2620cd92012-04-11 04:01:28 +00004955 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00004956 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00004957
John McCall53fa7142010-12-24 02:08:15 +00004958 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00004959 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00004960
4961 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004962
4963 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004964 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004965 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00004966 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4967 // too much hassle.
4968 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00004969
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004970 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004971
Chris Lattner371ed4e2008-04-06 06:57:35 +00004972 // Parse the declarator. This is "PrototypeContext", because we must
4973 // accept either 'declarator' or 'abstract-declarator' here.
4974 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4975 ParseDeclarator(ParmDecl);
4976
4977 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00004978 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00004979
Chris Lattner371ed4e2008-04-06 06:57:35 +00004980 // Remember this parsed parameter in ParamInfo.
4981 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00004982
Douglas Gregor4d87df52008-12-16 21:30:33 +00004983 // DefArgToks is used when the parsing of default arguments needs
4984 // to be delayed.
4985 CachedTokens *DefArgToks = 0;
4986
Chris Lattner371ed4e2008-04-06 06:57:35 +00004987 // If no parameter was specified, verify that *something* was specified,
4988 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004989 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4990 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00004991 // Completely missing, emit error.
4992 Diag(DSStart, diag::err_missing_param);
4993 } else {
4994 // Otherwise, we have something. Add it and let semantic analysis try
4995 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00004996
Chris Lattner371ed4e2008-04-06 06:57:35 +00004997 // Inform the actions module about the parameter declarator, so it gets
4998 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00004999 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005000
5001 // Parse the default argument, if any. We parse the default
5002 // arguments in all dialects; the semantic analysis in
5003 // ActOnParamDefaultArgument will reject the default argument in
5004 // C.
5005 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005006 SourceLocation EqualLoc = Tok.getLocation();
5007
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005008 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005009 if (D.getContext() == Declarator::MemberContext) {
5010 // If we're inside a class definition, cache the tokens
5011 // corresponding to the default argument. We'll actually parse
5012 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005013 // FIXME: Can we use a smart pointer for Toks?
5014 DefArgToks = new CachedTokens;
5015
Mike Stump11289f42009-09-09 15:08:12 +00005016 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00005017 /*StopAtSemi=*/true,
5018 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005019 delete DefArgToks;
5020 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005021 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005022 } else {
5023 // Mark the end of the default argument so that we know when to
5024 // stop when we parse it later on.
5025 Token DefArgEnd;
5026 DefArgEnd.startToken();
5027 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5028 DefArgEnd.setLocation(Tok.getLocation());
5029 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005030 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005031 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005032 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005033 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005034 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005035 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005036
Chad Rosierc1183952012-06-26 22:30:43 +00005037 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005038 // used.
5039 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005040 Sema::PotentiallyEvaluatedIfUsed,
5041 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005042
Sebastian Redldb63af22012-03-14 15:54:00 +00005043 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005044 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005045 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005046 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005047 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005048 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005049 if (DefArgResult.isInvalid()) {
5050 Actions.ActOnParamDefaultArgumentError(Param);
5051 SkipUntil(tok::comma, tok::r_paren, true, true);
5052 } else {
5053 // Inform the actions module about the default argument
5054 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005055 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005056 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005057 }
5058 }
Mike Stump11289f42009-09-09 15:08:12 +00005059
5060 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5061 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00005062 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005063 }
5064
5065 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005066 if (Tok.isNot(tok::comma)) {
5067 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005068 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005069
David Blaikiebbafb8a2012-03-11 07:00:24 +00005070 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005071 // We have ellipsis without a preceding ',', which is ill-formed
5072 // in C. Complain and provide the fix.
5073 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005074 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005075 }
5076 }
Chad Rosierc1183952012-06-26 22:30:43 +00005077
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005078 break;
5079 }
Mike Stump11289f42009-09-09 15:08:12 +00005080
Chris Lattner371ed4e2008-04-06 06:57:35 +00005081 // Consume the comma.
5082 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005083 }
Mike Stump11289f42009-09-09 15:08:12 +00005084
Chris Lattner6c940e62008-04-06 06:34:08 +00005085}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005086
Chris Lattnere8074e62006-08-06 18:30:15 +00005087/// [C90] direct-declarator '[' constant-expression[opt] ']'
5088/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5089/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5090/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5091/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005092/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5093/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005094void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005095 if (CheckProhibitedCXX11Attribute())
5096 return;
5097
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005098 BalancedDelimiterTracker T(*this, tok::l_square);
5099 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005100
Chris Lattner84a11622008-12-18 07:27:21 +00005101 // C array syntax has many features, but by-far the most common is [] and [4].
5102 // This code does a fast path to handle some of the most obvious cases.
5103 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005104 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005105 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005106 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005107
Chris Lattner84a11622008-12-18 07:27:21 +00005108 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005109 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005110 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005111 T.getOpenLocation(),
5112 T.getCloseLocation()),
5113 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005114 return;
5115 } else if (Tok.getKind() == tok::numeric_constant &&
5116 GetLookAheadToken(1).is(tok::r_square)) {
5117 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005118 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005119 ConsumeToken();
5120
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005121 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005122 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005123 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005124
Chris Lattner84a11622008-12-18 07:27:21 +00005125 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005126 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005127 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005128 T.getOpenLocation(),
5129 T.getCloseLocation()),
5130 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005131 return;
5132 }
Mike Stump11289f42009-09-09 15:08:12 +00005133
Chris Lattnere8074e62006-08-06 18:30:15 +00005134 // If valid, this location is the position where we read the 'static' keyword.
5135 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005136 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005137 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005138
Chris Lattnere8074e62006-08-06 18:30:15 +00005139 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005140 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005141 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005142 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005143
Chris Lattnere8074e62006-08-06 18:30:15 +00005144 // If we haven't already read 'static', check to see if there is one after the
5145 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005146 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005147 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005148
Chris Lattnere8074e62006-08-06 18:30:15 +00005149 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005150 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005151 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005152
Chris Lattner521ff2b2008-04-06 05:26:30 +00005153 // Handle the case where we have '[*]' as the array size. However, a leading
5154 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005155 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005156 // infrequent, use of lookahead is not costly here.
5157 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005158 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005159
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005160 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005161 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005162 StaticLoc = SourceLocation(); // Drop the static.
5163 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005164 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005165 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005166 // Note, in C89, this production uses the constant-expr production instead
5167 // of assignment-expr. The only difference is that assignment-expr allows
5168 // things like '=' and '*='. Sema rejects these in C89 mode because they
5169 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005170
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005171 // Parse the constant-expression or assignment-expression now (depending
5172 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005173 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005174 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005175 } else {
5176 EnterExpressionEvaluationContext Unevaluated(Actions,
5177 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005178 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005179 }
Chris Lattner62591722006-08-12 18:40:58 +00005180 }
Mike Stump11289f42009-09-09 15:08:12 +00005181
Chris Lattner62591722006-08-12 18:40:58 +00005182 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005183 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005184 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005185 // If the expression was invalid, skip it.
5186 SkipUntil(tok::r_square);
5187 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005188 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005189
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005190 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005191
John McCall084e83d2011-03-24 11:26:52 +00005192 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005193 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005194
Chris Lattner84a11622008-12-18 07:27:21 +00005195 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005196 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005197 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005198 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005199 T.getOpenLocation(),
5200 T.getCloseLocation()),
5201 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005202}
5203
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005204/// [GNU] typeof-specifier:
5205/// typeof ( expressions )
5206/// typeof ( type-name )
5207/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005208///
5209void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005210 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005211 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005212 SourceLocation StartLoc = ConsumeToken();
5213
John McCalle8595032010-01-13 20:03:27 +00005214 const bool hasParens = Tok.is(tok::l_paren);
5215
Eli Friedman15681d62012-09-26 04:34:21 +00005216 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5217 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005218
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005219 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005220 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005221 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005222 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5223 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005224 if (hasParens)
5225 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005226
5227 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005228 // FIXME: Not accurate, the range gets one token more than it should.
5229 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005230 else
5231 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005232
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005233 if (isCastExpr) {
5234 if (!CastTy) {
5235 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005236 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005237 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005238
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005239 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005240 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005241 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5242 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005243 DiagID, CastTy))
5244 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005245 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005246 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005247
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005248 // If we get here, the operand to the typeof was an expresion.
5249 if (Operand.isInvalid()) {
5250 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005251 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005252 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005253
Eli Friedmane0afc982012-01-21 01:01:51 +00005254 // We might need to transform the operand if it is potentially evaluated.
5255 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5256 if (Operand.isInvalid()) {
5257 DS.SetTypeSpecError();
5258 return;
5259 }
5260
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005261 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005262 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005263 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5264 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005265 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005266 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005267}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005268
Benjamin Kramere56f3932011-12-23 17:00:35 +00005269/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005270/// _Atomic ( type-name )
5271///
5272void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5273 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5274
5275 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005276 BalancedDelimiterTracker T(*this, tok::l_paren);
5277 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedman0dfb8892011-10-06 23:00:33 +00005278 SkipUntil(tok::r_paren);
5279 return;
5280 }
5281
5282 TypeResult Result = ParseTypeName();
5283 if (Result.isInvalid()) {
5284 SkipUntil(tok::r_paren);
5285 return;
5286 }
5287
5288 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005289 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005290
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005291 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005292 return;
5293
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005294 DS.setTypeofParensRange(T.getRange());
5295 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005296
5297 const char *PrevSpec = 0;
5298 unsigned DiagID;
5299 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5300 DiagID, Result.release()))
5301 Diag(StartLoc, DiagID) << PrevSpec;
5302}
5303
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005304
5305/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5306/// from TryAltiVecVectorToken.
5307bool Parser::TryAltiVecVectorTokenOutOfLine() {
5308 Token Next = NextToken();
5309 switch (Next.getKind()) {
5310 default: return false;
5311 case tok::kw_short:
5312 case tok::kw_long:
5313 case tok::kw_signed:
5314 case tok::kw_unsigned:
5315 case tok::kw_void:
5316 case tok::kw_char:
5317 case tok::kw_int:
5318 case tok::kw_float:
5319 case tok::kw_double:
5320 case tok::kw_bool:
5321 case tok::kw___pixel:
5322 Tok.setKind(tok::kw___vector);
5323 return true;
5324 case tok::identifier:
5325 if (Next.getIdentifierInfo() == Ident_pixel) {
5326 Tok.setKind(tok::kw___vector);
5327 return true;
5328 }
5329 return false;
5330 }
5331}
5332
5333bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5334 const char *&PrevSpec, unsigned &DiagID,
5335 bool &isInvalid) {
5336 if (Tok.getIdentifierInfo() == Ident_vector) {
5337 Token Next = NextToken();
5338 switch (Next.getKind()) {
5339 case tok::kw_short:
5340 case tok::kw_long:
5341 case tok::kw_signed:
5342 case tok::kw_unsigned:
5343 case tok::kw_void:
5344 case tok::kw_char:
5345 case tok::kw_int:
5346 case tok::kw_float:
5347 case tok::kw_double:
5348 case tok::kw_bool:
5349 case tok::kw___pixel:
5350 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5351 return true;
5352 case tok::identifier:
5353 if (Next.getIdentifierInfo() == Ident_pixel) {
5354 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5355 return true;
5356 }
5357 break;
5358 default:
5359 break;
5360 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005361 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005362 DS.isTypeAltiVecVector()) {
5363 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5364 return true;
5365 }
5366 return false;
5367}