blob: 26175a50f53fe543d730c33713d13382456f3c53 [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"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000017#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000018#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000022#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000024#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// C99 6.7: Declarations.
29//===----------------------------------------------------------------------===//
30
Chris Lattnerf5fbd792006-08-10 23:56:11 +000031/// ParseTypeName
32/// type-name: [C99 6.7.6]
33/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000034///
35/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000036TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000037 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000038 AccessSpecifier AS,
39 Decl **OwnedType) {
Richard Smith62dad822012-03-15 01:02:11 +000040 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000041 if (DSC == DSC_normal)
42 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000043
Chris Lattnerf5fbd792006-08-10 23:56:11 +000044 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000045 DeclSpec DS(AttrFactory);
Richard Smithbfdb1082012-03-12 08:56:40 +000046 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000047 if (OwnedType)
48 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000049
Chris Lattnerf5fbd792006-08-10 23:56:11 +000050 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000051 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000052 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000053 if (Range)
54 *Range = DeclaratorInfo.getSourceRange();
55
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000056 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000057 return true;
58
Douglas Gregor0be31a22010-07-02 17:43:08 +000059 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000060}
61
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000062
63/// isAttributeLateParsed - Return true if the attribute has arguments that
64/// require late parsing.
65static bool isAttributeLateParsed(const IdentifierInfo &II) {
66 return llvm::StringSwitch<bool>(II.getName())
67#include "clang/Parse/AttrLateParsed.inc"
68 .Default(false);
69}
70
Alexis Hunt96d5c762009-11-21 08:43:09 +000071/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000072///
73/// [GNU] attributes:
74/// attribute
75/// attributes attribute
76///
77/// [GNU] attribute:
78/// '__attribute__' '(' '(' attribute-list ')' ')'
79///
80/// [GNU] attribute-list:
81/// attrib
82/// attribute_list ',' attrib
83///
84/// [GNU] attrib:
85/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000086/// attrib-name
87/// attrib-name '(' identifier ')'
88/// attrib-name '(' identifier ',' nonempty-expr-list ')'
89/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000090///
Steve Naroff0f2fe172007-06-01 17:11:19 +000091/// [GNU] attrib-name:
92/// identifier
93/// typespec
94/// typequal
95/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000096///
Steve Naroff0f2fe172007-06-01 17:11:19 +000097/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000098/// token lookahead. Comment from gcc: "If they start with an identifier
99/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +0000100/// start with that identifier; otherwise they are an expression list."
101///
Richard Smithb12bf692011-10-17 21:20:17 +0000102/// GCC does not require the ',' between attribs in an attribute-list.
103///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000104/// At the moment, I am not doing 2 token lookahead. I am also unaware of
105/// any attributes that don't work (based on my limited testing). Most
106/// attributes are very simple in practice. Until we find a bug, I don't see
107/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000108
John McCall53fa7142010-12-24 02:08:15 +0000109void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000110 SourceLocation *endLoc,
111 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000112 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000113
Chris Lattner76c72282007-10-09 17:33:22 +0000114 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000115 ConsumeToken();
116 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
117 "attribute")) {
118 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000119 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000120 }
121 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
122 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000123 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000124 }
125 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000126 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
127 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000128 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000129 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
130 ConsumeToken();
131 continue;
132 }
133 // we have an identifier or declaration specifier (const, int, etc.)
134 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
135 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000136
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000137 if (Tok.is(tok::l_paren)) {
138 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000139 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000140 LateParsedAttribute *LA =
141 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
142 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000143
144 // Attributes in a class are parsed at the end of the class, along
145 // with other late-parsed declarations.
146 if (!ClassStack.empty())
147 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000148
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000149 // consume everything up to and including the matching right parens
150 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000151
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000152 Token Eof;
153 Eof.startToken();
154 Eof.setLocation(Tok.getLocation());
155 LA->Toks.push_back(Eof);
156 } else {
Michael Han23214e52012-10-03 01:56:22 +0000157 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000158 0, SourceLocation(), AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000159 }
160 } else {
John McCall084e83d2011-03-24 11:26:52 +0000161 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000162 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000163 }
164 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000165 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000166 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000167 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000168 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
169 SkipUntil(tok::r_paren, false);
170 }
John McCall53fa7142010-12-24 02:08:15 +0000171 if (endLoc)
172 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000173 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000174}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000175
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000176
Michael Han23214e52012-10-03 01:56:22 +0000177/// Parse the arguments to a parameterized GNU attribute or
178/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000179void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
180 SourceLocation AttrNameLoc,
181 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000182 SourceLocation *EndLoc,
183 IdentifierInfo *ScopeName,
184 SourceLocation ScopeLoc,
185 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000186
187 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
188
189 // Availability attributes have their own grammar.
190 if (AttrName->isStr("availability")) {
191 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
192 return;
193 }
194 // Thread safety attributes fit into the FIXME case above, so we
195 // just parse the arguments as a list of expressions
196 if (IsThreadSafetyAttribute(AttrName->getName())) {
197 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
198 return;
199 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000200 // Type safety attributes have their own grammar.
201 if (AttrName->isStr("type_tag_for_datatype")) {
202 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
203 return;
204 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000205
206 ConsumeParen(); // ignore the left paren loc for now
207
Richard Smithb12bf692011-10-17 21:20:17 +0000208 IdentifierInfo *ParmName = 0;
209 SourceLocation ParmLoc;
210 bool BuiltinType = false;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000211
Richard Smithb12bf692011-10-17 21:20:17 +0000212 switch (Tok.getKind()) {
213 case tok::kw_char:
214 case tok::kw_wchar_t:
215 case tok::kw_char16_t:
216 case tok::kw_char32_t:
217 case tok::kw_bool:
218 case tok::kw_short:
219 case tok::kw_int:
220 case tok::kw_long:
221 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000222 case tok::kw___int128:
Richard Smithb12bf692011-10-17 21:20:17 +0000223 case tok::kw_signed:
224 case tok::kw_unsigned:
225 case tok::kw_float:
226 case tok::kw_double:
227 case tok::kw_void:
228 case tok::kw_typeof:
229 // __attribute__(( vec_type_hint(char) ))
230 // FIXME: Don't just discard the builtin type token.
231 ConsumeToken();
232 BuiltinType = true;
233 break;
234
235 case tok::identifier:
236 ParmName = Tok.getIdentifierInfo();
237 ParmLoc = ConsumeToken();
238 break;
239
240 default:
241 break;
242 }
243
Benjamin Kramerf0623432012-08-23 22:51:59 +0000244 ExprVector ArgExprs;
Richard Smithb12bf692011-10-17 21:20:17 +0000245
246 if (!BuiltinType &&
247 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
248 // Eat the comma.
249 if (ParmLoc.isValid())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000250 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000251
Richard Smithb12bf692011-10-17 21:20:17 +0000252 // Parse the non-empty comma-separated list of expressions.
253 while (1) {
254 ExprResult ArgExpr(ParseAssignmentExpression());
255 if (ArgExpr.isInvalid()) {
256 SkipUntil(tok::r_paren);
257 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000258 }
Richard Smithb12bf692011-10-17 21:20:17 +0000259 ArgExprs.push_back(ArgExpr.release());
260 if (Tok.isNot(tok::comma))
261 break;
262 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000263 }
Richard Smithb12bf692011-10-17 21:20:17 +0000264 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000265 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
266 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
267 tok::greater)) {
Fariborz Jahanian7f733022011-10-18 23:13:50 +0000268 while (Tok.is(tok::identifier)) {
269 ConsumeToken();
270 if (Tok.is(tok::greater))
271 break;
272 if (Tok.is(tok::comma)) {
273 ConsumeToken();
274 continue;
275 }
276 }
277 if (Tok.isNot(tok::greater))
278 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000279 SkipUntil(tok::r_paren, false, true); // skip until ')'
280 }
281 }
Richard Smithb12bf692011-10-17 21:20:17 +0000282
283 SourceLocation RParen = Tok.getLocation();
284 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han360d2252012-10-04 16:42:52 +0000285 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithb12bf692011-10-17 21:20:17 +0000286 AttributeList *attr =
Michael Han360d2252012-10-04 16:42:52 +0000287 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen),
Michael Han23214e52012-10-03 01:56:22 +0000288 ScopeName, ScopeLoc, ParmName, ParmLoc,
289 ArgExprs.data(), ArgExprs.size(), Syntax);
Alexis Hunt3bc72c12012-06-19 23:57:03 +0000290 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithb12bf692011-10-17 21:20:17 +0000291 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000292 }
293}
294
Chad Rosierc1183952012-06-26 22:30:43 +0000295/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000296/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000297void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000298 SourceLocation AttrNameLoc,
299 ParsedAttributes &Attrs)
300{
301 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000302 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000303 AttrName->getNameStart(), tok::r_paren))
304 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000305
Aaron Ballman478faed2012-06-19 22:09:27 +0000306 ExprResult ArgExpr(ParseConstantExpression());
307 if (ArgExpr.isInvalid()) {
308 T.skipToEnd();
309 return;
310 }
311 Expr *ExprList = ArgExpr.take();
Chad Rosierc1183952012-06-26 22:30:43 +0000312 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000313 &ExprList, 1, AttributeList::AS_Declspec);
314
315 T.consumeClose();
316}
317
Chad Rosierc1183952012-06-26 22:30:43 +0000318/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000319/// arguments.
320bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
321 return llvm::StringSwitch<bool>(Ident->getName())
322 .Case("dllimport", true)
323 .Case("dllexport", true)
324 .Case("noreturn", true)
325 .Case("nothrow", true)
326 .Case("noinline", true)
327 .Case("naked", true)
328 .Case("appdomain", true)
329 .Case("process", true)
330 .Case("jitintrinsic", true)
331 .Case("noalias", true)
332 .Case("restrict", true)
333 .Case("novtable", true)
334 .Case("selectany", true)
335 .Case("thread", true)
336 .Default(false);
337}
338
Chad Rosierc1183952012-06-26 22:30:43 +0000339/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000340/// parameters). Will return false if we properly handled the declspec, or
341/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000342void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000343 SourceLocation Loc,
344 ParsedAttributes &Attrs) {
345 // Try to handle the easy case first -- these declspecs all take a single
346 // parameter as their argument.
347 if (llvm::StringSwitch<bool>(Ident->getName())
348 .Case("uuid", true)
349 .Case("align", true)
350 .Case("allocate", true)
351 .Default(false)) {
352 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
353 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000354 // The deprecated declspec has an optional single argument, so we will
355 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000356 // not.
357 if (Tok.getKind() == tok::l_paren)
358 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
359 else
Chad Rosierc1183952012-06-26 22:30:43 +0000360 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000361 AttributeList::AS_Declspec);
362 } else if (Ident->getName() == "property") {
363 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000364 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000365 // must be named get or put.
366 //
Chad Rosierc1183952012-06-26 22:30:43 +0000367 // For right now, we will just skip to the closing right paren of the
Aaron Ballman478faed2012-06-19 22:09:27 +0000368 // property expression.
369 //
370 // FIXME: we should deal with __declspec(property) at some point because it
371 // is used in the platform SDK headers for the Parallel Patterns Library
372 // and ATL.
373 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000374 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000375 Ident->getNameStart(), tok::r_paren))
376 return;
377 T.skipToEnd();
378 } else {
379 // We don't recognize this as a valid declspec, but instead of creating the
380 // attribute and allowing sema to warn about it, we will warn here instead.
381 // This is because some attributes have multiple spellings, but we need to
382 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000383 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000384 // both locations.
385 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
386
387 // If there's an open paren, we should eat the open and close parens under
388 // the assumption that this unknown declspec has parameters.
389 BalancedDelimiterTracker T(*this, tok::l_paren);
390 if (!T.consumeOpen())
391 T.skipToEnd();
392 }
393}
394
Eli Friedman06de2b52009-06-08 07:21:15 +0000395/// [MS] decl-specifier:
396/// __declspec ( extended-decl-modifier-seq )
397///
398/// [MS] extended-decl-modifier-seq:
399/// extended-decl-modifier[opt]
400/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000401void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000402 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000403
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000404 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000405 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000406 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000407 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000408 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000409
Chad Rosierc1183952012-06-26 22:30:43 +0000410 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000411 // you can specify multiple attributes per declspec.
412 while (Tok.getKind() != tok::r_paren) {
413 // We expect either a well-known identifier or a generic string. Anything
414 // else is a malformed declspec.
415 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000416 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000417 Tok.getKind() != tok::kw_restrict) {
418 Diag(Tok, diag::err_ms_declspec_type);
419 T.skipToEnd();
420 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000421 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000422
423 IdentifierInfo *AttrName;
424 SourceLocation AttrNameLoc;
425 if (IsString) {
426 SmallString<8> StrBuffer;
427 bool Invalid = false;
428 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
429 if (Invalid) {
430 T.skipToEnd();
431 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000432 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000433 AttrName = PP.getIdentifierInfo(Str);
434 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000435 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000436 AttrName = Tok.getIdentifierInfo();
437 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000438 }
Chad Rosierc1183952012-06-26 22:30:43 +0000439
Aaron Ballman478faed2012-06-19 22:09:27 +0000440 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000441 // If we have a generic string, we will allow it because there is no
442 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000443 // (for instance, SAL declspecs in older versions of MSVC).
444 //
Chad Rosierc1183952012-06-26 22:30:43 +0000445 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000446 // arguments and can be turned into an attribute directly.
Chad Rosierc1183952012-06-26 22:30:43 +0000447 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000448 0, 0, AttributeList::AS_Declspec);
449 else
450 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000451 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000452 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000453}
454
John McCall53fa7142010-12-24 02:08:15 +0000455void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000456 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000457 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000458 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000459 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +0000460 Tok.is(tok::kw___ptr32) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000461 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()) {
738 Diag(Tok, diag::err_expected_string_literal);
739 SkipUntil(tok::r_paren);
740 return;
741 }
742 MessageExpr = ParseStringLiteralExpression();
743 break;
744 }
Chad Rosierc1183952012-06-26 22:30:43 +0000745
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000746 SourceRange VersionRange;
747 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000748
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000749 if (Version.empty()) {
750 SkipUntil(tok::r_paren);
751 return;
752 }
753
754 unsigned Index;
755 if (Keyword == Ident_introduced)
756 Index = Introduced;
757 else if (Keyword == Ident_deprecated)
758 Index = Deprecated;
759 else if (Keyword == Ident_obsoleted)
760 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000761 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000762 Index = Unknown;
763
764 if (Index < Unknown) {
765 if (!Changes[Index].KeywordLoc.isInvalid()) {
766 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000767 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000768 << SourceRange(Changes[Index].KeywordLoc,
769 Changes[Index].VersionRange.getEnd());
770 }
771
772 Changes[Index].KeywordLoc = KeywordLoc;
773 Changes[Index].Version = Version;
774 Changes[Index].VersionRange = VersionRange;
775 } else {
776 Diag(KeywordLoc, diag::err_availability_unknown_change)
777 << Keyword << VersionRange;
778 }
779
780 if (Tok.isNot(tok::comma))
781 break;
782
783 ConsumeToken();
784 } while (true);
785
786 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000787 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000788 return;
789
790 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000791 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000792
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000793 // The 'unavailable' availability cannot be combined with any other
794 // availability changes. Make sure that hasn't happened.
795 if (UnavailableLoc.isValid()) {
796 bool Complained = false;
797 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
798 if (Changes[Index].KeywordLoc.isValid()) {
799 if (!Complained) {
800 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
801 << SourceRange(Changes[Index].KeywordLoc,
802 Changes[Index].VersionRange.getEnd());
803 Complained = true;
804 }
805
806 // Clear out the availability.
807 Changes[Index] = AvailabilityChange();
808 }
809 }
810 }
811
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000812 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000813 attrs.addNew(&Availability,
814 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000815 0, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000816 Platform, PlatformLoc,
817 Changes[Introduced],
818 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000819 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000820 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000821 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000822}
823
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000824
825// Late Parsed Attributes:
826// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
827
828void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
829
830void Parser::LateParsedClass::ParseLexedAttributes() {
831 Self->ParseLexedAttributes(*Class);
832}
833
834void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000835 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000836}
837
838/// Wrapper class which calls ParseLexedAttribute, after setting up the
839/// scope appropriately.
840void Parser::ParseLexedAttributes(ParsingClass &Class) {
841 // Deal with templates
842 // FIXME: Test cases to make sure this does the right thing for templates.
843 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
844 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
845 HasTemplateScope);
846 if (HasTemplateScope)
847 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
848
Douglas Gregor3024f072012-04-16 07:05:22 +0000849 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000850 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000851 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000852 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
853 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
854
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000855 // Enter the scope of nested classes
856 if (!AlreadyHasClassScope)
857 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
858 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +0000859 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +0000860 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
861 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
862 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000863 }
Chad Rosierc1183952012-06-26 22:30:43 +0000864
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000865 if (!AlreadyHasClassScope)
866 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
867 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000868}
869
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000870
871/// \brief Parse all attributes in LAs, and attach them to Decl D.
872void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
873 bool EnterScope, bool OnDefinition) {
874 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +0000875 if (D)
876 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000877 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +0000878 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000879 }
880 LAs.clear();
881}
882
883
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000884/// \brief Finish parsing an attribute for which parsing was delayed.
885/// This will be called at the end of parsing a class declaration
886/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +0000887/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000888/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000889void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
890 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000891 // Save the current token position.
892 SourceLocation OrigLoc = Tok.getLocation();
893
894 // Append the current token at the end of the new token stream so that it
895 // doesn't get lost.
896 LA.Toks.push_back(Tok);
897 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
898 // Consume the previously pushed token.
899 ConsumeAnyToken();
900
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000901 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
902 Diag(Tok, diag::warn_attribute_on_function_definition)
903 << LA.AttrName.getName();
904 }
905
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000906 ParsedAttributes Attrs(AttrFactory);
907 SourceLocation endLoc;
908
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000909 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000910 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000911 NamedDecl *ND = dyn_cast<NamedDecl>(D);
912 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000913
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000914 // Allow 'this' within late-parsed attributes.
915 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
916 /*TypeQuals=*/0,
917 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000918
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000919 if (LA.Decls.size() == 1) {
920 // If the Decl is templatized, add template parameters to scope.
921 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
922 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
923 if (HasTemplateScope)
924 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000925
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000926 // If the Decl is on a function, add function parameters to the scope.
927 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
928 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
929 if (HasFunScope)
930 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000931
Michael Han23214e52012-10-03 01:56:22 +0000932 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000933 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000934
935 if (HasFunScope) {
936 Actions.ActOnExitFunctionContext();
937 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
938 }
939 if (HasTemplateScope) {
940 TempScope.Exit();
941 }
942 } else {
943 // If there are multiple decls, then the decl cannot be within the
944 // function scope.
Michael Han23214e52012-10-03 01:56:22 +0000945 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000946 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000947 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +0000948 } else {
949 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000950 }
951
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000952 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
953 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
954 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000955
956 if (Tok.getLocation() != OrigLoc) {
957 // Due to a parsing error, we either went over the cached tokens or
958 // there are still cached tokens left, so we skip the leftover tokens.
959 // Since this is an uncommon situation that should be avoided, use the
960 // expensive isBeforeInTranslationUnit call.
961 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
962 OrigLoc))
963 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +0000964 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000965 }
966}
967
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000968/// \brief Wrapper around a case statement checking if AttrName is
969/// one of the thread safety attributes
970bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
971 return llvm::StringSwitch<bool>(AttrName)
972 .Case("guarded_by", true)
973 .Case("guarded_var", true)
974 .Case("pt_guarded_by", true)
975 .Case("pt_guarded_var", true)
976 .Case("lockable", true)
977 .Case("scoped_lockable", true)
978 .Case("no_thread_safety_analysis", true)
979 .Case("acquired_after", true)
980 .Case("acquired_before", true)
981 .Case("exclusive_lock_function", true)
982 .Case("shared_lock_function", true)
983 .Case("exclusive_trylock_function", true)
984 .Case("shared_trylock_function", true)
985 .Case("unlock_function", true)
986 .Case("lock_returned", true)
987 .Case("locks_excluded", true)
988 .Case("exclusive_locks_required", true)
989 .Case("shared_locks_required", true)
990 .Default(false);
991}
992
993/// \brief Parse the contents of thread safety attributes. These
994/// should always be parsed as an expression list.
995///
996/// We need to special case the parsing due to the fact that if the first token
997/// of the first argument is an identifier, the main parse loop will store
998/// that token as a "parameter" and the rest of
999/// the arguments will be added to a list of "arguments". However,
1000/// subsequent tokens in the first argument are lost. We instead parse each
1001/// argument as an expression and add all arguments to the list of "arguments".
1002/// In future, we will take advantage of this special case to also
1003/// deal with some argument scoping issues here (for example, referring to a
1004/// function parameter in the attribute on that function).
1005void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1006 SourceLocation AttrNameLoc,
1007 ParsedAttributes &Attrs,
1008 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001009 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001010
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001011 BalancedDelimiterTracker T(*this, tok::l_paren);
1012 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001013
Benjamin Kramerf0623432012-08-23 22:51:59 +00001014 ExprVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001015 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001016
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001017 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001018 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001019 ExprResult ArgExpr(ParseAssignmentExpression());
1020 if (ArgExpr.isInvalid()) {
1021 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001022 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001023 break;
1024 } else {
1025 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001026 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001027 if (Tok.isNot(tok::comma))
1028 break;
1029 ConsumeToken(); // Eat the comma, move to the next argument
1030 }
1031 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001032 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001033 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramerf0623432012-08-23 22:51:59 +00001034 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001035 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001036 if (EndLoc)
1037 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001038}
1039
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001040void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1041 SourceLocation AttrNameLoc,
1042 ParsedAttributes &Attrs,
1043 SourceLocation *EndLoc) {
1044 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1045
1046 BalancedDelimiterTracker T(*this, tok::l_paren);
1047 T.consumeOpen();
1048
1049 if (Tok.isNot(tok::identifier)) {
1050 Diag(Tok, diag::err_expected_ident);
1051 T.skipToEnd();
1052 return;
1053 }
1054 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1055 SourceLocation ArgumentKindLoc = ConsumeToken();
1056
1057 if (Tok.isNot(tok::comma)) {
1058 Diag(Tok, diag::err_expected_comma);
1059 T.skipToEnd();
1060 return;
1061 }
1062 ConsumeToken();
1063
1064 SourceRange MatchingCTypeRange;
1065 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1066 if (MatchingCType.isInvalid()) {
1067 T.skipToEnd();
1068 return;
1069 }
1070
1071 bool LayoutCompatible = false;
1072 bool MustBeNull = false;
1073 while (Tok.is(tok::comma)) {
1074 ConsumeToken();
1075 if (Tok.isNot(tok::identifier)) {
1076 Diag(Tok, diag::err_expected_ident);
1077 T.skipToEnd();
1078 return;
1079 }
1080 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1081 if (Flag->isStr("layout_compatible"))
1082 LayoutCompatible = true;
1083 else if (Flag->isStr("must_be_null"))
1084 MustBeNull = true;
1085 else {
1086 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1087 T.skipToEnd();
1088 return;
1089 }
1090 ConsumeToken(); // consume flag
1091 }
1092
1093 if (!T.consumeClose()) {
1094 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1095 ArgumentKind, ArgumentKindLoc,
1096 MatchingCType.release(), LayoutCompatible,
1097 MustBeNull, AttributeList::AS_GNU);
1098 }
1099
1100 if (EndLoc)
1101 *EndLoc = T.getCloseLocation();
1102}
1103
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001104/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1105/// of a C++11 attribute-specifier in a location where an attribute is not
1106/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1107/// situation.
1108///
1109/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1110/// this doesn't appear to actually be an attribute-specifier, and the caller
1111/// should try to parse it.
1112bool Parser::DiagnoseProhibitedCXX11Attribute() {
1113 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1114
1115 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1116 case CAK_NotAttributeSpecifier:
1117 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1118 return false;
1119
1120 case CAK_InvalidAttributeSpecifier:
1121 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1122 return false;
1123
1124 case CAK_AttributeSpecifier:
1125 // Parse and discard the attributes.
1126 SourceLocation BeginLoc = ConsumeBracket();
1127 ConsumeBracket();
1128 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1129 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1130 SourceLocation EndLoc = ConsumeBracket();
1131 Diag(BeginLoc, diag::err_attributes_not_allowed)
1132 << SourceRange(BeginLoc, EndLoc);
1133 return true;
1134 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001135 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001136}
1137
John McCall53fa7142010-12-24 02:08:15 +00001138void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1139 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1140 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001141}
1142
Chris Lattner53361ac2006-08-10 05:19:57 +00001143/// ParseDeclaration - Parse a full 'declaration', which consists of
1144/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001145/// 'Context' should be a Declarator::TheContext value. This returns the
1146/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001147///
1148/// declaration: [C99 6.7]
1149/// block-declaration ->
1150/// simple-declaration
1151/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001152/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001153/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001154/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001155/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001156/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001157/// others... [FIXME]
1158///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001159Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1160 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001161 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001162 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001163 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001164 // Must temporarily exit the objective-c container scope for
1165 // parsing c none objective-c decls.
1166 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001167
John McCall48871652010-08-21 09:40:31 +00001168 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001169 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001170 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001171 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001172 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001173 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001174 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001175 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001176 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001177 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001178 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001179 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001180 SourceLocation InlineLoc = ConsumeToken();
1181 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1182 break;
1183 }
Chad Rosierc1183952012-06-26 22:30:43 +00001184 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001185 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001186 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001187 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001188 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001189 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001190 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001191 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001192 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001193 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001194 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001195 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001196 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001197 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001198 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001199 default:
John McCall53fa7142010-12-24 02:08:15 +00001200 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001201 }
Chad Rosierc1183952012-06-26 22:30:43 +00001202
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001203 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001204 // single decl, convert it now. Alias declarations can also declare a type;
1205 // include that too if it is present.
1206 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001207}
1208
1209/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1210/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001211/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1212/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001213///[C90/C++]init-declarator-list ';' [TODO]
1214/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001215///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001216/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001217/// attribute-specifier-seq[opt] type-specifier-seq declarator
1218///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001219/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001220/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001221///
1222/// If FRI is non-null, we might be parsing a for-range-declaration instead
1223/// of a simple-declaration. If we find that we are, we also parse the
1224/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001225Parser::DeclGroupPtrTy
1226Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1227 SourceLocation &DeclEnd,
1228 ParsedAttributesWithRange &attrs,
1229 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001230 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001231 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +00001232 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001233
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001234 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001235 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001236
Chris Lattner0e894622006-08-13 19:58:17 +00001237 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1238 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001239 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001240 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001241 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001242 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001243 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001244 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001245 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001246 }
Chad Rosierc1183952012-06-26 22:30:43 +00001247
1248 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001249}
Mike Stump11289f42009-09-09 15:08:12 +00001250
Richard Smith09f76ee2011-10-19 21:33:05 +00001251/// Returns true if this might be the start of a declarator, or a common typo
1252/// for a declarator.
1253bool Parser::MightBeDeclarator(unsigned Context) {
1254 switch (Tok.getKind()) {
1255 case tok::annot_cxxscope:
1256 case tok::annot_template_id:
1257 case tok::caret:
1258 case tok::code_completion:
1259 case tok::coloncolon:
1260 case tok::ellipsis:
1261 case tok::kw___attribute:
1262 case tok::kw_operator:
1263 case tok::l_paren:
1264 case tok::star:
1265 return true;
1266
1267 case tok::amp:
1268 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001269 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001270
Richard Smithc8a79032012-01-09 22:31:44 +00001271 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001272 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smithc8a79032012-01-09 22:31:44 +00001273 NextToken().is(tok::l_square);
1274
1275 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001276 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001277
Richard Smith09f76ee2011-10-19 21:33:05 +00001278 case tok::identifier:
1279 switch (NextToken().getKind()) {
1280 case tok::code_completion:
1281 case tok::coloncolon:
1282 case tok::comma:
1283 case tok::equal:
1284 case tok::equalequal: // Might be a typo for '='.
1285 case tok::kw_alignas:
1286 case tok::kw_asm:
1287 case tok::kw___attribute:
1288 case tok::l_brace:
1289 case tok::l_paren:
1290 case tok::l_square:
1291 case tok::less:
1292 case tok::r_brace:
1293 case tok::r_paren:
1294 case tok::r_square:
1295 case tok::semi:
1296 return true;
1297
1298 case tok::colon:
1299 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001300 // and in block scope it's probably a label. Inside a class definition,
1301 // this is a bit-field.
1302 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001303 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001304
1305 case tok::identifier: // Possible virt-specifier.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001306 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001307
1308 default:
1309 return false;
1310 }
1311
1312 default:
1313 return false;
1314 }
1315}
1316
Richard Smithb8caac82012-04-11 20:59:20 +00001317/// Skip until we reach something which seems like a sensible place to pick
1318/// up parsing after a malformed declaration. This will sometimes stop sooner
1319/// than SkipUntil(tok::r_brace) would, but will never stop later.
1320void Parser::SkipMalformedDecl() {
1321 while (true) {
1322 switch (Tok.getKind()) {
1323 case tok::l_brace:
1324 // Skip until matching }, then stop. We've probably skipped over
1325 // a malformed class or function definition or similar.
1326 ConsumeBrace();
1327 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1328 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1329 // This declaration isn't over yet. Keep skipping.
1330 continue;
1331 }
1332 if (Tok.is(tok::semi))
1333 ConsumeToken();
1334 return;
1335
1336 case tok::l_square:
1337 ConsumeBracket();
1338 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1339 continue;
1340
1341 case tok::l_paren:
1342 ConsumeParen();
1343 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1344 continue;
1345
1346 case tok::r_brace:
1347 return;
1348
1349 case tok::semi:
1350 ConsumeToken();
1351 return;
1352
1353 case tok::kw_inline:
1354 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001355 // a good place to pick back up parsing, except in an Objective-C
1356 // @interface context.
1357 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1358 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001359 return;
1360 break;
1361
1362 case tok::kw_namespace:
1363 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001364 // place to pick back up parsing, except in an Objective-C
1365 // @interface context.
1366 if (Tok.isAtStartOfLine() &&
1367 (!ParsingInObjCContainer || CurParsedObjCImpl))
1368 return;
1369 break;
1370
1371 case tok::at:
1372 // @end is very much like } in Objective-C contexts.
1373 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1374 ParsingInObjCContainer)
1375 return;
1376 break;
1377
1378 case tok::minus:
1379 case tok::plus:
1380 // - and + probably start new method declarations in Objective-C contexts.
1381 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001382 return;
1383 break;
1384
1385 case tok::eof:
1386 return;
1387
1388 default:
1389 break;
1390 }
1391
1392 ConsumeAnyToken();
1393 }
1394}
1395
John McCalld5a36322009-11-03 19:26:08 +00001396/// ParseDeclGroup - Having concluded that this is either a function
1397/// definition or a group of object declarations, actually parse the
1398/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001399Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1400 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001401 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001402 SourceLocation *DeclEnd,
1403 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001404 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001405 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001406 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001407
John McCalld5a36322009-11-03 19:26:08 +00001408 // Bail out if the first declarator didn't seem well-formed.
1409 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001410 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001411 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001412 }
Mike Stump11289f42009-09-09 15:08:12 +00001413
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001414 // Save late-parsed attributes for now; they need to be parsed in the
1415 // appropriate function scope after the function Decl has been constructed.
1416 LateParsedAttrList LateParsedAttrs;
1417 if (D.isFunctionDeclarator())
1418 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1419
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001420 // Check to see if we have a function *definition* which must have a body.
1421 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1422 // Look at the next token to make sure that this isn't a function
1423 // declaration. We have to check this because __attribute__ might be the
1424 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001425 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001426
Chris Lattner13901342010-07-11 22:42:07 +00001427 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +00001428 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1429 Diag(Tok, diag::err_function_declared_typedef);
1430
1431 // Recover by treating the 'typedef' as spurious.
1432 DS.ClearStorageClassSpecs();
1433 }
1434
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001435 Decl *TheDecl =
1436 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld5a36322009-11-03 19:26:08 +00001437 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +00001438 }
Chad Rosierc1183952012-06-26 22:30:43 +00001439
Chris Lattner13901342010-07-11 22:42:07 +00001440 if (isDeclarationSpecifier()) {
1441 // If there is an invalid declaration specifier right after the function
1442 // prototype, then we must be in a missing semicolon case where this isn't
1443 // actually a body. Just fall through into the code that handles it as a
1444 // prototype, and let the top-level code handle the erroneous declspec
1445 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +00001446 } else {
1447 Diag(Tok, diag::err_expected_fn_body);
1448 SkipUntil(tok::semi);
1449 return DeclGroupPtrTy();
1450 }
1451 }
1452
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001453 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001454 return DeclGroupPtrTy();
1455
1456 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1457 // must parse and analyze the for-range-initializer before the declaration is
1458 // analyzed.
1459 if (FRI && Tok.is(tok::colon)) {
1460 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +00001461 if (Tok.is(tok::l_brace))
1462 FRI->RangeExpr = ParseBraceInitializer();
1463 else
1464 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +00001465 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1466 Actions.ActOnCXXForRangeDecl(ThisDecl);
1467 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001468 D.complete(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001469 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1470 }
1471
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001472 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001473 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001474 if (LateParsedAttrs.size() > 0)
1475 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001476 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001477 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001478 DeclsInGroup.push_back(FirstDecl);
1479
Richard Smith09f76ee2011-10-19 21:33:05 +00001480 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001481
John McCalld5a36322009-11-03 19:26:08 +00001482 // If we don't have a comma, it is either the end of the list (a ';') or an
1483 // error, bail out.
1484 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001485 SourceLocation CommaLoc = ConsumeToken();
1486
1487 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1488 // This comma was followed by a line-break and something which can't be
1489 // the start of a declarator. The comma was probably a typo for a
1490 // semicolon.
1491 Diag(CommaLoc, diag::err_expected_semi_declaration)
1492 << FixItHint::CreateReplacement(CommaLoc, ";");
1493 ExpectSemi = false;
1494 break;
1495 }
John McCalld5a36322009-11-03 19:26:08 +00001496
1497 // Parse the next declarator.
1498 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001499 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001500
1501 // Accept attributes in an init-declarator. In the first declarator in a
1502 // declaration, these would be part of the declspec. In subsequent
1503 // declarators, they become part of the declarator itself, so that they
1504 // don't apply to declarators after *this* one. Examples:
1505 // short __attribute__((common)) var; -> declspec
1506 // short var __attribute__((common)); -> declarator
1507 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001508 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001509
1510 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001511 if (!D.isInvalidType()) {
1512 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1513 D.complete(ThisDecl);
1514 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001515 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001516 }
John McCalld5a36322009-11-03 19:26:08 +00001517 }
1518
1519 if (DeclEnd)
1520 *DeclEnd = Tok.getLocation();
1521
Richard Smith09f76ee2011-10-19 21:33:05 +00001522 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001523 ExpectAndConsumeSemi(Context == Declarator::FileContext
1524 ? diag::err_invalid_token_after_toplevel_declarator
1525 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001526 // Okay, there was no semicolon and one was expected. If we see a
1527 // declaration specifier, just assume it was missing and continue parsing.
1528 // Otherwise things are very confused and we skip to recover.
1529 if (!isDeclarationSpecifier()) {
1530 SkipUntil(tok::r_brace, true, true);
1531 if (Tok.is(tok::semi))
1532 ConsumeToken();
1533 }
John McCalld5a36322009-11-03 19:26:08 +00001534 }
1535
Douglas Gregor0be31a22010-07-02 17:43:08 +00001536 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001537 DeclsInGroup.data(),
1538 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001539}
1540
Richard Smith02e85f32011-04-14 22:09:26 +00001541/// Parse an optional simple-asm-expr and attributes, and attach them to a
1542/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001543bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001544 // If a simple-asm-expr is present, parse it.
1545 if (Tok.is(tok::kw_asm)) {
1546 SourceLocation Loc;
1547 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1548 if (AsmLabel.isInvalid()) {
1549 SkipUntil(tok::semi, true, true);
1550 return true;
1551 }
1552
1553 D.setAsmLabel(AsmLabel.release());
1554 D.SetRangeEnd(Loc);
1555 }
1556
1557 MaybeParseGNUAttributes(D);
1558 return false;
1559}
1560
Douglas Gregor23996282009-05-12 21:31:51 +00001561/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1562/// declarator'. This method parses the remainder of the declaration
1563/// (including any attributes or initializer, among other things) and
1564/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001565///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001566/// init-declarator: [C99 6.7]
1567/// declarator
1568/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001569/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1570/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001571/// [C++] declarator initializer[opt]
1572///
1573/// [C++] initializer:
1574/// [C++] '=' initializer-clause
1575/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001576/// [C++0x] '=' 'default' [TODO]
1577/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001578/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001579///
1580/// According to the standard grammar, =default and =delete are function
1581/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001582///
John McCall48871652010-08-21 09:40:31 +00001583Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001584 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001585 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001586 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001587
Richard Smith02e85f32011-04-14 22:09:26 +00001588 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1589}
Mike Stump11289f42009-09-09 15:08:12 +00001590
Richard Smith02e85f32011-04-14 22:09:26 +00001591Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1592 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001593 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001594 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001595 switch (TemplateInfo.Kind) {
1596 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001597 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001598 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001599
Douglas Gregor450f00842009-09-25 18:43:00 +00001600 case ParsedTemplateInfo::Template:
1601 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001602 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001603 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001604 D);
1605 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001606
Douglas Gregor450f00842009-09-25 18:43:00 +00001607 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosierc1183952012-06-26 22:30:43 +00001608 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001609 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001610 TemplateInfo.ExternLoc,
1611 TemplateInfo.TemplateLoc,
1612 D);
1613 if (ThisRes.isInvalid()) {
1614 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001615 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001616 }
Chad Rosierc1183952012-06-26 22:30:43 +00001617
Douglas Gregor450f00842009-09-25 18:43:00 +00001618 ThisDecl = ThisRes.get();
1619 break;
1620 }
1621 }
Mike Stump11289f42009-09-09 15:08:12 +00001622
Richard Smith30482bc2011-02-20 03:19:35 +00001623 bool TypeContainsAuto =
1624 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1625
Douglas Gregor23996282009-05-12 21:31:51 +00001626 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001627 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001628 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001629 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001630 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001631 if (D.isFunctionDeclarator())
1632 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1633 << 1 /* delete */;
1634 else
1635 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001636 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001637 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001638 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1639 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001640 else
1641 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001642 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001643 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001644 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001645 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001646 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001647
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001648 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001649 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001650 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001651 cutOffParsing();
1652 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001653 }
Chad Rosierc1183952012-06-26 22:30:43 +00001654
John McCalldadc5752010-08-24 06:29:42 +00001655 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001656
David Blaikiebbafb8a2012-03-11 07:00:24 +00001657 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001658 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001659 ExitScope();
1660 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001661
Douglas Gregor23996282009-05-12 21:31:51 +00001662 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001663 SkipUntil(tok::comma, true, true);
1664 Actions.ActOnInitializerError(ThisDecl);
1665 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001666 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1667 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001668 }
1669 } else if (Tok.is(tok::l_paren)) {
1670 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001671 BalancedDelimiterTracker T(*this, tok::l_paren);
1672 T.consumeOpen();
1673
Benjamin Kramerf0623432012-08-23 22:51:59 +00001674 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001675 CommaLocsTy CommaLocs;
1676
David Blaikiebbafb8a2012-03-11 07:00:24 +00001677 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001678 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001679 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001680 }
1681
Douglas Gregor23996282009-05-12 21:31:51 +00001682 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001683 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00001684 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001685
David Blaikiebbafb8a2012-03-11 07:00:24 +00001686 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001687 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001688 ExitScope();
1689 }
Douglas Gregor23996282009-05-12 21:31:51 +00001690 } else {
1691 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001692 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001693
1694 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1695 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001696
David Blaikiebbafb8a2012-03-11 07:00:24 +00001697 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001698 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001699 ExitScope();
1700 }
1701
Sebastian Redla9351792012-02-11 23:51:47 +00001702 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1703 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001704 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001705 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1706 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001707 }
Fariborz Jahanian8a369a82012-07-03 22:54:28 +00001708 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001709 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001710 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001711 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1712
Sebastian Redl3da34892011-06-05 12:23:16 +00001713 if (D.getCXXScopeSpec().isSet()) {
1714 EnterScope(0);
1715 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1716 }
1717
1718 ExprResult Init(ParseBraceInitializer());
1719
1720 if (D.getCXXScopeSpec().isSet()) {
1721 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1722 ExitScope();
1723 }
1724
1725 if (Init.isInvalid()) {
1726 Actions.ActOnInitializerError(ThisDecl);
1727 } else
1728 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1729 /*DirectInit=*/true, TypeContainsAuto);
1730
Douglas Gregor23996282009-05-12 21:31:51 +00001731 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001732 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001733 }
1734
Richard Smithb2bc2e62011-02-21 20:05:19 +00001735 Actions.FinalizeDeclaration(ThisDecl);
1736
Douglas Gregor23996282009-05-12 21:31:51 +00001737 return ThisDecl;
1738}
1739
Chris Lattner1890ac82006-08-13 01:16:23 +00001740/// ParseSpecifierQualifierList
1741/// specifier-qualifier-list:
1742/// type-specifier specifier-qualifier-list[opt]
1743/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001744/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001745///
Richard Smithc5b05522012-03-12 07:56:15 +00001746void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1747 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001748 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1749 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001750 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001751 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001752
Chris Lattner1890ac82006-08-13 01:16:23 +00001753 // Validate declspec for type-name.
1754 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001755 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1756 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001757 Diag(Tok, diag::err_expected_type);
1758 DS.SetTypeSpecError();
1759 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1760 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001761 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001762 if (!DS.hasTypeSpecifier())
1763 DS.SetTypeSpecError();
1764 }
Mike Stump11289f42009-09-09 15:08:12 +00001765
Chris Lattner1b22eed2006-11-28 05:12:07 +00001766 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001767 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001768 if (DS.getStorageClassSpecLoc().isValid())
1769 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1770 else
1771 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001772 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001773 }
Mike Stump11289f42009-09-09 15:08:12 +00001774
Chris Lattner1b22eed2006-11-28 05:12:07 +00001775 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001776 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001777 if (DS.isInlineSpecified())
1778 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1779 if (DS.isVirtualSpecified())
1780 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1781 if (DS.isExplicitSpecified())
1782 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001783 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001784 }
Richard Smithc5b05522012-03-12 07:56:15 +00001785
1786 // Issue diagnostic and remove constexpr specfier if present.
1787 if (DS.isConstexprSpecified()) {
1788 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1789 DS.ClearConstexprSpec();
1790 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001791}
Chris Lattner53361ac2006-08-10 05:19:57 +00001792
Chris Lattner6cc055a2009-04-12 20:42:31 +00001793/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1794/// specified token is valid after the identifier in a declarator which
1795/// immediately follows the declspec. For example, these things are valid:
1796///
1797/// int x [ 4]; // direct-declarator
1798/// int x ( int y); // direct-declarator
1799/// int(int x ) // direct-declarator
1800/// int x ; // simple-declaration
1801/// int x = 17; // init-declarator-list
1802/// int x , y; // init-declarator-list
1803/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001804/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001805/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001806///
1807/// This is not, because 'x' does not immediately follow the declspec (though
1808/// ')' happens to be valid anyway).
1809/// int (x)
1810///
1811static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1812 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1813 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001814 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001815}
1816
Chris Lattner20a0c612009-04-14 21:34:55 +00001817
1818/// ParseImplicitInt - This method is called when we have an non-typename
1819/// identifier in a declspec (which normally terminates the decl spec) when
1820/// the declspec has no type specifier. In this case, the declspec is either
1821/// malformed or is "implicit int" (in K&R and C89).
1822///
1823/// This method handles diagnosing this prettily and returns false if the
1824/// declspec is done being processed. If it recovers and thinks there may be
1825/// other pieces of declspec after it, it returns true.
1826///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001827bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001828 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00001829 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001830 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001831
Chris Lattner20a0c612009-04-14 21:34:55 +00001832 SourceLocation Loc = Tok.getLocation();
1833 // If we see an identifier that is not a type name, we normally would
1834 // parse it as the identifer being declared. However, when a typename
1835 // is typo'd or the definition is not included, this will incorrectly
1836 // parse the typename as the identifier name and fall over misparsing
1837 // later parts of the diagnostic.
1838 //
1839 // As such, we try to do some look-ahead in cases where this would
1840 // otherwise be an "implicit-int" case to see if this is invalid. For
1841 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1842 // an identifier with implicit int, we'd get a parse error because the
1843 // next token is obviously invalid for a type. Parse these as a case
1844 // with an invalid type specifier.
1845 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001846
Chris Lattner20a0c612009-04-14 21:34:55 +00001847 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00001848 // error, do lookahead to try to do better recovery. This never applies
1849 // within a type specifier. Outside of C++, we allow this even if the
1850 // language doesn't "officially" support implicit int -- we support
1851 // implicit int as an extension in C99 and C11. Allegedly, MS also
1852 // supports implicit int in C++ mode.
Richard Smith2f07ad52012-05-09 20:55:26 +00001853 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smitha952ebb2012-05-15 21:01:51 +00001854 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smithc5b05522012-03-12 07:56:15 +00001855 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001856 // If this token is valid for implicit int, e.g. "static x = 4", then
1857 // we just avoid eating the identifier, so it will be parsed as the
1858 // identifier in the declarator.
1859 return false;
1860 }
Mike Stump11289f42009-09-09 15:08:12 +00001861
Richard Smitha952ebb2012-05-15 21:01:51 +00001862 if (getLangOpts().CPlusPlus &&
1863 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1864 // Don't require a type specifier if we have the 'auto' storage class
1865 // specifier in C++98 -- we'll promote it to a type specifier.
1866 return false;
1867 }
1868
Chris Lattner20a0c612009-04-14 21:34:55 +00001869 // Otherwise, if we don't consume this token, we are going to emit an
1870 // error anyway. Try to recover from various common problems. Check
1871 // to see if this was a reference to a tag name without a tag specified.
1872 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001873 //
1874 // C++ doesn't need this, and isTagName doesn't take SS.
1875 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001876 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001877 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001878
Douglas Gregor0be31a22010-07-02 17:43:08 +00001879 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001880 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001881 case DeclSpec::TST_enum:
1882 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1883 case DeclSpec::TST_union:
1884 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1885 case DeclSpec::TST_struct:
1886 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00001887 case DeclSpec::TST_interface:
1888 TagName="__interface"; FixitTagName = "__interface ";
1889 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001890 case DeclSpec::TST_class:
1891 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001892 }
Mike Stump11289f42009-09-09 15:08:12 +00001893
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001894 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001895 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1896 LookupResult R(Actions, TokenName, SourceLocation(),
1897 Sema::LookupOrdinaryName);
1898
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001899 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001900 << TokenName << TagName << getLangOpts().CPlusPlus
1901 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1902
1903 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1904 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1905 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00001906 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001907 << TokenName << TagName;
1908 }
Mike Stump11289f42009-09-09 15:08:12 +00001909
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001910 // Parse this as a tag as if the missing tag were present.
1911 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00001912 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001913 else
Richard Smithc5b05522012-03-12 07:56:15 +00001914 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1915 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001916 return true;
1917 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001918 }
Mike Stump11289f42009-09-09 15:08:12 +00001919
Richard Smithfe904f02012-05-15 21:29:55 +00001920 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00001921 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00001922 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1923 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00001924 // Look ahead to the next token to try to figure out what this declaration
1925 // was supposed to be.
1926 switch (NextToken().getKind()) {
1927 case tok::comma:
1928 case tok::equal:
1929 case tok::kw_asm:
1930 case tok::l_brace:
1931 case tok::l_square:
1932 case tok::semi:
1933 // This looks like a variable declaration. The type is probably missing.
1934 // We're done parsing decl-specifiers.
1935 return false;
1936
1937 case tok::l_paren: {
1938 // static x(4); // 'x' is not a type
1939 // x(int n); // 'x' is not a type
1940 // x (*p)[]; // 'x' is a type
1941 //
1942 // Since we're in an error case (or the rare 'implicit int in C++' MS
1943 // extension), we can afford to perform a tentative parse to determine
1944 // which case we're in.
1945 TentativeParsingAction PA(*this);
1946 ConsumeToken();
1947 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1948 PA.Revert();
1949 if (TPR == TPResult::False())
1950 return false;
1951 // The identifier is followed by a parenthesized declarator.
1952 // It's supposed to be a type.
1953 break;
1954 }
1955
1956 default:
1957 // This is probably supposed to be a type. This includes cases like:
1958 // int f(itn);
1959 // struct S { unsinged : 4; };
1960 break;
1961 }
1962 }
1963
Chad Rosierc1183952012-06-26 22:30:43 +00001964 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00001965 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001966 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001967 IdentifierInfo *II = Tok.getIdentifierInfo();
1968 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001969 // The action emitted a diagnostic, so we don't have to.
1970 if (T) {
1971 // The action has suggested that the type T could be used. Set that as
1972 // the type in the declaration specifiers, consume the would-be type
1973 // name token, and we're done.
1974 const char *PrevSpec;
1975 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001976 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001977 DS.SetRangeEnd(Tok.getLocation());
1978 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001979 // There may be other declaration specifiers after this.
1980 return true;
1981 } else if (II != Tok.getIdentifierInfo()) {
1982 // If no type was suggested, the correction is to a keyword
1983 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00001984 // There may be other declaration specifiers after this.
1985 return true;
1986 }
Chad Rosierc1183952012-06-26 22:30:43 +00001987
Douglas Gregor15e56022009-10-13 23:27:22 +00001988 // Fall through; the action had no suggestion for us.
1989 } else {
1990 // The action did not emit a diagnostic, so emit one now.
1991 SourceRange R;
1992 if (SS) R = SS->getRange();
1993 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1994 }
Mike Stump11289f42009-09-09 15:08:12 +00001995
Douglas Gregor15e56022009-10-13 23:27:22 +00001996 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00001997 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00001998 DS.SetRangeEnd(Tok.getLocation());
1999 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002000
Chris Lattner20a0c612009-04-14 21:34:55 +00002001 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2002 // avoid rippling error messages on subsequent uses of the same type,
2003 // could be useful if #include was forgotten.
2004 return false;
2005}
2006
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002007/// \brief Determine the declaration specifier context from the declarator
2008/// context.
2009///
2010/// \param Context the declarator context, which is one of the
2011/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002012Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002013Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2014 if (Context == Declarator::MemberContext)
2015 return DSC_class;
2016 if (Context == Declarator::FileContext)
2017 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002018 if (Context == Declarator::TrailingReturnContext)
2019 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002020 return DSC_normal;
2021}
2022
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002023/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2024///
2025/// FIXME: Simply returns an alignof() expression if the argument is a
2026/// type. Ideally, the type should be propagated directly into Sema.
2027///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002028/// [C11] type-id
2029/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002030/// [C++0x] type-id ...[opt]
2031/// [C++0x] assignment-expression ...[opt]
2032ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2033 SourceLocation &EllipsisLoc) {
2034 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002035 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002036 SourceLocation TypeLoc = Tok.getLocation();
2037 ParsedType Ty = ParseTypeName().get();
2038 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002039 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2040 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002041 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002042 ER = ParseConstantExpression();
2043
David Blaikiebbafb8a2012-03-11 07:00:24 +00002044 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002045 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002046
2047 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002048}
2049
2050/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2051/// attribute to Attrs.
2052///
2053/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002054/// [C11] '_Alignas' '(' type-id ')'
2055/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002056/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
2057/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002058void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2059 SourceLocation *endLoc) {
2060 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2061 "Not an alignment-specifier!");
2062
2063 SourceLocation KWLoc = Tok.getLocation();
2064 ConsumeToken();
2065
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002066 BalancedDelimiterTracker T(*this, tok::l_paren);
2067 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002068 return;
2069
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002070 SourceLocation EllipsisLoc;
2071 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002072 if (ArgExpr.isInvalid()) {
2073 SkipUntil(tok::r_paren);
2074 return;
2075 }
2076
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002077 T.consumeClose();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002078 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002079 *endLoc = T.getCloseLocation();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002080
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002081 // FIXME: Handle pack-expansions here.
2082 if (EllipsisLoc.isValid()) {
2083 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2084 return;
2085 }
2086
Benjamin Kramerf0623432012-08-23 22:51:59 +00002087 ExprVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002088 ArgExprs.push_back(ArgExpr.release());
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002089 // FIXME: This should not be GNU, but we since the attribute used is
2090 // based on the spelling, and there is no true spelling for
2091 // C++11 attributes, this isn't accepted.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002092 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002093 0, T.getOpenLocation(), ArgExprs.data(), 1,
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002094 AttributeList::AS_GNU);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002095}
2096
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002097/// ParseDeclarationSpecifiers
2098/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002099/// storage-class-specifier declaration-specifiers[opt]
2100/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002101/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002102/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002103/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002104/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002105///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002106/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002107/// 'typedef'
2108/// 'extern'
2109/// 'static'
2110/// 'auto'
2111/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002112/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002113/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002114/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002115/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002116/// [C++] 'virtual'
2117/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002118/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002119/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002120/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002121
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002122///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002123void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002124 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002125 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002126 DeclSpecContext DSContext,
2127 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002128 if (DS.getSourceRange().isInvalid()) {
2129 DS.SetRangeStart(Tok.getLocation());
2130 DS.SetRangeEnd(Tok.getLocation());
2131 }
Chad Rosierc1183952012-06-26 22:30:43 +00002132
Douglas Gregordf593fb2011-11-07 17:33:42 +00002133 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002134 bool AttrsLastTime = false;
2135 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002136 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002137 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002138 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002139 unsigned DiagID = 0;
2140
Chris Lattner4d8f8732006-11-28 05:05:08 +00002141 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002142
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002143 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002144 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002145 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002146 if (!AttrsLastTime)
2147 ProhibitAttributes(attrs);
2148 else
2149 DS.takeAttributesFrom(attrs);
Peter Collingbourne70188b32011-09-29 18:03:57 +00002150
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002151 // If this is not a declaration specifier token, we're done reading decl
2152 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002153 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002154 return;
Mike Stump11289f42009-09-09 15:08:12 +00002155
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002156 case tok::l_square:
2157 case tok::kw_alignas:
2158 if (!isCXX11AttributeSpecifier())
2159 goto DoneWithDeclSpec;
2160
2161 ProhibitAttributes(attrs);
2162 // FIXME: It would be good to recover by accepting the attributes,
2163 // but attempting to do that now would cause serious
2164 // madness in terms of diagnostics.
2165 attrs.clear();
2166 attrs.Range = SourceRange();
2167
2168 ParseCXX11Attributes(attrs);
2169 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002170 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002171
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002172 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002173 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002174 if (DS.hasTypeSpecifier()) {
2175 bool AllowNonIdentifiers
2176 = (getCurScope()->getFlags() & (Scope::ControlScope |
2177 Scope::BlockScope |
2178 Scope::TemplateParamScope |
2179 Scope::FunctionPrototypeScope |
2180 Scope::AtCatchScope)) == 0;
2181 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002182 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002183 (DSContext == DSC_class && DS.isFriendSpecified());
2184
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002185 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002186 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002187 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002188 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002189 }
2190
Douglas Gregor80039242011-02-15 20:33:25 +00002191 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2192 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2193 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002194 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002195 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002196 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002197 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002198 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002199 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002200
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002201 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002202 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002203 }
2204
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002205 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002206 // C++ scope specifier. Annotate and loop, or bail out on error.
2207 if (TryAnnotateCXXScopeToken(true)) {
2208 if (!DS.hasTypeSpecifier())
2209 DS.SetTypeSpecError();
2210 goto DoneWithDeclSpec;
2211 }
John McCall8bc2a702010-03-01 18:20:46 +00002212 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2213 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002214 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002215
2216 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002217 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002218 goto DoneWithDeclSpec;
2219
John McCall9dab4e62009-12-12 11:40:51 +00002220 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002221 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2222 Tok.getAnnotationRange(),
2223 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002224
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002225 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002226 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002227 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002228 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002229 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002230 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002231
2232 // C++ [class.qual]p2:
2233 // In a lookup in which the constructor is an acceptable lookup
2234 // result and the nested-name-specifier nominates a class C:
2235 //
2236 // - if the name specified after the
2237 // nested-name-specifier, when looked up in C, is the
2238 // injected-class-name of C (Clause 9), or
2239 //
2240 // - if the name specified after the nested-name-specifier
2241 // is the same as the identifier or the
2242 // simple-template-id's template-name in the last
2243 // component of the nested-name-specifier,
2244 //
2245 // the name is instead considered to name the constructor of
2246 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002247 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002248 // Thus, if the template-name is actually the constructor
2249 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002250 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002251 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCall84821e72010-04-13 06:39:49 +00002252 if ((DSContext == DSC_top_level ||
2253 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2254 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002255 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002256 if (isConstructorDeclarator()) {
2257 // The user meant this to be an out-of-line constructor
2258 // definition, but template arguments are not allowed
2259 // there. Just allow this as a constructor; we'll
2260 // complain about it later.
2261 goto DoneWithDeclSpec;
2262 }
2263
2264 // The user meant this to name a type, but it actually names
2265 // a constructor with some extraneous template
2266 // arguments. Complain, then parse it as a type as the user
2267 // intended.
2268 Diag(TemplateId->TemplateNameLoc,
2269 diag::err_out_of_line_template_id_names_constructor)
2270 << TemplateId->Name;
2271 }
2272
John McCall9dab4e62009-12-12 11:40:51 +00002273 DS.getTypeSpecScope() = SS;
2274 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002275 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002276 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002277 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002278 continue;
2279 }
2280
Douglas Gregorc5790df2009-09-28 07:26:33 +00002281 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002282 DS.getTypeSpecScope() = SS;
2283 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002284 if (Tok.getAnnotationValue()) {
2285 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002286 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002287 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002288 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002289 if (isInvalid)
2290 break;
John McCallba7bf592010-08-24 05:47:05 +00002291 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002292 else
2293 DS.SetTypeSpecError();
2294 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2295 ConsumeToken(); // The typename
2296 }
2297
Douglas Gregor167fa622009-03-25 15:40:00 +00002298 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002299 goto DoneWithDeclSpec;
2300
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002301 // If we're in a context where the identifier could be a class name,
2302 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00002303 if ((DSContext == DSC_top_level ||
2304 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002305 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002306 &SS)) {
2307 if (isConstructorDeclarator())
2308 goto DoneWithDeclSpec;
2309
2310 // As noted in C++ [class.qual]p2 (cited above), when the name
2311 // of the class is qualified in a context where it could name
2312 // a constructor, its a constructor name. However, we've
2313 // looked at the declarator, and the user probably meant this
2314 // to be a type. Complain that it isn't supposed to be treated
2315 // as a type, then proceed to parse it as a type.
2316 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2317 << Next.getIdentifierInfo();
2318 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002319
John McCallba7bf592010-08-24 05:47:05 +00002320 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2321 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002322 getCurScope(), &SS,
2323 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002324 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002325 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002326
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002327 // If the referenced identifier is not a type, then this declspec is
2328 // erroneous: We already checked about that it has no type specifier, and
2329 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002330 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002331 if (TypeRep == 0) {
2332 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smithc5b05522012-03-12 07:56:15 +00002333 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002334 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002335 }
Mike Stump11289f42009-09-09 15:08:12 +00002336
John McCall9dab4e62009-12-12 11:40:51 +00002337 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002338 ConsumeToken(); // The C++ scope.
2339
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002340 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002341 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002342 if (isInvalid)
2343 break;
Mike Stump11289f42009-09-09 15:08:12 +00002344
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002345 DS.SetRangeEnd(Tok.getLocation());
2346 ConsumeToken(); // The typename.
2347
2348 continue;
2349 }
Mike Stump11289f42009-09-09 15:08:12 +00002350
Chris Lattnere387d9e2009-01-21 19:48:37 +00002351 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002352 if (Tok.getAnnotationValue()) {
2353 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002354 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002355 DiagID, T);
2356 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002357 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002358
Chris Lattner005fc1b2010-04-05 18:18:31 +00002359 if (isInvalid)
2360 break;
2361
Chris Lattnere387d9e2009-01-21 19:48:37 +00002362 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2363 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002364
Chris Lattnere387d9e2009-01-21 19:48:37 +00002365 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2366 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002367 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002368 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002369 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002370
Chris Lattnere387d9e2009-01-21 19:48:37 +00002371 continue;
2372 }
Mike Stump11289f42009-09-09 15:08:12 +00002373
Douglas Gregor06873092011-04-28 15:48:45 +00002374 case tok::kw___is_signed:
2375 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2376 // typically treats it as a trait. If we see __is_signed as it appears
2377 // in libstdc++, e.g.,
2378 //
2379 // static const bool __is_signed;
2380 //
2381 // then treat __is_signed as an identifier rather than as a keyword.
2382 if (DS.getTypeSpecType() == TST_bool &&
2383 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2384 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2385 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2386 Tok.setKind(tok::identifier);
2387 }
2388
2389 // We're done with the declaration-specifiers.
2390 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002391
Chris Lattner16fac4f2008-07-26 01:18:38 +00002392 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002393 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002394 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002395 // In C++, check to see if this is a scope specifier like foo::bar::, if
2396 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002397 if (getLangOpts().CPlusPlus) {
John McCall1f476a12010-02-26 08:45:28 +00002398 if (TryAnnotateCXXScopeToken(true)) {
2399 if (!DS.hasTypeSpecifier())
2400 DS.SetTypeSpecError();
2401 goto DoneWithDeclSpec;
2402 }
2403 if (!Tok.is(tok::identifier))
2404 continue;
2405 }
Mike Stump11289f42009-09-09 15:08:12 +00002406
Chris Lattner16fac4f2008-07-26 01:18:38 +00002407 // This identifier can only be a typedef name if we haven't already seen
2408 // a type-specifier. Without this check we misparse:
2409 // typedef int X; struct Y { short X; }; as 'short int'.
2410 if (DS.hasTypeSpecifier())
2411 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002412
John Thompson22334602010-02-05 00:12:22 +00002413 // Check for need to substitute AltiVec keyword tokens.
2414 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2415 break;
2416
Richard Smith3092a3b2012-05-09 18:56:43 +00002417 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2418 // allow the use of a typedef name as a type specifier.
2419 if (DS.isTypeAltiVecVector())
2420 goto DoneWithDeclSpec;
2421
John McCallba7bf592010-08-24 05:47:05 +00002422 ParsedType TypeRep =
2423 Actions.getTypeName(*Tok.getIdentifierInfo(),
2424 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002425
Chris Lattner6cc055a2009-04-12 20:42:31 +00002426 // If this is not a typedef name, don't parse it as part of the declspec,
2427 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002428 if (!TypeRep) {
Richard Smithc5b05522012-03-12 07:56:15 +00002429 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002430 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002431 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002432
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002433 // If we're in a context where the identifier could be a class name,
2434 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002435 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002436 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002437 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002438 goto DoneWithDeclSpec;
2439
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002440 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002441 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002442 if (isInvalid)
2443 break;
Mike Stump11289f42009-09-09 15:08:12 +00002444
Chris Lattner16fac4f2008-07-26 01:18:38 +00002445 DS.SetRangeEnd(Tok.getLocation());
2446 ConsumeToken(); // The identifier
2447
2448 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2449 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002450 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002451 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002452 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002453
Steve Naroffcd5e7822008-09-22 10:28:57 +00002454 // Need to support trailing type qualifiers (e.g. "id<p> const").
2455 // If a type specifier follows, it will be diagnosed elsewhere.
2456 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002457 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002458
2459 // type-name
2460 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002461 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002462 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002463 // This template-id does not refer to a type name, so we're
2464 // done with the type-specifiers.
2465 goto DoneWithDeclSpec;
2466 }
2467
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002468 // If we're in a context where the template-id could be a
2469 // constructor name or specialization, check whether this is a
2470 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002471 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002472 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002473 isConstructorDeclarator())
2474 goto DoneWithDeclSpec;
2475
Douglas Gregor7f741122009-02-25 19:37:18 +00002476 // Turn the template-id annotation token into a type annotation
2477 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002478 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002479 continue;
2480 }
2481
Chris Lattnere37e2332006-08-15 04:50:22 +00002482 // GNU attributes support.
2483 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002484 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002485 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002486
2487 // Microsoft declspec support.
2488 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002489 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002490 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002491
Steve Naroff44ac7772008-12-25 14:16:32 +00002492 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002493 case tok::kw___forceinline: {
2494 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2495 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002496 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002497 // FIXME: This does not work correctly if it is set to be a declspec
2498 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002499 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002500 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002501 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002502 }
Eli Friedman53339e02009-06-08 23:27:34 +00002503
2504 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002505 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002506 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002507 case tok::kw___cdecl:
2508 case tok::kw___stdcall:
2509 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002510 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002511 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002512 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002513 continue;
2514
Dawn Perchik335e16b2010-09-03 01:29:35 +00002515 // Borland single token adornments.
2516 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002517 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002518 continue;
2519
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002520 // OpenCL single token adornments.
2521 case tok::kw___kernel:
2522 ParseOpenCLAttributes(DS.getAttributes());
2523 continue;
2524
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002525 // storage-class-specifier
2526 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002527 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2528 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002529 break;
2530 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00002531 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002532 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002533 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2534 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002535 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002536 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002537 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2538 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002539 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002540 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00002541 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002542 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002543 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2544 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002545 break;
2546 case tok::kw_auto:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002547 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002548 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002549 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2550 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002551 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002552 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002553 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002554 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002555 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2556 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002557 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002558 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2559 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002560 break;
2561 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002562 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2563 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002564 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002565 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002566 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2567 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002568 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002569 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00002570 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002571 break;
Mike Stump11289f42009-09-09 15:08:12 +00002572
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002573 // function-specifier
2574 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00002575 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002576 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002577 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00002578 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002579 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002580 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00002581 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002582 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002583
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002584 // alignment-specifier
2585 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002586 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002587 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002588 ParseAlignmentSpecifier(DS.getAttributes());
2589 continue;
2590
Anders Carlssoncd8db412009-05-06 04:46:28 +00002591 // friend
2592 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002593 if (DSContext == DSC_class)
2594 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2595 else {
2596 PrevSpec = ""; // not actually used by the diagnostic
2597 DiagID = diag::err_friend_invalid_in_context;
2598 isInvalid = true;
2599 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002600 break;
Mike Stump11289f42009-09-09 15:08:12 +00002601
Douglas Gregor26701a42011-09-09 02:06:17 +00002602 // Modules
2603 case tok::kw___module_private__:
2604 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2605 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002606
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002607 // constexpr
2608 case tok::kw_constexpr:
2609 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2610 break;
2611
Chris Lattnere387d9e2009-01-21 19:48:37 +00002612 // type-specifier
2613 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002614 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2615 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002616 break;
2617 case tok::kw_long:
2618 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002619 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2620 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002621 else
John McCall49bfce42009-08-03 20:12:06 +00002622 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2623 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002624 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002625 case tok::kw___int64:
2626 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2627 DiagID);
2628 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002629 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002630 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2631 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002632 break;
2633 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002634 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2635 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002636 break;
2637 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002638 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2639 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002640 break;
2641 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002642 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2643 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002644 break;
2645 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002646 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2647 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002648 break;
2649 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002650 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2651 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002652 break;
2653 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002654 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2655 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002656 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002657 case tok::kw___int128:
2658 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2659 DiagID);
2660 break;
2661 case tok::kw_half:
2662 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2663 DiagID);
2664 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002665 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002666 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2667 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002668 break;
2669 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002670 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2671 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002672 break;
2673 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002674 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2675 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002676 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002677 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002678 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2679 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002680 break;
2681 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002682 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2683 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002684 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002685 case tok::kw_bool:
2686 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002687 if (Tok.is(tok::kw_bool) &&
2688 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2689 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2690 PrevSpec = ""; // Not used by the diagnostic.
2691 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002692 // For better error recovery.
2693 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002694 isInvalid = true;
2695 } else {
2696 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2697 DiagID);
2698 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002699 break;
2700 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002701 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2702 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002703 break;
2704 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002705 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2706 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002707 break;
2708 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002709 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2710 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002711 break;
John Thompson22334602010-02-05 00:12:22 +00002712 case tok::kw___vector:
2713 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2714 break;
2715 case tok::kw___pixel:
2716 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2717 break;
John McCall39439732011-04-09 22:50:59 +00002718 case tok::kw___unknown_anytype:
2719 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2720 PrevSpec, DiagID);
2721 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002722
2723 // class-specifier:
2724 case tok::kw_class:
2725 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00002726 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002727 case tok::kw_union: {
2728 tok::TokenKind Kind = Tok.getKind();
2729 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002730 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2731 EnteringContext, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002732 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002733 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002734
2735 // enum-specifier:
2736 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002737 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002738 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002739 continue;
2740
2741 // cv-qualifier:
2742 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002743 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002744 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002745 break;
2746 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002747 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002748 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002749 break;
2750 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002751 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002752 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002753 break;
2754
Douglas Gregor333489b2009-03-27 23:10:48 +00002755 // C++ typename-specifier:
2756 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00002757 if (TryAnnotateTypeOrScopeToken()) {
2758 DS.SetTypeSpecError();
2759 goto DoneWithDeclSpec;
2760 }
2761 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00002762 continue;
2763 break;
2764
Chris Lattnere387d9e2009-01-21 19:48:37 +00002765 // GNU typeof support.
2766 case tok::kw_typeof:
2767 ParseTypeofSpecifier(DS);
2768 continue;
2769
David Blaikie15a430a2011-12-04 05:04:18 +00002770 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00002771 ParseDecltypeSpecifier(DS);
2772 continue;
2773
Alexis Hunt4a257072011-05-19 05:37:45 +00002774 case tok::kw___underlying_type:
2775 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00002776 continue;
2777
2778 case tok::kw__Atomic:
2779 ParseAtomicSpecifier(DS);
2780 continue;
Alexis Hunt4a257072011-05-19 05:37:45 +00002781
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002782 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00002783 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002784 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002785 goto DoneWithDeclSpec;
2786 case tok::kw___private:
2787 case tok::kw___global:
2788 case tok::kw___local:
2789 case tok::kw___constant:
2790 case tok::kw___read_only:
2791 case tok::kw___write_only:
2792 case tok::kw___read_write:
2793 ParseOpenCLQualifiers(DS);
2794 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002795
Steve Naroffcfdf6162008-06-05 00:02:44 +00002796 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002797 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00002798 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2799 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002800 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00002801 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002802
Douglas Gregor3a001f42010-11-19 17:10:50 +00002803 if (!ParseObjCProtocolQualifiers(DS))
2804 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2805 << FixItHint::CreateInsertion(Loc, "id")
2806 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00002807
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002808 // Need to support trailing type qualifiers (e.g. "id<p> const").
2809 // If a type specifier follows, it will be diagnosed elsewhere.
2810 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002811 }
John McCall49bfce42009-08-03 20:12:06 +00002812 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002813 if (isInvalid) {
2814 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002815 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00002816
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002817 if (DiagID == diag::ext_duplicate_declspec)
2818 Diag(Tok, DiagID)
2819 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2820 else
2821 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002822 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002823
Chris Lattner2e232092008-03-13 06:29:04 +00002824 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002825 if (DiagID != diag::err_bool_redeclaration)
2826 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002827
2828 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002829 }
2830}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002831
Chris Lattner70ae4912007-10-29 04:42:53 +00002832/// ParseStructDeclaration - Parse a struct declaration without the terminating
2833/// semicolon.
2834///
Chris Lattner90a26b02007-01-23 04:38:16 +00002835/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002836/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002837/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002838/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002839/// struct-declarator-list:
2840/// struct-declarator
2841/// struct-declarator-list ',' struct-declarator
2842/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2843/// struct-declarator:
2844/// declarator
2845/// [GNU] declarator attributes[opt]
2846/// declarator[opt] ':' constant-expression
2847/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2848///
Chris Lattnera12405b2008-04-10 06:46:29 +00002849void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002850ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00002851
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002852 if (Tok.is(tok::kw___extension__)) {
2853 // __extension__ silences extension warnings in the subexpression.
2854 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002855 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002856 return ParseStructDeclaration(DS, Fields);
2857 }
Mike Stump11289f42009-09-09 15:08:12 +00002858
Steve Naroff97170802007-08-20 22:28:22 +00002859 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002860 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002861
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002862 // If there are no declarators, this is a free-standing declaration
2863 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002864 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002865 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2866 DS);
2867 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00002868 return;
2869 }
2870
2871 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002872 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00002873 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00002874 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002875 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00002876 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00002877
2878 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002879 if (!FirstDeclarator)
2880 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002881
Steve Naroff97170802007-08-20 22:28:22 +00002882 /// struct-declarator: declarator
2883 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002884 if (Tok.isNot(tok::colon)) {
2885 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2886 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002887 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002888 }
Mike Stump11289f42009-09-09 15:08:12 +00002889
Chris Lattner76c72282007-10-09 17:33:22 +00002890 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002891 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002892 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002893 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002894 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002895 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002896 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002897 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002898
Steve Naroff97170802007-08-20 22:28:22 +00002899 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002900 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002901
John McCallcfefb6d2009-11-03 02:38:08 +00002902 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00002903 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00002904
Steve Naroff97170802007-08-20 22:28:22 +00002905 // If we don't have a comma, it is either the end of the list (a ';')
2906 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002907 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002908 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002909
Steve Naroff97170802007-08-20 22:28:22 +00002910 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00002911 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002912
John McCallcfefb6d2009-11-03 02:38:08 +00002913 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002914 }
Steve Naroff97170802007-08-20 22:28:22 +00002915}
2916
2917/// ParseStructUnionBody
2918/// struct-contents:
2919/// struct-declaration-list
2920/// [EXT] empty
2921/// [GNU] "struct-declaration-list" without terminatoring ';'
2922/// struct-declaration-list:
2923/// struct-declaration
2924/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002925/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002926///
Chris Lattner1300fb92007-01-23 23:42:53 +00002927void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002928 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002929 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2930 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002931
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002932 BalancedDelimiterTracker T(*this, tok::l_brace);
2933 if (T.consumeOpen())
2934 return;
Mike Stump11289f42009-09-09 15:08:12 +00002935
Douglas Gregor658b9552009-01-09 22:42:13 +00002936 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002937 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002938
Chris Lattner7b9ace62007-01-23 20:11:08 +00002939 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2940 // C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002941 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithe4345902011-12-29 21:57:33 +00002942 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2943 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2944 }
Chris Lattner7b9ace62007-01-23 20:11:08 +00002945
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002946 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002947
Chris Lattner7b9ace62007-01-23 20:11:08 +00002948 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002949 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002950 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002951
Chris Lattner736ed5d2007-06-09 05:59:07 +00002952 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002953 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002954 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00002955 continue;
2956 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002957
John McCallcfefb6d2009-11-03 02:38:08 +00002958 if (!Tok.is(tok::at)) {
2959 struct CFieldCallback : FieldCallback {
2960 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002961 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002962 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002963
John McCall48871652010-08-21 09:40:31 +00002964 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002965 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002966 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2967
Eli Friedman934dbbf2012-08-08 23:53:27 +00002968 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002969 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002970 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002971 FD.D.getDeclSpec().getSourceRange().getBegin(),
2972 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002973 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002974 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00002975 }
John McCallcfefb6d2009-11-03 02:38:08 +00002976 } Callback(*this, TagDecl, FieldDecls);
2977
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002978 // Parse all the comma separated declarators.
2979 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002980 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002981 } else { // Handle @defs
2982 ConsumeToken();
2983 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2984 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002985 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002986 continue;
2987 }
2988 ConsumeToken();
2989 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2990 if (!Tok.is(tok::identifier)) {
2991 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002992 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002993 continue;
2994 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002995 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002996 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002997 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002998 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2999 ConsumeToken();
3000 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003001 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003002
Chris Lattner76c72282007-10-09 17:33:22 +00003003 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003004 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003005 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003006 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003007 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003008 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003009 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3010 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00003011 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00003012 // If we stopped at a ';', eat it.
3013 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003014 }
3015 }
Mike Stump11289f42009-09-09 15:08:12 +00003016
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003017 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003018
John McCall084e83d2011-03-24 11:26:52 +00003019 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003020 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003021 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003022
Douglas Gregor0be31a22010-07-02 17:43:08 +00003023 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003024 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003025 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003026 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003027 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003028 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3029 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003030}
3031
Chris Lattner3b561a32006-08-13 00:12:11 +00003032/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003033/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003034/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003035///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003036/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3037/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003038/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3039/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003040/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003041/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003042///
Richard Smith7d137e32012-03-23 03:33:32 +00003043/// [C++11] enum-head '{' enumerator-list[opt] '}'
3044/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003045///
Richard Smith7d137e32012-03-23 03:33:32 +00003046/// enum-head: [C++11]
3047/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3048/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3049/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003050///
Richard Smith7d137e32012-03-23 03:33:32 +00003051/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003052/// 'enum'
3053/// 'enum' 'class'
3054/// 'enum' 'struct'
3055///
Richard Smith7d137e32012-03-23 03:33:32 +00003056/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003057/// ':' type-specifier-seq
3058///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003059/// [C++] elaborated-type-specifier:
3060/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3061///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003062void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003063 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003064 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003065 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003066 if (Tok.is(tok::code_completion)) {
3067 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003068 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003069 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003070 }
John McCallcb432fa2011-07-06 05:58:41 +00003071
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003072 // If attributes exist after tag, parse them.
3073 ParsedAttributesWithRange attrs(AttrFactory);
3074 MaybeParseGNUAttributes(attrs);
3075 MaybeParseCXX0XAttributes(attrs);
3076
3077 // If declspecs exist after tag, parse them.
3078 while (Tok.is(tok::kw___declspec))
3079 ParseMicrosoftDeclSpec(attrs);
3080
Richard Smith0f8ee222012-01-10 01:33:14 +00003081 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003082 bool IsScopedUsingClassTag = false;
3083
John McCallbeae29a2012-06-23 22:30:04 +00003084 // In C++11, recognize 'enum class' and 'enum struct'.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003085 if (getLangOpts().CPlusPlus0x &&
John McCallcb432fa2011-07-06 05:58:41 +00003086 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003087 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003088 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003089 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003090
John McCallbeae29a2012-06-23 22:30:04 +00003091 // Attributes are not allowed between these keywords. Diagnose,
3092 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003093 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003094
3095 // They are allowed afterwards, though.
3096 MaybeParseGNUAttributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003097 MaybeParseCXX0XAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003098 while (Tok.is(tok::kw___declspec))
3099 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003100 }
Richard Smith7d137e32012-03-23 03:33:32 +00003101
John McCall6347b682012-05-07 06:16:58 +00003102 // C++11 [temp.explicit]p12:
3103 // The usual access controls do not apply to names used to specify
3104 // explicit instantiations.
3105 // We extend this to also cover explicit specializations. Note that
3106 // we don't suppress if this turns out to be an elaborated type
3107 // specifier.
3108 bool shouldDelayDiagsInTag =
3109 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3110 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3111 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003112
Richard Smithbfdb1082012-03-12 08:56:40 +00003113 // Enum definitions should not be parsed in a trailing-return-type.
3114 bool AllowDeclaration = DSC != DSC_trailing;
3115
3116 bool AllowFixedUnderlyingType = AllowDeclaration &&
3117 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
3118 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003119
Abramo Bagnarad7548482010-05-19 21:37:53 +00003120 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003121 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003122 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3123 // if a fixed underlying type is allowed.
3124 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003125
3126 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003127 /*EnteringContext=*/false))
John McCall1f476a12010-02-26 08:45:28 +00003128 return;
3129
3130 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003131 Diag(Tok, diag::err_expected_ident);
3132 if (Tok.isNot(tok::l_brace)) {
3133 // Has no name and is not a definition.
3134 // Skip the rest of this declarator, up until the comma or semicolon.
3135 SkipUntil(tok::comma, true);
3136 return;
3137 }
3138 }
3139 }
Mike Stump11289f42009-09-09 15:08:12 +00003140
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003141 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003142 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003143 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003144 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003145
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003146 // Skip the rest of this declarator, up until the comma or semicolon.
3147 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003148 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003149 }
Mike Stump11289f42009-09-09 15:08:12 +00003150
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003151 // If an identifier is present, consume and remember it.
3152 IdentifierInfo *Name = 0;
3153 SourceLocation NameLoc;
3154 if (Tok.is(tok::identifier)) {
3155 Name = Tok.getIdentifierInfo();
3156 NameLoc = ConsumeToken();
3157 }
Mike Stump11289f42009-09-09 15:08:12 +00003158
Richard Smith0f8ee222012-01-10 01:33:14 +00003159 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003160 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3161 // declaration of a scoped enumeration.
3162 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003163 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003164 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003165 }
3166
John McCall6347b682012-05-07 06:16:58 +00003167 // Okay, end the suppression area. We'll decide whether to emit the
3168 // diagnostics in a second.
3169 if (shouldDelayDiagsInTag)
3170 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003171
Douglas Gregor0bf31402010-10-08 23:50:27 +00003172 TypeResult BaseType;
3173
Douglas Gregord1f69f62010-12-01 17:42:47 +00003174 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003175 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003176 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003177 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003178 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003179 // If we're in class scope, this can either be an enum declaration with
3180 // an underlying type, or a declaration of a bitfield member. We try to
3181 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003182 // (integer literal, sizeof); if it's still ambiguous, we then consider
3183 // anything that's a simple-type-specifier followed by '(' as an
3184 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003185 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003186 EnterExpressionEvaluationContext Unevaluated(Actions,
3187 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003188 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003189 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003190 // bit-field. This is the common case.
3191 if (TPR == TPResult::True())
3192 PossibleBitfield = true;
3193 // If the next token starts a type-specifier-seq, it may be either a
3194 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003195 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003196 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003197 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003198 GetLookAheadToken(2).getKind() == tok::semi) {
3199 // Consume the ':'.
3200 ConsumeToken();
3201 } else {
3202 // We have the start of a type-specifier-seq, so we have to perform
3203 // tentative parsing to determine whether we have an expression or a
3204 // type.
3205 TentativeParsingAction TPA(*this);
3206
3207 // Consume the ':'.
3208 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003209
3210 // If we see a type specifier followed by an open-brace, we have an
3211 // ambiguity between an underlying type and a C++11 braced
3212 // function-style cast. Resolve this by always treating it as an
3213 // underlying type.
3214 // FIXME: The standard is not entirely clear on how to disambiguate in
3215 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003216 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003217 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003218 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003219 // We'll parse this as a bitfield later.
3220 PossibleBitfield = true;
3221 TPA.Revert();
3222 } else {
3223 // We have a type-specifier-seq.
3224 TPA.Commit();
3225 }
3226 }
3227 } else {
3228 // Consume the ':'.
3229 ConsumeToken();
3230 }
3231
3232 if (!PossibleBitfield) {
3233 SourceRange Range;
3234 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003235
David Blaikiebbafb8a2012-03-11 07:00:24 +00003236 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregora1aec292011-02-22 20:32:04 +00003237 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
3238 << Range;
David Blaikiebbafb8a2012-03-11 07:00:24 +00003239 if (getLangOpts().CPlusPlus0x)
Richard Smith5d164bc2011-10-15 05:09:34 +00003240 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003241 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003242 }
3243
Richard Smith0f8ee222012-01-10 01:33:14 +00003244 // There are four options here. If we have 'friend enum foo;' then this is a
3245 // friend declaration, and cannot have an accompanying definition. If we have
3246 // 'enum foo;', then this is a forward declaration. If we have
3247 // 'enum foo {...' then this is a definition. Otherwise we have something
3248 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003249 //
3250 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3251 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3252 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3253 //
John McCallfaf5fb42010-08-26 23:41:50 +00003254 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003255 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003256 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003257 } else if (Tok.is(tok::l_brace)) {
3258 if (DS.isFriendSpecified()) {
3259 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3260 << SourceRange(DS.getFriendSpecLoc());
3261 ConsumeBrace();
3262 SkipUntil(tok::r_brace);
3263 TUK = Sema::TUK_Friend;
3264 } else {
3265 TUK = Sema::TUK_Definition;
3266 }
Richard Smith369b9f92012-06-25 21:37:02 +00003267 } else if (DSC != DSC_type_specifier &&
3268 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003269 (Tok.isAtStartOfLine() &&
3270 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003271 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3272 if (Tok.isNot(tok::semi)) {
3273 // A semicolon was missing after this declaration. Diagnose and recover.
3274 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3275 "enum");
3276 PP.EnterToken(Tok);
3277 Tok.setKind(tok::semi);
3278 }
John McCall6347b682012-05-07 06:16:58 +00003279 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003280 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003281 }
3282
3283 // If this is an elaborated type specifier, and we delayed
3284 // diagnostics before, just merge them into the current pool.
3285 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3286 diagsFromTag.redelay();
3287 }
Richard Smith7d137e32012-03-23 03:33:32 +00003288
3289 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003290 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003291 TUK != Sema::TUK_Reference) {
Richard Smith7d137e32012-03-23 03:33:32 +00003292 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3293 // Skip the rest of this declarator, up until the comma or semicolon.
3294 Diag(Tok, diag::err_enum_template);
3295 SkipUntil(tok::comma, true);
3296 return;
3297 }
3298
3299 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3300 // Enumerations can't be explicitly instantiated.
3301 DS.SetTypeSpecError();
3302 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3303 return;
3304 }
3305
3306 assert(TemplateInfo.TemplateParams && "no template parameters");
3307 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3308 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003309 }
Chad Rosierc1183952012-06-26 22:30:43 +00003310
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003311 if (TUK == Sema::TUK_Reference)
3312 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003313
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003314 if (!Name && TUK != Sema::TUK_Definition) {
3315 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003316
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003317 // Skip the rest of this declarator, up until the comma or semicolon.
3318 SkipUntil(tok::comma, true);
3319 return;
3320 }
Richard Smith7d137e32012-03-23 03:33:32 +00003321
Douglas Gregord6ab8742009-05-28 23:31:59 +00003322 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003323 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003324 const char *PrevSpec = 0;
3325 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003326 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003327 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003328 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003329 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003330 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003331
Douglas Gregorba41d012010-04-24 16:38:41 +00003332 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003333 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003334 // dependent tag.
3335 if (!Name) {
3336 DS.SetTypeSpecError();
3337 Diag(Tok, diag::err_expected_type_name_after_typename);
3338 return;
3339 }
Chad Rosierc1183952012-06-26 22:30:43 +00003340
Douglas Gregor0be31a22010-07-02 17:43:08 +00003341 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003342 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003343 NameLoc);
3344 if (Type.isInvalid()) {
3345 DS.SetTypeSpecError();
3346 return;
3347 }
Chad Rosierc1183952012-06-26 22:30:43 +00003348
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003349 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3350 NameLoc.isValid() ? NameLoc : StartLoc,
3351 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003352 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003353
Douglas Gregorba41d012010-04-24 16:38:41 +00003354 return;
3355 }
Mike Stump11289f42009-09-09 15:08:12 +00003356
John McCall48871652010-08-21 09:40:31 +00003357 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003358 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003359 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003360 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003361 ConsumeBrace();
3362 SkipUntil(tok::r_brace);
3363 }
Chad Rosierc1183952012-06-26 22:30:43 +00003364
Douglas Gregorba41d012010-04-24 16:38:41 +00003365 DS.SetTypeSpecError();
3366 return;
3367 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003368
Richard Smith369b9f92012-06-25 21:37:02 +00003369 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003370 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003371
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003372 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3373 NameLoc.isValid() ? NameLoc : StartLoc,
3374 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003375 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003376}
3377
Chris Lattnerc1915e22007-01-25 07:29:02 +00003378/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3379/// enumerator-list:
3380/// enumerator
3381/// enumerator-list ',' enumerator
3382/// enumerator:
3383/// enumeration-constant
3384/// enumeration-constant '=' constant-expression
3385/// enumeration-constant:
3386/// identifier
3387///
John McCall48871652010-08-21 09:40:31 +00003388void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003389 // Enter the scope of the enum body and start the definition.
3390 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003391 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003392
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003393 BalancedDelimiterTracker T(*this, tok::l_brace);
3394 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003395
Chris Lattner37256fb2007-08-27 17:24:30 +00003396 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003397 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003398 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003399
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003400 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003401
John McCall48871652010-08-21 09:40:31 +00003402 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003403
Chris Lattnerc1915e22007-01-25 07:29:02 +00003404 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003405 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003406 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3407 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003408
John McCall811a0f52010-10-22 23:36:17 +00003409 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003410 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003411 MaybeParseGNUAttributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003412 MaybeParseCXX0XAttributes(attrs);
3413 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003414
Chris Lattnerc1915e22007-01-25 07:29:02 +00003415 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003416 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003417 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003418
Chris Lattner76c72282007-10-09 17:33:22 +00003419 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003420 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003421 AssignedVal = ParseConstantExpression();
3422 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003423 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003424 }
Mike Stump11289f42009-09-09 15:08:12 +00003425
Chris Lattnerc1915e22007-01-25 07:29:02 +00003426 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003427 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3428 LastEnumConstDecl,
3429 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003430 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003431 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003432 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003433
Chris Lattner4ef40012007-06-11 01:28:17 +00003434 EnumConstantDecls.push_back(EnumConstDecl);
3435 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003436
Douglas Gregorce66d022010-09-07 14:51:08 +00003437 if (Tok.is(tok::identifier)) {
3438 // We're missing a comma between enumerators.
3439 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003440 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003441 << FixItHint::CreateInsertion(Loc, ", ");
3442 continue;
3443 }
Chad Rosierc1183952012-06-26 22:30:43 +00003444
Chris Lattner76c72282007-10-09 17:33:22 +00003445 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003446 break;
3447 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003448
Richard Smith5d164bc2011-10-15 05:09:34 +00003449 if (Tok.isNot(tok::identifier)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003450 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith87f5dc52012-07-23 05:45:25 +00003451 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3452 diag::ext_enumerator_list_comma_cxx :
3453 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003454 << FixItHint::CreateRemoval(CommaLoc);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003455 else if (getLangOpts().CPlusPlus0x)
Richard Smith5d164bc2011-10-15 05:09:34 +00003456 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3457 << FixItHint::CreateRemoval(CommaLoc);
3458 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003459 }
Mike Stump11289f42009-09-09 15:08:12 +00003460
Chris Lattnerc1915e22007-01-25 07:29:02 +00003461 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003462 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003463
Chris Lattnerc1915e22007-01-25 07:29:02 +00003464 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003465 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003466 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003467
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003468 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3469 EnumDecl, EnumConstantDecls.data(),
3470 EnumConstantDecls.size(), getCurScope(),
3471 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003472
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003473 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003474 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3475 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003476
3477 // The next token must be valid after an enum definition. If not, a ';'
3478 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003479 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3480 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003481 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3482 // Push this token back into the preprocessor and change our current token
3483 // to ';' so that the rest of the code recovers as though there were an
3484 // ';' after the definition.
3485 PP.EnterToken(Tok);
3486 Tok.setKind(tok::semi);
3487 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003488}
Chris Lattner3b561a32006-08-13 00:12:11 +00003489
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003490/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003491/// start of a type-qualifier-list.
3492bool Parser::isTypeQualifier() const {
3493 switch (Tok.getKind()) {
3494 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003495
3496 // type-qualifier only in OpenCL
3497 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003498 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003499
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003500 // type-qualifier
3501 case tok::kw_const:
3502 case tok::kw_volatile:
3503 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003504 case tok::kw___private:
3505 case tok::kw___local:
3506 case tok::kw___global:
3507 case tok::kw___constant:
3508 case tok::kw___read_only:
3509 case tok::kw___read_write:
3510 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003511 return true;
3512 }
3513}
3514
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003515/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3516/// is definitely a type-specifier. Return false if it isn't part of a type
3517/// specifier or if we're not sure.
3518bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3519 switch (Tok.getKind()) {
3520 default: return false;
3521 // type-specifiers
3522 case tok::kw_short:
3523 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003524 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003525 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003526 case tok::kw_signed:
3527 case tok::kw_unsigned:
3528 case tok::kw__Complex:
3529 case tok::kw__Imaginary:
3530 case tok::kw_void:
3531 case tok::kw_char:
3532 case tok::kw_wchar_t:
3533 case tok::kw_char16_t:
3534 case tok::kw_char32_t:
3535 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003536 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003537 case tok::kw_float:
3538 case tok::kw_double:
3539 case tok::kw_bool:
3540 case tok::kw__Bool:
3541 case tok::kw__Decimal32:
3542 case tok::kw__Decimal64:
3543 case tok::kw__Decimal128:
3544 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003545
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003546 // struct-or-union-specifier (C99) or class-specifier (C++)
3547 case tok::kw_class:
3548 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003549 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003550 case tok::kw_union:
3551 // enum-specifier
3552 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003553
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003554 // typedef-name
3555 case tok::annot_typename:
3556 return true;
3557 }
3558}
3559
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003560/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003561/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003562bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003563 switch (Tok.getKind()) {
3564 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003565
Chris Lattner020bab92009-01-04 23:41:41 +00003566 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003567 if (TryAltiVecVectorToken())
3568 return true;
3569 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003570 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003571 // Annotate typenames and C++ scope specifiers. If we get one, just
3572 // recurse to handle whatever we get.
3573 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003574 return true;
3575 if (Tok.is(tok::identifier))
3576 return false;
3577 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003578
Chris Lattner020bab92009-01-04 23:41:41 +00003579 case tok::coloncolon: // ::foo::bar
3580 if (NextToken().is(tok::kw_new) || // ::new
3581 NextToken().is(tok::kw_delete)) // ::delete
3582 return false;
3583
Chris Lattner020bab92009-01-04 23:41:41 +00003584 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003585 return true;
3586 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003587
Chris Lattnere37e2332006-08-15 04:50:22 +00003588 // GNU attributes support.
3589 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003590 // GNU typeof support.
3591 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003592
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003593 // type-specifiers
3594 case tok::kw_short:
3595 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003596 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003597 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003598 case tok::kw_signed:
3599 case tok::kw_unsigned:
3600 case tok::kw__Complex:
3601 case tok::kw__Imaginary:
3602 case tok::kw_void:
3603 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003604 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003605 case tok::kw_char16_t:
3606 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003607 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003608 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003609 case tok::kw_float:
3610 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003611 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003612 case tok::kw__Bool:
3613 case tok::kw__Decimal32:
3614 case tok::kw__Decimal64:
3615 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003616 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003617
Chris Lattner861a2262008-04-13 18:59:07 +00003618 // struct-or-union-specifier (C99) or class-specifier (C++)
3619 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003620 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003621 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003622 case tok::kw_union:
3623 // enum-specifier
3624 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003625
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003626 // type-qualifier
3627 case tok::kw_const:
3628 case tok::kw_volatile:
3629 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003630
3631 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003632 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003633 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003634
Chris Lattner409bf7d2008-10-20 00:25:30 +00003635 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3636 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003637 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003638
Steve Naroff44ac7772008-12-25 14:16:32 +00003639 case tok::kw___cdecl:
3640 case tok::kw___stdcall:
3641 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003642 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003643 case tok::kw___w64:
3644 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003645 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003646 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003647 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003648
3649 case tok::kw___private:
3650 case tok::kw___local:
3651 case tok::kw___global:
3652 case tok::kw___constant:
3653 case tok::kw___read_only:
3654 case tok::kw___read_write:
3655 case tok::kw___write_only:
3656
Eli Friedman53339e02009-06-08 23:27:34 +00003657 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003658
3659 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003660 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003661
Benjamin Kramere56f3932011-12-23 17:00:35 +00003662 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003663 case tok::kw__Atomic:
3664 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003665 }
3666}
3667
Chris Lattneracd58a32006-08-06 17:24:14 +00003668/// isDeclarationSpecifier() - Return true if the current token is part of a
3669/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003670///
3671/// \param DisambiguatingWithExpression True to indicate that the purpose of
3672/// this check is to disambiguate between an expression and a declaration.
3673bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003674 switch (Tok.getKind()) {
3675 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003676
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003677 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003678 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003679
Chris Lattner020bab92009-01-04 23:41:41 +00003680 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003681 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003682 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00003683 return false;
John Thompson22334602010-02-05 00:12:22 +00003684 if (TryAltiVecVectorToken())
3685 return true;
3686 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00003687 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00003688 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003689 // Annotate typenames and C++ scope specifiers. If we get one, just
3690 // recurse to handle whatever we get.
3691 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003692 return true;
3693 if (Tok.is(tok::identifier))
3694 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003695
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003696 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00003697 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003698 // expression is permitted, then this is probably a class message send
3699 // missing the initial '['. In this case, we won't consider this to be
3700 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00003701 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003702 isStartOfObjCClassMessageMissingOpenBracket())
3703 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003704
John McCall1f476a12010-02-26 08:45:28 +00003705 return isDeclarationSpecifier();
3706
Chris Lattner020bab92009-01-04 23:41:41 +00003707 case tok::coloncolon: // ::foo::bar
3708 if (NextToken().is(tok::kw_new) || // ::new
3709 NextToken().is(tok::kw_delete)) // ::delete
3710 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003711
Chris Lattner020bab92009-01-04 23:41:41 +00003712 // Annotate typenames and C++ scope specifiers. If we get one, just
3713 // recurse to handle whatever we get.
3714 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003715 return true;
3716 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003717
Chris Lattneracd58a32006-08-06 17:24:14 +00003718 // storage-class-specifier
3719 case tok::kw_typedef:
3720 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003721 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003722 case tok::kw_static:
3723 case tok::kw_auto:
3724 case tok::kw_register:
3725 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003726
Douglas Gregor26701a42011-09-09 02:06:17 +00003727 // Modules
3728 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00003729
Chris Lattneracd58a32006-08-06 17:24:14 +00003730 // type-specifiers
3731 case tok::kw_short:
3732 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003733 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003734 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00003735 case tok::kw_signed:
3736 case tok::kw_unsigned:
3737 case tok::kw__Complex:
3738 case tok::kw__Imaginary:
3739 case tok::kw_void:
3740 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003741 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003742 case tok::kw_char16_t:
3743 case tok::kw_char32_t:
3744
Chris Lattneracd58a32006-08-06 17:24:14 +00003745 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003746 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00003747 case tok::kw_float:
3748 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003749 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003750 case tok::kw__Bool:
3751 case tok::kw__Decimal32:
3752 case tok::kw__Decimal64:
3753 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003754 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003755
Chris Lattner861a2262008-04-13 18:59:07 +00003756 // struct-or-union-specifier (C99) or class-specifier (C++)
3757 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003758 case tok::kw_struct:
3759 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00003760 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00003761 // enum-specifier
3762 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003763
Chris Lattneracd58a32006-08-06 17:24:14 +00003764 // type-qualifier
3765 case tok::kw_const:
3766 case tok::kw_volatile:
3767 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003768
Chris Lattneracd58a32006-08-06 17:24:14 +00003769 // function-specifier
3770 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003771 case tok::kw_virtual:
3772 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003773
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003774 // static_assert-declaration
3775 case tok::kw__Static_assert:
3776
Chris Lattner599e47e2007-08-09 17:01:07 +00003777 // GNU typeof support.
3778 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003779
Chris Lattner599e47e2007-08-09 17:01:07 +00003780 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003781 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00003782 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003783
Francois Pichete878cb62011-06-19 08:02:06 +00003784 // C++0x decltype.
David Blaikie15a430a2011-12-04 05:04:18 +00003785 case tok::annot_decltype:
Francois Pichete878cb62011-06-19 08:02:06 +00003786 return true;
3787
Benjamin Kramere56f3932011-12-23 17:00:35 +00003788 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003789 case tok::kw__Atomic:
3790 return true;
3791
Chris Lattner8b2ec162008-07-26 03:38:44 +00003792 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3793 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003794 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003795
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003796 // typedef-name
3797 case tok::annot_typename:
3798 return !DisambiguatingWithExpression ||
3799 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00003800
Steve Narofff192fab2009-01-06 19:34:12 +00003801 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003802 case tok::kw___cdecl:
3803 case tok::kw___stdcall:
3804 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003805 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003806 case tok::kw___w64:
3807 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003808 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003809 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003810 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003811 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003812
3813 case tok::kw___private:
3814 case tok::kw___local:
3815 case tok::kw___global:
3816 case tok::kw___constant:
3817 case tok::kw___read_only:
3818 case tok::kw___read_write:
3819 case tok::kw___write_only:
3820
Eli Friedman53339e02009-06-08 23:27:34 +00003821 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003822 }
3823}
3824
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003825bool Parser::isConstructorDeclarator() {
3826 TentativeParsingAction TPA(*this);
3827
3828 // Parse the C++ scope specifier.
3829 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00003830 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003831 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00003832 TPA.Revert();
3833 return false;
3834 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003835
3836 // Parse the constructor name.
3837 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3838 // We already know that we have a constructor name; just consume
3839 // the token.
3840 ConsumeToken();
3841 } else {
3842 TPA.Revert();
3843 return false;
3844 }
3845
Richard Smith43f340f2012-03-27 23:05:05 +00003846 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003847 if (Tok.isNot(tok::l_paren)) {
3848 TPA.Revert();
3849 return false;
3850 }
3851 ConsumeParen();
3852
Richard Smith43f340f2012-03-27 23:05:05 +00003853 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3854 // that we have a constructor.
3855 if (Tok.is(tok::r_paren) ||
3856 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003857 TPA.Revert();
3858 return true;
3859 }
3860
3861 // If we need to, enter the specified scope.
3862 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003863 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003864 DeclScopeObj.EnterDeclaratorScope();
3865
Francois Pichet79f3a872011-01-31 04:54:32 +00003866 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003867 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003868 MaybeParseMicrosoftAttributes(Attrs);
3869
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003870 // Check whether the next token(s) are part of a declaration
3871 // specifier, in which case we have the start of a parameter and,
3872 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00003873 bool IsConstructor = false;
3874 if (isDeclarationSpecifier())
3875 IsConstructor = true;
3876 else if (Tok.is(tok::identifier) ||
3877 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3878 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3879 // This might be a parenthesized member name, but is more likely to
3880 // be a constructor declaration with an invalid argument type. Keep
3881 // looking.
3882 if (Tok.is(tok::annot_cxxscope))
3883 ConsumeToken();
3884 ConsumeToken();
3885
3886 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00003887 // which must have one of the following syntactic forms (see the
3888 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00003889 switch (Tok.getKind()) {
3890 case tok::l_paren:
3891 // C(X ( int));
3892 case tok::l_square:
3893 // C(X [ 5]);
3894 // C(X [ [attribute]]);
3895 case tok::coloncolon:
3896 // C(X :: Y);
3897 // C(X :: *p);
3898 case tok::r_paren:
3899 // C(X )
3900 // Assume this isn't a constructor, rather than assuming it's a
3901 // constructor with an unnamed parameter of an ill-formed type.
3902 break;
3903
3904 default:
3905 IsConstructor = true;
3906 break;
3907 }
3908 }
3909
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003910 TPA.Revert();
3911 return IsConstructor;
3912}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003913
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003914/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003915/// type-qualifier-list: [C99 6.7.5]
3916/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00003917/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00003918/// [ only if VendorAttributesAllowed=true ]
3919/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00003920/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00003921/// [ only if VendorAttributesAllowed=true ]
3922/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3923/// [ only if CXX0XAttributesAllowed=true ]
3924/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003925///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003926void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3927 bool VendorAttributesAllowed,
Richard Smith3dff2512012-04-10 03:25:07 +00003928 bool CXX11AttributesAllowed) {
3929 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003930 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00003931 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00003932 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003933 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003934 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003935
3936 SourceLocation EndLoc;
3937
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003938 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003939 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003940 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003941 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003942 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003943
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003944 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003945 case tok::code_completion:
3946 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003947 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00003948
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003949 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003950 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003951 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003952 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003953 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003954 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003955 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003956 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003957 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003958 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003959 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003960 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003961
3962 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003963 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003964 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003965 goto DoneWithTypeQuals;
3966 case tok::kw___private:
3967 case tok::kw___global:
3968 case tok::kw___local:
3969 case tok::kw___constant:
3970 case tok::kw___read_only:
3971 case tok::kw___write_only:
3972 case tok::kw___read_write:
3973 ParseOpenCLQualifiers(DS);
3974 break;
3975
Eli Friedman53339e02009-06-08 23:27:34 +00003976 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003977 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003978 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00003979 case tok::kw___cdecl:
3980 case tok::kw___stdcall:
3981 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003982 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00003983 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003984 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003985 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003986 continue;
3987 }
3988 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003989 case tok::kw___pascal:
3990 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003991 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003992 continue;
3993 }
3994 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003995 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003996 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003997 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003998 continue; // do *not* consume the next token!
3999 }
4000 // otherwise, FALL THROUGH!
4001 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004002 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004003 // If this is not a type-qualifier token, we're done reading type
4004 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004005 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004006 if (EndLoc.isValid())
4007 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004008 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004009 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004010
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004011 // If the specifier combination wasn't legal, issue a diagnostic.
4012 if (isInvalid) {
4013 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004014 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004015 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004016 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004017 }
4018}
4019
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004020
4021/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4022///
4023void Parser::ParseDeclarator(Declarator &D) {
4024 /// This implements the 'declarator' production in the C grammar, then checks
4025 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004026 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004027}
4028
Richard Smith0efa75c2012-03-29 01:16:42 +00004029static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4030 if (Kind == tok::star || Kind == tok::caret)
4031 return true;
4032
4033 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4034 if (!Lang.CPlusPlus)
4035 return false;
4036
4037 return Kind == tok::amp || Kind == tok::ampamp;
4038}
4039
Sebastian Redlbd150f42008-11-21 19:14:01 +00004040/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4041/// is parsed by the function passed to it. Pass null, and the direct-declarator
4042/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004043/// ptr-operator production.
4044///
Richard Smith09f76ee2011-10-19 21:33:05 +00004045/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004046/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4047/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004048///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004049/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4050/// [C] pointer[opt] direct-declarator
4051/// [C++] direct-declarator
4052/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004053///
4054/// pointer: [C99 6.7.5]
4055/// '*' type-qualifier-list[opt]
4056/// '*' type-qualifier-list[opt] pointer
4057///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004058/// ptr-operator:
4059/// '*' cv-qualifier-seq[opt]
4060/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004061/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004062/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004063/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004064/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004065void Parser::ParseDeclaratorInternal(Declarator &D,
4066 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004067 if (Diags.hasAllExtensionsSilenced())
4068 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004069
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004070 // C++ member pointers start with a '::' or a nested-name.
4071 // Member pointers get special handling, since there's no place for the
4072 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004073 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004074 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4075 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004076 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4077 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004078 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004079 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004080
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004081 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004082 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004083 // The scope spec really belongs to the direct-declarator.
4084 D.getCXXScopeSpec() = SS;
4085 if (DirectDeclParser)
4086 (this->*DirectDeclParser)(D);
4087 return;
4088 }
4089
4090 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004091 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004092 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004093 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004094 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004095
4096 // Recurse to parse whatever is left.
4097 ParseDeclaratorInternal(D, DirectDeclParser);
4098
4099 // Sema will have to catch (syntactically invalid) pointers into global
4100 // scope. It has to catch pointers into namespace scope anyway.
4101 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004102 Loc),
4103 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004104 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004105 return;
4106 }
4107 }
4108
4109 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004110 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004111 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004112 if (DirectDeclParser)
4113 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004114 return;
4115 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004116
Sebastian Redled0f3b02009-03-15 22:02:01 +00004117 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4118 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004119 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004120 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004121
Chris Lattner9eac9312009-03-27 04:18:06 +00004122 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004123 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004124 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004125
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004126 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004127 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004128 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004129
Bill Wendling3708c182007-05-27 10:15:43 +00004130 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004131 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004132 if (Kind == tok::star)
4133 // Remember that we parsed a pointer type, and remember the type-quals.
4134 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004135 DS.getConstSpecLoc(),
4136 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004137 DS.getRestrictSpecLoc()),
4138 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004139 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004140 else
4141 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004142 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004143 Loc),
4144 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004145 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004146 } else {
4147 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004148 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004149
Sebastian Redl3b27be62009-03-23 00:00:23 +00004150 // Complain about rvalue references in C++03, but then go on and build
4151 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004152 if (Kind == tok::ampamp)
David Blaikiebbafb8a2012-03-11 07:00:24 +00004153 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004154 diag::warn_cxx98_compat_rvalue_reference :
4155 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004156
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004157 // GNU-style and C++11 attributes are allowed here, as is restrict.
4158 ParseTypeQualifierListOpt(DS);
4159 D.ExtendWithDeclSpec(DS);
4160
Bill Wendling93efb222007-06-02 23:28:54 +00004161 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4162 // cv-qualifiers are introduced through the use of a typedef or of a
4163 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004164 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4165 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4166 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004167 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004168 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4169 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004170 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00004171 }
Bill Wendling3708c182007-05-27 10:15:43 +00004172
4173 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004174 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004175
Douglas Gregor66583c52008-11-03 15:51:28 +00004176 if (D.getNumTypeObjects() > 0) {
4177 // C++ [dcl.ref]p4: There shall be no references to references.
4178 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4179 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004180 if (const IdentifierInfo *II = D.getIdentifier())
4181 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4182 << II;
4183 else
4184 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4185 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004186
Sebastian Redlbd150f42008-11-21 19:14:01 +00004187 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004188 // can go ahead and build the (technically ill-formed)
4189 // declarator: reference collapsing will take care of it.
4190 }
4191 }
4192
Bill Wendling3708c182007-05-27 10:15:43 +00004193 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00004194 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004195 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004196 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004197 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004198 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004199}
4200
Richard Smith0efa75c2012-03-29 01:16:42 +00004201static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4202 SourceLocation EllipsisLoc) {
4203 if (EllipsisLoc.isValid()) {
4204 FixItHint Insertion;
4205 if (!D.getEllipsisLoc().isValid()) {
4206 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4207 D.setEllipsisLoc(EllipsisLoc);
4208 }
4209 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4210 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4211 }
4212}
4213
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004214/// ParseDirectDeclarator
4215/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004216/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004217/// '(' declarator ')'
4218/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004219/// [C90] direct-declarator '[' constant-expression[opt] ']'
4220/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4221/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4222/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4223/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004224/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4225/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004226/// direct-declarator '(' parameter-type-list ')'
4227/// direct-declarator '(' identifier-list[opt] ')'
4228/// [GNU] direct-declarator '(' parameter-forward-declarations
4229/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004230/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4231/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004232/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4233/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4234/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004235/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004236/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004237///
4238/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004239/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004240/// '::'[opt] nested-name-specifier[opt] type-name
4241///
4242/// id-expression: [C++ 5.1]
4243/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004244/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004245///
4246/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004247/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004248/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004249/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004250/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004251/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004252///
Richard Smith1453e312012-03-27 01:42:32 +00004253/// Note, any additional constructs added here may need corresponding changes
4254/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004255void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004256 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004257
David Blaikiebbafb8a2012-03-11 07:00:24 +00004258 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004259 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004260 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004261 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4262 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004263 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004264 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004265 }
4266
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004267 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004268 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004269 // Change the declaration context for name lookup, until this function
4270 // is exited (and the declarator has been parsed).
4271 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004272 }
4273
Douglas Gregor27b4c162010-12-23 22:44:42 +00004274 // C++0x [dcl.fct]p14:
4275 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004276 // of a parameter-declaration-clause without a preceding comma. In
4277 // this case, the ellipsis is parsed as part of the
4278 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004279 // parameter pack that has not been expanded; otherwise, it is parsed
4280 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004281 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004282 !((D.getContext() == Declarator::PrototypeContext ||
4283 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004284 NextToken().is(tok::r_paren) &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004285 !Actions.containsUnexpandedParameterPacks(D))) {
4286 SourceLocation EllipsisLoc = ConsumeToken();
4287 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4288 // The ellipsis was put in the wrong place. Recover, and explain to
4289 // the user what they should have done.
4290 ParseDeclarator(D);
4291 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4292 return;
4293 } else
4294 D.setEllipsisLoc(EllipsisLoc);
4295
4296 // The ellipsis can't be followed by a parenthesized declarator. We
4297 // check for that in ParseParenDeclarator, after we have disambiguated
4298 // the l_paren token.
4299 }
4300
Douglas Gregor7861a802009-11-03 01:35:08 +00004301 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4302 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4303 // We found something that indicates the start of an unqualified-id.
4304 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004305 bool AllowConstructorName;
4306 if (D.getDeclSpec().hasTypeSpecifier())
4307 AllowConstructorName = false;
4308 else if (D.getCXXScopeSpec().isSet())
4309 AllowConstructorName =
4310 (D.getContext() == Declarator::FileContext ||
4311 (D.getContext() == Declarator::MemberContext &&
4312 D.getDeclSpec().isFriendSpecified()));
4313 else
4314 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4315
Abramo Bagnara7945c982012-01-27 09:46:47 +00004316 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004317 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4318 /*EnteringContext=*/true,
4319 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004320 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004321 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004322 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004323 D.getName()) ||
4324 // Once we're past the identifier, if the scope was bad, mark the
4325 // whole declarator bad.
4326 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004327 D.SetIdentifier(0, Tok.getLocation());
4328 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004329 } else {
4330 // Parsed the unqualified-id; update range information and move along.
4331 if (D.getSourceRange().getBegin().isInvalid())
4332 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4333 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004334 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004335 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004336 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004337 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004338 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004339 "There's a C++-specific check for tok::identifier above");
4340 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4341 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4342 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004343 goto PastIdentifier;
4344 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004345
Douglas Gregor7861a802009-11-03 01:35:08 +00004346 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004347 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004348 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004349 // Example: 'char (*X)' or 'int (*XX)(void)'
4350 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004351
4352 // If the declarator was parenthesized, we entered the declarator
4353 // scope when parsing the parenthesized declarator, then exited
4354 // the scope already. Re-enter the scope, if we need to.
4355 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004356 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004357 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004358 if (!D.isInvalidType() &&
4359 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004360 // Change the declaration context for name lookup, until this function
4361 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004362 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004363 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004364 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004365 // This could be something simple like "int" (in which case the declarator
4366 // portion is empty), if an abstract-declarator is allowed.
4367 D.SetIdentifier(0, Tok.getLocation());
4368 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004369 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004370 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004371 if (D.getContext() == Declarator::MemberContext)
4372 Diag(Tok, diag::err_expected_member_name_or_semi)
4373 << D.getDeclSpec().getSourceRange();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004374 else if (getLangOpts().CPlusPlus)
4375 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004376 else
Chris Lattner6d29c102008-11-18 07:48:38 +00004377 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004378 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004379 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004380 }
Mike Stump11289f42009-09-09 15:08:12 +00004381
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004382 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004383 assert(D.isPastIdentifier() &&
4384 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004385
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004386 // Don't parse attributes unless we have parsed an unparenthesized name.
4387 if (D.hasName() && !D.getNumTypeObjects())
John McCall53fa7142010-12-24 02:08:15 +00004388 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004389
Chris Lattneracd58a32006-08-06 17:24:14 +00004390 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004391 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004392 // Enter function-declaration scope, limiting any declarators to the
4393 // function prototype scope, including parameter declarators.
4394 ParseScope PrototypeScope(this,
4395 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004396 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4397 // In such a case, check if we actually have a function declarator; if it
4398 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004399 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004400 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4401 // The name of the declarator, if any, is tentatively declared within
4402 // a possible direct initializer.
4403 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4404 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4405 TentativelyDeclaredIdentifiers.pop_back();
4406 if (!IsFunctionDecl)
4407 break;
4408 }
John McCall084e83d2011-03-24 11:26:52 +00004409 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004410 BalancedDelimiterTracker T(*this, tok::l_paren);
4411 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004412 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004413 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004414 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004415 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004416 } else {
4417 break;
4418 }
4419 }
Chad Rosierc1183952012-06-26 22:30:43 +00004420}
Chris Lattneracd58a32006-08-06 17:24:14 +00004421
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004422/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4423/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004424/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004425/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4426///
4427/// direct-declarator:
4428/// '(' declarator ')'
4429/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004430/// direct-declarator '(' parameter-type-list ')'
4431/// direct-declarator '(' identifier-list[opt] ')'
4432/// [GNU] direct-declarator '(' parameter-forward-declarations
4433/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004434///
4435void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004436 BalancedDelimiterTracker T(*this, tok::l_paren);
4437 T.consumeOpen();
4438
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004439 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004440
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004441 // Eat any attributes before we look at whether this is a grouping or function
4442 // declarator paren. If this is a grouping paren, the attribute applies to
4443 // the type being built up, for example:
4444 // int (__attribute__(()) *x)(long y)
4445 // If this ends up not being a grouping paren, the attribute applies to the
4446 // first argument, for example:
4447 // int (__attribute__(()) int x)
4448 // In either case, we need to eat any attributes to be able to determine what
4449 // sort of paren this is.
4450 //
John McCall084e83d2011-03-24 11:26:52 +00004451 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004452 bool RequiresArg = false;
4453 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004454 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004455
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004456 // We require that the argument list (if this is a non-grouping paren) be
4457 // present even if the attribute list was empty.
4458 RequiresArg = true;
4459 }
Steve Naroff44ac7772008-12-25 14:16:32 +00004460 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00004461 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00004462 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet17ed0202011-08-18 09:59:55 +00004463 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +00004464 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall53fa7142010-12-24 02:08:15 +00004465 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00004466 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00004467 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004468 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004469 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004470
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004471 // If we haven't past the identifier yet (or where the identifier would be
4472 // stored, if this is an abstract declarator), then this is probably just
4473 // grouping parens. However, if this could be an abstract-declarator, then
4474 // this could also be the start of function arguments (consider 'void()').
4475 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004476
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004477 if (!D.mayOmitIdentifier()) {
4478 // If this can't be an abstract-declarator, this *must* be a grouping
4479 // paren, because we haven't seen the identifier yet.
4480 isGrouping = true;
4481 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004482 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4483 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004484 isDeclarationSpecifier() || // 'int(int)' is a function.
4485 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004486 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4487 // considered to be a type, not a K&R identifier-list.
4488 isGrouping = false;
4489 } else {
4490 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4491 isGrouping = true;
4492 }
Mike Stump11289f42009-09-09 15:08:12 +00004493
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004494 // If this is a grouping paren, handle:
4495 // direct-declarator: '(' declarator ')'
4496 // direct-declarator: '(' attributes declarator ')'
4497 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004498 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4499 D.setEllipsisLoc(SourceLocation());
4500
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004501 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004502 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004503 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004504 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004505 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004506 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004507 T.getCloseLocation()),
4508 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004509
4510 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004511
4512 // An ellipsis cannot be placed outside parentheses.
4513 if (EllipsisLoc.isValid())
4514 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4515
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004516 return;
4517 }
Mike Stump11289f42009-09-09 15:08:12 +00004518
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004519 // Okay, if this wasn't a grouping paren, it must be the start of a function
4520 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004521 // identifier (and remember where it would have been), then call into
4522 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004523 D.SetIdentifier(0, Tok.getLocation());
4524
David Blaikie15a430a2011-12-04 05:04:18 +00004525 // Enter function-declaration scope, limiting any declarators to the
4526 // function prototype scope, including parameter declarators.
4527 ParseScope PrototypeScope(this,
4528 Scope::FunctionPrototypeScope|Scope::DeclScope);
Richard Smith943c4402012-07-30 21:30:52 +00004529 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004530 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004531}
4532
4533/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4534/// declarator D up to a paren, which indicates that we are parsing function
4535/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004536///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004537/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4538/// immediately after the open paren - they should be considered to be the
4539/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004540///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004541/// If RequiresArg is true, then the first argument of the function is required
4542/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004543///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004544/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4545/// (C++11) ref-qualifier[opt], exception-specification[opt],
4546/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4547///
4548/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004549/// dynamic-exception-specification
4550/// noexcept-specification
4551///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004552void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004553 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004554 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004555 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004556 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004557 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004558 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004559 // lparen is already consumed!
4560 assert(D.isPastIdentifier() && "Should not call before identifier!");
4561
4562 // This should be true when the function has typed arguments.
4563 // Otherwise, it is treated as a K&R-style function.
4564 bool HasProto = false;
4565 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004566 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004567 // Remember where we see an ellipsis, if any.
4568 SourceLocation EllipsisLoc;
4569
4570 DeclSpec DS(AttrFactory);
4571 bool RefQualifierIsLValueRef = true;
4572 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004573 SourceLocation ConstQualifierLoc;
4574 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004575 ExceptionSpecificationType ESpecType = EST_None;
4576 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004577 SmallVector<ParsedType, 2> DynamicExceptions;
4578 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004579 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004580 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004581 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004582
James Molloy6f8780b2012-02-29 10:24:19 +00004583 Actions.ActOnStartFunctionDeclarator();
4584
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004585 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4586 EndLoc is the end location for the function declarator.
4587 They differ for trailing return types. */
4588 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004589 SourceLocation LParenLoc, RParenLoc;
4590 LParenLoc = Tracker.getOpenLocation();
4591 StartLoc = LParenLoc;
4592
Douglas Gregor9e66af42011-07-05 16:44:18 +00004593 if (isFunctionDeclaratorIdentifierList()) {
4594 if (RequiresArg)
4595 Diag(Tok, diag::err_argument_required_after_attribute);
4596
4597 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4598
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004599 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004600 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004601 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004602 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004603 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004604 if (Tok.isNot(tok::r_paren))
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004605 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004606 else if (RequiresArg)
4607 Diag(Tok, diag::err_argument_required_after_attribute);
4608
David Blaikiebbafb8a2012-03-11 07:00:24 +00004609 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004610
4611 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004612 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004613 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004614 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004615 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004616
David Blaikiebbafb8a2012-03-11 07:00:24 +00004617 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004618 // FIXME: Accept these components in any order, and produce fixits to
4619 // correct the order if the user gets it wrong. Ideally we should deal
4620 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004621
4622 // Parse cv-qualifier-seq[opt].
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004623 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4624 if (!DS.getSourceRange().getEnd().isInvalid()) {
4625 EndLoc = DS.getSourceRange().getEnd();
4626 ConstQualifierLoc = DS.getConstSpecLoc();
4627 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4628 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004629
4630 // Parse ref-qualifier[opt].
4631 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004632 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004633 diag::warn_cxx98_compat_ref_qualifier :
4634 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004635
Douglas Gregor9e66af42011-07-05 16:44:18 +00004636 RefQualifierIsLValueRef = Tok.is(tok::amp);
4637 RefQualifierLoc = ConsumeToken();
4638 EndLoc = RefQualifierLoc;
4639 }
4640
Douglas Gregor3024f072012-04-16 07:05:22 +00004641 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00004642 // If a declaration declares a member function or member function
4643 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00004644 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00004645 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00004646 // declarator.
Chad Rosierc1183952012-06-26 22:30:43 +00004647 bool IsCXX11MemberFunction =
Douglas Gregor3024f072012-04-16 07:05:22 +00004648 getLangOpts().CPlusPlus0x &&
4649 (D.getContext() == Declarator::MemberContext ||
4650 (D.getContext() == Declarator::FileContext &&
Chad Rosierc1183952012-06-26 22:30:43 +00004651 D.getCXXScopeSpec().isValid() &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004652 Actions.CurContext->isRecord()));
4653 Sema::CXXThisScopeRAII ThisScope(Actions,
4654 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4655 DS.getTypeQualifiers(),
4656 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00004657
Douglas Gregor9e66af42011-07-05 16:44:18 +00004658 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00004659 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00004660 DynamicExceptions,
4661 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00004662 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004663 if (ESpecType != EST_None)
4664 EndLoc = ESpecRange.getEnd();
4665
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004666 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4667 // after the exception-specification.
4668 MaybeParseCXX0XAttributes(FnAttrs);
4669
Douglas Gregor9e66af42011-07-05 16:44:18 +00004670 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004671 LocalEndLoc = EndLoc;
David Blaikiebbafb8a2012-03-11 07:00:24 +00004672 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00004673 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004674 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4675 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004676 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004677 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00004678 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004679 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004680 }
4681 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004682 }
4683
4684 // Remember that we parsed a function type, and remember the attributes.
4685 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004686 IsAmbiguous,
4687 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004688 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004689 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004690 DS.getTypeQualifiers(),
4691 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00004692 RefQualifierLoc, ConstQualifierLoc,
4693 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00004694 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00004695 ESpecType, ESpecRange.getBegin(),
4696 DynamicExceptions.data(),
4697 DynamicExceptionRanges.data(),
4698 DynamicExceptions.size(),
4699 NoexceptExpr.isUsable() ?
4700 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004701 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004702 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004703 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00004704
4705 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004706}
4707
4708/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4709/// identifier list form for a K&R-style function: void foo(a,b,c)
4710///
4711/// Note that identifier-lists are only allowed for normal declarators, not for
4712/// abstract-declarators.
4713bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004714 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00004715 && Tok.is(tok::identifier)
4716 && !TryAltiVecVectorToken()
4717 // K&R identifier lists can't have typedefs as identifiers, per C99
4718 // 6.7.5.3p11.
4719 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4720 // Identifier lists follow a really simple grammar: the identifiers can
4721 // be followed *only* by a ", identifier" or ")". However, K&R
4722 // identifier lists are really rare in the brave new modern world, and
4723 // it is very common for someone to typo a type in a non-K&R style
4724 // list. If we are presented with something like: "void foo(intptr x,
4725 // float y)", we don't want to start parsing the function declarator as
4726 // though it is a K&R style declarator just because intptr is an
4727 // invalid type.
4728 //
4729 // To handle this, we check to see if the token after the first
4730 // identifier is a "," or ")". Only then do we parse it as an
4731 // identifier list.
4732 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4733}
4734
4735/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4736/// we found a K&R-style identifier list instead of a typed parameter list.
4737///
4738/// After returning, ParamInfo will hold the parsed parameters.
4739///
4740/// identifier-list: [C99 6.7.5]
4741/// identifier
4742/// identifier-list ',' identifier
4743///
4744void Parser::ParseFunctionDeclaratorIdentifierList(
4745 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004746 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004747 // If there was no identifier specified for the declarator, either we are in
4748 // an abstract-declarator, or we are in a parameter declarator which was found
4749 // to be abstract. In abstract-declarators, identifier lists are not valid:
4750 // diagnose this.
4751 if (!D.getIdentifier())
4752 Diag(Tok, diag::ext_ident_list_in_param);
4753
4754 // Maintain an efficient lookup of params we have seen so far.
4755 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4756
4757 while (1) {
4758 // If this isn't an identifier, report the error and skip until ')'.
4759 if (Tok.isNot(tok::identifier)) {
4760 Diag(Tok, diag::err_expected_ident);
4761 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4762 // Forget we parsed anything.
4763 ParamInfo.clear();
4764 return;
4765 }
4766
4767 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4768
4769 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4770 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4771 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4772
4773 // Verify that the argument identifier has not already been mentioned.
4774 if (!ParamsSoFar.insert(ParmII)) {
4775 Diag(Tok, diag::err_param_redefinition) << ParmII;
4776 } else {
4777 // Remember this identifier in ParamInfo.
4778 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4779 Tok.getLocation(),
4780 0));
4781 }
4782
4783 // Eat the identifier.
4784 ConsumeToken();
4785
4786 // The list continues if we see a comma.
4787 if (Tok.isNot(tok::comma))
4788 break;
4789 ConsumeToken();
4790 }
4791}
4792
4793/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4794/// after the opening parenthesis. This function will not parse a K&R-style
4795/// identifier list.
4796///
Richard Smith2620cd92012-04-11 04:01:28 +00004797/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4798/// caller parsed those arguments immediately after the open paren - they should
4799/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004800///
4801/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4802/// be the location of the ellipsis, if any was parsed.
4803///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004804/// parameter-type-list: [C99 6.7.5]
4805/// parameter-list
4806/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004807/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004808///
4809/// parameter-list: [C99 6.7.5]
4810/// parameter-declaration
4811/// parameter-list ',' parameter-declaration
4812///
4813/// parameter-declaration: [C99 6.7.5]
4814/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004815/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00004816/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00004817/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00004818/// declaration-specifiers abstract-declarator[opt]
4819/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00004820/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004821/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00004822/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004823///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004824void Parser::ParseParameterDeclarationClause(
4825 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00004826 ParsedAttributes &FirstArgAttrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004827 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004828 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00004829
Chris Lattner371ed4e2008-04-06 06:57:35 +00004830 while (1) {
4831 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00004832 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4833 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00004834 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00004835 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00004836 }
Mike Stump11289f42009-09-09 15:08:12 +00004837
Chris Lattner371ed4e2008-04-06 06:57:35 +00004838 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00004839 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00004840 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004841
Richard Smith2620cd92012-04-11 04:01:28 +00004842 // Parse any C++11 attributes.
4843 MaybeParseCXX0XAttributes(DS.getAttributes());
4844
John McCall53fa7142010-12-24 02:08:15 +00004845 // Skip any Microsoft attributes before a param.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004846 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall53fa7142010-12-24 02:08:15 +00004847 ParseMicrosoftAttributes(DS.getAttributes());
4848
4849 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004850
4851 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004852 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004853 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00004854 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4855 // too much hassle.
4856 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00004857
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004858 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004859
Chris Lattner371ed4e2008-04-06 06:57:35 +00004860 // Parse the declarator. This is "PrototypeContext", because we must
4861 // accept either 'declarator' or 'abstract-declarator' here.
4862 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4863 ParseDeclarator(ParmDecl);
4864
4865 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00004866 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00004867
Chris Lattner371ed4e2008-04-06 06:57:35 +00004868 // Remember this parsed parameter in ParamInfo.
4869 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00004870
Douglas Gregor4d87df52008-12-16 21:30:33 +00004871 // DefArgToks is used when the parsing of default arguments needs
4872 // to be delayed.
4873 CachedTokens *DefArgToks = 0;
4874
Chris Lattner371ed4e2008-04-06 06:57:35 +00004875 // If no parameter was specified, verify that *something* was specified,
4876 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004877 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4878 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00004879 // Completely missing, emit error.
4880 Diag(DSStart, diag::err_missing_param);
4881 } else {
4882 // Otherwise, we have something. Add it and let semantic analysis try
4883 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00004884
Chris Lattner371ed4e2008-04-06 06:57:35 +00004885 // Inform the actions module about the parameter declarator, so it gets
4886 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00004887 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004888
4889 // Parse the default argument, if any. We parse the default
4890 // arguments in all dialects; the semantic analysis in
4891 // ActOnParamDefaultArgument will reject the default argument in
4892 // C.
4893 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00004894 SourceLocation EqualLoc = Tok.getLocation();
4895
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004896 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00004897 if (D.getContext() == Declarator::MemberContext) {
4898 // If we're inside a class definition, cache the tokens
4899 // corresponding to the default argument. We'll actually parse
4900 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00004901 // FIXME: Can we use a smart pointer for Toks?
4902 DefArgToks = new CachedTokens;
4903
Mike Stump11289f42009-09-09 15:08:12 +00004904 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00004905 /*StopAtSemi=*/true,
4906 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004907 delete DefArgToks;
4908 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00004909 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004910 } else {
4911 // Mark the end of the default argument so that we know when to
4912 // stop when we parse it later on.
4913 Token DefArgEnd;
4914 DefArgEnd.startToken();
4915 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4916 DefArgEnd.setLocation(Tok.getLocation());
4917 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00004918 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00004919 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004920 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004921 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004922 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00004923 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004924
Chad Rosierc1183952012-06-26 22:30:43 +00004925 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004926 // used.
4927 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00004928 Sema::PotentiallyEvaluatedIfUsed,
4929 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004930
Sebastian Redldb63af22012-03-14 15:54:00 +00004931 ExprResult DefArgResult;
Sebastian Redl1678d5f2012-03-18 22:25:45 +00004932 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4933 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00004934 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00004935 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00004936 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00004937 if (DefArgResult.isInvalid()) {
4938 Actions.ActOnParamDefaultArgumentError(Param);
4939 SkipUntil(tok::comma, tok::r_paren, true, true);
4940 } else {
4941 // Inform the actions module about the default argument
4942 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00004943 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004944 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004945 }
4946 }
Mike Stump11289f42009-09-09 15:08:12 +00004947
4948 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4949 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00004950 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00004951 }
4952
4953 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004954 if (Tok.isNot(tok::comma)) {
4955 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004956 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00004957
David Blaikiebbafb8a2012-03-11 07:00:24 +00004958 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004959 // We have ellipsis without a preceding ',', which is ill-formed
4960 // in C. Complain and provide the fix.
4961 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00004962 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004963 }
4964 }
Chad Rosierc1183952012-06-26 22:30:43 +00004965
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004966 break;
4967 }
Mike Stump11289f42009-09-09 15:08:12 +00004968
Chris Lattner371ed4e2008-04-06 06:57:35 +00004969 // Consume the comma.
4970 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00004971 }
Mike Stump11289f42009-09-09 15:08:12 +00004972
Chris Lattner6c940e62008-04-06 06:34:08 +00004973}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004974
Chris Lattnere8074e62006-08-06 18:30:15 +00004975/// [C90] direct-declarator '[' constant-expression[opt] ']'
4976/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4977/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4978/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4979/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004980/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4981/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00004982void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004983 if (CheckProhibitedCXX11Attribute())
4984 return;
4985
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004986 BalancedDelimiterTracker T(*this, tok::l_square);
4987 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00004988
Chris Lattner84a11622008-12-18 07:27:21 +00004989 // C array syntax has many features, but by-far the most common is [] and [4].
4990 // This code does a fast path to handle some of the most obvious cases.
4991 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004992 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00004993 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004994 MaybeParseCXX0XAttributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00004995
Chris Lattner84a11622008-12-18 07:27:21 +00004996 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004997 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004998 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004999 T.getOpenLocation(),
5000 T.getCloseLocation()),
5001 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005002 return;
5003 } else if (Tok.getKind() == tok::numeric_constant &&
5004 GetLookAheadToken(1).is(tok::r_square)) {
5005 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005006 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005007 ConsumeToken();
5008
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005009 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005010 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00005011 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005012
Chris Lattner84a11622008-12-18 07:27:21 +00005013 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005014 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00005015 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005016 T.getOpenLocation(),
5017 T.getCloseLocation()),
5018 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005019 return;
5020 }
Mike Stump11289f42009-09-09 15:08:12 +00005021
Chris Lattnere8074e62006-08-06 18:30:15 +00005022 // If valid, this location is the position where we read the 'static' keyword.
5023 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005024 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005025 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005026
Chris Lattnere8074e62006-08-06 18:30:15 +00005027 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005028 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005029 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005030 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005031
Chris Lattnere8074e62006-08-06 18:30:15 +00005032 // If we haven't already read 'static', check to see if there is one after the
5033 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005034 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005035 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005036
Chris Lattnere8074e62006-08-06 18:30:15 +00005037 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005038 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005039 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005040
Chris Lattner521ff2b2008-04-06 05:26:30 +00005041 // Handle the case where we have '[*]' as the array size. However, a leading
5042 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005043 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005044 // infrequent, use of lookahead is not costly here.
5045 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005046 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005047
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005048 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005049 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005050 StaticLoc = SourceLocation(); // Drop the static.
5051 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005052 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005053 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005054 // Note, in C89, this production uses the constant-expr production instead
5055 // of assignment-expr. The only difference is that assignment-expr allows
5056 // things like '=' and '*='. Sema rejects these in C89 mode because they
5057 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005058
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005059 // Parse the constant-expression or assignment-expression now (depending
5060 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005061 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005062 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005063 } else {
5064 EnterExpressionEvaluationContext Unevaluated(Actions,
5065 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005066 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005067 }
Chris Lattner62591722006-08-12 18:40:58 +00005068 }
Mike Stump11289f42009-09-09 15:08:12 +00005069
Chris Lattner62591722006-08-12 18:40:58 +00005070 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005071 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005072 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005073 // If the expression was invalid, skip it.
5074 SkipUntil(tok::r_square);
5075 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005076 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005077
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005078 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005079
John McCall084e83d2011-03-24 11:26:52 +00005080 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00005081 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005082
Chris Lattner84a11622008-12-18 07:27:21 +00005083 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005084 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005085 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005086 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005087 T.getOpenLocation(),
5088 T.getCloseLocation()),
5089 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005090}
5091
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005092/// [GNU] typeof-specifier:
5093/// typeof ( expressions )
5094/// typeof ( type-name )
5095/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005096///
5097void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005098 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005099 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005100 SourceLocation StartLoc = ConsumeToken();
5101
John McCalle8595032010-01-13 20:03:27 +00005102 const bool hasParens = Tok.is(tok::l_paren);
5103
Eli Friedman15681d62012-09-26 04:34:21 +00005104 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5105 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005106
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005107 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005108 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005109 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005110 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5111 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005112 if (hasParens)
5113 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005114
5115 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005116 // FIXME: Not accurate, the range gets one token more than it should.
5117 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005118 else
5119 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005120
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005121 if (isCastExpr) {
5122 if (!CastTy) {
5123 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005124 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005125 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005126
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005127 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005128 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005129 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5130 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005131 DiagID, CastTy))
5132 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005133 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005134 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005135
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005136 // If we get here, the operand to the typeof was an expresion.
5137 if (Operand.isInvalid()) {
5138 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005139 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005140 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005141
Eli Friedmane0afc982012-01-21 01:01:51 +00005142 // We might need to transform the operand if it is potentially evaluated.
5143 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5144 if (Operand.isInvalid()) {
5145 DS.SetTypeSpecError();
5146 return;
5147 }
5148
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005149 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005150 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005151 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5152 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005153 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005154 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005155}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005156
Benjamin Kramere56f3932011-12-23 17:00:35 +00005157/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005158/// _Atomic ( type-name )
5159///
5160void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5161 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5162
5163 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005164 BalancedDelimiterTracker T(*this, tok::l_paren);
5165 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedman0dfb8892011-10-06 23:00:33 +00005166 SkipUntil(tok::r_paren);
5167 return;
5168 }
5169
5170 TypeResult Result = ParseTypeName();
5171 if (Result.isInvalid()) {
5172 SkipUntil(tok::r_paren);
5173 return;
5174 }
5175
5176 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005177 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005178
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005179 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005180 return;
5181
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005182 DS.setTypeofParensRange(T.getRange());
5183 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005184
5185 const char *PrevSpec = 0;
5186 unsigned DiagID;
5187 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5188 DiagID, Result.release()))
5189 Diag(StartLoc, DiagID) << PrevSpec;
5190}
5191
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005192
5193/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5194/// from TryAltiVecVectorToken.
5195bool Parser::TryAltiVecVectorTokenOutOfLine() {
5196 Token Next = NextToken();
5197 switch (Next.getKind()) {
5198 default: return false;
5199 case tok::kw_short:
5200 case tok::kw_long:
5201 case tok::kw_signed:
5202 case tok::kw_unsigned:
5203 case tok::kw_void:
5204 case tok::kw_char:
5205 case tok::kw_int:
5206 case tok::kw_float:
5207 case tok::kw_double:
5208 case tok::kw_bool:
5209 case tok::kw___pixel:
5210 Tok.setKind(tok::kw___vector);
5211 return true;
5212 case tok::identifier:
5213 if (Next.getIdentifierInfo() == Ident_pixel) {
5214 Tok.setKind(tok::kw___vector);
5215 return true;
5216 }
5217 return false;
5218 }
5219}
5220
5221bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5222 const char *&PrevSpec, unsigned &DiagID,
5223 bool &isInvalid) {
5224 if (Tok.getIdentifierInfo() == Ident_vector) {
5225 Token Next = NextToken();
5226 switch (Next.getKind()) {
5227 case tok::kw_short:
5228 case tok::kw_long:
5229 case tok::kw_signed:
5230 case tok::kw_unsigned:
5231 case tok::kw_void:
5232 case tok::kw_char:
5233 case tok::kw_int:
5234 case tok::kw_float:
5235 case tok::kw_double:
5236 case tok::kw_bool:
5237 case tok::kw___pixel:
5238 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5239 return true;
5240 case tok::identifier:
5241 if (Next.getIdentifierInfo() == Ident_pixel) {
5242 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5243 return true;
5244 }
5245 break;
5246 default:
5247 break;
5248 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005249 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005250 DS.isTypeAltiVecVector()) {
5251 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5252 return true;
5253 }
5254 return false;
5255}