blob: d45f038f31c977f257311ea0ff62a8a1ccf56551 [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.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000146 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000147 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) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000874 assert(LAs.parseSoon() &&
875 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000876 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +0000877 if (D)
878 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000879 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +0000880 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000881 }
882 LAs.clear();
883}
884
885
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000886/// \brief Finish parsing an attribute for which parsing was delayed.
887/// This will be called at the end of parsing a class declaration
888/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +0000889/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000890/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000891void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
892 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000893 // Save the current token position.
894 SourceLocation OrigLoc = Tok.getLocation();
895
896 // Append the current token at the end of the new token stream so that it
897 // doesn't get lost.
898 LA.Toks.push_back(Tok);
899 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
900 // Consume the previously pushed token.
901 ConsumeAnyToken();
902
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000903 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
904 Diag(Tok, diag::warn_attribute_on_function_definition)
905 << LA.AttrName.getName();
906 }
907
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000908 ParsedAttributes Attrs(AttrFactory);
909 SourceLocation endLoc;
910
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000911 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000912 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000913 NamedDecl *ND = dyn_cast<NamedDecl>(D);
914 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000915
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000916 // Allow 'this' within late-parsed attributes.
917 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
918 /*TypeQuals=*/0,
919 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000920
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000921 if (LA.Decls.size() == 1) {
922 // If the Decl is templatized, add template parameters to scope.
923 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
924 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
925 if (HasTemplateScope)
926 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000927
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000928 // If the Decl is on a function, add function parameters to the scope.
929 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
930 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
931 if (HasFunScope)
932 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000933
Michael Han23214e52012-10-03 01:56:22 +0000934 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000935 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000936
937 if (HasFunScope) {
938 Actions.ActOnExitFunctionContext();
939 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
940 }
941 if (HasTemplateScope) {
942 TempScope.Exit();
943 }
944 } else {
945 // If there are multiple decls, then the decl cannot be within the
946 // function scope.
Michael Han23214e52012-10-03 01:56:22 +0000947 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000948 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000949 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +0000950 } else {
951 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000952 }
953
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000954 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
955 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
956 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000957
958 if (Tok.getLocation() != OrigLoc) {
959 // Due to a parsing error, we either went over the cached tokens or
960 // there are still cached tokens left, so we skip the leftover tokens.
961 // Since this is an uncommon situation that should be avoided, use the
962 // expensive isBeforeInTranslationUnit call.
963 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
964 OrigLoc))
965 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +0000966 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000967 }
968}
969
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000970/// \brief Wrapper around a case statement checking if AttrName is
971/// one of the thread safety attributes
972bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
973 return llvm::StringSwitch<bool>(AttrName)
974 .Case("guarded_by", true)
975 .Case("guarded_var", true)
976 .Case("pt_guarded_by", true)
977 .Case("pt_guarded_var", true)
978 .Case("lockable", true)
979 .Case("scoped_lockable", true)
980 .Case("no_thread_safety_analysis", true)
981 .Case("acquired_after", true)
982 .Case("acquired_before", true)
983 .Case("exclusive_lock_function", true)
984 .Case("shared_lock_function", true)
985 .Case("exclusive_trylock_function", true)
986 .Case("shared_trylock_function", true)
987 .Case("unlock_function", true)
988 .Case("lock_returned", true)
989 .Case("locks_excluded", true)
990 .Case("exclusive_locks_required", true)
991 .Case("shared_locks_required", true)
992 .Default(false);
993}
994
995/// \brief Parse the contents of thread safety attributes. These
996/// should always be parsed as an expression list.
997///
998/// We need to special case the parsing due to the fact that if the first token
999/// of the first argument is an identifier, the main parse loop will store
1000/// that token as a "parameter" and the rest of
1001/// the arguments will be added to a list of "arguments". However,
1002/// subsequent tokens in the first argument are lost. We instead parse each
1003/// argument as an expression and add all arguments to the list of "arguments".
1004/// In future, we will take advantage of this special case to also
1005/// deal with some argument scoping issues here (for example, referring to a
1006/// function parameter in the attribute on that function).
1007void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1008 SourceLocation AttrNameLoc,
1009 ParsedAttributes &Attrs,
1010 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001011 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001012
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001013 BalancedDelimiterTracker T(*this, tok::l_paren);
1014 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001015
Benjamin Kramerf0623432012-08-23 22:51:59 +00001016 ExprVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001017 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001018
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001019 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001020 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001021 ExprResult ArgExpr(ParseAssignmentExpression());
1022 if (ArgExpr.isInvalid()) {
1023 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001024 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001025 break;
1026 } else {
1027 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001028 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001029 if (Tok.isNot(tok::comma))
1030 break;
1031 ConsumeToken(); // Eat the comma, move to the next argument
1032 }
1033 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001034 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001035 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramerf0623432012-08-23 22:51:59 +00001036 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001037 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001038 if (EndLoc)
1039 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001040}
1041
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001042void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1043 SourceLocation AttrNameLoc,
1044 ParsedAttributes &Attrs,
1045 SourceLocation *EndLoc) {
1046 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1047
1048 BalancedDelimiterTracker T(*this, tok::l_paren);
1049 T.consumeOpen();
1050
1051 if (Tok.isNot(tok::identifier)) {
1052 Diag(Tok, diag::err_expected_ident);
1053 T.skipToEnd();
1054 return;
1055 }
1056 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1057 SourceLocation ArgumentKindLoc = ConsumeToken();
1058
1059 if (Tok.isNot(tok::comma)) {
1060 Diag(Tok, diag::err_expected_comma);
1061 T.skipToEnd();
1062 return;
1063 }
1064 ConsumeToken();
1065
1066 SourceRange MatchingCTypeRange;
1067 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1068 if (MatchingCType.isInvalid()) {
1069 T.skipToEnd();
1070 return;
1071 }
1072
1073 bool LayoutCompatible = false;
1074 bool MustBeNull = false;
1075 while (Tok.is(tok::comma)) {
1076 ConsumeToken();
1077 if (Tok.isNot(tok::identifier)) {
1078 Diag(Tok, diag::err_expected_ident);
1079 T.skipToEnd();
1080 return;
1081 }
1082 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1083 if (Flag->isStr("layout_compatible"))
1084 LayoutCompatible = true;
1085 else if (Flag->isStr("must_be_null"))
1086 MustBeNull = true;
1087 else {
1088 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1089 T.skipToEnd();
1090 return;
1091 }
1092 ConsumeToken(); // consume flag
1093 }
1094
1095 if (!T.consumeClose()) {
1096 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1097 ArgumentKind, ArgumentKindLoc,
1098 MatchingCType.release(), LayoutCompatible,
1099 MustBeNull, AttributeList::AS_GNU);
1100 }
1101
1102 if (EndLoc)
1103 *EndLoc = T.getCloseLocation();
1104}
1105
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001106/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1107/// of a C++11 attribute-specifier in a location where an attribute is not
1108/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1109/// situation.
1110///
1111/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1112/// this doesn't appear to actually be an attribute-specifier, and the caller
1113/// should try to parse it.
1114bool Parser::DiagnoseProhibitedCXX11Attribute() {
1115 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1116
1117 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1118 case CAK_NotAttributeSpecifier:
1119 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1120 return false;
1121
1122 case CAK_InvalidAttributeSpecifier:
1123 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1124 return false;
1125
1126 case CAK_AttributeSpecifier:
1127 // Parse and discard the attributes.
1128 SourceLocation BeginLoc = ConsumeBracket();
1129 ConsumeBracket();
1130 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1131 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1132 SourceLocation EndLoc = ConsumeBracket();
1133 Diag(BeginLoc, diag::err_attributes_not_allowed)
1134 << SourceRange(BeginLoc, EndLoc);
1135 return true;
1136 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001137 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001138}
1139
John McCall53fa7142010-12-24 02:08:15 +00001140void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1141 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1142 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001143}
1144
Chris Lattner53361ac2006-08-10 05:19:57 +00001145/// ParseDeclaration - Parse a full 'declaration', which consists of
1146/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001147/// 'Context' should be a Declarator::TheContext value. This returns the
1148/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001149///
1150/// declaration: [C99 6.7]
1151/// block-declaration ->
1152/// simple-declaration
1153/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001154/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001155/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001156/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001157/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001158/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001159/// others... [FIXME]
1160///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001161Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1162 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001163 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001164 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001165 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001166 // Must temporarily exit the objective-c container scope for
1167 // parsing c none objective-c decls.
1168 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001169
John McCall48871652010-08-21 09:40:31 +00001170 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001171 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001172 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001173 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001174 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001175 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001176 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001177 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001178 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001179 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001180 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001181 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001182 SourceLocation InlineLoc = ConsumeToken();
1183 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1184 break;
1185 }
Chad Rosierc1183952012-06-26 22:30:43 +00001186 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001187 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001188 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001189 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001190 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001191 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001192 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001193 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001194 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001195 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001196 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001197 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001198 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001199 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001200 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001201 default:
John McCall53fa7142010-12-24 02:08:15 +00001202 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001203 }
Chad Rosierc1183952012-06-26 22:30:43 +00001204
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001205 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001206 // single decl, convert it now. Alias declarations can also declare a type;
1207 // include that too if it is present.
1208 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001209}
1210
1211/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1212/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001213/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1214/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001215///[C90/C++]init-declarator-list ';' [TODO]
1216/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001217///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001218/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001219/// attribute-specifier-seq[opt] type-specifier-seq declarator
1220///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001221/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001222/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001223///
1224/// If FRI is non-null, we might be parsing a for-range-declaration instead
1225/// of a simple-declaration. If we find that we are, we also parse the
1226/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001227Parser::DeclGroupPtrTy
1228Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1229 SourceLocation &DeclEnd,
1230 ParsedAttributesWithRange &attrs,
1231 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001232 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001233 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +00001234 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001235
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001236 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001237 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001238
Chris Lattner0e894622006-08-13 19:58:17 +00001239 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1240 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001241 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001242 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001243 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001244 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001245 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001246 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001247 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001248 }
Chad Rosierc1183952012-06-26 22:30:43 +00001249
1250 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001251}
Mike Stump11289f42009-09-09 15:08:12 +00001252
Richard Smith09f76ee2011-10-19 21:33:05 +00001253/// Returns true if this might be the start of a declarator, or a common typo
1254/// for a declarator.
1255bool Parser::MightBeDeclarator(unsigned Context) {
1256 switch (Tok.getKind()) {
1257 case tok::annot_cxxscope:
1258 case tok::annot_template_id:
1259 case tok::caret:
1260 case tok::code_completion:
1261 case tok::coloncolon:
1262 case tok::ellipsis:
1263 case tok::kw___attribute:
1264 case tok::kw_operator:
1265 case tok::l_paren:
1266 case tok::star:
1267 return true;
1268
1269 case tok::amp:
1270 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001271 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001272
Richard Smithc8a79032012-01-09 22:31:44 +00001273 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001274 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smithc8a79032012-01-09 22:31:44 +00001275 NextToken().is(tok::l_square);
1276
1277 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001278 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001279
Richard Smith09f76ee2011-10-19 21:33:05 +00001280 case tok::identifier:
1281 switch (NextToken().getKind()) {
1282 case tok::code_completion:
1283 case tok::coloncolon:
1284 case tok::comma:
1285 case tok::equal:
1286 case tok::equalequal: // Might be a typo for '='.
1287 case tok::kw_alignas:
1288 case tok::kw_asm:
1289 case tok::kw___attribute:
1290 case tok::l_brace:
1291 case tok::l_paren:
1292 case tok::l_square:
1293 case tok::less:
1294 case tok::r_brace:
1295 case tok::r_paren:
1296 case tok::r_square:
1297 case tok::semi:
1298 return true;
1299
1300 case tok::colon:
1301 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001302 // and in block scope it's probably a label. Inside a class definition,
1303 // this is a bit-field.
1304 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001305 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001306
1307 case tok::identifier: // Possible virt-specifier.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001308 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001309
1310 default:
1311 return false;
1312 }
1313
1314 default:
1315 return false;
1316 }
1317}
1318
Richard Smithb8caac82012-04-11 20:59:20 +00001319/// Skip until we reach something which seems like a sensible place to pick
1320/// up parsing after a malformed declaration. This will sometimes stop sooner
1321/// than SkipUntil(tok::r_brace) would, but will never stop later.
1322void Parser::SkipMalformedDecl() {
1323 while (true) {
1324 switch (Tok.getKind()) {
1325 case tok::l_brace:
1326 // Skip until matching }, then stop. We've probably skipped over
1327 // a malformed class or function definition or similar.
1328 ConsumeBrace();
1329 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1330 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1331 // This declaration isn't over yet. Keep skipping.
1332 continue;
1333 }
1334 if (Tok.is(tok::semi))
1335 ConsumeToken();
1336 return;
1337
1338 case tok::l_square:
1339 ConsumeBracket();
1340 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1341 continue;
1342
1343 case tok::l_paren:
1344 ConsumeParen();
1345 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1346 continue;
1347
1348 case tok::r_brace:
1349 return;
1350
1351 case tok::semi:
1352 ConsumeToken();
1353 return;
1354
1355 case tok::kw_inline:
1356 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001357 // a good place to pick back up parsing, except in an Objective-C
1358 // @interface context.
1359 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1360 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001361 return;
1362 break;
1363
1364 case tok::kw_namespace:
1365 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001366 // place to pick back up parsing, except in an Objective-C
1367 // @interface context.
1368 if (Tok.isAtStartOfLine() &&
1369 (!ParsingInObjCContainer || CurParsedObjCImpl))
1370 return;
1371 break;
1372
1373 case tok::at:
1374 // @end is very much like } in Objective-C contexts.
1375 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1376 ParsingInObjCContainer)
1377 return;
1378 break;
1379
1380 case tok::minus:
1381 case tok::plus:
1382 // - and + probably start new method declarations in Objective-C contexts.
1383 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001384 return;
1385 break;
1386
1387 case tok::eof:
1388 return;
1389
1390 default:
1391 break;
1392 }
1393
1394 ConsumeAnyToken();
1395 }
1396}
1397
John McCalld5a36322009-11-03 19:26:08 +00001398/// ParseDeclGroup - Having concluded that this is either a function
1399/// definition or a group of object declarations, actually parse the
1400/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001401Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1402 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001403 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001404 SourceLocation *DeclEnd,
1405 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001406 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001407 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001408 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001409
John McCalld5a36322009-11-03 19:26:08 +00001410 // Bail out if the first declarator didn't seem well-formed.
1411 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001412 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001413 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001414 }
Mike Stump11289f42009-09-09 15:08:12 +00001415
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001416 // Save late-parsed attributes for now; they need to be parsed in the
1417 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001418 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1419 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001420 if (D.isFunctionDeclarator())
1421 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1422
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001423 // Check to see if we have a function *definition* which must have a body.
1424 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1425 // Look at the next token to make sure that this isn't a function
1426 // declaration. We have to check this because __attribute__ might be the
1427 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001428 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001429
Chris Lattner13901342010-07-11 22:42:07 +00001430 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +00001431 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1432 Diag(Tok, diag::err_function_declared_typedef);
1433
1434 // Recover by treating the 'typedef' as spurious.
1435 DS.ClearStorageClassSpecs();
1436 }
1437
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001438 Decl *TheDecl =
1439 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld5a36322009-11-03 19:26:08 +00001440 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +00001441 }
Chad Rosierc1183952012-06-26 22:30:43 +00001442
Chris Lattner13901342010-07-11 22:42:07 +00001443 if (isDeclarationSpecifier()) {
1444 // If there is an invalid declaration specifier right after the function
1445 // prototype, then we must be in a missing semicolon case where this isn't
1446 // actually a body. Just fall through into the code that handles it as a
1447 // prototype, and let the top-level code handle the erroneous declspec
1448 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +00001449 } else {
1450 Diag(Tok, diag::err_expected_fn_body);
1451 SkipUntil(tok::semi);
1452 return DeclGroupPtrTy();
1453 }
1454 }
1455
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001456 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001457 return DeclGroupPtrTy();
1458
1459 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1460 // must parse and analyze the for-range-initializer before the declaration is
1461 // analyzed.
1462 if (FRI && Tok.is(tok::colon)) {
1463 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +00001464 if (Tok.is(tok::l_brace))
1465 FRI->RangeExpr = ParseBraceInitializer();
1466 else
1467 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +00001468 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1469 Actions.ActOnCXXForRangeDecl(ThisDecl);
1470 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001471 D.complete(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001472 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1473 }
1474
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001475 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001476 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001477 if (LateParsedAttrs.size() > 0)
1478 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001479 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001480 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001481 DeclsInGroup.push_back(FirstDecl);
1482
Richard Smith09f76ee2011-10-19 21:33:05 +00001483 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001484
John McCalld5a36322009-11-03 19:26:08 +00001485 // If we don't have a comma, it is either the end of the list (a ';') or an
1486 // error, bail out.
1487 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001488 SourceLocation CommaLoc = ConsumeToken();
1489
1490 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1491 // This comma was followed by a line-break and something which can't be
1492 // the start of a declarator. The comma was probably a typo for a
1493 // semicolon.
1494 Diag(CommaLoc, diag::err_expected_semi_declaration)
1495 << FixItHint::CreateReplacement(CommaLoc, ";");
1496 ExpectSemi = false;
1497 break;
1498 }
John McCalld5a36322009-11-03 19:26:08 +00001499
1500 // Parse the next declarator.
1501 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001502 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001503
1504 // Accept attributes in an init-declarator. In the first declarator in a
1505 // declaration, these would be part of the declspec. In subsequent
1506 // declarators, they become part of the declarator itself, so that they
1507 // don't apply to declarators after *this* one. Examples:
1508 // short __attribute__((common)) var; -> declspec
1509 // short var __attribute__((common)); -> declarator
1510 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001511 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001512
1513 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001514 if (!D.isInvalidType()) {
1515 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1516 D.complete(ThisDecl);
1517 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001518 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001519 }
John McCalld5a36322009-11-03 19:26:08 +00001520 }
1521
1522 if (DeclEnd)
1523 *DeclEnd = Tok.getLocation();
1524
Richard Smith09f76ee2011-10-19 21:33:05 +00001525 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001526 ExpectAndConsumeSemi(Context == Declarator::FileContext
1527 ? diag::err_invalid_token_after_toplevel_declarator
1528 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001529 // Okay, there was no semicolon and one was expected. If we see a
1530 // declaration specifier, just assume it was missing and continue parsing.
1531 // Otherwise things are very confused and we skip to recover.
1532 if (!isDeclarationSpecifier()) {
1533 SkipUntil(tok::r_brace, true, true);
1534 if (Tok.is(tok::semi))
1535 ConsumeToken();
1536 }
John McCalld5a36322009-11-03 19:26:08 +00001537 }
1538
Douglas Gregor0be31a22010-07-02 17:43:08 +00001539 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001540 DeclsInGroup.data(),
1541 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001542}
1543
Richard Smith02e85f32011-04-14 22:09:26 +00001544/// Parse an optional simple-asm-expr and attributes, and attach them to a
1545/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001546bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001547 // If a simple-asm-expr is present, parse it.
1548 if (Tok.is(tok::kw_asm)) {
1549 SourceLocation Loc;
1550 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1551 if (AsmLabel.isInvalid()) {
1552 SkipUntil(tok::semi, true, true);
1553 return true;
1554 }
1555
1556 D.setAsmLabel(AsmLabel.release());
1557 D.SetRangeEnd(Loc);
1558 }
1559
1560 MaybeParseGNUAttributes(D);
1561 return false;
1562}
1563
Douglas Gregor23996282009-05-12 21:31:51 +00001564/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1565/// declarator'. This method parses the remainder of the declaration
1566/// (including any attributes or initializer, among other things) and
1567/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001568///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001569/// init-declarator: [C99 6.7]
1570/// declarator
1571/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001572/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1573/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001574/// [C++] declarator initializer[opt]
1575///
1576/// [C++] initializer:
1577/// [C++] '=' initializer-clause
1578/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001579/// [C++0x] '=' 'default' [TODO]
1580/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001581/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001582///
1583/// According to the standard grammar, =default and =delete are function
1584/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001585///
John McCall48871652010-08-21 09:40:31 +00001586Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001587 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001588 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001589 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001590
Richard Smith02e85f32011-04-14 22:09:26 +00001591 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1592}
Mike Stump11289f42009-09-09 15:08:12 +00001593
Richard Smith02e85f32011-04-14 22:09:26 +00001594Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1595 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001596 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001597 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001598 switch (TemplateInfo.Kind) {
1599 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001600 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001601 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001602
Douglas Gregor450f00842009-09-25 18:43:00 +00001603 case ParsedTemplateInfo::Template:
1604 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001605 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001606 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001607 D);
1608 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001609
Douglas Gregor450f00842009-09-25 18:43:00 +00001610 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosierc1183952012-06-26 22:30:43 +00001611 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001612 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001613 TemplateInfo.ExternLoc,
1614 TemplateInfo.TemplateLoc,
1615 D);
1616 if (ThisRes.isInvalid()) {
1617 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001618 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001619 }
Chad Rosierc1183952012-06-26 22:30:43 +00001620
Douglas Gregor450f00842009-09-25 18:43:00 +00001621 ThisDecl = ThisRes.get();
1622 break;
1623 }
1624 }
Mike Stump11289f42009-09-09 15:08:12 +00001625
Richard Smith30482bc2011-02-20 03:19:35 +00001626 bool TypeContainsAuto =
1627 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1628
Douglas Gregor23996282009-05-12 21:31:51 +00001629 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001630 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001631 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001632 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001633 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001634 if (D.isFunctionDeclarator())
1635 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1636 << 1 /* delete */;
1637 else
1638 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001639 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001640 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001641 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1642 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001643 else
1644 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001645 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001646 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001647 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001648 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001649 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001650
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001651 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001652 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001653 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001654 cutOffParsing();
1655 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001656 }
Chad Rosierc1183952012-06-26 22:30:43 +00001657
John McCalldadc5752010-08-24 06:29:42 +00001658 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001659
David Blaikiebbafb8a2012-03-11 07:00:24 +00001660 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001661 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001662 ExitScope();
1663 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001664
Douglas Gregor23996282009-05-12 21:31:51 +00001665 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001666 SkipUntil(tok::comma, true, true);
1667 Actions.ActOnInitializerError(ThisDecl);
1668 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001669 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1670 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001671 }
1672 } else if (Tok.is(tok::l_paren)) {
1673 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001674 BalancedDelimiterTracker T(*this, tok::l_paren);
1675 T.consumeOpen();
1676
Benjamin Kramerf0623432012-08-23 22:51:59 +00001677 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001678 CommaLocsTy CommaLocs;
1679
David Blaikiebbafb8a2012-03-11 07:00:24 +00001680 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001681 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001682 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001683 }
1684
Douglas Gregor23996282009-05-12 21:31:51 +00001685 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001686 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00001687 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001688
David Blaikiebbafb8a2012-03-11 07:00:24 +00001689 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001690 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001691 ExitScope();
1692 }
Douglas Gregor23996282009-05-12 21:31:51 +00001693 } else {
1694 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001695 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001696
1697 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1698 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001699
David Blaikiebbafb8a2012-03-11 07:00:24 +00001700 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001701 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001702 ExitScope();
1703 }
1704
Sebastian Redla9351792012-02-11 23:51:47 +00001705 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1706 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001707 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001708 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1709 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001710 }
Fariborz Jahanian8a369a82012-07-03 22:54:28 +00001711 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001712 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001713 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001714 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1715
Sebastian Redl3da34892011-06-05 12:23:16 +00001716 if (D.getCXXScopeSpec().isSet()) {
1717 EnterScope(0);
1718 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1719 }
1720
1721 ExprResult Init(ParseBraceInitializer());
1722
1723 if (D.getCXXScopeSpec().isSet()) {
1724 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1725 ExitScope();
1726 }
1727
1728 if (Init.isInvalid()) {
1729 Actions.ActOnInitializerError(ThisDecl);
1730 } else
1731 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1732 /*DirectInit=*/true, TypeContainsAuto);
1733
Douglas Gregor23996282009-05-12 21:31:51 +00001734 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001735 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001736 }
1737
Richard Smithb2bc2e62011-02-21 20:05:19 +00001738 Actions.FinalizeDeclaration(ThisDecl);
1739
Douglas Gregor23996282009-05-12 21:31:51 +00001740 return ThisDecl;
1741}
1742
Chris Lattner1890ac82006-08-13 01:16:23 +00001743/// ParseSpecifierQualifierList
1744/// specifier-qualifier-list:
1745/// type-specifier specifier-qualifier-list[opt]
1746/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001747/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001748///
Richard Smithc5b05522012-03-12 07:56:15 +00001749void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1750 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001751 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1752 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001753 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001754 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001755
Chris Lattner1890ac82006-08-13 01:16:23 +00001756 // Validate declspec for type-name.
1757 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001758 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1759 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001760 Diag(Tok, diag::err_expected_type);
1761 DS.SetTypeSpecError();
1762 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1763 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001764 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001765 if (!DS.hasTypeSpecifier())
1766 DS.SetTypeSpecError();
1767 }
Mike Stump11289f42009-09-09 15:08:12 +00001768
Chris Lattner1b22eed2006-11-28 05:12:07 +00001769 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001770 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001771 if (DS.getStorageClassSpecLoc().isValid())
1772 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1773 else
1774 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001775 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001776 }
Mike Stump11289f42009-09-09 15:08:12 +00001777
Chris Lattner1b22eed2006-11-28 05:12:07 +00001778 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001779 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001780 if (DS.isInlineSpecified())
1781 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1782 if (DS.isVirtualSpecified())
1783 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1784 if (DS.isExplicitSpecified())
1785 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001786 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001787 }
Richard Smithc5b05522012-03-12 07:56:15 +00001788
1789 // Issue diagnostic and remove constexpr specfier if present.
1790 if (DS.isConstexprSpecified()) {
1791 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1792 DS.ClearConstexprSpec();
1793 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001794}
Chris Lattner53361ac2006-08-10 05:19:57 +00001795
Chris Lattner6cc055a2009-04-12 20:42:31 +00001796/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1797/// specified token is valid after the identifier in a declarator which
1798/// immediately follows the declspec. For example, these things are valid:
1799///
1800/// int x [ 4]; // direct-declarator
1801/// int x ( int y); // direct-declarator
1802/// int(int x ) // direct-declarator
1803/// int x ; // simple-declaration
1804/// int x = 17; // init-declarator-list
1805/// int x , y; // init-declarator-list
1806/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001807/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001808/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001809///
1810/// This is not, because 'x' does not immediately follow the declspec (though
1811/// ')' happens to be valid anyway).
1812/// int (x)
1813///
1814static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1815 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1816 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001817 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001818}
1819
Chris Lattner20a0c612009-04-14 21:34:55 +00001820
1821/// ParseImplicitInt - This method is called when we have an non-typename
1822/// identifier in a declspec (which normally terminates the decl spec) when
1823/// the declspec has no type specifier. In this case, the declspec is either
1824/// malformed or is "implicit int" (in K&R and C89).
1825///
1826/// This method handles diagnosing this prettily and returns false if the
1827/// declspec is done being processed. If it recovers and thinks there may be
1828/// other pieces of declspec after it, it returns true.
1829///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001830bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001831 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00001832 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001833 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001834
Chris Lattner20a0c612009-04-14 21:34:55 +00001835 SourceLocation Loc = Tok.getLocation();
1836 // If we see an identifier that is not a type name, we normally would
1837 // parse it as the identifer being declared. However, when a typename
1838 // is typo'd or the definition is not included, this will incorrectly
1839 // parse the typename as the identifier name and fall over misparsing
1840 // later parts of the diagnostic.
1841 //
1842 // As such, we try to do some look-ahead in cases where this would
1843 // otherwise be an "implicit-int" case to see if this is invalid. For
1844 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1845 // an identifier with implicit int, we'd get a parse error because the
1846 // next token is obviously invalid for a type. Parse these as a case
1847 // with an invalid type specifier.
1848 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001849
Chris Lattner20a0c612009-04-14 21:34:55 +00001850 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00001851 // error, do lookahead to try to do better recovery. This never applies
1852 // within a type specifier. Outside of C++, we allow this even if the
1853 // language doesn't "officially" support implicit int -- we support
1854 // implicit int as an extension in C99 and C11. Allegedly, MS also
1855 // supports implicit int in C++ mode.
Richard Smith2f07ad52012-05-09 20:55:26 +00001856 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smitha952ebb2012-05-15 21:01:51 +00001857 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smithc5b05522012-03-12 07:56:15 +00001858 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001859 // If this token is valid for implicit int, e.g. "static x = 4", then
1860 // we just avoid eating the identifier, so it will be parsed as the
1861 // identifier in the declarator.
1862 return false;
1863 }
Mike Stump11289f42009-09-09 15:08:12 +00001864
Richard Smitha952ebb2012-05-15 21:01:51 +00001865 if (getLangOpts().CPlusPlus &&
1866 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1867 // Don't require a type specifier if we have the 'auto' storage class
1868 // specifier in C++98 -- we'll promote it to a type specifier.
1869 return false;
1870 }
1871
Chris Lattner20a0c612009-04-14 21:34:55 +00001872 // Otherwise, if we don't consume this token, we are going to emit an
1873 // error anyway. Try to recover from various common problems. Check
1874 // to see if this was a reference to a tag name without a tag specified.
1875 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001876 //
1877 // C++ doesn't need this, and isTagName doesn't take SS.
1878 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001879 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001880 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001881
Douglas Gregor0be31a22010-07-02 17:43:08 +00001882 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001883 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001884 case DeclSpec::TST_enum:
1885 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1886 case DeclSpec::TST_union:
1887 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1888 case DeclSpec::TST_struct:
1889 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00001890 case DeclSpec::TST_interface:
1891 TagName="__interface"; FixitTagName = "__interface ";
1892 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001893 case DeclSpec::TST_class:
1894 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001895 }
Mike Stump11289f42009-09-09 15:08:12 +00001896
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001897 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001898 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1899 LookupResult R(Actions, TokenName, SourceLocation(),
1900 Sema::LookupOrdinaryName);
1901
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001902 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001903 << TokenName << TagName << getLangOpts().CPlusPlus
1904 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1905
1906 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1907 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1908 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00001909 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001910 << TokenName << TagName;
1911 }
Mike Stump11289f42009-09-09 15:08:12 +00001912
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001913 // Parse this as a tag as if the missing tag were present.
1914 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00001915 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001916 else
Richard Smithc5b05522012-03-12 07:56:15 +00001917 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1918 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001919 return true;
1920 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001921 }
Mike Stump11289f42009-09-09 15:08:12 +00001922
Richard Smithfe904f02012-05-15 21:29:55 +00001923 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00001924 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00001925 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1926 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00001927 // Look ahead to the next token to try to figure out what this declaration
1928 // was supposed to be.
1929 switch (NextToken().getKind()) {
1930 case tok::comma:
1931 case tok::equal:
1932 case tok::kw_asm:
1933 case tok::l_brace:
1934 case tok::l_square:
1935 case tok::semi:
1936 // This looks like a variable declaration. The type is probably missing.
1937 // We're done parsing decl-specifiers.
1938 return false;
1939
1940 case tok::l_paren: {
1941 // static x(4); // 'x' is not a type
1942 // x(int n); // 'x' is not a type
1943 // x (*p)[]; // 'x' is a type
1944 //
1945 // Since we're in an error case (or the rare 'implicit int in C++' MS
1946 // extension), we can afford to perform a tentative parse to determine
1947 // which case we're in.
1948 TentativeParsingAction PA(*this);
1949 ConsumeToken();
1950 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1951 PA.Revert();
1952 if (TPR == TPResult::False())
1953 return false;
1954 // The identifier is followed by a parenthesized declarator.
1955 // It's supposed to be a type.
1956 break;
1957 }
1958
1959 default:
1960 // This is probably supposed to be a type. This includes cases like:
1961 // int f(itn);
1962 // struct S { unsinged : 4; };
1963 break;
1964 }
1965 }
1966
Chad Rosierc1183952012-06-26 22:30:43 +00001967 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00001968 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001969 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001970 IdentifierInfo *II = Tok.getIdentifierInfo();
1971 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001972 // The action emitted a diagnostic, so we don't have to.
1973 if (T) {
1974 // The action has suggested that the type T could be used. Set that as
1975 // the type in the declaration specifiers, consume the would-be type
1976 // name token, and we're done.
1977 const char *PrevSpec;
1978 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001979 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001980 DS.SetRangeEnd(Tok.getLocation());
1981 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001982 // There may be other declaration specifiers after this.
1983 return true;
1984 } else if (II != Tok.getIdentifierInfo()) {
1985 // If no type was suggested, the correction is to a keyword
1986 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00001987 // There may be other declaration specifiers after this.
1988 return true;
1989 }
Chad Rosierc1183952012-06-26 22:30:43 +00001990
Douglas Gregor15e56022009-10-13 23:27:22 +00001991 // Fall through; the action had no suggestion for us.
1992 } else {
1993 // The action did not emit a diagnostic, so emit one now.
1994 SourceRange R;
1995 if (SS) R = SS->getRange();
1996 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1997 }
Mike Stump11289f42009-09-09 15:08:12 +00001998
Douglas Gregor15e56022009-10-13 23:27:22 +00001999 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002000 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002001 DS.SetRangeEnd(Tok.getLocation());
2002 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002003
Chris Lattner20a0c612009-04-14 21:34:55 +00002004 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2005 // avoid rippling error messages on subsequent uses of the same type,
2006 // could be useful if #include was forgotten.
2007 return false;
2008}
2009
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002010/// \brief Determine the declaration specifier context from the declarator
2011/// context.
2012///
2013/// \param Context the declarator context, which is one of the
2014/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002015Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002016Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2017 if (Context == Declarator::MemberContext)
2018 return DSC_class;
2019 if (Context == Declarator::FileContext)
2020 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002021 if (Context == Declarator::TrailingReturnContext)
2022 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002023 return DSC_normal;
2024}
2025
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002026/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2027///
2028/// FIXME: Simply returns an alignof() expression if the argument is a
2029/// type. Ideally, the type should be propagated directly into Sema.
2030///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002031/// [C11] type-id
2032/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002033/// [C++0x] type-id ...[opt]
2034/// [C++0x] assignment-expression ...[opt]
2035ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2036 SourceLocation &EllipsisLoc) {
2037 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002038 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002039 SourceLocation TypeLoc = Tok.getLocation();
2040 ParsedType Ty = ParseTypeName().get();
2041 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002042 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2043 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002044 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002045 ER = ParseConstantExpression();
2046
David Blaikiebbafb8a2012-03-11 07:00:24 +00002047 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002048 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002049
2050 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002051}
2052
2053/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2054/// attribute to Attrs.
2055///
2056/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002057/// [C11] '_Alignas' '(' type-id ')'
2058/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002059/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
2060/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002061void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2062 SourceLocation *endLoc) {
2063 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2064 "Not an alignment-specifier!");
2065
2066 SourceLocation KWLoc = Tok.getLocation();
2067 ConsumeToken();
2068
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002069 BalancedDelimiterTracker T(*this, tok::l_paren);
2070 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002071 return;
2072
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002073 SourceLocation EllipsisLoc;
2074 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002075 if (ArgExpr.isInvalid()) {
2076 SkipUntil(tok::r_paren);
2077 return;
2078 }
2079
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002080 T.consumeClose();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002081 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002082 *endLoc = T.getCloseLocation();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002083
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002084 // FIXME: Handle pack-expansions here.
2085 if (EllipsisLoc.isValid()) {
2086 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2087 return;
2088 }
2089
Benjamin Kramerf0623432012-08-23 22:51:59 +00002090 ExprVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002091 ArgExprs.push_back(ArgExpr.release());
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002092 // FIXME: This should not be GNU, but we since the attribute used is
2093 // based on the spelling, and there is no true spelling for
2094 // C++11 attributes, this isn't accepted.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002095 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002096 0, T.getOpenLocation(), ArgExprs.data(), 1,
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002097 AttributeList::AS_GNU);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002098}
2099
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002100/// ParseDeclarationSpecifiers
2101/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002102/// storage-class-specifier declaration-specifiers[opt]
2103/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002104/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002105/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002106/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002107/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002108///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002109/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002110/// 'typedef'
2111/// 'extern'
2112/// 'static'
2113/// 'auto'
2114/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002115/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002116/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002117/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002118/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002119/// [C++] 'virtual'
2120/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002121/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002122/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002123/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002124
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002125///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002126void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002127 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002128 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002129 DeclSpecContext DSContext,
2130 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002131 if (DS.getSourceRange().isInvalid()) {
2132 DS.SetRangeStart(Tok.getLocation());
2133 DS.SetRangeEnd(Tok.getLocation());
2134 }
Chad Rosierc1183952012-06-26 22:30:43 +00002135
Douglas Gregordf593fb2011-11-07 17:33:42 +00002136 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002137 bool AttrsLastTime = false;
2138 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002139 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002140 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002141 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002142 unsigned DiagID = 0;
2143
Chris Lattner4d8f8732006-11-28 05:05:08 +00002144 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002145
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002146 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002147 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002148 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002149 if (!AttrsLastTime)
2150 ProhibitAttributes(attrs);
2151 else
2152 DS.takeAttributesFrom(attrs);
Peter Collingbourne70188b32011-09-29 18:03:57 +00002153
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002154 // If this is not a declaration specifier token, we're done reading decl
2155 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002156 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002157 return;
Mike Stump11289f42009-09-09 15:08:12 +00002158
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002159 case tok::l_square:
2160 case tok::kw_alignas:
2161 if (!isCXX11AttributeSpecifier())
2162 goto DoneWithDeclSpec;
2163
2164 ProhibitAttributes(attrs);
2165 // FIXME: It would be good to recover by accepting the attributes,
2166 // but attempting to do that now would cause serious
2167 // madness in terms of diagnostics.
2168 attrs.clear();
2169 attrs.Range = SourceRange();
2170
2171 ParseCXX11Attributes(attrs);
2172 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002173 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002174
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002175 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002176 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002177 if (DS.hasTypeSpecifier()) {
2178 bool AllowNonIdentifiers
2179 = (getCurScope()->getFlags() & (Scope::ControlScope |
2180 Scope::BlockScope |
2181 Scope::TemplateParamScope |
2182 Scope::FunctionPrototypeScope |
2183 Scope::AtCatchScope)) == 0;
2184 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002185 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002186 (DSContext == DSC_class && DS.isFriendSpecified());
2187
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002188 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002189 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002190 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002191 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002192 }
2193
Douglas Gregor80039242011-02-15 20:33:25 +00002194 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2195 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2196 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002197 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002198 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002199 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002200 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002201 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002202 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002203
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002204 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002205 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002206 }
2207
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002208 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002209 // C++ scope specifier. Annotate and loop, or bail out on error.
2210 if (TryAnnotateCXXScopeToken(true)) {
2211 if (!DS.hasTypeSpecifier())
2212 DS.SetTypeSpecError();
2213 goto DoneWithDeclSpec;
2214 }
John McCall8bc2a702010-03-01 18:20:46 +00002215 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2216 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002217 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002218
2219 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002220 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002221 goto DoneWithDeclSpec;
2222
John McCall9dab4e62009-12-12 11:40:51 +00002223 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002224 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2225 Tok.getAnnotationRange(),
2226 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002227
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002228 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002229 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002230 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002231 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002232 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002233 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002234
2235 // C++ [class.qual]p2:
2236 // In a lookup in which the constructor is an acceptable lookup
2237 // result and the nested-name-specifier nominates a class C:
2238 //
2239 // - if the name specified after the
2240 // nested-name-specifier, when looked up in C, is the
2241 // injected-class-name of C (Clause 9), or
2242 //
2243 // - if the name specified after the nested-name-specifier
2244 // is the same as the identifier or the
2245 // simple-template-id's template-name in the last
2246 // component of the nested-name-specifier,
2247 //
2248 // the name is instead considered to name the constructor of
2249 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002250 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002251 // Thus, if the template-name is actually the constructor
2252 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002253 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002254 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCall84821e72010-04-13 06:39:49 +00002255 if ((DSContext == DSC_top_level ||
2256 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2257 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002258 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002259 if (isConstructorDeclarator()) {
2260 // The user meant this to be an out-of-line constructor
2261 // definition, but template arguments are not allowed
2262 // there. Just allow this as a constructor; we'll
2263 // complain about it later.
2264 goto DoneWithDeclSpec;
2265 }
2266
2267 // The user meant this to name a type, but it actually names
2268 // a constructor with some extraneous template
2269 // arguments. Complain, then parse it as a type as the user
2270 // intended.
2271 Diag(TemplateId->TemplateNameLoc,
2272 diag::err_out_of_line_template_id_names_constructor)
2273 << TemplateId->Name;
2274 }
2275
John McCall9dab4e62009-12-12 11:40:51 +00002276 DS.getTypeSpecScope() = SS;
2277 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002278 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002279 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002280 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002281 continue;
2282 }
2283
Douglas Gregorc5790df2009-09-28 07:26:33 +00002284 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002285 DS.getTypeSpecScope() = SS;
2286 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002287 if (Tok.getAnnotationValue()) {
2288 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002289 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002290 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002291 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002292 if (isInvalid)
2293 break;
John McCallba7bf592010-08-24 05:47:05 +00002294 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002295 else
2296 DS.SetTypeSpecError();
2297 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2298 ConsumeToken(); // The typename
2299 }
2300
Douglas Gregor167fa622009-03-25 15:40:00 +00002301 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002302 goto DoneWithDeclSpec;
2303
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002304 // If we're in a context where the identifier could be a class name,
2305 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00002306 if ((DSContext == DSC_top_level ||
2307 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002308 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002309 &SS)) {
2310 if (isConstructorDeclarator())
2311 goto DoneWithDeclSpec;
2312
2313 // As noted in C++ [class.qual]p2 (cited above), when the name
2314 // of the class is qualified in a context where it could name
2315 // a constructor, its a constructor name. However, we've
2316 // looked at the declarator, and the user probably meant this
2317 // to be a type. Complain that it isn't supposed to be treated
2318 // as a type, then proceed to parse it as a type.
2319 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2320 << Next.getIdentifierInfo();
2321 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002322
John McCallba7bf592010-08-24 05:47:05 +00002323 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2324 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002325 getCurScope(), &SS,
2326 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002327 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002328 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002329
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002330 // If the referenced identifier is not a type, then this declspec is
2331 // erroneous: We already checked about that it has no type specifier, and
2332 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002333 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002334 if (TypeRep == 0) {
2335 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smithc5b05522012-03-12 07:56:15 +00002336 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002337 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002338 }
Mike Stump11289f42009-09-09 15:08:12 +00002339
John McCall9dab4e62009-12-12 11:40:51 +00002340 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002341 ConsumeToken(); // The C++ scope.
2342
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002343 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002344 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002345 if (isInvalid)
2346 break;
Mike Stump11289f42009-09-09 15:08:12 +00002347
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002348 DS.SetRangeEnd(Tok.getLocation());
2349 ConsumeToken(); // The typename.
2350
2351 continue;
2352 }
Mike Stump11289f42009-09-09 15:08:12 +00002353
Chris Lattnere387d9e2009-01-21 19:48:37 +00002354 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002355 if (Tok.getAnnotationValue()) {
2356 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002357 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002358 DiagID, T);
2359 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002360 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002361
Chris Lattner005fc1b2010-04-05 18:18:31 +00002362 if (isInvalid)
2363 break;
2364
Chris Lattnere387d9e2009-01-21 19:48:37 +00002365 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2366 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002367
Chris Lattnere387d9e2009-01-21 19:48:37 +00002368 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2369 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002370 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002371 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002372 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002373
Chris Lattnere387d9e2009-01-21 19:48:37 +00002374 continue;
2375 }
Mike Stump11289f42009-09-09 15:08:12 +00002376
Douglas Gregor06873092011-04-28 15:48:45 +00002377 case tok::kw___is_signed:
2378 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2379 // typically treats it as a trait. If we see __is_signed as it appears
2380 // in libstdc++, e.g.,
2381 //
2382 // static const bool __is_signed;
2383 //
2384 // then treat __is_signed as an identifier rather than as a keyword.
2385 if (DS.getTypeSpecType() == TST_bool &&
2386 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2387 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2388 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2389 Tok.setKind(tok::identifier);
2390 }
2391
2392 // We're done with the declaration-specifiers.
2393 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002394
Chris Lattner16fac4f2008-07-26 01:18:38 +00002395 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002396 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002397 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002398 // In C++, check to see if this is a scope specifier like foo::bar::, if
2399 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002400 if (getLangOpts().CPlusPlus) {
John McCall1f476a12010-02-26 08:45:28 +00002401 if (TryAnnotateCXXScopeToken(true)) {
2402 if (!DS.hasTypeSpecifier())
2403 DS.SetTypeSpecError();
2404 goto DoneWithDeclSpec;
2405 }
2406 if (!Tok.is(tok::identifier))
2407 continue;
2408 }
Mike Stump11289f42009-09-09 15:08:12 +00002409
Chris Lattner16fac4f2008-07-26 01:18:38 +00002410 // This identifier can only be a typedef name if we haven't already seen
2411 // a type-specifier. Without this check we misparse:
2412 // typedef int X; struct Y { short X; }; as 'short int'.
2413 if (DS.hasTypeSpecifier())
2414 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002415
John Thompson22334602010-02-05 00:12:22 +00002416 // Check for need to substitute AltiVec keyword tokens.
2417 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2418 break;
2419
Richard Smith3092a3b2012-05-09 18:56:43 +00002420 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2421 // allow the use of a typedef name as a type specifier.
2422 if (DS.isTypeAltiVecVector())
2423 goto DoneWithDeclSpec;
2424
John McCallba7bf592010-08-24 05:47:05 +00002425 ParsedType TypeRep =
2426 Actions.getTypeName(*Tok.getIdentifierInfo(),
2427 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002428
Chris Lattner6cc055a2009-04-12 20:42:31 +00002429 // If this is not a typedef name, don't parse it as part of the declspec,
2430 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002431 if (!TypeRep) {
Richard Smithc5b05522012-03-12 07:56:15 +00002432 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002433 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002434 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002435
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002436 // If we're in a context where the identifier could be a class name,
2437 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002438 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002439 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002440 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002441 goto DoneWithDeclSpec;
2442
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002443 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002444 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002445 if (isInvalid)
2446 break;
Mike Stump11289f42009-09-09 15:08:12 +00002447
Chris Lattner16fac4f2008-07-26 01:18:38 +00002448 DS.SetRangeEnd(Tok.getLocation());
2449 ConsumeToken(); // The identifier
2450
2451 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2452 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002453 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002454 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002455 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002456
Steve Naroffcd5e7822008-09-22 10:28:57 +00002457 // Need to support trailing type qualifiers (e.g. "id<p> const").
2458 // If a type specifier follows, it will be diagnosed elsewhere.
2459 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002460 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002461
2462 // type-name
2463 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002464 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002465 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002466 // This template-id does not refer to a type name, so we're
2467 // done with the type-specifiers.
2468 goto DoneWithDeclSpec;
2469 }
2470
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002471 // If we're in a context where the template-id could be a
2472 // constructor name or specialization, check whether this is a
2473 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002474 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002475 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002476 isConstructorDeclarator())
2477 goto DoneWithDeclSpec;
2478
Douglas Gregor7f741122009-02-25 19:37:18 +00002479 // Turn the template-id annotation token into a type annotation
2480 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002481 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002482 continue;
2483 }
2484
Chris Lattnere37e2332006-08-15 04:50:22 +00002485 // GNU attributes support.
2486 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002487 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002488 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002489
2490 // Microsoft declspec support.
2491 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002492 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002493 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002494
Steve Naroff44ac7772008-12-25 14:16:32 +00002495 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002496 case tok::kw___forceinline: {
2497 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2498 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002499 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002500 // FIXME: This does not work correctly if it is set to be a declspec
2501 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002502 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002503 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002504 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002505 }
Eli Friedman53339e02009-06-08 23:27:34 +00002506
2507 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002508 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002509 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002510 case tok::kw___cdecl:
2511 case tok::kw___stdcall:
2512 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002513 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002514 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002515 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002516 continue;
2517
Dawn Perchik335e16b2010-09-03 01:29:35 +00002518 // Borland single token adornments.
2519 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002520 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002521 continue;
2522
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002523 // OpenCL single token adornments.
2524 case tok::kw___kernel:
2525 ParseOpenCLAttributes(DS.getAttributes());
2526 continue;
2527
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002528 // storage-class-specifier
2529 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002530 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2531 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002532 break;
2533 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00002534 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002535 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002536 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2537 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002538 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002539 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002540 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2541 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002542 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002543 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00002544 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002545 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002546 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2547 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002548 break;
2549 case tok::kw_auto:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002550 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002551 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002552 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2553 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002554 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002555 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002556 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002557 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002558 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2559 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002560 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002561 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2562 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002563 break;
2564 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002565 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2566 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002567 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002568 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002569 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2570 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002571 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002572 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00002573 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002574 break;
Mike Stump11289f42009-09-09 15:08:12 +00002575
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002576 // function-specifier
2577 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00002578 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002579 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002580 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00002581 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002582 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002583 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00002584 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002585 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002586
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002587 // alignment-specifier
2588 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002589 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002590 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002591 ParseAlignmentSpecifier(DS.getAttributes());
2592 continue;
2593
Anders Carlssoncd8db412009-05-06 04:46:28 +00002594 // friend
2595 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002596 if (DSContext == DSC_class)
2597 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2598 else {
2599 PrevSpec = ""; // not actually used by the diagnostic
2600 DiagID = diag::err_friend_invalid_in_context;
2601 isInvalid = true;
2602 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002603 break;
Mike Stump11289f42009-09-09 15:08:12 +00002604
Douglas Gregor26701a42011-09-09 02:06:17 +00002605 // Modules
2606 case tok::kw___module_private__:
2607 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2608 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002609
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002610 // constexpr
2611 case tok::kw_constexpr:
2612 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2613 break;
2614
Chris Lattnere387d9e2009-01-21 19:48:37 +00002615 // type-specifier
2616 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002617 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2618 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002619 break;
2620 case tok::kw_long:
2621 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002622 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2623 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002624 else
John McCall49bfce42009-08-03 20:12:06 +00002625 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2626 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002627 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002628 case tok::kw___int64:
2629 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2630 DiagID);
2631 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002632 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002633 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2634 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002635 break;
2636 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002637 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2638 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002639 break;
2640 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002641 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2642 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002643 break;
2644 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002645 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2646 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002647 break;
2648 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002649 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2650 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002651 break;
2652 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002653 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2654 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002655 break;
2656 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002657 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2658 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002659 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002660 case tok::kw___int128:
2661 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2662 DiagID);
2663 break;
2664 case tok::kw_half:
2665 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2666 DiagID);
2667 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002668 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002669 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2670 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002671 break;
2672 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002673 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2674 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002675 break;
2676 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002677 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2678 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002679 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002680 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002681 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2682 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002683 break;
2684 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002685 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2686 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002687 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002688 case tok::kw_bool:
2689 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002690 if (Tok.is(tok::kw_bool) &&
2691 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2692 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2693 PrevSpec = ""; // Not used by the diagnostic.
2694 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002695 // For better error recovery.
2696 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002697 isInvalid = true;
2698 } else {
2699 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2700 DiagID);
2701 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002702 break;
2703 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002704 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2705 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002706 break;
2707 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002708 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2709 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002710 break;
2711 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2713 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002714 break;
John Thompson22334602010-02-05 00:12:22 +00002715 case tok::kw___vector:
2716 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2717 break;
2718 case tok::kw___pixel:
2719 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2720 break;
John McCall39439732011-04-09 22:50:59 +00002721 case tok::kw___unknown_anytype:
2722 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2723 PrevSpec, DiagID);
2724 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002725
2726 // class-specifier:
2727 case tok::kw_class:
2728 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00002729 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002730 case tok::kw_union: {
2731 tok::TokenKind Kind = Tok.getKind();
2732 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002733 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2734 EnteringContext, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002735 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002736 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002737
2738 // enum-specifier:
2739 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002740 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002741 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002742 continue;
2743
2744 // cv-qualifier:
2745 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002746 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002747 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002748 break;
2749 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002750 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002751 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002752 break;
2753 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002754 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002755 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002756 break;
2757
Douglas Gregor333489b2009-03-27 23:10:48 +00002758 // C++ typename-specifier:
2759 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00002760 if (TryAnnotateTypeOrScopeToken()) {
2761 DS.SetTypeSpecError();
2762 goto DoneWithDeclSpec;
2763 }
2764 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00002765 continue;
2766 break;
2767
Chris Lattnere387d9e2009-01-21 19:48:37 +00002768 // GNU typeof support.
2769 case tok::kw_typeof:
2770 ParseTypeofSpecifier(DS);
2771 continue;
2772
David Blaikie15a430a2011-12-04 05:04:18 +00002773 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00002774 ParseDecltypeSpecifier(DS);
2775 continue;
2776
Alexis Hunt4a257072011-05-19 05:37:45 +00002777 case tok::kw___underlying_type:
2778 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00002779 continue;
2780
2781 case tok::kw__Atomic:
2782 ParseAtomicSpecifier(DS);
2783 continue;
Alexis Hunt4a257072011-05-19 05:37:45 +00002784
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002785 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00002786 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002787 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002788 goto DoneWithDeclSpec;
2789 case tok::kw___private:
2790 case tok::kw___global:
2791 case tok::kw___local:
2792 case tok::kw___constant:
2793 case tok::kw___read_only:
2794 case tok::kw___write_only:
2795 case tok::kw___read_write:
2796 ParseOpenCLQualifiers(DS);
2797 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002798
Steve Naroffcfdf6162008-06-05 00:02:44 +00002799 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002800 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00002801 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2802 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002803 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00002804 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002805
Douglas Gregor3a001f42010-11-19 17:10:50 +00002806 if (!ParseObjCProtocolQualifiers(DS))
2807 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2808 << FixItHint::CreateInsertion(Loc, "id")
2809 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00002810
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002811 // Need to support trailing type qualifiers (e.g. "id<p> const").
2812 // If a type specifier follows, it will be diagnosed elsewhere.
2813 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002814 }
John McCall49bfce42009-08-03 20:12:06 +00002815 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002816 if (isInvalid) {
2817 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002818 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00002819
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002820 if (DiagID == diag::ext_duplicate_declspec)
2821 Diag(Tok, DiagID)
2822 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2823 else
2824 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002825 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002826
Chris Lattner2e232092008-03-13 06:29:04 +00002827 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002828 if (DiagID != diag::err_bool_redeclaration)
2829 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002830
2831 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002832 }
2833}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002834
Chris Lattner70ae4912007-10-29 04:42:53 +00002835/// ParseStructDeclaration - Parse a struct declaration without the terminating
2836/// semicolon.
2837///
Chris Lattner90a26b02007-01-23 04:38:16 +00002838/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002839/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002840/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002841/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002842/// struct-declarator-list:
2843/// struct-declarator
2844/// struct-declarator-list ',' struct-declarator
2845/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2846/// struct-declarator:
2847/// declarator
2848/// [GNU] declarator attributes[opt]
2849/// declarator[opt] ':' constant-expression
2850/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2851///
Chris Lattnera12405b2008-04-10 06:46:29 +00002852void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002853ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00002854
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002855 if (Tok.is(tok::kw___extension__)) {
2856 // __extension__ silences extension warnings in the subexpression.
2857 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002858 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002859 return ParseStructDeclaration(DS, Fields);
2860 }
Mike Stump11289f42009-09-09 15:08:12 +00002861
Steve Naroff97170802007-08-20 22:28:22 +00002862 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002863 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002864
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002865 // If there are no declarators, this is a free-standing declaration
2866 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002867 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002868 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2869 DS);
2870 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00002871 return;
2872 }
2873
2874 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002875 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00002876 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00002877 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002878 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00002879 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00002880
2881 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002882 if (!FirstDeclarator)
2883 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002884
Steve Naroff97170802007-08-20 22:28:22 +00002885 /// struct-declarator: declarator
2886 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002887 if (Tok.isNot(tok::colon)) {
2888 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2889 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002890 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002891 }
Mike Stump11289f42009-09-09 15:08:12 +00002892
Chris Lattner76c72282007-10-09 17:33:22 +00002893 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002894 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002895 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002896 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002897 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002898 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002899 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002900 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002901
Steve Naroff97170802007-08-20 22:28:22 +00002902 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002903 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002904
John McCallcfefb6d2009-11-03 02:38:08 +00002905 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00002906 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00002907
Steve Naroff97170802007-08-20 22:28:22 +00002908 // If we don't have a comma, it is either the end of the list (a ';')
2909 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002910 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002911 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002912
Steve Naroff97170802007-08-20 22:28:22 +00002913 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00002914 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002915
John McCallcfefb6d2009-11-03 02:38:08 +00002916 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002917 }
Steve Naroff97170802007-08-20 22:28:22 +00002918}
2919
2920/// ParseStructUnionBody
2921/// struct-contents:
2922/// struct-declaration-list
2923/// [EXT] empty
2924/// [GNU] "struct-declaration-list" without terminatoring ';'
2925/// struct-declaration-list:
2926/// struct-declaration
2927/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002928/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002929///
Chris Lattner1300fb92007-01-23 23:42:53 +00002930void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002931 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002932 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2933 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002934
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002935 BalancedDelimiterTracker T(*this, tok::l_brace);
2936 if (T.consumeOpen())
2937 return;
Mike Stump11289f42009-09-09 15:08:12 +00002938
Douglas Gregor658b9552009-01-09 22:42:13 +00002939 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002940 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002941
Chris Lattner7b9ace62007-01-23 20:11:08 +00002942 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2943 // C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002944 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithe4345902011-12-29 21:57:33 +00002945 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2946 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2947 }
Chris Lattner7b9ace62007-01-23 20:11:08 +00002948
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002949 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002950
Chris Lattner7b9ace62007-01-23 20:11:08 +00002951 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002952 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002953 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002954
Chris Lattner736ed5d2007-06-09 05:59:07 +00002955 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002956 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002957 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00002958 continue;
2959 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002960
John McCallcfefb6d2009-11-03 02:38:08 +00002961 if (!Tok.is(tok::at)) {
2962 struct CFieldCallback : FieldCallback {
2963 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002964 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002965 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002966
John McCall48871652010-08-21 09:40:31 +00002967 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002968 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002969 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2970
Eli Friedman934dbbf2012-08-08 23:53:27 +00002971 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002972 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002973 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002974 FD.D.getDeclSpec().getSourceRange().getBegin(),
2975 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002976 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002977 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00002978 }
John McCallcfefb6d2009-11-03 02:38:08 +00002979 } Callback(*this, TagDecl, FieldDecls);
2980
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002981 // Parse all the comma separated declarators.
2982 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002983 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002984 } else { // Handle @defs
2985 ConsumeToken();
2986 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2987 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002988 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002989 continue;
2990 }
2991 ConsumeToken();
2992 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2993 if (!Tok.is(tok::identifier)) {
2994 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002995 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002996 continue;
2997 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002998 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002999 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003000 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003001 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3002 ConsumeToken();
3003 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003004 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003005
Chris Lattner76c72282007-10-09 17:33:22 +00003006 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003007 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003008 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003009 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003010 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003011 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003012 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3013 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00003014 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00003015 // If we stopped at a ';', eat it.
3016 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003017 }
3018 }
Mike Stump11289f42009-09-09 15:08:12 +00003019
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003020 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003021
John McCall084e83d2011-03-24 11:26:52 +00003022 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003023 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003024 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003025
Douglas Gregor0be31a22010-07-02 17:43:08 +00003026 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003027 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003028 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003029 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003030 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003031 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3032 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003033}
3034
Chris Lattner3b561a32006-08-13 00:12:11 +00003035/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003036/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003037/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003038///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003039/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3040/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003041/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3042/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003043/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003044/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003045///
Richard Smith7d137e32012-03-23 03:33:32 +00003046/// [C++11] enum-head '{' enumerator-list[opt] '}'
3047/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003048///
Richard Smith7d137e32012-03-23 03:33:32 +00003049/// enum-head: [C++11]
3050/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3051/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3052/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003053///
Richard Smith7d137e32012-03-23 03:33:32 +00003054/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003055/// 'enum'
3056/// 'enum' 'class'
3057/// 'enum' 'struct'
3058///
Richard Smith7d137e32012-03-23 03:33:32 +00003059/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003060/// ':' type-specifier-seq
3061///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003062/// [C++] elaborated-type-specifier:
3063/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3064///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003065void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003066 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003067 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003068 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003069 if (Tok.is(tok::code_completion)) {
3070 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003071 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003072 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003073 }
John McCallcb432fa2011-07-06 05:58:41 +00003074
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003075 // If attributes exist after tag, parse them.
3076 ParsedAttributesWithRange attrs(AttrFactory);
3077 MaybeParseGNUAttributes(attrs);
3078 MaybeParseCXX0XAttributes(attrs);
3079
3080 // If declspecs exist after tag, parse them.
3081 while (Tok.is(tok::kw___declspec))
3082 ParseMicrosoftDeclSpec(attrs);
3083
Richard Smith0f8ee222012-01-10 01:33:14 +00003084 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003085 bool IsScopedUsingClassTag = false;
3086
John McCallbeae29a2012-06-23 22:30:04 +00003087 // In C++11, recognize 'enum class' and 'enum struct'.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003088 if (getLangOpts().CPlusPlus0x &&
John McCallcb432fa2011-07-06 05:58:41 +00003089 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003090 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003091 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003092 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003093
John McCallbeae29a2012-06-23 22:30:04 +00003094 // Attributes are not allowed between these keywords. Diagnose,
3095 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003096 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003097
3098 // They are allowed afterwards, though.
3099 MaybeParseGNUAttributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003100 MaybeParseCXX0XAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003101 while (Tok.is(tok::kw___declspec))
3102 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003103 }
Richard Smith7d137e32012-03-23 03:33:32 +00003104
John McCall6347b682012-05-07 06:16:58 +00003105 // C++11 [temp.explicit]p12:
3106 // The usual access controls do not apply to names used to specify
3107 // explicit instantiations.
3108 // We extend this to also cover explicit specializations. Note that
3109 // we don't suppress if this turns out to be an elaborated type
3110 // specifier.
3111 bool shouldDelayDiagsInTag =
3112 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3113 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3114 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003115
Richard Smithbfdb1082012-03-12 08:56:40 +00003116 // Enum definitions should not be parsed in a trailing-return-type.
3117 bool AllowDeclaration = DSC != DSC_trailing;
3118
3119 bool AllowFixedUnderlyingType = AllowDeclaration &&
3120 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
3121 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003122
Abramo Bagnarad7548482010-05-19 21:37:53 +00003123 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003124 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003125 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3126 // if a fixed underlying type is allowed.
3127 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003128
3129 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003130 /*EnteringContext=*/false))
John McCall1f476a12010-02-26 08:45:28 +00003131 return;
3132
3133 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003134 Diag(Tok, diag::err_expected_ident);
3135 if (Tok.isNot(tok::l_brace)) {
3136 // Has no name and is not a definition.
3137 // Skip the rest of this declarator, up until the comma or semicolon.
3138 SkipUntil(tok::comma, true);
3139 return;
3140 }
3141 }
3142 }
Mike Stump11289f42009-09-09 15:08:12 +00003143
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003144 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003145 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003146 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003147 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003148
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003149 // Skip the rest of this declarator, up until the comma or semicolon.
3150 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003151 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003152 }
Mike Stump11289f42009-09-09 15:08:12 +00003153
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003154 // If an identifier is present, consume and remember it.
3155 IdentifierInfo *Name = 0;
3156 SourceLocation NameLoc;
3157 if (Tok.is(tok::identifier)) {
3158 Name = Tok.getIdentifierInfo();
3159 NameLoc = ConsumeToken();
3160 }
Mike Stump11289f42009-09-09 15:08:12 +00003161
Richard Smith0f8ee222012-01-10 01:33:14 +00003162 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003163 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3164 // declaration of a scoped enumeration.
3165 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003166 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003167 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003168 }
3169
John McCall6347b682012-05-07 06:16:58 +00003170 // Okay, end the suppression area. We'll decide whether to emit the
3171 // diagnostics in a second.
3172 if (shouldDelayDiagsInTag)
3173 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003174
Douglas Gregor0bf31402010-10-08 23:50:27 +00003175 TypeResult BaseType;
3176
Douglas Gregord1f69f62010-12-01 17:42:47 +00003177 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003178 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003179 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003180 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003181 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003182 // If we're in class scope, this can either be an enum declaration with
3183 // an underlying type, or a declaration of a bitfield member. We try to
3184 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003185 // (integer literal, sizeof); if it's still ambiguous, we then consider
3186 // anything that's a simple-type-specifier followed by '(' as an
3187 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003188 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003189 EnterExpressionEvaluationContext Unevaluated(Actions,
3190 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003191 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003192 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003193 // bit-field. This is the common case.
3194 if (TPR == TPResult::True())
3195 PossibleBitfield = true;
3196 // If the next token starts a type-specifier-seq, it may be either a
3197 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003198 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003199 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003200 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003201 GetLookAheadToken(2).getKind() == tok::semi) {
3202 // Consume the ':'.
3203 ConsumeToken();
3204 } else {
3205 // We have the start of a type-specifier-seq, so we have to perform
3206 // tentative parsing to determine whether we have an expression or a
3207 // type.
3208 TentativeParsingAction TPA(*this);
3209
3210 // Consume the ':'.
3211 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003212
3213 // If we see a type specifier followed by an open-brace, we have an
3214 // ambiguity between an underlying type and a C++11 braced
3215 // function-style cast. Resolve this by always treating it as an
3216 // underlying type.
3217 // FIXME: The standard is not entirely clear on how to disambiguate in
3218 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003219 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003220 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003221 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003222 // We'll parse this as a bitfield later.
3223 PossibleBitfield = true;
3224 TPA.Revert();
3225 } else {
3226 // We have a type-specifier-seq.
3227 TPA.Commit();
3228 }
3229 }
3230 } else {
3231 // Consume the ':'.
3232 ConsumeToken();
3233 }
3234
3235 if (!PossibleBitfield) {
3236 SourceRange Range;
3237 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003238
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003239 if (getLangOpts().CPlusPlus0x) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003240 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003241 } else if (!getLangOpts().ObjC2) {
3242 if (getLangOpts().CPlusPlus)
3243 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3244 else
3245 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3246 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003247 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003248 }
3249
Richard Smith0f8ee222012-01-10 01:33:14 +00003250 // There are four options here. If we have 'friend enum foo;' then this is a
3251 // friend declaration, and cannot have an accompanying definition. If we have
3252 // 'enum foo;', then this is a forward declaration. If we have
3253 // 'enum foo {...' then this is a definition. Otherwise we have something
3254 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003255 //
3256 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3257 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3258 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3259 //
John McCallfaf5fb42010-08-26 23:41:50 +00003260 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003261 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003262 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003263 } else if (Tok.is(tok::l_brace)) {
3264 if (DS.isFriendSpecified()) {
3265 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3266 << SourceRange(DS.getFriendSpecLoc());
3267 ConsumeBrace();
3268 SkipUntil(tok::r_brace);
3269 TUK = Sema::TUK_Friend;
3270 } else {
3271 TUK = Sema::TUK_Definition;
3272 }
Richard Smith369b9f92012-06-25 21:37:02 +00003273 } else if (DSC != DSC_type_specifier &&
3274 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003275 (Tok.isAtStartOfLine() &&
3276 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003277 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3278 if (Tok.isNot(tok::semi)) {
3279 // A semicolon was missing after this declaration. Diagnose and recover.
3280 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3281 "enum");
3282 PP.EnterToken(Tok);
3283 Tok.setKind(tok::semi);
3284 }
John McCall6347b682012-05-07 06:16:58 +00003285 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003286 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003287 }
3288
3289 // If this is an elaborated type specifier, and we delayed
3290 // diagnostics before, just merge them into the current pool.
3291 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3292 diagsFromTag.redelay();
3293 }
Richard Smith7d137e32012-03-23 03:33:32 +00003294
3295 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003296 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003297 TUK != Sema::TUK_Reference) {
Richard Smith7d137e32012-03-23 03:33:32 +00003298 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3299 // Skip the rest of this declarator, up until the comma or semicolon.
3300 Diag(Tok, diag::err_enum_template);
3301 SkipUntil(tok::comma, true);
3302 return;
3303 }
3304
3305 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3306 // Enumerations can't be explicitly instantiated.
3307 DS.SetTypeSpecError();
3308 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3309 return;
3310 }
3311
3312 assert(TemplateInfo.TemplateParams && "no template parameters");
3313 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3314 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003315 }
Chad Rosierc1183952012-06-26 22:30:43 +00003316
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003317 if (TUK == Sema::TUK_Reference)
3318 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003319
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003320 if (!Name && TUK != Sema::TUK_Definition) {
3321 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003322
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003323 // Skip the rest of this declarator, up until the comma or semicolon.
3324 SkipUntil(tok::comma, true);
3325 return;
3326 }
Richard Smith7d137e32012-03-23 03:33:32 +00003327
Douglas Gregord6ab8742009-05-28 23:31:59 +00003328 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003329 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003330 const char *PrevSpec = 0;
3331 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003332 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003333 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003334 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003335 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003336 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003337
Douglas Gregorba41d012010-04-24 16:38:41 +00003338 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003339 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003340 // dependent tag.
3341 if (!Name) {
3342 DS.SetTypeSpecError();
3343 Diag(Tok, diag::err_expected_type_name_after_typename);
3344 return;
3345 }
Chad Rosierc1183952012-06-26 22:30:43 +00003346
Douglas Gregor0be31a22010-07-02 17:43:08 +00003347 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003348 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003349 NameLoc);
3350 if (Type.isInvalid()) {
3351 DS.SetTypeSpecError();
3352 return;
3353 }
Chad Rosierc1183952012-06-26 22:30:43 +00003354
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003355 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3356 NameLoc.isValid() ? NameLoc : StartLoc,
3357 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003358 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003359
Douglas Gregorba41d012010-04-24 16:38:41 +00003360 return;
3361 }
Mike Stump11289f42009-09-09 15:08:12 +00003362
John McCall48871652010-08-21 09:40:31 +00003363 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003364 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003365 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003366 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003367 ConsumeBrace();
3368 SkipUntil(tok::r_brace);
3369 }
Chad Rosierc1183952012-06-26 22:30:43 +00003370
Douglas Gregorba41d012010-04-24 16:38:41 +00003371 DS.SetTypeSpecError();
3372 return;
3373 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003374
Richard Smith369b9f92012-06-25 21:37:02 +00003375 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003376 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003377
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003378 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3379 NameLoc.isValid() ? NameLoc : StartLoc,
3380 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003381 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003382}
3383
Chris Lattnerc1915e22007-01-25 07:29:02 +00003384/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3385/// enumerator-list:
3386/// enumerator
3387/// enumerator-list ',' enumerator
3388/// enumerator:
3389/// enumeration-constant
3390/// enumeration-constant '=' constant-expression
3391/// enumeration-constant:
3392/// identifier
3393///
John McCall48871652010-08-21 09:40:31 +00003394void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003395 // Enter the scope of the enum body and start the definition.
3396 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003397 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003398
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003399 BalancedDelimiterTracker T(*this, tok::l_brace);
3400 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003401
Chris Lattner37256fb2007-08-27 17:24:30 +00003402 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003403 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003404 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003405
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003406 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003407
John McCall48871652010-08-21 09:40:31 +00003408 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003409
Chris Lattnerc1915e22007-01-25 07:29:02 +00003410 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003411 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003412 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3413 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003414
John McCall811a0f52010-10-22 23:36:17 +00003415 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003416 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003417 MaybeParseGNUAttributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003418 MaybeParseCXX0XAttributes(attrs);
3419 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003420
Chris Lattnerc1915e22007-01-25 07:29:02 +00003421 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003422 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003423 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003424
Chris Lattner76c72282007-10-09 17:33:22 +00003425 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003426 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003427 AssignedVal = ParseConstantExpression();
3428 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003429 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003430 }
Mike Stump11289f42009-09-09 15:08:12 +00003431
Chris Lattnerc1915e22007-01-25 07:29:02 +00003432 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003433 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3434 LastEnumConstDecl,
3435 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003436 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003437 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003438 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003439
Chris Lattner4ef40012007-06-11 01:28:17 +00003440 EnumConstantDecls.push_back(EnumConstDecl);
3441 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003442
Douglas Gregorce66d022010-09-07 14:51:08 +00003443 if (Tok.is(tok::identifier)) {
3444 // We're missing a comma between enumerators.
3445 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003446 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003447 << FixItHint::CreateInsertion(Loc, ", ");
3448 continue;
3449 }
Chad Rosierc1183952012-06-26 22:30:43 +00003450
Chris Lattner76c72282007-10-09 17:33:22 +00003451 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003452 break;
3453 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003454
Richard Smith5d164bc2011-10-15 05:09:34 +00003455 if (Tok.isNot(tok::identifier)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003456 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith87f5dc52012-07-23 05:45:25 +00003457 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3458 diag::ext_enumerator_list_comma_cxx :
3459 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003460 << FixItHint::CreateRemoval(CommaLoc);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003461 else if (getLangOpts().CPlusPlus0x)
Richard Smith5d164bc2011-10-15 05:09:34 +00003462 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3463 << FixItHint::CreateRemoval(CommaLoc);
3464 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003465 }
Mike Stump11289f42009-09-09 15:08:12 +00003466
Chris Lattnerc1915e22007-01-25 07:29:02 +00003467 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003468 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003469
Chris Lattnerc1915e22007-01-25 07:29:02 +00003470 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003471 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003472 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003473
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003474 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3475 EnumDecl, EnumConstantDecls.data(),
3476 EnumConstantDecls.size(), getCurScope(),
3477 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003478
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003479 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003480 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3481 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003482
3483 // The next token must be valid after an enum definition. If not, a ';'
3484 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003485 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3486 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003487 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3488 // Push this token back into the preprocessor and change our current token
3489 // to ';' so that the rest of the code recovers as though there were an
3490 // ';' after the definition.
3491 PP.EnterToken(Tok);
3492 Tok.setKind(tok::semi);
3493 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003494}
Chris Lattner3b561a32006-08-13 00:12:11 +00003495
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003496/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003497/// start of a type-qualifier-list.
3498bool Parser::isTypeQualifier() const {
3499 switch (Tok.getKind()) {
3500 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003501
3502 // type-qualifier only in OpenCL
3503 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003504 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003505
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003506 // type-qualifier
3507 case tok::kw_const:
3508 case tok::kw_volatile:
3509 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003510 case tok::kw___private:
3511 case tok::kw___local:
3512 case tok::kw___global:
3513 case tok::kw___constant:
3514 case tok::kw___read_only:
3515 case tok::kw___read_write:
3516 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003517 return true;
3518 }
3519}
3520
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003521/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3522/// is definitely a type-specifier. Return false if it isn't part of a type
3523/// specifier or if we're not sure.
3524bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3525 switch (Tok.getKind()) {
3526 default: return false;
3527 // type-specifiers
3528 case tok::kw_short:
3529 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003530 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003531 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003532 case tok::kw_signed:
3533 case tok::kw_unsigned:
3534 case tok::kw__Complex:
3535 case tok::kw__Imaginary:
3536 case tok::kw_void:
3537 case tok::kw_char:
3538 case tok::kw_wchar_t:
3539 case tok::kw_char16_t:
3540 case tok::kw_char32_t:
3541 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003542 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003543 case tok::kw_float:
3544 case tok::kw_double:
3545 case tok::kw_bool:
3546 case tok::kw__Bool:
3547 case tok::kw__Decimal32:
3548 case tok::kw__Decimal64:
3549 case tok::kw__Decimal128:
3550 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003551
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003552 // struct-or-union-specifier (C99) or class-specifier (C++)
3553 case tok::kw_class:
3554 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003555 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003556 case tok::kw_union:
3557 // enum-specifier
3558 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003559
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003560 // typedef-name
3561 case tok::annot_typename:
3562 return true;
3563 }
3564}
3565
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003566/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003567/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003568bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003569 switch (Tok.getKind()) {
3570 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003571
Chris Lattner020bab92009-01-04 23:41:41 +00003572 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003573 if (TryAltiVecVectorToken())
3574 return true;
3575 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003576 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003577 // Annotate typenames and C++ scope specifiers. If we get one, just
3578 // recurse to handle whatever we get.
3579 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003580 return true;
3581 if (Tok.is(tok::identifier))
3582 return false;
3583 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003584
Chris Lattner020bab92009-01-04 23:41:41 +00003585 case tok::coloncolon: // ::foo::bar
3586 if (NextToken().is(tok::kw_new) || // ::new
3587 NextToken().is(tok::kw_delete)) // ::delete
3588 return false;
3589
Chris Lattner020bab92009-01-04 23:41:41 +00003590 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003591 return true;
3592 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003593
Chris Lattnere37e2332006-08-15 04:50:22 +00003594 // GNU attributes support.
3595 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003596 // GNU typeof support.
3597 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003598
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003599 // type-specifiers
3600 case tok::kw_short:
3601 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003602 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003603 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003604 case tok::kw_signed:
3605 case tok::kw_unsigned:
3606 case tok::kw__Complex:
3607 case tok::kw__Imaginary:
3608 case tok::kw_void:
3609 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003610 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003611 case tok::kw_char16_t:
3612 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003613 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003614 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003615 case tok::kw_float:
3616 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003617 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003618 case tok::kw__Bool:
3619 case tok::kw__Decimal32:
3620 case tok::kw__Decimal64:
3621 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003622 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003623
Chris Lattner861a2262008-04-13 18:59:07 +00003624 // struct-or-union-specifier (C99) or class-specifier (C++)
3625 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003626 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003627 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003628 case tok::kw_union:
3629 // enum-specifier
3630 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003631
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003632 // type-qualifier
3633 case tok::kw_const:
3634 case tok::kw_volatile:
3635 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003636
3637 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003638 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003639 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003640
Chris Lattner409bf7d2008-10-20 00:25:30 +00003641 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3642 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003643 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003644
Steve Naroff44ac7772008-12-25 14:16:32 +00003645 case tok::kw___cdecl:
3646 case tok::kw___stdcall:
3647 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003648 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003649 case tok::kw___w64:
3650 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003651 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003652 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003653 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003654
3655 case tok::kw___private:
3656 case tok::kw___local:
3657 case tok::kw___global:
3658 case tok::kw___constant:
3659 case tok::kw___read_only:
3660 case tok::kw___read_write:
3661 case tok::kw___write_only:
3662
Eli Friedman53339e02009-06-08 23:27:34 +00003663 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003664
3665 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003666 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003667
Benjamin Kramere56f3932011-12-23 17:00:35 +00003668 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003669 case tok::kw__Atomic:
3670 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003671 }
3672}
3673
Chris Lattneracd58a32006-08-06 17:24:14 +00003674/// isDeclarationSpecifier() - Return true if the current token is part of a
3675/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003676///
3677/// \param DisambiguatingWithExpression True to indicate that the purpose of
3678/// this check is to disambiguate between an expression and a declaration.
3679bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003680 switch (Tok.getKind()) {
3681 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003682
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003683 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003684 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003685
Chris Lattner020bab92009-01-04 23:41:41 +00003686 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003687 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003688 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00003689 return false;
John Thompson22334602010-02-05 00:12:22 +00003690 if (TryAltiVecVectorToken())
3691 return true;
3692 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00003693 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00003694 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003695 // Annotate typenames and C++ scope specifiers. If we get one, just
3696 // recurse to handle whatever we get.
3697 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003698 return true;
3699 if (Tok.is(tok::identifier))
3700 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003701
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003702 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00003703 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003704 // expression is permitted, then this is probably a class message send
3705 // missing the initial '['. In this case, we won't consider this to be
3706 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00003707 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003708 isStartOfObjCClassMessageMissingOpenBracket())
3709 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003710
John McCall1f476a12010-02-26 08:45:28 +00003711 return isDeclarationSpecifier();
3712
Chris Lattner020bab92009-01-04 23:41:41 +00003713 case tok::coloncolon: // ::foo::bar
3714 if (NextToken().is(tok::kw_new) || // ::new
3715 NextToken().is(tok::kw_delete)) // ::delete
3716 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003717
Chris Lattner020bab92009-01-04 23:41:41 +00003718 // Annotate typenames and C++ scope specifiers. If we get one, just
3719 // recurse to handle whatever we get.
3720 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003721 return true;
3722 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003723
Chris Lattneracd58a32006-08-06 17:24:14 +00003724 // storage-class-specifier
3725 case tok::kw_typedef:
3726 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003727 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003728 case tok::kw_static:
3729 case tok::kw_auto:
3730 case tok::kw_register:
3731 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003732
Douglas Gregor26701a42011-09-09 02:06:17 +00003733 // Modules
3734 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00003735
Chris Lattneracd58a32006-08-06 17:24:14 +00003736 // type-specifiers
3737 case tok::kw_short:
3738 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003739 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003740 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00003741 case tok::kw_signed:
3742 case tok::kw_unsigned:
3743 case tok::kw__Complex:
3744 case tok::kw__Imaginary:
3745 case tok::kw_void:
3746 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003747 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003748 case tok::kw_char16_t:
3749 case tok::kw_char32_t:
3750
Chris Lattneracd58a32006-08-06 17:24:14 +00003751 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003752 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00003753 case tok::kw_float:
3754 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003755 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003756 case tok::kw__Bool:
3757 case tok::kw__Decimal32:
3758 case tok::kw__Decimal64:
3759 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003760 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003761
Chris Lattner861a2262008-04-13 18:59:07 +00003762 // struct-or-union-specifier (C99) or class-specifier (C++)
3763 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003764 case tok::kw_struct:
3765 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00003766 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00003767 // enum-specifier
3768 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003769
Chris Lattneracd58a32006-08-06 17:24:14 +00003770 // type-qualifier
3771 case tok::kw_const:
3772 case tok::kw_volatile:
3773 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003774
Chris Lattneracd58a32006-08-06 17:24:14 +00003775 // function-specifier
3776 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003777 case tok::kw_virtual:
3778 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003779
Richard Smithd16fe122012-10-25 00:00:53 +00003780 // friend keyword.
3781 case tok::kw_friend:
3782
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003783 // static_assert-declaration
3784 case tok::kw__Static_assert:
3785
Chris Lattner599e47e2007-08-09 17:01:07 +00003786 // GNU typeof support.
3787 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003788
Chris Lattner599e47e2007-08-09 17:01:07 +00003789 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003790 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00003791
Richard Smithd16fe122012-10-25 00:00:53 +00003792 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00003793 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00003794 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00003795
Benjamin Kramere56f3932011-12-23 17:00:35 +00003796 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003797 case tok::kw__Atomic:
3798 return true;
3799
Chris Lattner8b2ec162008-07-26 03:38:44 +00003800 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3801 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003802 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003803
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003804 // typedef-name
3805 case tok::annot_typename:
3806 return !DisambiguatingWithExpression ||
3807 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00003808
Steve Narofff192fab2009-01-06 19:34:12 +00003809 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003810 case tok::kw___cdecl:
3811 case tok::kw___stdcall:
3812 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003813 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003814 case tok::kw___w64:
3815 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003816 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003817 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003818 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003819 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003820
3821 case tok::kw___private:
3822 case tok::kw___local:
3823 case tok::kw___global:
3824 case tok::kw___constant:
3825 case tok::kw___read_only:
3826 case tok::kw___read_write:
3827 case tok::kw___write_only:
3828
Eli Friedman53339e02009-06-08 23:27:34 +00003829 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003830 }
3831}
3832
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003833bool Parser::isConstructorDeclarator() {
3834 TentativeParsingAction TPA(*this);
3835
3836 // Parse the C++ scope specifier.
3837 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00003838 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003839 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00003840 TPA.Revert();
3841 return false;
3842 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003843
3844 // Parse the constructor name.
3845 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3846 // We already know that we have a constructor name; just consume
3847 // the token.
3848 ConsumeToken();
3849 } else {
3850 TPA.Revert();
3851 return false;
3852 }
3853
Richard Smith43f340f2012-03-27 23:05:05 +00003854 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003855 if (Tok.isNot(tok::l_paren)) {
3856 TPA.Revert();
3857 return false;
3858 }
3859 ConsumeParen();
3860
Richard Smith43f340f2012-03-27 23:05:05 +00003861 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3862 // that we have a constructor.
3863 if (Tok.is(tok::r_paren) ||
3864 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003865 TPA.Revert();
3866 return true;
3867 }
3868
3869 // If we need to, enter the specified scope.
3870 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003871 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003872 DeclScopeObj.EnterDeclaratorScope();
3873
Francois Pichet79f3a872011-01-31 04:54:32 +00003874 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003875 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003876 MaybeParseMicrosoftAttributes(Attrs);
3877
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003878 // Check whether the next token(s) are part of a declaration
3879 // specifier, in which case we have the start of a parameter and,
3880 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00003881 bool IsConstructor = false;
3882 if (isDeclarationSpecifier())
3883 IsConstructor = true;
3884 else if (Tok.is(tok::identifier) ||
3885 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3886 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3887 // This might be a parenthesized member name, but is more likely to
3888 // be a constructor declaration with an invalid argument type. Keep
3889 // looking.
3890 if (Tok.is(tok::annot_cxxscope))
3891 ConsumeToken();
3892 ConsumeToken();
3893
3894 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00003895 // which must have one of the following syntactic forms (see the
3896 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00003897 switch (Tok.getKind()) {
3898 case tok::l_paren:
3899 // C(X ( int));
3900 case tok::l_square:
3901 // C(X [ 5]);
3902 // C(X [ [attribute]]);
3903 case tok::coloncolon:
3904 // C(X :: Y);
3905 // C(X :: *p);
3906 case tok::r_paren:
3907 // C(X )
3908 // Assume this isn't a constructor, rather than assuming it's a
3909 // constructor with an unnamed parameter of an ill-formed type.
3910 break;
3911
3912 default:
3913 IsConstructor = true;
3914 break;
3915 }
3916 }
3917
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003918 TPA.Revert();
3919 return IsConstructor;
3920}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003921
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003922/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003923/// type-qualifier-list: [C99 6.7.5]
3924/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00003925/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00003926/// [ only if VendorAttributesAllowed=true ]
3927/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00003928/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00003929/// [ only if VendorAttributesAllowed=true ]
3930/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3931/// [ only if CXX0XAttributesAllowed=true ]
3932/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003933///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003934void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3935 bool VendorAttributesAllowed,
Richard Smith3dff2512012-04-10 03:25:07 +00003936 bool CXX11AttributesAllowed) {
3937 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003938 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00003939 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00003940 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003941 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003942 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003943
3944 SourceLocation EndLoc;
3945
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003946 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003947 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003948 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003949 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003950 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003951
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003952 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003953 case tok::code_completion:
3954 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003955 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00003956
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003957 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003958 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003959 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003960 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003961 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003962 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003963 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003964 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003965 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003966 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003967 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003968 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003969
3970 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003971 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003972 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003973 goto DoneWithTypeQuals;
3974 case tok::kw___private:
3975 case tok::kw___global:
3976 case tok::kw___local:
3977 case tok::kw___constant:
3978 case tok::kw___read_only:
3979 case tok::kw___write_only:
3980 case tok::kw___read_write:
3981 ParseOpenCLQualifiers(DS);
3982 break;
3983
Eli Friedman53339e02009-06-08 23:27:34 +00003984 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003985 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003986 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00003987 case tok::kw___cdecl:
3988 case tok::kw___stdcall:
3989 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003990 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00003991 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003992 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003993 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003994 continue;
3995 }
3996 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003997 case tok::kw___pascal:
3998 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003999 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004000 continue;
4001 }
4002 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004003 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004004 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004005 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004006 continue; // do *not* consume the next token!
4007 }
4008 // otherwise, FALL THROUGH!
4009 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004010 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004011 // If this is not a type-qualifier token, we're done reading type
4012 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004013 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004014 if (EndLoc.isValid())
4015 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004016 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004017 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004018
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004019 // If the specifier combination wasn't legal, issue a diagnostic.
4020 if (isInvalid) {
4021 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004022 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004023 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004024 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004025 }
4026}
4027
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004028
4029/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4030///
4031void Parser::ParseDeclarator(Declarator &D) {
4032 /// This implements the 'declarator' production in the C grammar, then checks
4033 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004034 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004035}
4036
Richard Smith0efa75c2012-03-29 01:16:42 +00004037static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4038 if (Kind == tok::star || Kind == tok::caret)
4039 return true;
4040
4041 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4042 if (!Lang.CPlusPlus)
4043 return false;
4044
4045 return Kind == tok::amp || Kind == tok::ampamp;
4046}
4047
Sebastian Redlbd150f42008-11-21 19:14:01 +00004048/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4049/// is parsed by the function passed to it. Pass null, and the direct-declarator
4050/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004051/// ptr-operator production.
4052///
Richard Smith09f76ee2011-10-19 21:33:05 +00004053/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004054/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4055/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004056///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004057/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4058/// [C] pointer[opt] direct-declarator
4059/// [C++] direct-declarator
4060/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004061///
4062/// pointer: [C99 6.7.5]
4063/// '*' type-qualifier-list[opt]
4064/// '*' type-qualifier-list[opt] pointer
4065///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004066/// ptr-operator:
4067/// '*' cv-qualifier-seq[opt]
4068/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004069/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004070/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004071/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004072/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004073void Parser::ParseDeclaratorInternal(Declarator &D,
4074 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004075 if (Diags.hasAllExtensionsSilenced())
4076 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004077
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004078 // C++ member pointers start with a '::' or a nested-name.
4079 // Member pointers get special handling, since there's no place for the
4080 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004081 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004082 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4083 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004084 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4085 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004086 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004087 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004088
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004089 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004090 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004091 // The scope spec really belongs to the direct-declarator.
4092 D.getCXXScopeSpec() = SS;
4093 if (DirectDeclParser)
4094 (this->*DirectDeclParser)(D);
4095 return;
4096 }
4097
4098 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004099 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004100 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004101 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004102 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004103
4104 // Recurse to parse whatever is left.
4105 ParseDeclaratorInternal(D, DirectDeclParser);
4106
4107 // Sema will have to catch (syntactically invalid) pointers into global
4108 // scope. It has to catch pointers into namespace scope anyway.
4109 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004110 Loc),
4111 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004112 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004113 return;
4114 }
4115 }
4116
4117 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004118 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004119 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004120 if (DirectDeclParser)
4121 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004122 return;
4123 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004124
Sebastian Redled0f3b02009-03-15 22:02:01 +00004125 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4126 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004127 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004128 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004129
Chris Lattner9eac9312009-03-27 04:18:06 +00004130 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004131 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004132 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004133
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004134 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004135 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004136 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004137
Bill Wendling3708c182007-05-27 10:15:43 +00004138 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004139 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004140 if (Kind == tok::star)
4141 // Remember that we parsed a pointer type, and remember the type-quals.
4142 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004143 DS.getConstSpecLoc(),
4144 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004145 DS.getRestrictSpecLoc()),
4146 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004147 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004148 else
4149 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004150 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004151 Loc),
4152 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004153 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004154 } else {
4155 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004156 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004157
Sebastian Redl3b27be62009-03-23 00:00:23 +00004158 // Complain about rvalue references in C++03, but then go on and build
4159 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004160 if (Kind == tok::ampamp)
David Blaikiebbafb8a2012-03-11 07:00:24 +00004161 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004162 diag::warn_cxx98_compat_rvalue_reference :
4163 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004164
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004165 // GNU-style and C++11 attributes are allowed here, as is restrict.
4166 ParseTypeQualifierListOpt(DS);
4167 D.ExtendWithDeclSpec(DS);
4168
Bill Wendling93efb222007-06-02 23:28:54 +00004169 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4170 // cv-qualifiers are introduced through the use of a typedef or of a
4171 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004172 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4173 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4174 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004175 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004176 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4177 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004178 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00004179 }
Bill Wendling3708c182007-05-27 10:15:43 +00004180
4181 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004182 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004183
Douglas Gregor66583c52008-11-03 15:51:28 +00004184 if (D.getNumTypeObjects() > 0) {
4185 // C++ [dcl.ref]p4: There shall be no references to references.
4186 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4187 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004188 if (const IdentifierInfo *II = D.getIdentifier())
4189 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4190 << II;
4191 else
4192 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4193 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004194
Sebastian Redlbd150f42008-11-21 19:14:01 +00004195 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004196 // can go ahead and build the (technically ill-formed)
4197 // declarator: reference collapsing will take care of it.
4198 }
4199 }
4200
Bill Wendling3708c182007-05-27 10:15:43 +00004201 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00004202 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004203 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004204 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004205 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004206 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004207}
4208
Richard Smith0efa75c2012-03-29 01:16:42 +00004209static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4210 SourceLocation EllipsisLoc) {
4211 if (EllipsisLoc.isValid()) {
4212 FixItHint Insertion;
4213 if (!D.getEllipsisLoc().isValid()) {
4214 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4215 D.setEllipsisLoc(EllipsisLoc);
4216 }
4217 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4218 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4219 }
4220}
4221
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004222/// ParseDirectDeclarator
4223/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004224/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004225/// '(' declarator ')'
4226/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004227/// [C90] direct-declarator '[' constant-expression[opt] ']'
4228/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4229/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4230/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4231/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004232/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4233/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004234/// direct-declarator '(' parameter-type-list ')'
4235/// direct-declarator '(' identifier-list[opt] ')'
4236/// [GNU] direct-declarator '(' parameter-forward-declarations
4237/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004238/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4239/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004240/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4241/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4242/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004243/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004244/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004245///
4246/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004247/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004248/// '::'[opt] nested-name-specifier[opt] type-name
4249///
4250/// id-expression: [C++ 5.1]
4251/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004252/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004253///
4254/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004255/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004256/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004257/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004258/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004259/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004260///
Richard Smith1453e312012-03-27 01:42:32 +00004261/// Note, any additional constructs added here may need corresponding changes
4262/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004263void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004264 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004265
David Blaikiebbafb8a2012-03-11 07:00:24 +00004266 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004267 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004268 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004269 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4270 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004271 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004272 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004273 }
4274
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004275 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004276 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004277 // Change the declaration context for name lookup, until this function
4278 // is exited (and the declarator has been parsed).
4279 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004280 }
4281
Douglas Gregor27b4c162010-12-23 22:44:42 +00004282 // C++0x [dcl.fct]p14:
4283 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004284 // of a parameter-declaration-clause without a preceding comma. In
4285 // this case, the ellipsis is parsed as part of the
4286 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004287 // parameter pack that has not been expanded; otherwise, it is parsed
4288 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004289 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004290 !((D.getContext() == Declarator::PrototypeContext ||
4291 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004292 NextToken().is(tok::r_paren) &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004293 !Actions.containsUnexpandedParameterPacks(D))) {
4294 SourceLocation EllipsisLoc = ConsumeToken();
4295 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4296 // The ellipsis was put in the wrong place. Recover, and explain to
4297 // the user what they should have done.
4298 ParseDeclarator(D);
4299 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4300 return;
4301 } else
4302 D.setEllipsisLoc(EllipsisLoc);
4303
4304 // The ellipsis can't be followed by a parenthesized declarator. We
4305 // check for that in ParseParenDeclarator, after we have disambiguated
4306 // the l_paren token.
4307 }
4308
Douglas Gregor7861a802009-11-03 01:35:08 +00004309 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4310 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4311 // We found something that indicates the start of an unqualified-id.
4312 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004313 bool AllowConstructorName;
4314 if (D.getDeclSpec().hasTypeSpecifier())
4315 AllowConstructorName = false;
4316 else if (D.getCXXScopeSpec().isSet())
4317 AllowConstructorName =
4318 (D.getContext() == Declarator::FileContext ||
4319 (D.getContext() == Declarator::MemberContext &&
4320 D.getDeclSpec().isFriendSpecified()));
4321 else
4322 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4323
Abramo Bagnara7945c982012-01-27 09:46:47 +00004324 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004325 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4326 /*EnteringContext=*/true,
4327 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004328 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004329 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004330 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004331 D.getName()) ||
4332 // Once we're past the identifier, if the scope was bad, mark the
4333 // whole declarator bad.
4334 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004335 D.SetIdentifier(0, Tok.getLocation());
4336 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004337 } else {
4338 // Parsed the unqualified-id; update range information and move along.
4339 if (D.getSourceRange().getBegin().isInvalid())
4340 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4341 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004342 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004343 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004344 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004345 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004346 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004347 "There's a C++-specific check for tok::identifier above");
4348 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4349 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4350 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004351 goto PastIdentifier;
4352 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004353
Douglas Gregor7861a802009-11-03 01:35:08 +00004354 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004355 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004356 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004357 // Example: 'char (*X)' or 'int (*XX)(void)'
4358 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004359
4360 // If the declarator was parenthesized, we entered the declarator
4361 // scope when parsing the parenthesized declarator, then exited
4362 // the scope already. Re-enter the scope, if we need to.
4363 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004364 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004365 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004366 if (!D.isInvalidType() &&
4367 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004368 // Change the declaration context for name lookup, until this function
4369 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004370 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004371 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004372 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004373 // This could be something simple like "int" (in which case the declarator
4374 // portion is empty), if an abstract-declarator is allowed.
4375 D.SetIdentifier(0, Tok.getLocation());
4376 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004377 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004378 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004379 if (D.getContext() == Declarator::MemberContext)
4380 Diag(Tok, diag::err_expected_member_name_or_semi)
4381 << D.getDeclSpec().getSourceRange();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004382 else if (getLangOpts().CPlusPlus)
4383 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004384 else
Chris Lattner6d29c102008-11-18 07:48:38 +00004385 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004386 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004387 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004388 }
Mike Stump11289f42009-09-09 15:08:12 +00004389
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004390 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004391 assert(D.isPastIdentifier() &&
4392 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004393
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004394 // Don't parse attributes unless we have parsed an unparenthesized name.
4395 if (D.hasName() && !D.getNumTypeObjects())
John McCall53fa7142010-12-24 02:08:15 +00004396 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004397
Chris Lattneracd58a32006-08-06 17:24:14 +00004398 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004399 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004400 // Enter function-declaration scope, limiting any declarators to the
4401 // function prototype scope, including parameter declarators.
4402 ParseScope PrototypeScope(this,
4403 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004404 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4405 // In such a case, check if we actually have a function declarator; if it
4406 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004407 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004408 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4409 // The name of the declarator, if any, is tentatively declared within
4410 // a possible direct initializer.
4411 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4412 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4413 TentativelyDeclaredIdentifiers.pop_back();
4414 if (!IsFunctionDecl)
4415 break;
4416 }
John McCall084e83d2011-03-24 11:26:52 +00004417 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004418 BalancedDelimiterTracker T(*this, tok::l_paren);
4419 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004420 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004421 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004422 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004423 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004424 } else {
4425 break;
4426 }
4427 }
Chad Rosierc1183952012-06-26 22:30:43 +00004428}
Chris Lattneracd58a32006-08-06 17:24:14 +00004429
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004430/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4431/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004432/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004433/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4434///
4435/// direct-declarator:
4436/// '(' declarator ')'
4437/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004438/// direct-declarator '(' parameter-type-list ')'
4439/// direct-declarator '(' identifier-list[opt] ')'
4440/// [GNU] direct-declarator '(' parameter-forward-declarations
4441/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004442///
4443void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004444 BalancedDelimiterTracker T(*this, tok::l_paren);
4445 T.consumeOpen();
4446
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004447 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004448
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004449 // Eat any attributes before we look at whether this is a grouping or function
4450 // declarator paren. If this is a grouping paren, the attribute applies to
4451 // the type being built up, for example:
4452 // int (__attribute__(()) *x)(long y)
4453 // If this ends up not being a grouping paren, the attribute applies to the
4454 // first argument, for example:
4455 // int (__attribute__(()) int x)
4456 // In either case, we need to eat any attributes to be able to determine what
4457 // sort of paren this is.
4458 //
John McCall084e83d2011-03-24 11:26:52 +00004459 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004460 bool RequiresArg = false;
4461 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004462 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004463
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004464 // We require that the argument list (if this is a non-grouping paren) be
4465 // present even if the attribute list was empty.
4466 RequiresArg = true;
4467 }
Steve Naroff44ac7772008-12-25 14:16:32 +00004468 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00004469 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00004470 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet17ed0202011-08-18 09:59:55 +00004471 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +00004472 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall53fa7142010-12-24 02:08:15 +00004473 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00004474 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00004475 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004476 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004477 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004478
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004479 // If we haven't past the identifier yet (or where the identifier would be
4480 // stored, if this is an abstract declarator), then this is probably just
4481 // grouping parens. However, if this could be an abstract-declarator, then
4482 // this could also be the start of function arguments (consider 'void()').
4483 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004484
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004485 if (!D.mayOmitIdentifier()) {
4486 // If this can't be an abstract-declarator, this *must* be a grouping
4487 // paren, because we haven't seen the identifier yet.
4488 isGrouping = true;
4489 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004490 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4491 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004492 isDeclarationSpecifier() || // 'int(int)' is a function.
4493 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004494 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4495 // considered to be a type, not a K&R identifier-list.
4496 isGrouping = false;
4497 } else {
4498 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4499 isGrouping = true;
4500 }
Mike Stump11289f42009-09-09 15:08:12 +00004501
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004502 // If this is a grouping paren, handle:
4503 // direct-declarator: '(' declarator ')'
4504 // direct-declarator: '(' attributes declarator ')'
4505 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004506 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4507 D.setEllipsisLoc(SourceLocation());
4508
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004509 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004510 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004511 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004512 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004513 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004514 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004515 T.getCloseLocation()),
4516 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004517
4518 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004519
4520 // An ellipsis cannot be placed outside parentheses.
4521 if (EllipsisLoc.isValid())
4522 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4523
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004524 return;
4525 }
Mike Stump11289f42009-09-09 15:08:12 +00004526
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004527 // Okay, if this wasn't a grouping paren, it must be the start of a function
4528 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004529 // identifier (and remember where it would have been), then call into
4530 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004531 D.SetIdentifier(0, Tok.getLocation());
4532
David Blaikie15a430a2011-12-04 05:04:18 +00004533 // Enter function-declaration scope, limiting any declarators to the
4534 // function prototype scope, including parameter declarators.
4535 ParseScope PrototypeScope(this,
4536 Scope::FunctionPrototypeScope|Scope::DeclScope);
Richard Smith943c4402012-07-30 21:30:52 +00004537 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004538 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004539}
4540
4541/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4542/// declarator D up to a paren, which indicates that we are parsing function
4543/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004544///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004545/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4546/// immediately after the open paren - they should be considered to be the
4547/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004548///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004549/// If RequiresArg is true, then the first argument of the function is required
4550/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004551///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004552/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4553/// (C++11) ref-qualifier[opt], exception-specification[opt],
4554/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4555///
4556/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004557/// dynamic-exception-specification
4558/// noexcept-specification
4559///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004560void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004561 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004562 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004563 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004564 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004565 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004566 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004567 // lparen is already consumed!
4568 assert(D.isPastIdentifier() && "Should not call before identifier!");
4569
4570 // This should be true when the function has typed arguments.
4571 // Otherwise, it is treated as a K&R-style function.
4572 bool HasProto = false;
4573 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004574 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004575 // Remember where we see an ellipsis, if any.
4576 SourceLocation EllipsisLoc;
4577
4578 DeclSpec DS(AttrFactory);
4579 bool RefQualifierIsLValueRef = true;
4580 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004581 SourceLocation ConstQualifierLoc;
4582 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004583 ExceptionSpecificationType ESpecType = EST_None;
4584 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004585 SmallVector<ParsedType, 2> DynamicExceptions;
4586 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004587 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004588 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004589 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004590
James Molloy6f8780b2012-02-29 10:24:19 +00004591 Actions.ActOnStartFunctionDeclarator();
4592
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004593 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4594 EndLoc is the end location for the function declarator.
4595 They differ for trailing return types. */
4596 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004597 SourceLocation LParenLoc, RParenLoc;
4598 LParenLoc = Tracker.getOpenLocation();
4599 StartLoc = LParenLoc;
4600
Douglas Gregor9e66af42011-07-05 16:44:18 +00004601 if (isFunctionDeclaratorIdentifierList()) {
4602 if (RequiresArg)
4603 Diag(Tok, diag::err_argument_required_after_attribute);
4604
4605 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4606
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004607 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004608 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004609 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004610 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004611 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004612 if (Tok.isNot(tok::r_paren))
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004613 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004614 else if (RequiresArg)
4615 Diag(Tok, diag::err_argument_required_after_attribute);
4616
David Blaikiebbafb8a2012-03-11 07:00:24 +00004617 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004618
4619 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004620 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004621 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004622 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004623 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004624
David Blaikiebbafb8a2012-03-11 07:00:24 +00004625 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004626 // FIXME: Accept these components in any order, and produce fixits to
4627 // correct the order if the user gets it wrong. Ideally we should deal
4628 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004629
4630 // Parse cv-qualifier-seq[opt].
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004631 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4632 if (!DS.getSourceRange().getEnd().isInvalid()) {
4633 EndLoc = DS.getSourceRange().getEnd();
4634 ConstQualifierLoc = DS.getConstSpecLoc();
4635 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4636 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004637
4638 // Parse ref-qualifier[opt].
4639 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004640 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004641 diag::warn_cxx98_compat_ref_qualifier :
4642 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004643
Douglas Gregor9e66af42011-07-05 16:44:18 +00004644 RefQualifierIsLValueRef = Tok.is(tok::amp);
4645 RefQualifierLoc = ConsumeToken();
4646 EndLoc = RefQualifierLoc;
4647 }
4648
Douglas Gregor3024f072012-04-16 07:05:22 +00004649 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00004650 // If a declaration declares a member function or member function
4651 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00004652 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00004653 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00004654 // declarator.
Chad Rosierc1183952012-06-26 22:30:43 +00004655 bool IsCXX11MemberFunction =
Douglas Gregor3024f072012-04-16 07:05:22 +00004656 getLangOpts().CPlusPlus0x &&
4657 (D.getContext() == Declarator::MemberContext ||
4658 (D.getContext() == Declarator::FileContext &&
Chad Rosierc1183952012-06-26 22:30:43 +00004659 D.getCXXScopeSpec().isValid() &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004660 Actions.CurContext->isRecord()));
4661 Sema::CXXThisScopeRAII ThisScope(Actions,
4662 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4663 DS.getTypeQualifiers(),
4664 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00004665
Douglas Gregor9e66af42011-07-05 16:44:18 +00004666 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00004667 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00004668 DynamicExceptions,
4669 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00004670 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004671 if (ESpecType != EST_None)
4672 EndLoc = ESpecRange.getEnd();
4673
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004674 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4675 // after the exception-specification.
4676 MaybeParseCXX0XAttributes(FnAttrs);
4677
Douglas Gregor9e66af42011-07-05 16:44:18 +00004678 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004679 LocalEndLoc = EndLoc;
David Blaikiebbafb8a2012-03-11 07:00:24 +00004680 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00004681 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004682 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4683 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004684 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004685 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00004686 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004687 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004688 }
4689 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004690 }
4691
4692 // Remember that we parsed a function type, and remember the attributes.
4693 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004694 IsAmbiguous,
4695 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004696 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004697 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004698 DS.getTypeQualifiers(),
4699 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00004700 RefQualifierLoc, ConstQualifierLoc,
4701 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00004702 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00004703 ESpecType, ESpecRange.getBegin(),
4704 DynamicExceptions.data(),
4705 DynamicExceptionRanges.data(),
4706 DynamicExceptions.size(),
4707 NoexceptExpr.isUsable() ?
4708 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004709 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004710 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004711 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00004712
4713 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004714}
4715
4716/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4717/// identifier list form for a K&R-style function: void foo(a,b,c)
4718///
4719/// Note that identifier-lists are only allowed for normal declarators, not for
4720/// abstract-declarators.
4721bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004722 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00004723 && Tok.is(tok::identifier)
4724 && !TryAltiVecVectorToken()
4725 // K&R identifier lists can't have typedefs as identifiers, per C99
4726 // 6.7.5.3p11.
4727 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4728 // Identifier lists follow a really simple grammar: the identifiers can
4729 // be followed *only* by a ", identifier" or ")". However, K&R
4730 // identifier lists are really rare in the brave new modern world, and
4731 // it is very common for someone to typo a type in a non-K&R style
4732 // list. If we are presented with something like: "void foo(intptr x,
4733 // float y)", we don't want to start parsing the function declarator as
4734 // though it is a K&R style declarator just because intptr is an
4735 // invalid type.
4736 //
4737 // To handle this, we check to see if the token after the first
4738 // identifier is a "," or ")". Only then do we parse it as an
4739 // identifier list.
4740 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4741}
4742
4743/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4744/// we found a K&R-style identifier list instead of a typed parameter list.
4745///
4746/// After returning, ParamInfo will hold the parsed parameters.
4747///
4748/// identifier-list: [C99 6.7.5]
4749/// identifier
4750/// identifier-list ',' identifier
4751///
4752void Parser::ParseFunctionDeclaratorIdentifierList(
4753 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004754 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004755 // If there was no identifier specified for the declarator, either we are in
4756 // an abstract-declarator, or we are in a parameter declarator which was found
4757 // to be abstract. In abstract-declarators, identifier lists are not valid:
4758 // diagnose this.
4759 if (!D.getIdentifier())
4760 Diag(Tok, diag::ext_ident_list_in_param);
4761
4762 // Maintain an efficient lookup of params we have seen so far.
4763 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4764
4765 while (1) {
4766 // If this isn't an identifier, report the error and skip until ')'.
4767 if (Tok.isNot(tok::identifier)) {
4768 Diag(Tok, diag::err_expected_ident);
4769 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4770 // Forget we parsed anything.
4771 ParamInfo.clear();
4772 return;
4773 }
4774
4775 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4776
4777 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4778 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4779 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4780
4781 // Verify that the argument identifier has not already been mentioned.
4782 if (!ParamsSoFar.insert(ParmII)) {
4783 Diag(Tok, diag::err_param_redefinition) << ParmII;
4784 } else {
4785 // Remember this identifier in ParamInfo.
4786 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4787 Tok.getLocation(),
4788 0));
4789 }
4790
4791 // Eat the identifier.
4792 ConsumeToken();
4793
4794 // The list continues if we see a comma.
4795 if (Tok.isNot(tok::comma))
4796 break;
4797 ConsumeToken();
4798 }
4799}
4800
4801/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4802/// after the opening parenthesis. This function will not parse a K&R-style
4803/// identifier list.
4804///
Richard Smith2620cd92012-04-11 04:01:28 +00004805/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4806/// caller parsed those arguments immediately after the open paren - they should
4807/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004808///
4809/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4810/// be the location of the ellipsis, if any was parsed.
4811///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004812/// parameter-type-list: [C99 6.7.5]
4813/// parameter-list
4814/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004815/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004816///
4817/// parameter-list: [C99 6.7.5]
4818/// parameter-declaration
4819/// parameter-list ',' parameter-declaration
4820///
4821/// parameter-declaration: [C99 6.7.5]
4822/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004823/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00004824/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00004825/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00004826/// declaration-specifiers abstract-declarator[opt]
4827/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00004828/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004829/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00004830/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004831///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004832void Parser::ParseParameterDeclarationClause(
4833 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00004834 ParsedAttributes &FirstArgAttrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004835 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004836 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00004837
Chris Lattner371ed4e2008-04-06 06:57:35 +00004838 while (1) {
4839 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00004840 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4841 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00004842 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00004843 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00004844 }
Mike Stump11289f42009-09-09 15:08:12 +00004845
Chris Lattner371ed4e2008-04-06 06:57:35 +00004846 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00004847 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00004848 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004849
Richard Smith2620cd92012-04-11 04:01:28 +00004850 // Parse any C++11 attributes.
4851 MaybeParseCXX0XAttributes(DS.getAttributes());
4852
John McCall53fa7142010-12-24 02:08:15 +00004853 // Skip any Microsoft attributes before a param.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004854 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall53fa7142010-12-24 02:08:15 +00004855 ParseMicrosoftAttributes(DS.getAttributes());
4856
4857 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004858
4859 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004860 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004861 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00004862 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4863 // too much hassle.
4864 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00004865
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004866 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004867
Chris Lattner371ed4e2008-04-06 06:57:35 +00004868 // Parse the declarator. This is "PrototypeContext", because we must
4869 // accept either 'declarator' or 'abstract-declarator' here.
4870 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4871 ParseDeclarator(ParmDecl);
4872
4873 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00004874 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00004875
Chris Lattner371ed4e2008-04-06 06:57:35 +00004876 // Remember this parsed parameter in ParamInfo.
4877 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00004878
Douglas Gregor4d87df52008-12-16 21:30:33 +00004879 // DefArgToks is used when the parsing of default arguments needs
4880 // to be delayed.
4881 CachedTokens *DefArgToks = 0;
4882
Chris Lattner371ed4e2008-04-06 06:57:35 +00004883 // If no parameter was specified, verify that *something* was specified,
4884 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004885 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4886 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00004887 // Completely missing, emit error.
4888 Diag(DSStart, diag::err_missing_param);
4889 } else {
4890 // Otherwise, we have something. Add it and let semantic analysis try
4891 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00004892
Chris Lattner371ed4e2008-04-06 06:57:35 +00004893 // Inform the actions module about the parameter declarator, so it gets
4894 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00004895 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004896
4897 // Parse the default argument, if any. We parse the default
4898 // arguments in all dialects; the semantic analysis in
4899 // ActOnParamDefaultArgument will reject the default argument in
4900 // C.
4901 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00004902 SourceLocation EqualLoc = Tok.getLocation();
4903
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004904 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00004905 if (D.getContext() == Declarator::MemberContext) {
4906 // If we're inside a class definition, cache the tokens
4907 // corresponding to the default argument. We'll actually parse
4908 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00004909 // FIXME: Can we use a smart pointer for Toks?
4910 DefArgToks = new CachedTokens;
4911
Mike Stump11289f42009-09-09 15:08:12 +00004912 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00004913 /*StopAtSemi=*/true,
4914 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004915 delete DefArgToks;
4916 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00004917 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004918 } else {
4919 // Mark the end of the default argument so that we know when to
4920 // stop when we parse it later on.
4921 Token DefArgEnd;
4922 DefArgEnd.startToken();
4923 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4924 DefArgEnd.setLocation(Tok.getLocation());
4925 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00004926 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00004927 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004928 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004929 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004930 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00004931 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004932
Chad Rosierc1183952012-06-26 22:30:43 +00004933 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004934 // used.
4935 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00004936 Sema::PotentiallyEvaluatedIfUsed,
4937 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004938
Sebastian Redldb63af22012-03-14 15:54:00 +00004939 ExprResult DefArgResult;
Sebastian Redl1678d5f2012-03-18 22:25:45 +00004940 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4941 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00004942 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00004943 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00004944 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00004945 if (DefArgResult.isInvalid()) {
4946 Actions.ActOnParamDefaultArgumentError(Param);
4947 SkipUntil(tok::comma, tok::r_paren, true, true);
4948 } else {
4949 // Inform the actions module about the default argument
4950 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00004951 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004952 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004953 }
4954 }
Mike Stump11289f42009-09-09 15:08:12 +00004955
4956 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4957 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00004958 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00004959 }
4960
4961 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004962 if (Tok.isNot(tok::comma)) {
4963 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004964 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00004965
David Blaikiebbafb8a2012-03-11 07:00:24 +00004966 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004967 // We have ellipsis without a preceding ',', which is ill-formed
4968 // in C. Complain and provide the fix.
4969 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00004970 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004971 }
4972 }
Chad Rosierc1183952012-06-26 22:30:43 +00004973
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004974 break;
4975 }
Mike Stump11289f42009-09-09 15:08:12 +00004976
Chris Lattner371ed4e2008-04-06 06:57:35 +00004977 // Consume the comma.
4978 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00004979 }
Mike Stump11289f42009-09-09 15:08:12 +00004980
Chris Lattner6c940e62008-04-06 06:34:08 +00004981}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004982
Chris Lattnere8074e62006-08-06 18:30:15 +00004983/// [C90] direct-declarator '[' constant-expression[opt] ']'
4984/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4985/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4986/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4987/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004988/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4989/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00004990void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004991 if (CheckProhibitedCXX11Attribute())
4992 return;
4993
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004994 BalancedDelimiterTracker T(*this, tok::l_square);
4995 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00004996
Chris Lattner84a11622008-12-18 07:27:21 +00004997 // C array syntax has many features, but by-far the most common is [] and [4].
4998 // This code does a fast path to handle some of the most obvious cases.
4999 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005000 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005001 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00005002 MaybeParseCXX0XAttributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005003
Chris Lattner84a11622008-12-18 07:27:21 +00005004 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005005 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005006 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005007 T.getOpenLocation(),
5008 T.getCloseLocation()),
5009 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005010 return;
5011 } else if (Tok.getKind() == tok::numeric_constant &&
5012 GetLookAheadToken(1).is(tok::r_square)) {
5013 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005014 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005015 ConsumeToken();
5016
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005017 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005018 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00005019 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005020
Chris Lattner84a11622008-12-18 07:27:21 +00005021 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005022 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00005023 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005024 T.getOpenLocation(),
5025 T.getCloseLocation()),
5026 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005027 return;
5028 }
Mike Stump11289f42009-09-09 15:08:12 +00005029
Chris Lattnere8074e62006-08-06 18:30:15 +00005030 // If valid, this location is the position where we read the 'static' keyword.
5031 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005032 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005033 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005034
Chris Lattnere8074e62006-08-06 18:30:15 +00005035 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005036 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005037 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005038 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005039
Chris Lattnere8074e62006-08-06 18:30:15 +00005040 // If we haven't already read 'static', check to see if there is one after the
5041 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005042 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005043 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005044
Chris Lattnere8074e62006-08-06 18:30:15 +00005045 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005046 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005047 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005048
Chris Lattner521ff2b2008-04-06 05:26:30 +00005049 // Handle the case where we have '[*]' as the array size. However, a leading
5050 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005051 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005052 // infrequent, use of lookahead is not costly here.
5053 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005054 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005055
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005056 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005057 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005058 StaticLoc = SourceLocation(); // Drop the static.
5059 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005060 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005061 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005062 // Note, in C89, this production uses the constant-expr production instead
5063 // of assignment-expr. The only difference is that assignment-expr allows
5064 // things like '=' and '*='. Sema rejects these in C89 mode because they
5065 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005066
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005067 // Parse the constant-expression or assignment-expression now (depending
5068 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005069 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005070 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005071 } else {
5072 EnterExpressionEvaluationContext Unevaluated(Actions,
5073 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005074 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005075 }
Chris Lattner62591722006-08-12 18:40:58 +00005076 }
Mike Stump11289f42009-09-09 15:08:12 +00005077
Chris Lattner62591722006-08-12 18:40:58 +00005078 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005079 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005080 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005081 // If the expression was invalid, skip it.
5082 SkipUntil(tok::r_square);
5083 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005084 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005085
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005086 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005087
John McCall084e83d2011-03-24 11:26:52 +00005088 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00005089 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005090
Chris Lattner84a11622008-12-18 07:27:21 +00005091 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005092 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005093 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005094 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005095 T.getOpenLocation(),
5096 T.getCloseLocation()),
5097 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005098}
5099
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005100/// [GNU] typeof-specifier:
5101/// typeof ( expressions )
5102/// typeof ( type-name )
5103/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005104///
5105void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005106 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005107 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005108 SourceLocation StartLoc = ConsumeToken();
5109
John McCalle8595032010-01-13 20:03:27 +00005110 const bool hasParens = Tok.is(tok::l_paren);
5111
Eli Friedman15681d62012-09-26 04:34:21 +00005112 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5113 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005114
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005115 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005116 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005117 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005118 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5119 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005120 if (hasParens)
5121 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005122
5123 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005124 // FIXME: Not accurate, the range gets one token more than it should.
5125 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005126 else
5127 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005128
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005129 if (isCastExpr) {
5130 if (!CastTy) {
5131 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005132 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005133 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005134
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005135 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005136 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005137 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5138 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005139 DiagID, CastTy))
5140 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005141 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005142 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005143
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005144 // If we get here, the operand to the typeof was an expresion.
5145 if (Operand.isInvalid()) {
5146 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005147 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005148 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005149
Eli Friedmane0afc982012-01-21 01:01:51 +00005150 // We might need to transform the operand if it is potentially evaluated.
5151 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5152 if (Operand.isInvalid()) {
5153 DS.SetTypeSpecError();
5154 return;
5155 }
5156
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005157 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005158 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005159 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5160 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005161 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005162 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005163}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005164
Benjamin Kramere56f3932011-12-23 17:00:35 +00005165/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005166/// _Atomic ( type-name )
5167///
5168void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5169 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5170
5171 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005172 BalancedDelimiterTracker T(*this, tok::l_paren);
5173 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedman0dfb8892011-10-06 23:00:33 +00005174 SkipUntil(tok::r_paren);
5175 return;
5176 }
5177
5178 TypeResult Result = ParseTypeName();
5179 if (Result.isInvalid()) {
5180 SkipUntil(tok::r_paren);
5181 return;
5182 }
5183
5184 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005185 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005186
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005187 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005188 return;
5189
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005190 DS.setTypeofParensRange(T.getRange());
5191 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005192
5193 const char *PrevSpec = 0;
5194 unsigned DiagID;
5195 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5196 DiagID, Result.release()))
5197 Diag(StartLoc, DiagID) << PrevSpec;
5198}
5199
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005200
5201/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5202/// from TryAltiVecVectorToken.
5203bool Parser::TryAltiVecVectorTokenOutOfLine() {
5204 Token Next = NextToken();
5205 switch (Next.getKind()) {
5206 default: return false;
5207 case tok::kw_short:
5208 case tok::kw_long:
5209 case tok::kw_signed:
5210 case tok::kw_unsigned:
5211 case tok::kw_void:
5212 case tok::kw_char:
5213 case tok::kw_int:
5214 case tok::kw_float:
5215 case tok::kw_double:
5216 case tok::kw_bool:
5217 case tok::kw___pixel:
5218 Tok.setKind(tok::kw___vector);
5219 return true;
5220 case tok::identifier:
5221 if (Next.getIdentifierInfo() == Ident_pixel) {
5222 Tok.setKind(tok::kw___vector);
5223 return true;
5224 }
5225 return false;
5226 }
5227}
5228
5229bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5230 const char *&PrevSpec, unsigned &DiagID,
5231 bool &isInvalid) {
5232 if (Tok.getIdentifierInfo() == Ident_vector) {
5233 Token Next = NextToken();
5234 switch (Next.getKind()) {
5235 case tok::kw_short:
5236 case tok::kw_long:
5237 case tok::kw_signed:
5238 case tok::kw_unsigned:
5239 case tok::kw_void:
5240 case tok::kw_char:
5241 case tok::kw_int:
5242 case tok::kw_float:
5243 case tok::kw_double:
5244 case tok::kw_bool:
5245 case tok::kw___pixel:
5246 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5247 return true;
5248 case tok::identifier:
5249 if (Next.getIdentifierInfo() == Ident_pixel) {
5250 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5251 return true;
5252 }
5253 break;
5254 default:
5255 break;
5256 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005257 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005258 DS.isTypeAltiVecVector()) {
5259 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5260 return true;
5261 }
5262 return false;
5263}