blob: f73907a7c409ee6fdbd283a7e2b94cf09d692e2d [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
Michael Han64536a62012-11-06 19:34:54 +00001145void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1146 AttributeList *AttrList = attrs.getList();
1147 while (AttrList) {
1148 if (AttrList->isCXX0XAttribute()) {
1149 Diag(AttrList->getLoc(), diag::warn_attribute_no_decl)
1150 << AttrList->getName();
1151 AttrList->setInvalid();
1152 }
1153 AttrList = AttrList->getNext();
1154 }
1155}
1156
Chris Lattner53361ac2006-08-10 05:19:57 +00001157/// ParseDeclaration - Parse a full 'declaration', which consists of
1158/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001159/// 'Context' should be a Declarator::TheContext value. This returns the
1160/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001161///
1162/// declaration: [C99 6.7]
1163/// block-declaration ->
1164/// simple-declaration
1165/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001166/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001167/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001168/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001169/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001170/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001171/// others... [FIXME]
1172///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001173Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1174 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001175 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001176 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001177 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001178 // Must temporarily exit the objective-c container scope for
1179 // parsing c none objective-c decls.
1180 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001181
John McCall48871652010-08-21 09:40:31 +00001182 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001183 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001184 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001185 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001186 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001187 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001188 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001189 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001190 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001191 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001192 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001193 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001194 SourceLocation InlineLoc = ConsumeToken();
1195 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1196 break;
1197 }
Chad Rosierc1183952012-06-26 22:30:43 +00001198 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001199 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001200 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001201 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001202 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001203 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001204 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001205 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001206 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001207 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001208 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001209 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001210 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001211 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001212 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001213 default:
John McCall53fa7142010-12-24 02:08:15 +00001214 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001215 }
Chad Rosierc1183952012-06-26 22:30:43 +00001216
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001217 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001218 // single decl, convert it now. Alias declarations can also declare a type;
1219 // include that too if it is present.
1220 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001221}
1222
1223/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1224/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001225/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1226/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001227///[C90/C++]init-declarator-list ';' [TODO]
1228/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001229///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001230/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001231/// attribute-specifier-seq[opt] type-specifier-seq declarator
1232///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001233/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001234/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001235///
1236/// If FRI is non-null, we might be parsing a for-range-declaration instead
1237/// of a simple-declaration. If we find that we are, we also parse the
1238/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001239Parser::DeclGroupPtrTy
1240Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1241 SourceLocation &DeclEnd,
1242 ParsedAttributesWithRange &attrs,
1243 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001244 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001245 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +00001246 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001247
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001248 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001249 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001250
Chris Lattner0e894622006-08-13 19:58:17 +00001251 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1252 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001253 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001254 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001255 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001256 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001257 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001258 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001259 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001260 }
Chad Rosierc1183952012-06-26 22:30:43 +00001261
1262 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001263}
Mike Stump11289f42009-09-09 15:08:12 +00001264
Richard Smith09f76ee2011-10-19 21:33:05 +00001265/// Returns true if this might be the start of a declarator, or a common typo
1266/// for a declarator.
1267bool Parser::MightBeDeclarator(unsigned Context) {
1268 switch (Tok.getKind()) {
1269 case tok::annot_cxxscope:
1270 case tok::annot_template_id:
1271 case tok::caret:
1272 case tok::code_completion:
1273 case tok::coloncolon:
1274 case tok::ellipsis:
1275 case tok::kw___attribute:
1276 case tok::kw_operator:
1277 case tok::l_paren:
1278 case tok::star:
1279 return true;
1280
1281 case tok::amp:
1282 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001283 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001284
Richard Smithc8a79032012-01-09 22:31:44 +00001285 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001286 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smithc8a79032012-01-09 22:31:44 +00001287 NextToken().is(tok::l_square);
1288
1289 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001290 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001291
Richard Smith09f76ee2011-10-19 21:33:05 +00001292 case tok::identifier:
1293 switch (NextToken().getKind()) {
1294 case tok::code_completion:
1295 case tok::coloncolon:
1296 case tok::comma:
1297 case tok::equal:
1298 case tok::equalequal: // Might be a typo for '='.
1299 case tok::kw_alignas:
1300 case tok::kw_asm:
1301 case tok::kw___attribute:
1302 case tok::l_brace:
1303 case tok::l_paren:
1304 case tok::l_square:
1305 case tok::less:
1306 case tok::r_brace:
1307 case tok::r_paren:
1308 case tok::r_square:
1309 case tok::semi:
1310 return true;
1311
1312 case tok::colon:
1313 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001314 // and in block scope it's probably a label. Inside a class definition,
1315 // this is a bit-field.
1316 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001317 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001318
1319 case tok::identifier: // Possible virt-specifier.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001320 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001321
1322 default:
1323 return false;
1324 }
1325
1326 default:
1327 return false;
1328 }
1329}
1330
Richard Smithb8caac82012-04-11 20:59:20 +00001331/// Skip until we reach something which seems like a sensible place to pick
1332/// up parsing after a malformed declaration. This will sometimes stop sooner
1333/// than SkipUntil(tok::r_brace) would, but will never stop later.
1334void Parser::SkipMalformedDecl() {
1335 while (true) {
1336 switch (Tok.getKind()) {
1337 case tok::l_brace:
1338 // Skip until matching }, then stop. We've probably skipped over
1339 // a malformed class or function definition or similar.
1340 ConsumeBrace();
1341 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1342 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1343 // This declaration isn't over yet. Keep skipping.
1344 continue;
1345 }
1346 if (Tok.is(tok::semi))
1347 ConsumeToken();
1348 return;
1349
1350 case tok::l_square:
1351 ConsumeBracket();
1352 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1353 continue;
1354
1355 case tok::l_paren:
1356 ConsumeParen();
1357 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1358 continue;
1359
1360 case tok::r_brace:
1361 return;
1362
1363 case tok::semi:
1364 ConsumeToken();
1365 return;
1366
1367 case tok::kw_inline:
1368 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001369 // a good place to pick back up parsing, except in an Objective-C
1370 // @interface context.
1371 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1372 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001373 return;
1374 break;
1375
1376 case tok::kw_namespace:
1377 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001378 // place to pick back up parsing, except in an Objective-C
1379 // @interface context.
1380 if (Tok.isAtStartOfLine() &&
1381 (!ParsingInObjCContainer || CurParsedObjCImpl))
1382 return;
1383 break;
1384
1385 case tok::at:
1386 // @end is very much like } in Objective-C contexts.
1387 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1388 ParsingInObjCContainer)
1389 return;
1390 break;
1391
1392 case tok::minus:
1393 case tok::plus:
1394 // - and + probably start new method declarations in Objective-C contexts.
1395 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001396 return;
1397 break;
1398
1399 case tok::eof:
1400 return;
1401
1402 default:
1403 break;
1404 }
1405
1406 ConsumeAnyToken();
1407 }
1408}
1409
John McCalld5a36322009-11-03 19:26:08 +00001410/// ParseDeclGroup - Having concluded that this is either a function
1411/// definition or a group of object declarations, actually parse the
1412/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001413Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1414 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001415 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001416 SourceLocation *DeclEnd,
1417 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001418 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001419 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001420 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001421
John McCalld5a36322009-11-03 19:26:08 +00001422 // Bail out if the first declarator didn't seem well-formed.
1423 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001424 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001425 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001426 }
Mike Stump11289f42009-09-09 15:08:12 +00001427
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001428 // Save late-parsed attributes for now; they need to be parsed in the
1429 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001430 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1431 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001432 if (D.isFunctionDeclarator())
1433 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1434
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001435 // Check to see if we have a function *definition* which must have a body.
1436 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1437 // Look at the next token to make sure that this isn't a function
1438 // declaration. We have to check this because __attribute__ might be the
1439 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001440 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001441
Chris Lattner13901342010-07-11 22:42:07 +00001442 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +00001443 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1444 Diag(Tok, diag::err_function_declared_typedef);
1445
1446 // Recover by treating the 'typedef' as spurious.
1447 DS.ClearStorageClassSpecs();
1448 }
1449
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001450 Decl *TheDecl =
1451 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld5a36322009-11-03 19:26:08 +00001452 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +00001453 }
Chad Rosierc1183952012-06-26 22:30:43 +00001454
Chris Lattner13901342010-07-11 22:42:07 +00001455 if (isDeclarationSpecifier()) {
1456 // If there is an invalid declaration specifier right after the function
1457 // prototype, then we must be in a missing semicolon case where this isn't
1458 // actually a body. Just fall through into the code that handles it as a
1459 // prototype, and let the top-level code handle the erroneous declspec
1460 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +00001461 } else {
1462 Diag(Tok, diag::err_expected_fn_body);
1463 SkipUntil(tok::semi);
1464 return DeclGroupPtrTy();
1465 }
1466 }
1467
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001468 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001469 return DeclGroupPtrTy();
1470
1471 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1472 // must parse and analyze the for-range-initializer before the declaration is
1473 // analyzed.
1474 if (FRI && Tok.is(tok::colon)) {
1475 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +00001476 if (Tok.is(tok::l_brace))
1477 FRI->RangeExpr = ParseBraceInitializer();
1478 else
1479 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +00001480 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1481 Actions.ActOnCXXForRangeDecl(ThisDecl);
1482 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001483 D.complete(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001484 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1485 }
1486
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001487 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001488 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001489 if (LateParsedAttrs.size() > 0)
1490 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001491 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001492 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001493 DeclsInGroup.push_back(FirstDecl);
1494
Richard Smith09f76ee2011-10-19 21:33:05 +00001495 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001496
John McCalld5a36322009-11-03 19:26:08 +00001497 // If we don't have a comma, it is either the end of the list (a ';') or an
1498 // error, bail out.
1499 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001500 SourceLocation CommaLoc = ConsumeToken();
1501
1502 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1503 // This comma was followed by a line-break and something which can't be
1504 // the start of a declarator. The comma was probably a typo for a
1505 // semicolon.
1506 Diag(CommaLoc, diag::err_expected_semi_declaration)
1507 << FixItHint::CreateReplacement(CommaLoc, ";");
1508 ExpectSemi = false;
1509 break;
1510 }
John McCalld5a36322009-11-03 19:26:08 +00001511
1512 // Parse the next declarator.
1513 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001514 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001515
1516 // Accept attributes in an init-declarator. In the first declarator in a
1517 // declaration, these would be part of the declspec. In subsequent
1518 // declarators, they become part of the declarator itself, so that they
1519 // don't apply to declarators after *this* one. Examples:
1520 // short __attribute__((common)) var; -> declspec
1521 // short var __attribute__((common)); -> declarator
1522 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001523 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001524
1525 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001526 if (!D.isInvalidType()) {
1527 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1528 D.complete(ThisDecl);
1529 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001530 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001531 }
John McCalld5a36322009-11-03 19:26:08 +00001532 }
1533
1534 if (DeclEnd)
1535 *DeclEnd = Tok.getLocation();
1536
Richard Smith09f76ee2011-10-19 21:33:05 +00001537 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001538 ExpectAndConsumeSemi(Context == Declarator::FileContext
1539 ? diag::err_invalid_token_after_toplevel_declarator
1540 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001541 // Okay, there was no semicolon and one was expected. If we see a
1542 // declaration specifier, just assume it was missing and continue parsing.
1543 // Otherwise things are very confused and we skip to recover.
1544 if (!isDeclarationSpecifier()) {
1545 SkipUntil(tok::r_brace, true, true);
1546 if (Tok.is(tok::semi))
1547 ConsumeToken();
1548 }
John McCalld5a36322009-11-03 19:26:08 +00001549 }
1550
Douglas Gregor0be31a22010-07-02 17:43:08 +00001551 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001552 DeclsInGroup.data(),
1553 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001554}
1555
Richard Smith02e85f32011-04-14 22:09:26 +00001556/// Parse an optional simple-asm-expr and attributes, and attach them to a
1557/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001558bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001559 // If a simple-asm-expr is present, parse it.
1560 if (Tok.is(tok::kw_asm)) {
1561 SourceLocation Loc;
1562 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1563 if (AsmLabel.isInvalid()) {
1564 SkipUntil(tok::semi, true, true);
1565 return true;
1566 }
1567
1568 D.setAsmLabel(AsmLabel.release());
1569 D.SetRangeEnd(Loc);
1570 }
1571
1572 MaybeParseGNUAttributes(D);
1573 return false;
1574}
1575
Douglas Gregor23996282009-05-12 21:31:51 +00001576/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1577/// declarator'. This method parses the remainder of the declaration
1578/// (including any attributes or initializer, among other things) and
1579/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001580///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001581/// init-declarator: [C99 6.7]
1582/// declarator
1583/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001584/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1585/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001586/// [C++] declarator initializer[opt]
1587///
1588/// [C++] initializer:
1589/// [C++] '=' initializer-clause
1590/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001591/// [C++0x] '=' 'default' [TODO]
1592/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001593/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001594///
1595/// According to the standard grammar, =default and =delete are function
1596/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001597///
John McCall48871652010-08-21 09:40:31 +00001598Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001599 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001600 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001601 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001602
Richard Smith02e85f32011-04-14 22:09:26 +00001603 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1604}
Mike Stump11289f42009-09-09 15:08:12 +00001605
Richard Smith02e85f32011-04-14 22:09:26 +00001606Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1607 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001608 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001609 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001610 switch (TemplateInfo.Kind) {
1611 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001612 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001613 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001614
Douglas Gregor450f00842009-09-25 18:43:00 +00001615 case ParsedTemplateInfo::Template:
1616 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001617 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001618 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001619 D);
1620 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001621
Douglas Gregor450f00842009-09-25 18:43:00 +00001622 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosierc1183952012-06-26 22:30:43 +00001623 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001624 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001625 TemplateInfo.ExternLoc,
1626 TemplateInfo.TemplateLoc,
1627 D);
1628 if (ThisRes.isInvalid()) {
1629 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001630 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001631 }
Chad Rosierc1183952012-06-26 22:30:43 +00001632
Douglas Gregor450f00842009-09-25 18:43:00 +00001633 ThisDecl = ThisRes.get();
1634 break;
1635 }
1636 }
Mike Stump11289f42009-09-09 15:08:12 +00001637
Richard Smith30482bc2011-02-20 03:19:35 +00001638 bool TypeContainsAuto =
1639 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1640
Douglas Gregor23996282009-05-12 21:31:51 +00001641 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001642 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001643 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001644 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001645 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001646 if (D.isFunctionDeclarator())
1647 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1648 << 1 /* delete */;
1649 else
1650 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001651 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001652 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001653 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1654 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001655 else
1656 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001657 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001658 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001659 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001660 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001661 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001662
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001663 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001664 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001665 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001666 cutOffParsing();
1667 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001668 }
Chad Rosierc1183952012-06-26 22:30:43 +00001669
John McCalldadc5752010-08-24 06:29:42 +00001670 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001671
David Blaikiebbafb8a2012-03-11 07:00:24 +00001672 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001673 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001674 ExitScope();
1675 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001676
Douglas Gregor23996282009-05-12 21:31:51 +00001677 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001678 SkipUntil(tok::comma, true, true);
1679 Actions.ActOnInitializerError(ThisDecl);
1680 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001681 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1682 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001683 }
1684 } else if (Tok.is(tok::l_paren)) {
1685 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001686 BalancedDelimiterTracker T(*this, tok::l_paren);
1687 T.consumeOpen();
1688
Benjamin Kramerf0623432012-08-23 22:51:59 +00001689 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001690 CommaLocsTy CommaLocs;
1691
David Blaikiebbafb8a2012-03-11 07:00:24 +00001692 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001693 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001694 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001695 }
1696
Douglas Gregor23996282009-05-12 21:31:51 +00001697 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001698 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00001699 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001700
David Blaikiebbafb8a2012-03-11 07:00:24 +00001701 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001702 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001703 ExitScope();
1704 }
Douglas Gregor23996282009-05-12 21:31:51 +00001705 } else {
1706 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001707 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001708
1709 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1710 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001711
David Blaikiebbafb8a2012-03-11 07:00:24 +00001712 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001713 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001714 ExitScope();
1715 }
1716
Sebastian Redla9351792012-02-11 23:51:47 +00001717 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1718 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001719 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001720 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1721 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001722 }
Fariborz Jahanian8a369a82012-07-03 22:54:28 +00001723 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001724 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001725 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001726 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1727
Sebastian Redl3da34892011-06-05 12:23:16 +00001728 if (D.getCXXScopeSpec().isSet()) {
1729 EnterScope(0);
1730 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1731 }
1732
1733 ExprResult Init(ParseBraceInitializer());
1734
1735 if (D.getCXXScopeSpec().isSet()) {
1736 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1737 ExitScope();
1738 }
1739
1740 if (Init.isInvalid()) {
1741 Actions.ActOnInitializerError(ThisDecl);
1742 } else
1743 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1744 /*DirectInit=*/true, TypeContainsAuto);
1745
Douglas Gregor23996282009-05-12 21:31:51 +00001746 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001747 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001748 }
1749
Richard Smithb2bc2e62011-02-21 20:05:19 +00001750 Actions.FinalizeDeclaration(ThisDecl);
1751
Douglas Gregor23996282009-05-12 21:31:51 +00001752 return ThisDecl;
1753}
1754
Chris Lattner1890ac82006-08-13 01:16:23 +00001755/// ParseSpecifierQualifierList
1756/// specifier-qualifier-list:
1757/// type-specifier specifier-qualifier-list[opt]
1758/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001759/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001760///
Richard Smithc5b05522012-03-12 07:56:15 +00001761void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1762 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001763 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1764 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001765 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001766 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001767
Chris Lattner1890ac82006-08-13 01:16:23 +00001768 // Validate declspec for type-name.
1769 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001770 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1771 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001772 Diag(Tok, diag::err_expected_type);
1773 DS.SetTypeSpecError();
1774 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1775 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001776 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001777 if (!DS.hasTypeSpecifier())
1778 DS.SetTypeSpecError();
1779 }
Mike Stump11289f42009-09-09 15:08:12 +00001780
Chris Lattner1b22eed2006-11-28 05:12:07 +00001781 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001782 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001783 if (DS.getStorageClassSpecLoc().isValid())
1784 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1785 else
1786 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001787 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001788 }
Mike Stump11289f42009-09-09 15:08:12 +00001789
Chris Lattner1b22eed2006-11-28 05:12:07 +00001790 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001791 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001792 if (DS.isInlineSpecified())
1793 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1794 if (DS.isVirtualSpecified())
1795 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1796 if (DS.isExplicitSpecified())
1797 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001798 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001799 }
Richard Smithc5b05522012-03-12 07:56:15 +00001800
1801 // Issue diagnostic and remove constexpr specfier if present.
1802 if (DS.isConstexprSpecified()) {
1803 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1804 DS.ClearConstexprSpec();
1805 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001806}
Chris Lattner53361ac2006-08-10 05:19:57 +00001807
Chris Lattner6cc055a2009-04-12 20:42:31 +00001808/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1809/// specified token is valid after the identifier in a declarator which
1810/// immediately follows the declspec. For example, these things are valid:
1811///
1812/// int x [ 4]; // direct-declarator
1813/// int x ( int y); // direct-declarator
1814/// int(int x ) // direct-declarator
1815/// int x ; // simple-declaration
1816/// int x = 17; // init-declarator-list
1817/// int x , y; // init-declarator-list
1818/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001819/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001820/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001821///
1822/// This is not, because 'x' does not immediately follow the declspec (though
1823/// ')' happens to be valid anyway).
1824/// int (x)
1825///
1826static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1827 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1828 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001829 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001830}
1831
Chris Lattner20a0c612009-04-14 21:34:55 +00001832
1833/// ParseImplicitInt - This method is called when we have an non-typename
1834/// identifier in a declspec (which normally terminates the decl spec) when
1835/// the declspec has no type specifier. In this case, the declspec is either
1836/// malformed or is "implicit int" (in K&R and C89).
1837///
1838/// This method handles diagnosing this prettily and returns false if the
1839/// declspec is done being processed. If it recovers and thinks there may be
1840/// other pieces of declspec after it, it returns true.
1841///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001842bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001843 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00001844 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001845 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001846
Chris Lattner20a0c612009-04-14 21:34:55 +00001847 SourceLocation Loc = Tok.getLocation();
1848 // If we see an identifier that is not a type name, we normally would
1849 // parse it as the identifer being declared. However, when a typename
1850 // is typo'd or the definition is not included, this will incorrectly
1851 // parse the typename as the identifier name and fall over misparsing
1852 // later parts of the diagnostic.
1853 //
1854 // As such, we try to do some look-ahead in cases where this would
1855 // otherwise be an "implicit-int" case to see if this is invalid. For
1856 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1857 // an identifier with implicit int, we'd get a parse error because the
1858 // next token is obviously invalid for a type. Parse these as a case
1859 // with an invalid type specifier.
1860 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001861
Chris Lattner20a0c612009-04-14 21:34:55 +00001862 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00001863 // error, do lookahead to try to do better recovery. This never applies
1864 // within a type specifier. Outside of C++, we allow this even if the
1865 // language doesn't "officially" support implicit int -- we support
1866 // implicit int as an extension in C99 and C11. Allegedly, MS also
1867 // supports implicit int in C++ mode.
Richard Smith2f07ad52012-05-09 20:55:26 +00001868 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smitha952ebb2012-05-15 21:01:51 +00001869 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smithc5b05522012-03-12 07:56:15 +00001870 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001871 // If this token is valid for implicit int, e.g. "static x = 4", then
1872 // we just avoid eating the identifier, so it will be parsed as the
1873 // identifier in the declarator.
1874 return false;
1875 }
Mike Stump11289f42009-09-09 15:08:12 +00001876
Richard Smitha952ebb2012-05-15 21:01:51 +00001877 if (getLangOpts().CPlusPlus &&
1878 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1879 // Don't require a type specifier if we have the 'auto' storage class
1880 // specifier in C++98 -- we'll promote it to a type specifier.
1881 return false;
1882 }
1883
Chris Lattner20a0c612009-04-14 21:34:55 +00001884 // Otherwise, if we don't consume this token, we are going to emit an
1885 // error anyway. Try to recover from various common problems. Check
1886 // to see if this was a reference to a tag name without a tag specified.
1887 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001888 //
1889 // C++ doesn't need this, and isTagName doesn't take SS.
1890 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001891 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001892 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001893
Douglas Gregor0be31a22010-07-02 17:43:08 +00001894 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001895 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001896 case DeclSpec::TST_enum:
1897 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1898 case DeclSpec::TST_union:
1899 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1900 case DeclSpec::TST_struct:
1901 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00001902 case DeclSpec::TST_interface:
1903 TagName="__interface"; FixitTagName = "__interface ";
1904 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001905 case DeclSpec::TST_class:
1906 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001907 }
Mike Stump11289f42009-09-09 15:08:12 +00001908
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001909 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001910 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1911 LookupResult R(Actions, TokenName, SourceLocation(),
1912 Sema::LookupOrdinaryName);
1913
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001914 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001915 << TokenName << TagName << getLangOpts().CPlusPlus
1916 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1917
1918 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1919 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1920 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00001921 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001922 << TokenName << TagName;
1923 }
Mike Stump11289f42009-09-09 15:08:12 +00001924
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001925 // Parse this as a tag as if the missing tag were present.
1926 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00001927 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001928 else
Richard Smithc5b05522012-03-12 07:56:15 +00001929 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1930 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001931 return true;
1932 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001933 }
Mike Stump11289f42009-09-09 15:08:12 +00001934
Richard Smithfe904f02012-05-15 21:29:55 +00001935 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00001936 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00001937 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1938 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00001939 // Look ahead to the next token to try to figure out what this declaration
1940 // was supposed to be.
1941 switch (NextToken().getKind()) {
1942 case tok::comma:
1943 case tok::equal:
1944 case tok::kw_asm:
1945 case tok::l_brace:
1946 case tok::l_square:
1947 case tok::semi:
1948 // This looks like a variable declaration. The type is probably missing.
1949 // We're done parsing decl-specifiers.
1950 return false;
1951
1952 case tok::l_paren: {
1953 // static x(4); // 'x' is not a type
1954 // x(int n); // 'x' is not a type
1955 // x (*p)[]; // 'x' is a type
1956 //
1957 // Since we're in an error case (or the rare 'implicit int in C++' MS
1958 // extension), we can afford to perform a tentative parse to determine
1959 // which case we're in.
1960 TentativeParsingAction PA(*this);
1961 ConsumeToken();
1962 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1963 PA.Revert();
1964 if (TPR == TPResult::False())
1965 return false;
1966 // The identifier is followed by a parenthesized declarator.
1967 // It's supposed to be a type.
1968 break;
1969 }
1970
1971 default:
1972 // This is probably supposed to be a type. This includes cases like:
1973 // int f(itn);
1974 // struct S { unsinged : 4; };
1975 break;
1976 }
1977 }
1978
Chad Rosierc1183952012-06-26 22:30:43 +00001979 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00001980 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001981 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001982 IdentifierInfo *II = Tok.getIdentifierInfo();
1983 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001984 // The action emitted a diagnostic, so we don't have to.
1985 if (T) {
1986 // The action has suggested that the type T could be used. Set that as
1987 // the type in the declaration specifiers, consume the would-be type
1988 // name token, and we're done.
1989 const char *PrevSpec;
1990 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001991 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001992 DS.SetRangeEnd(Tok.getLocation());
1993 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001994 // There may be other declaration specifiers after this.
1995 return true;
1996 } else if (II != Tok.getIdentifierInfo()) {
1997 // If no type was suggested, the correction is to a keyword
1998 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00001999 // There may be other declaration specifiers after this.
2000 return true;
2001 }
Chad Rosierc1183952012-06-26 22:30:43 +00002002
Douglas Gregor15e56022009-10-13 23:27:22 +00002003 // Fall through; the action had no suggestion for us.
2004 } else {
2005 // The action did not emit a diagnostic, so emit one now.
2006 SourceRange R;
2007 if (SS) R = SS->getRange();
2008 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2009 }
Mike Stump11289f42009-09-09 15:08:12 +00002010
Douglas Gregor15e56022009-10-13 23:27:22 +00002011 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002012 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002013 DS.SetRangeEnd(Tok.getLocation());
2014 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002015
Chris Lattner20a0c612009-04-14 21:34:55 +00002016 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2017 // avoid rippling error messages on subsequent uses of the same type,
2018 // could be useful if #include was forgotten.
2019 return false;
2020}
2021
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002022/// \brief Determine the declaration specifier context from the declarator
2023/// context.
2024///
2025/// \param Context the declarator context, which is one of the
2026/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002027Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002028Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2029 if (Context == Declarator::MemberContext)
2030 return DSC_class;
2031 if (Context == Declarator::FileContext)
2032 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002033 if (Context == Declarator::TrailingReturnContext)
2034 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002035 return DSC_normal;
2036}
2037
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002038/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2039///
2040/// FIXME: Simply returns an alignof() expression if the argument is a
2041/// type. Ideally, the type should be propagated directly into Sema.
2042///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002043/// [C11] type-id
2044/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002045/// [C++0x] type-id ...[opt]
2046/// [C++0x] assignment-expression ...[opt]
2047ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2048 SourceLocation &EllipsisLoc) {
2049 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002050 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002051 SourceLocation TypeLoc = Tok.getLocation();
2052 ParsedType Ty = ParseTypeName().get();
2053 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002054 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2055 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002056 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002057 ER = ParseConstantExpression();
2058
David Blaikiebbafb8a2012-03-11 07:00:24 +00002059 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002060 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002061
2062 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002063}
2064
2065/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2066/// attribute to Attrs.
2067///
2068/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002069/// [C11] '_Alignas' '(' type-id ')'
2070/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002071/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
2072/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002073void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2074 SourceLocation *endLoc) {
2075 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2076 "Not an alignment-specifier!");
2077
2078 SourceLocation KWLoc = Tok.getLocation();
2079 ConsumeToken();
2080
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002081 BalancedDelimiterTracker T(*this, tok::l_paren);
2082 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002083 return;
2084
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002085 SourceLocation EllipsisLoc;
2086 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002087 if (ArgExpr.isInvalid()) {
2088 SkipUntil(tok::r_paren);
2089 return;
2090 }
2091
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002092 T.consumeClose();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002093 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002094 *endLoc = T.getCloseLocation();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002095
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002096 // FIXME: Handle pack-expansions here.
2097 if (EllipsisLoc.isValid()) {
2098 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2099 return;
2100 }
2101
Benjamin Kramerf0623432012-08-23 22:51:59 +00002102 ExprVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002103 ArgExprs.push_back(ArgExpr.release());
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002104 // FIXME: This should not be GNU, but we since the attribute used is
2105 // based on the spelling, and there is no true spelling for
2106 // C++11 attributes, this isn't accepted.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002107 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002108 0, T.getOpenLocation(), ArgExprs.data(), 1,
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002109 AttributeList::AS_GNU);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002110}
2111
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002112/// ParseDeclarationSpecifiers
2113/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002114/// storage-class-specifier declaration-specifiers[opt]
2115/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002116/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002117/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002118/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002119/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002120///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002121/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002122/// 'typedef'
2123/// 'extern'
2124/// 'static'
2125/// 'auto'
2126/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002127/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002128/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002129/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002130/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002131/// [C++] 'virtual'
2132/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002133/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002134/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002135/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002136
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002137///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002138void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002139 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002140 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002141 DeclSpecContext DSContext,
2142 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002143 if (DS.getSourceRange().isInvalid()) {
2144 DS.SetRangeStart(Tok.getLocation());
2145 DS.SetRangeEnd(Tok.getLocation());
2146 }
Chad Rosierc1183952012-06-26 22:30:43 +00002147
Douglas Gregordf593fb2011-11-07 17:33:42 +00002148 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002149 bool AttrsLastTime = false;
2150 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002151 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002152 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002153 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002154 unsigned DiagID = 0;
2155
Chris Lattner4d8f8732006-11-28 05:05:08 +00002156 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002157
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002158 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002159 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002160 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002161 if (!AttrsLastTime)
2162 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002163 else {
2164 // Reject C++11 attributes that appertain to decl specifiers as
2165 // we don't support any C++11 attributes that appertain to decl
2166 // specifiers. This also conforms to what g++ 4.8 is doing.
2167 ProhibitCXX11Attributes(attrs);
2168
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002169 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002170 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002171
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002172 // If this is not a declaration specifier token, we're done reading decl
2173 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002174 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002175 return;
Mike Stump11289f42009-09-09 15:08:12 +00002176
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002177 case tok::l_square:
2178 case tok::kw_alignas:
2179 if (!isCXX11AttributeSpecifier())
2180 goto DoneWithDeclSpec;
2181
2182 ProhibitAttributes(attrs);
2183 // FIXME: It would be good to recover by accepting the attributes,
2184 // but attempting to do that now would cause serious
2185 // madness in terms of diagnostics.
2186 attrs.clear();
2187 attrs.Range = SourceRange();
2188
2189 ParseCXX11Attributes(attrs);
2190 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002191 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002192
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002193 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002194 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002195 if (DS.hasTypeSpecifier()) {
2196 bool AllowNonIdentifiers
2197 = (getCurScope()->getFlags() & (Scope::ControlScope |
2198 Scope::BlockScope |
2199 Scope::TemplateParamScope |
2200 Scope::FunctionPrototypeScope |
2201 Scope::AtCatchScope)) == 0;
2202 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002203 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002204 (DSContext == DSC_class && DS.isFriendSpecified());
2205
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002206 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002207 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002208 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002209 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002210 }
2211
Douglas Gregor80039242011-02-15 20:33:25 +00002212 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2213 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2214 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002215 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002216 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002217 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002218 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002219 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002220 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002221
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002222 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002223 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002224 }
2225
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002226 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002227 // C++ scope specifier. Annotate and loop, or bail out on error.
2228 if (TryAnnotateCXXScopeToken(true)) {
2229 if (!DS.hasTypeSpecifier())
2230 DS.SetTypeSpecError();
2231 goto DoneWithDeclSpec;
2232 }
John McCall8bc2a702010-03-01 18:20:46 +00002233 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2234 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002235 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002236
2237 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002238 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002239 goto DoneWithDeclSpec;
2240
John McCall9dab4e62009-12-12 11:40:51 +00002241 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002242 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2243 Tok.getAnnotationRange(),
2244 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002245
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002246 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002247 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002248 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002249 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002250 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002251 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002252
2253 // C++ [class.qual]p2:
2254 // In a lookup in which the constructor is an acceptable lookup
2255 // result and the nested-name-specifier nominates a class C:
2256 //
2257 // - if the name specified after the
2258 // nested-name-specifier, when looked up in C, is the
2259 // injected-class-name of C (Clause 9), or
2260 //
2261 // - if the name specified after the nested-name-specifier
2262 // is the same as the identifier or the
2263 // simple-template-id's template-name in the last
2264 // component of the nested-name-specifier,
2265 //
2266 // the name is instead considered to name the constructor of
2267 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002268 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002269 // Thus, if the template-name is actually the constructor
2270 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002271 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002272 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCall84821e72010-04-13 06:39:49 +00002273 if ((DSContext == DSC_top_level ||
2274 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2275 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002276 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002277 if (isConstructorDeclarator()) {
2278 // The user meant this to be an out-of-line constructor
2279 // definition, but template arguments are not allowed
2280 // there. Just allow this as a constructor; we'll
2281 // complain about it later.
2282 goto DoneWithDeclSpec;
2283 }
2284
2285 // The user meant this to name a type, but it actually names
2286 // a constructor with some extraneous template
2287 // arguments. Complain, then parse it as a type as the user
2288 // intended.
2289 Diag(TemplateId->TemplateNameLoc,
2290 diag::err_out_of_line_template_id_names_constructor)
2291 << TemplateId->Name;
2292 }
2293
John McCall9dab4e62009-12-12 11:40:51 +00002294 DS.getTypeSpecScope() = SS;
2295 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002296 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002297 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002298 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002299 continue;
2300 }
2301
Douglas Gregorc5790df2009-09-28 07:26:33 +00002302 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002303 DS.getTypeSpecScope() = SS;
2304 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002305 if (Tok.getAnnotationValue()) {
2306 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002307 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002308 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002309 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002310 if (isInvalid)
2311 break;
John McCallba7bf592010-08-24 05:47:05 +00002312 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002313 else
2314 DS.SetTypeSpecError();
2315 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2316 ConsumeToken(); // The typename
2317 }
2318
Douglas Gregor167fa622009-03-25 15:40:00 +00002319 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002320 goto DoneWithDeclSpec;
2321
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002322 // If we're in a context where the identifier could be a class name,
2323 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00002324 if ((DSContext == DSC_top_level ||
2325 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002326 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002327 &SS)) {
2328 if (isConstructorDeclarator())
2329 goto DoneWithDeclSpec;
2330
2331 // As noted in C++ [class.qual]p2 (cited above), when the name
2332 // of the class is qualified in a context where it could name
2333 // a constructor, its a constructor name. However, we've
2334 // looked at the declarator, and the user probably meant this
2335 // to be a type. Complain that it isn't supposed to be treated
2336 // as a type, then proceed to parse it as a type.
2337 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2338 << Next.getIdentifierInfo();
2339 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002340
John McCallba7bf592010-08-24 05:47:05 +00002341 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2342 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002343 getCurScope(), &SS,
2344 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002345 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002346 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002347
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002348 // If the referenced identifier is not a type, then this declspec is
2349 // erroneous: We already checked about that it has no type specifier, and
2350 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002351 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002352 if (TypeRep == 0) {
2353 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smithc5b05522012-03-12 07:56:15 +00002354 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002355 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002356 }
Mike Stump11289f42009-09-09 15:08:12 +00002357
John McCall9dab4e62009-12-12 11:40:51 +00002358 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002359 ConsumeToken(); // The C++ scope.
2360
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002361 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002362 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002363 if (isInvalid)
2364 break;
Mike Stump11289f42009-09-09 15:08:12 +00002365
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002366 DS.SetRangeEnd(Tok.getLocation());
2367 ConsumeToken(); // The typename.
2368
2369 continue;
2370 }
Mike Stump11289f42009-09-09 15:08:12 +00002371
Chris Lattnere387d9e2009-01-21 19:48:37 +00002372 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002373 if (Tok.getAnnotationValue()) {
2374 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002375 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002376 DiagID, T);
2377 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002378 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002379
Chris Lattner005fc1b2010-04-05 18:18:31 +00002380 if (isInvalid)
2381 break;
2382
Chris Lattnere387d9e2009-01-21 19:48:37 +00002383 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2384 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002385
Chris Lattnere387d9e2009-01-21 19:48:37 +00002386 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2387 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002388 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002389 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002390 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002391
Chris Lattnere387d9e2009-01-21 19:48:37 +00002392 continue;
2393 }
Mike Stump11289f42009-09-09 15:08:12 +00002394
Douglas Gregor06873092011-04-28 15:48:45 +00002395 case tok::kw___is_signed:
2396 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2397 // typically treats it as a trait. If we see __is_signed as it appears
2398 // in libstdc++, e.g.,
2399 //
2400 // static const bool __is_signed;
2401 //
2402 // then treat __is_signed as an identifier rather than as a keyword.
2403 if (DS.getTypeSpecType() == TST_bool &&
2404 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2405 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2406 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2407 Tok.setKind(tok::identifier);
2408 }
2409
2410 // We're done with the declaration-specifiers.
2411 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002412
Chris Lattner16fac4f2008-07-26 01:18:38 +00002413 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002414 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002415 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002416 // In C++, check to see if this is a scope specifier like foo::bar::, if
2417 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002418 if (getLangOpts().CPlusPlus) {
John McCall1f476a12010-02-26 08:45:28 +00002419 if (TryAnnotateCXXScopeToken(true)) {
2420 if (!DS.hasTypeSpecifier())
2421 DS.SetTypeSpecError();
2422 goto DoneWithDeclSpec;
2423 }
2424 if (!Tok.is(tok::identifier))
2425 continue;
2426 }
Mike Stump11289f42009-09-09 15:08:12 +00002427
Chris Lattner16fac4f2008-07-26 01:18:38 +00002428 // This identifier can only be a typedef name if we haven't already seen
2429 // a type-specifier. Without this check we misparse:
2430 // typedef int X; struct Y { short X; }; as 'short int'.
2431 if (DS.hasTypeSpecifier())
2432 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002433
John Thompson22334602010-02-05 00:12:22 +00002434 // Check for need to substitute AltiVec keyword tokens.
2435 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2436 break;
2437
Richard Smith3092a3b2012-05-09 18:56:43 +00002438 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2439 // allow the use of a typedef name as a type specifier.
2440 if (DS.isTypeAltiVecVector())
2441 goto DoneWithDeclSpec;
2442
John McCallba7bf592010-08-24 05:47:05 +00002443 ParsedType TypeRep =
2444 Actions.getTypeName(*Tok.getIdentifierInfo(),
2445 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002446
Chris Lattner6cc055a2009-04-12 20:42:31 +00002447 // If this is not a typedef name, don't parse it as part of the declspec,
2448 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002449 if (!TypeRep) {
Richard Smithc5b05522012-03-12 07:56:15 +00002450 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002451 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002452 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002453
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002454 // If we're in a context where the identifier could be a class name,
2455 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002456 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002457 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002458 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002459 goto DoneWithDeclSpec;
2460
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002461 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002462 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002463 if (isInvalid)
2464 break;
Mike Stump11289f42009-09-09 15:08:12 +00002465
Chris Lattner16fac4f2008-07-26 01:18:38 +00002466 DS.SetRangeEnd(Tok.getLocation());
2467 ConsumeToken(); // The identifier
2468
2469 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2470 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002471 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002472 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002473 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002474
Steve Naroffcd5e7822008-09-22 10:28:57 +00002475 // Need to support trailing type qualifiers (e.g. "id<p> const").
2476 // If a type specifier follows, it will be diagnosed elsewhere.
2477 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002478 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002479
2480 // type-name
2481 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002482 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002483 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002484 // This template-id does not refer to a type name, so we're
2485 // done with the type-specifiers.
2486 goto DoneWithDeclSpec;
2487 }
2488
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002489 // If we're in a context where the template-id could be a
2490 // constructor name or specialization, check whether this is a
2491 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002492 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002493 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002494 isConstructorDeclarator())
2495 goto DoneWithDeclSpec;
2496
Douglas Gregor7f741122009-02-25 19:37:18 +00002497 // Turn the template-id annotation token into a type annotation
2498 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002499 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002500 continue;
2501 }
2502
Chris Lattnere37e2332006-08-15 04:50:22 +00002503 // GNU attributes support.
2504 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002505 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002506 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002507
2508 // Microsoft declspec support.
2509 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002510 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002511 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002512
Steve Naroff44ac7772008-12-25 14:16:32 +00002513 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002514 case tok::kw___forceinline: {
2515 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2516 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002517 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002518 // FIXME: This does not work correctly if it is set to be a declspec
2519 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002520 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002521 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002522 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002523 }
Eli Friedman53339e02009-06-08 23:27:34 +00002524
2525 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002526 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002527 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002528 case tok::kw___cdecl:
2529 case tok::kw___stdcall:
2530 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002531 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002532 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002533 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002534 continue;
2535
Dawn Perchik335e16b2010-09-03 01:29:35 +00002536 // Borland single token adornments.
2537 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002538 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002539 continue;
2540
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002541 // OpenCL single token adornments.
2542 case tok::kw___kernel:
2543 ParseOpenCLAttributes(DS.getAttributes());
2544 continue;
2545
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002546 // storage-class-specifier
2547 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002548 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2549 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002550 break;
2551 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00002552 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002553 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002554 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2555 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002556 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002557 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002558 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2559 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002560 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002561 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00002562 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002563 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002564 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2565 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002566 break;
2567 case tok::kw_auto:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002568 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002569 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002570 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2571 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002572 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002573 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002574 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002575 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002576 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2577 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002578 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002579 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2580 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002581 break;
2582 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002583 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2584 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002585 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002586 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002587 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2588 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002589 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002590 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00002591 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002592 break;
Mike Stump11289f42009-09-09 15:08:12 +00002593
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002594 // function-specifier
2595 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00002596 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002597 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002598 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00002599 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002600 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002601 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00002602 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002603 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002604
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002605 // alignment-specifier
2606 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002607 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002608 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002609 ParseAlignmentSpecifier(DS.getAttributes());
2610 continue;
2611
Anders Carlssoncd8db412009-05-06 04:46:28 +00002612 // friend
2613 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002614 if (DSContext == DSC_class)
2615 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2616 else {
2617 PrevSpec = ""; // not actually used by the diagnostic
2618 DiagID = diag::err_friend_invalid_in_context;
2619 isInvalid = true;
2620 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002621 break;
Mike Stump11289f42009-09-09 15:08:12 +00002622
Douglas Gregor26701a42011-09-09 02:06:17 +00002623 // Modules
2624 case tok::kw___module_private__:
2625 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2626 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002627
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002628 // constexpr
2629 case tok::kw_constexpr:
2630 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2631 break;
2632
Chris Lattnere387d9e2009-01-21 19:48:37 +00002633 // type-specifier
2634 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002635 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2636 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002637 break;
2638 case tok::kw_long:
2639 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002640 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2641 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002642 else
John McCall49bfce42009-08-03 20:12:06 +00002643 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2644 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002645 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002646 case tok::kw___int64:
2647 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2648 DiagID);
2649 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002650 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002651 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2652 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002653 break;
2654 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002655 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2656 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002657 break;
2658 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002659 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2660 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002661 break;
2662 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002663 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2664 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002665 break;
2666 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002667 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2668 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002669 break;
2670 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002671 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2672 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002673 break;
2674 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002675 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2676 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002677 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002678 case tok::kw___int128:
2679 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2680 DiagID);
2681 break;
2682 case tok::kw_half:
2683 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2684 DiagID);
2685 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002686 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002687 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2688 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002689 break;
2690 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002691 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2692 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002693 break;
2694 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002695 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2696 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002697 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002698 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002699 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2700 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002701 break;
2702 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002703 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2704 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002705 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002706 case tok::kw_bool:
2707 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002708 if (Tok.is(tok::kw_bool) &&
2709 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2710 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2711 PrevSpec = ""; // Not used by the diagnostic.
2712 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002713 // For better error recovery.
2714 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002715 isInvalid = true;
2716 } else {
2717 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2718 DiagID);
2719 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002720 break;
2721 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002722 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2723 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002724 break;
2725 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002726 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2727 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002728 break;
2729 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002730 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2731 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002732 break;
John Thompson22334602010-02-05 00:12:22 +00002733 case tok::kw___vector:
2734 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2735 break;
2736 case tok::kw___pixel:
2737 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2738 break;
John McCall39439732011-04-09 22:50:59 +00002739 case tok::kw___unknown_anytype:
2740 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2741 PrevSpec, DiagID);
2742 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002743
2744 // class-specifier:
2745 case tok::kw_class:
2746 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00002747 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002748 case tok::kw_union: {
2749 tok::TokenKind Kind = Tok.getKind();
2750 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002751 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2752 EnteringContext, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002753 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002754 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002755
2756 // enum-specifier:
2757 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002758 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002759 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002760 continue;
2761
2762 // cv-qualifier:
2763 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002764 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002765 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002766 break;
2767 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002768 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002769 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002770 break;
2771 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002772 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002773 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002774 break;
2775
Douglas Gregor333489b2009-03-27 23:10:48 +00002776 // C++ typename-specifier:
2777 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00002778 if (TryAnnotateTypeOrScopeToken()) {
2779 DS.SetTypeSpecError();
2780 goto DoneWithDeclSpec;
2781 }
2782 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00002783 continue;
2784 break;
2785
Chris Lattnere387d9e2009-01-21 19:48:37 +00002786 // GNU typeof support.
2787 case tok::kw_typeof:
2788 ParseTypeofSpecifier(DS);
2789 continue;
2790
David Blaikie15a430a2011-12-04 05:04:18 +00002791 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00002792 ParseDecltypeSpecifier(DS);
2793 continue;
2794
Alexis Hunt4a257072011-05-19 05:37:45 +00002795 case tok::kw___underlying_type:
2796 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00002797 continue;
2798
2799 case tok::kw__Atomic:
2800 ParseAtomicSpecifier(DS);
2801 continue;
Alexis Hunt4a257072011-05-19 05:37:45 +00002802
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002803 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00002804 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002805 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002806 goto DoneWithDeclSpec;
2807 case tok::kw___private:
2808 case tok::kw___global:
2809 case tok::kw___local:
2810 case tok::kw___constant:
2811 case tok::kw___read_only:
2812 case tok::kw___write_only:
2813 case tok::kw___read_write:
2814 ParseOpenCLQualifiers(DS);
2815 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002816
Steve Naroffcfdf6162008-06-05 00:02:44 +00002817 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002818 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00002819 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2820 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002821 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00002822 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002823
Douglas Gregor3a001f42010-11-19 17:10:50 +00002824 if (!ParseObjCProtocolQualifiers(DS))
2825 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2826 << FixItHint::CreateInsertion(Loc, "id")
2827 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00002828
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002829 // Need to support trailing type qualifiers (e.g. "id<p> const").
2830 // If a type specifier follows, it will be diagnosed elsewhere.
2831 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002832 }
John McCall49bfce42009-08-03 20:12:06 +00002833 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002834 if (isInvalid) {
2835 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002836 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00002837
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002838 if (DiagID == diag::ext_duplicate_declspec)
2839 Diag(Tok, DiagID)
2840 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2841 else
2842 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002843 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002844
Chris Lattner2e232092008-03-13 06:29:04 +00002845 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002846 if (DiagID != diag::err_bool_redeclaration)
2847 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002848
2849 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002850 }
2851}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002852
Chris Lattner70ae4912007-10-29 04:42:53 +00002853/// ParseStructDeclaration - Parse a struct declaration without the terminating
2854/// semicolon.
2855///
Chris Lattner90a26b02007-01-23 04:38:16 +00002856/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002857/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002858/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002859/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002860/// struct-declarator-list:
2861/// struct-declarator
2862/// struct-declarator-list ',' struct-declarator
2863/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2864/// struct-declarator:
2865/// declarator
2866/// [GNU] declarator attributes[opt]
2867/// declarator[opt] ':' constant-expression
2868/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2869///
Chris Lattnera12405b2008-04-10 06:46:29 +00002870void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002871ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00002872
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002873 if (Tok.is(tok::kw___extension__)) {
2874 // __extension__ silences extension warnings in the subexpression.
2875 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002876 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002877 return ParseStructDeclaration(DS, Fields);
2878 }
Mike Stump11289f42009-09-09 15:08:12 +00002879
Steve Naroff97170802007-08-20 22:28:22 +00002880 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002881 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002882
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002883 // If there are no declarators, this is a free-standing declaration
2884 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002885 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002886 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2887 DS);
2888 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00002889 return;
2890 }
2891
2892 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002893 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00002894 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00002895 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002896 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00002897 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00002898
2899 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002900 if (!FirstDeclarator)
2901 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002902
Steve Naroff97170802007-08-20 22:28:22 +00002903 /// struct-declarator: declarator
2904 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002905 if (Tok.isNot(tok::colon)) {
2906 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2907 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002908 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002909 }
Mike Stump11289f42009-09-09 15:08:12 +00002910
Chris Lattner76c72282007-10-09 17:33:22 +00002911 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002912 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002913 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002914 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002915 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002916 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002917 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002918 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002919
Steve Naroff97170802007-08-20 22:28:22 +00002920 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002921 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002922
John McCallcfefb6d2009-11-03 02:38:08 +00002923 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00002924 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00002925
Steve Naroff97170802007-08-20 22:28:22 +00002926 // If we don't have a comma, it is either the end of the list (a ';')
2927 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002928 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002929 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002930
Steve Naroff97170802007-08-20 22:28:22 +00002931 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00002932 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002933
John McCallcfefb6d2009-11-03 02:38:08 +00002934 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002935 }
Steve Naroff97170802007-08-20 22:28:22 +00002936}
2937
2938/// ParseStructUnionBody
2939/// struct-contents:
2940/// struct-declaration-list
2941/// [EXT] empty
2942/// [GNU] "struct-declaration-list" without terminatoring ';'
2943/// struct-declaration-list:
2944/// struct-declaration
2945/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002946/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002947///
Chris Lattner1300fb92007-01-23 23:42:53 +00002948void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002949 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002950 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2951 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002952
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002953 BalancedDelimiterTracker T(*this, tok::l_brace);
2954 if (T.consumeOpen())
2955 return;
Mike Stump11289f42009-09-09 15:08:12 +00002956
Douglas Gregor658b9552009-01-09 22:42:13 +00002957 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002958 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002959
Chris Lattner7b9ace62007-01-23 20:11:08 +00002960 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2961 // C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002962 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithe4345902011-12-29 21:57:33 +00002963 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2964 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2965 }
Chris Lattner7b9ace62007-01-23 20:11:08 +00002966
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002967 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002968
Chris Lattner7b9ace62007-01-23 20:11:08 +00002969 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002970 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002971 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002972
Chris Lattner736ed5d2007-06-09 05:59:07 +00002973 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002974 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002975 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00002976 continue;
2977 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002978
John McCallcfefb6d2009-11-03 02:38:08 +00002979 if (!Tok.is(tok::at)) {
2980 struct CFieldCallback : FieldCallback {
2981 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002982 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002983 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002984
John McCall48871652010-08-21 09:40:31 +00002985 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002986 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002987 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2988
Eli Friedman934dbbf2012-08-08 23:53:27 +00002989 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002990 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002991 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002992 FD.D.getDeclSpec().getSourceRange().getBegin(),
2993 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002994 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002995 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00002996 }
John McCallcfefb6d2009-11-03 02:38:08 +00002997 } Callback(*this, TagDecl, FieldDecls);
2998
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002999 // Parse all the comma separated declarators.
3000 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003001 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003002 } else { // Handle @defs
3003 ConsumeToken();
3004 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3005 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00003006 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003007 continue;
3008 }
3009 ConsumeToken();
3010 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3011 if (!Tok.is(tok::identifier)) {
3012 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00003013 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003014 continue;
3015 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003016 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003017 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003018 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003019 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3020 ConsumeToken();
3021 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003022 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003023
Chris Lattner76c72282007-10-09 17:33:22 +00003024 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003025 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003026 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003027 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003028 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003029 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003030 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3031 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00003032 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00003033 // If we stopped at a ';', eat it.
3034 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003035 }
3036 }
Mike Stump11289f42009-09-09 15:08:12 +00003037
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003038 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003039
John McCall084e83d2011-03-24 11:26:52 +00003040 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003041 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003042 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003043
Douglas Gregor0be31a22010-07-02 17:43:08 +00003044 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003045 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003046 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003047 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003048 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003049 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3050 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003051}
3052
Chris Lattner3b561a32006-08-13 00:12:11 +00003053/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003054/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003055/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003056///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003057/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3058/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003059/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3060/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003061/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003062/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003063///
Richard Smith7d137e32012-03-23 03:33:32 +00003064/// [C++11] enum-head '{' enumerator-list[opt] '}'
3065/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003066///
Richard Smith7d137e32012-03-23 03:33:32 +00003067/// enum-head: [C++11]
3068/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3069/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3070/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003071///
Richard Smith7d137e32012-03-23 03:33:32 +00003072/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003073/// 'enum'
3074/// 'enum' 'class'
3075/// 'enum' 'struct'
3076///
Richard Smith7d137e32012-03-23 03:33:32 +00003077/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003078/// ':' type-specifier-seq
3079///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003080/// [C++] elaborated-type-specifier:
3081/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3082///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003083void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003084 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003085 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003086 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003087 if (Tok.is(tok::code_completion)) {
3088 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003089 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003090 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003091 }
John McCallcb432fa2011-07-06 05:58:41 +00003092
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003093 // If attributes exist after tag, parse them.
3094 ParsedAttributesWithRange attrs(AttrFactory);
3095 MaybeParseGNUAttributes(attrs);
3096 MaybeParseCXX0XAttributes(attrs);
3097
3098 // If declspecs exist after tag, parse them.
3099 while (Tok.is(tok::kw___declspec))
3100 ParseMicrosoftDeclSpec(attrs);
3101
Richard Smith0f8ee222012-01-10 01:33:14 +00003102 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003103 bool IsScopedUsingClassTag = false;
3104
John McCallbeae29a2012-06-23 22:30:04 +00003105 // In C++11, recognize 'enum class' and 'enum struct'.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003106 if (getLangOpts().CPlusPlus0x &&
John McCallcb432fa2011-07-06 05:58:41 +00003107 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003108 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003109 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003110 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003111
John McCallbeae29a2012-06-23 22:30:04 +00003112 // Attributes are not allowed between these keywords. Diagnose,
3113 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003114 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003115
3116 // They are allowed afterwards, though.
3117 MaybeParseGNUAttributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003118 MaybeParseCXX0XAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003119 while (Tok.is(tok::kw___declspec))
3120 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003121 }
Richard Smith7d137e32012-03-23 03:33:32 +00003122
John McCall6347b682012-05-07 06:16:58 +00003123 // C++11 [temp.explicit]p12:
3124 // The usual access controls do not apply to names used to specify
3125 // explicit instantiations.
3126 // We extend this to also cover explicit specializations. Note that
3127 // we don't suppress if this turns out to be an elaborated type
3128 // specifier.
3129 bool shouldDelayDiagsInTag =
3130 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3131 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3132 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003133
Richard Smithbfdb1082012-03-12 08:56:40 +00003134 // Enum definitions should not be parsed in a trailing-return-type.
3135 bool AllowDeclaration = DSC != DSC_trailing;
3136
3137 bool AllowFixedUnderlyingType = AllowDeclaration &&
3138 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
3139 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003140
Abramo Bagnarad7548482010-05-19 21:37:53 +00003141 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003142 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003143 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3144 // if a fixed underlying type is allowed.
3145 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003146
3147 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003148 /*EnteringContext=*/false))
John McCall1f476a12010-02-26 08:45:28 +00003149 return;
3150
3151 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003152 Diag(Tok, diag::err_expected_ident);
3153 if (Tok.isNot(tok::l_brace)) {
3154 // Has no name and is not a definition.
3155 // Skip the rest of this declarator, up until the comma or semicolon.
3156 SkipUntil(tok::comma, true);
3157 return;
3158 }
3159 }
3160 }
Mike Stump11289f42009-09-09 15:08:12 +00003161
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003162 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003163 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003164 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003165 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003166
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003167 // Skip the rest of this declarator, up until the comma or semicolon.
3168 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003169 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003170 }
Mike Stump11289f42009-09-09 15:08:12 +00003171
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003172 // If an identifier is present, consume and remember it.
3173 IdentifierInfo *Name = 0;
3174 SourceLocation NameLoc;
3175 if (Tok.is(tok::identifier)) {
3176 Name = Tok.getIdentifierInfo();
3177 NameLoc = ConsumeToken();
3178 }
Mike Stump11289f42009-09-09 15:08:12 +00003179
Richard Smith0f8ee222012-01-10 01:33:14 +00003180 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003181 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3182 // declaration of a scoped enumeration.
3183 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003184 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003185 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003186 }
3187
John McCall6347b682012-05-07 06:16:58 +00003188 // Okay, end the suppression area. We'll decide whether to emit the
3189 // diagnostics in a second.
3190 if (shouldDelayDiagsInTag)
3191 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003192
Douglas Gregor0bf31402010-10-08 23:50:27 +00003193 TypeResult BaseType;
3194
Douglas Gregord1f69f62010-12-01 17:42:47 +00003195 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003196 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003197 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003198 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003199 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003200 // If we're in class scope, this can either be an enum declaration with
3201 // an underlying type, or a declaration of a bitfield member. We try to
3202 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003203 // (integer literal, sizeof); if it's still ambiguous, we then consider
3204 // anything that's a simple-type-specifier followed by '(' as an
3205 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003206 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003207 EnterExpressionEvaluationContext Unevaluated(Actions,
3208 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003209 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003210 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003211 // bit-field. This is the common case.
3212 if (TPR == TPResult::True())
3213 PossibleBitfield = true;
3214 // If the next token starts a type-specifier-seq, it may be either a
3215 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003216 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003217 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003218 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003219 GetLookAheadToken(2).getKind() == tok::semi) {
3220 // Consume the ':'.
3221 ConsumeToken();
3222 } else {
3223 // We have the start of a type-specifier-seq, so we have to perform
3224 // tentative parsing to determine whether we have an expression or a
3225 // type.
3226 TentativeParsingAction TPA(*this);
3227
3228 // Consume the ':'.
3229 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003230
3231 // If we see a type specifier followed by an open-brace, we have an
3232 // ambiguity between an underlying type and a C++11 braced
3233 // function-style cast. Resolve this by always treating it as an
3234 // underlying type.
3235 // FIXME: The standard is not entirely clear on how to disambiguate in
3236 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003237 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003238 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003239 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003240 // We'll parse this as a bitfield later.
3241 PossibleBitfield = true;
3242 TPA.Revert();
3243 } else {
3244 // We have a type-specifier-seq.
3245 TPA.Commit();
3246 }
3247 }
3248 } else {
3249 // Consume the ':'.
3250 ConsumeToken();
3251 }
3252
3253 if (!PossibleBitfield) {
3254 SourceRange Range;
3255 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003256
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003257 if (getLangOpts().CPlusPlus0x) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003258 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003259 } else if (!getLangOpts().ObjC2) {
3260 if (getLangOpts().CPlusPlus)
3261 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3262 else
3263 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3264 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003265 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003266 }
3267
Richard Smith0f8ee222012-01-10 01:33:14 +00003268 // There are four options here. If we have 'friend enum foo;' then this is a
3269 // friend declaration, and cannot have an accompanying definition. If we have
3270 // 'enum foo;', then this is a forward declaration. If we have
3271 // 'enum foo {...' then this is a definition. Otherwise we have something
3272 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003273 //
3274 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3275 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3276 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3277 //
John McCallfaf5fb42010-08-26 23:41:50 +00003278 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003279 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003280 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003281 } else if (Tok.is(tok::l_brace)) {
3282 if (DS.isFriendSpecified()) {
3283 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3284 << SourceRange(DS.getFriendSpecLoc());
3285 ConsumeBrace();
3286 SkipUntil(tok::r_brace);
3287 TUK = Sema::TUK_Friend;
3288 } else {
3289 TUK = Sema::TUK_Definition;
3290 }
Richard Smith369b9f92012-06-25 21:37:02 +00003291 } else if (DSC != DSC_type_specifier &&
3292 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003293 (Tok.isAtStartOfLine() &&
3294 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003295 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3296 if (Tok.isNot(tok::semi)) {
3297 // A semicolon was missing after this declaration. Diagnose and recover.
3298 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3299 "enum");
3300 PP.EnterToken(Tok);
3301 Tok.setKind(tok::semi);
3302 }
John McCall6347b682012-05-07 06:16:58 +00003303 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003304 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003305 }
3306
3307 // If this is an elaborated type specifier, and we delayed
3308 // diagnostics before, just merge them into the current pool.
3309 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3310 diagsFromTag.redelay();
3311 }
Richard Smith7d137e32012-03-23 03:33:32 +00003312
3313 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003314 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003315 TUK != Sema::TUK_Reference) {
Richard Smith7d137e32012-03-23 03:33:32 +00003316 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3317 // Skip the rest of this declarator, up until the comma or semicolon.
3318 Diag(Tok, diag::err_enum_template);
3319 SkipUntil(tok::comma, true);
3320 return;
3321 }
3322
3323 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3324 // Enumerations can't be explicitly instantiated.
3325 DS.SetTypeSpecError();
3326 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3327 return;
3328 }
3329
3330 assert(TemplateInfo.TemplateParams && "no template parameters");
3331 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3332 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003333 }
Chad Rosierc1183952012-06-26 22:30:43 +00003334
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003335 if (TUK == Sema::TUK_Reference)
3336 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003337
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003338 if (!Name && TUK != Sema::TUK_Definition) {
3339 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003340
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003341 // Skip the rest of this declarator, up until the comma or semicolon.
3342 SkipUntil(tok::comma, true);
3343 return;
3344 }
Richard Smith7d137e32012-03-23 03:33:32 +00003345
Douglas Gregord6ab8742009-05-28 23:31:59 +00003346 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003347 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003348 const char *PrevSpec = 0;
3349 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003350 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003351 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003352 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003353 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003354 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003355
Douglas Gregorba41d012010-04-24 16:38:41 +00003356 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003357 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003358 // dependent tag.
3359 if (!Name) {
3360 DS.SetTypeSpecError();
3361 Diag(Tok, diag::err_expected_type_name_after_typename);
3362 return;
3363 }
Chad Rosierc1183952012-06-26 22:30:43 +00003364
Douglas Gregor0be31a22010-07-02 17:43:08 +00003365 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003366 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003367 NameLoc);
3368 if (Type.isInvalid()) {
3369 DS.SetTypeSpecError();
3370 return;
3371 }
Chad Rosierc1183952012-06-26 22:30:43 +00003372
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003373 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3374 NameLoc.isValid() ? NameLoc : StartLoc,
3375 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003376 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003377
Douglas Gregorba41d012010-04-24 16:38:41 +00003378 return;
3379 }
Mike Stump11289f42009-09-09 15:08:12 +00003380
John McCall48871652010-08-21 09:40:31 +00003381 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003382 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003383 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003384 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003385 ConsumeBrace();
3386 SkipUntil(tok::r_brace);
3387 }
Chad Rosierc1183952012-06-26 22:30:43 +00003388
Douglas Gregorba41d012010-04-24 16:38:41 +00003389 DS.SetTypeSpecError();
3390 return;
3391 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003392
Richard Smith369b9f92012-06-25 21:37:02 +00003393 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003394 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003395
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003396 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3397 NameLoc.isValid() ? NameLoc : StartLoc,
3398 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003399 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003400}
3401
Chris Lattnerc1915e22007-01-25 07:29:02 +00003402/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3403/// enumerator-list:
3404/// enumerator
3405/// enumerator-list ',' enumerator
3406/// enumerator:
3407/// enumeration-constant
3408/// enumeration-constant '=' constant-expression
3409/// enumeration-constant:
3410/// identifier
3411///
John McCall48871652010-08-21 09:40:31 +00003412void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003413 // Enter the scope of the enum body and start the definition.
3414 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003415 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003416
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003417 BalancedDelimiterTracker T(*this, tok::l_brace);
3418 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003419
Chris Lattner37256fb2007-08-27 17:24:30 +00003420 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003421 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003422 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003423
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003424 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003425
John McCall48871652010-08-21 09:40:31 +00003426 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003427
Chris Lattnerc1915e22007-01-25 07:29:02 +00003428 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003429 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003430 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3431 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003432
John McCall811a0f52010-10-22 23:36:17 +00003433 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003434 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003435 MaybeParseGNUAttributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003436 MaybeParseCXX0XAttributes(attrs);
3437 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003438
Chris Lattnerc1915e22007-01-25 07:29:02 +00003439 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003440 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003441 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003442
Chris Lattner76c72282007-10-09 17:33:22 +00003443 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003444 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003445 AssignedVal = ParseConstantExpression();
3446 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003447 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003448 }
Mike Stump11289f42009-09-09 15:08:12 +00003449
Chris Lattnerc1915e22007-01-25 07:29:02 +00003450 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003451 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3452 LastEnumConstDecl,
3453 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003454 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003455 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003456 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003457
Chris Lattner4ef40012007-06-11 01:28:17 +00003458 EnumConstantDecls.push_back(EnumConstDecl);
3459 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003460
Douglas Gregorce66d022010-09-07 14:51:08 +00003461 if (Tok.is(tok::identifier)) {
3462 // We're missing a comma between enumerators.
3463 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003464 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003465 << FixItHint::CreateInsertion(Loc, ", ");
3466 continue;
3467 }
Chad Rosierc1183952012-06-26 22:30:43 +00003468
Chris Lattner76c72282007-10-09 17:33:22 +00003469 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003470 break;
3471 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003472
Richard Smith5d164bc2011-10-15 05:09:34 +00003473 if (Tok.isNot(tok::identifier)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003474 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith87f5dc52012-07-23 05:45:25 +00003475 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3476 diag::ext_enumerator_list_comma_cxx :
3477 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003478 << FixItHint::CreateRemoval(CommaLoc);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003479 else if (getLangOpts().CPlusPlus0x)
Richard Smith5d164bc2011-10-15 05:09:34 +00003480 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3481 << FixItHint::CreateRemoval(CommaLoc);
3482 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003483 }
Mike Stump11289f42009-09-09 15:08:12 +00003484
Chris Lattnerc1915e22007-01-25 07:29:02 +00003485 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003486 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003487
Chris Lattnerc1915e22007-01-25 07:29:02 +00003488 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003489 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003490 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003491
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003492 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3493 EnumDecl, EnumConstantDecls.data(),
3494 EnumConstantDecls.size(), getCurScope(),
3495 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003496
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003497 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003498 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3499 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003500
3501 // The next token must be valid after an enum definition. If not, a ';'
3502 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003503 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3504 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003505 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3506 // Push this token back into the preprocessor and change our current token
3507 // to ';' so that the rest of the code recovers as though there were an
3508 // ';' after the definition.
3509 PP.EnterToken(Tok);
3510 Tok.setKind(tok::semi);
3511 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003512}
Chris Lattner3b561a32006-08-13 00:12:11 +00003513
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003514/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003515/// start of a type-qualifier-list.
3516bool Parser::isTypeQualifier() const {
3517 switch (Tok.getKind()) {
3518 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003519
3520 // type-qualifier only in OpenCL
3521 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003522 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003523
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003524 // type-qualifier
3525 case tok::kw_const:
3526 case tok::kw_volatile:
3527 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003528 case tok::kw___private:
3529 case tok::kw___local:
3530 case tok::kw___global:
3531 case tok::kw___constant:
3532 case tok::kw___read_only:
3533 case tok::kw___read_write:
3534 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003535 return true;
3536 }
3537}
3538
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003539/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3540/// is definitely a type-specifier. Return false if it isn't part of a type
3541/// specifier or if we're not sure.
3542bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3543 switch (Tok.getKind()) {
3544 default: return false;
3545 // type-specifiers
3546 case tok::kw_short:
3547 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003548 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003549 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003550 case tok::kw_signed:
3551 case tok::kw_unsigned:
3552 case tok::kw__Complex:
3553 case tok::kw__Imaginary:
3554 case tok::kw_void:
3555 case tok::kw_char:
3556 case tok::kw_wchar_t:
3557 case tok::kw_char16_t:
3558 case tok::kw_char32_t:
3559 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003560 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003561 case tok::kw_float:
3562 case tok::kw_double:
3563 case tok::kw_bool:
3564 case tok::kw__Bool:
3565 case tok::kw__Decimal32:
3566 case tok::kw__Decimal64:
3567 case tok::kw__Decimal128:
3568 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003569
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003570 // struct-or-union-specifier (C99) or class-specifier (C++)
3571 case tok::kw_class:
3572 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003573 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003574 case tok::kw_union:
3575 // enum-specifier
3576 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003577
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003578 // typedef-name
3579 case tok::annot_typename:
3580 return true;
3581 }
3582}
3583
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003584/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003585/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003586bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003587 switch (Tok.getKind()) {
3588 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003589
Chris Lattner020bab92009-01-04 23:41:41 +00003590 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003591 if (TryAltiVecVectorToken())
3592 return true;
3593 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003594 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003595 // Annotate typenames and C++ scope specifiers. If we get one, just
3596 // recurse to handle whatever we get.
3597 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003598 return true;
3599 if (Tok.is(tok::identifier))
3600 return false;
3601 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003602
Chris Lattner020bab92009-01-04 23:41:41 +00003603 case tok::coloncolon: // ::foo::bar
3604 if (NextToken().is(tok::kw_new) || // ::new
3605 NextToken().is(tok::kw_delete)) // ::delete
3606 return false;
3607
Chris Lattner020bab92009-01-04 23:41:41 +00003608 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003609 return true;
3610 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003611
Chris Lattnere37e2332006-08-15 04:50:22 +00003612 // GNU attributes support.
3613 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003614 // GNU typeof support.
3615 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003616
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003617 // type-specifiers
3618 case tok::kw_short:
3619 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003620 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003621 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003622 case tok::kw_signed:
3623 case tok::kw_unsigned:
3624 case tok::kw__Complex:
3625 case tok::kw__Imaginary:
3626 case tok::kw_void:
3627 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003628 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003629 case tok::kw_char16_t:
3630 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003631 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003632 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003633 case tok::kw_float:
3634 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003635 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003636 case tok::kw__Bool:
3637 case tok::kw__Decimal32:
3638 case tok::kw__Decimal64:
3639 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003640 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003641
Chris Lattner861a2262008-04-13 18:59:07 +00003642 // struct-or-union-specifier (C99) or class-specifier (C++)
3643 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003644 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003645 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003646 case tok::kw_union:
3647 // enum-specifier
3648 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003649
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003650 // type-qualifier
3651 case tok::kw_const:
3652 case tok::kw_volatile:
3653 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003654
3655 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003656 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003657 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003658
Chris Lattner409bf7d2008-10-20 00:25:30 +00003659 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3660 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003661 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003662
Steve Naroff44ac7772008-12-25 14:16:32 +00003663 case tok::kw___cdecl:
3664 case tok::kw___stdcall:
3665 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003666 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003667 case tok::kw___w64:
3668 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003669 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003670 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003671 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003672
3673 case tok::kw___private:
3674 case tok::kw___local:
3675 case tok::kw___global:
3676 case tok::kw___constant:
3677 case tok::kw___read_only:
3678 case tok::kw___read_write:
3679 case tok::kw___write_only:
3680
Eli Friedman53339e02009-06-08 23:27:34 +00003681 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003682
3683 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003684 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003685
Benjamin Kramere56f3932011-12-23 17:00:35 +00003686 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003687 case tok::kw__Atomic:
3688 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003689 }
3690}
3691
Chris Lattneracd58a32006-08-06 17:24:14 +00003692/// isDeclarationSpecifier() - Return true if the current token is part of a
3693/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003694///
3695/// \param DisambiguatingWithExpression True to indicate that the purpose of
3696/// this check is to disambiguate between an expression and a declaration.
3697bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003698 switch (Tok.getKind()) {
3699 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003700
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003701 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003702 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003703
Chris Lattner020bab92009-01-04 23:41:41 +00003704 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003705 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003706 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00003707 return false;
John Thompson22334602010-02-05 00:12:22 +00003708 if (TryAltiVecVectorToken())
3709 return true;
3710 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00003711 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00003712 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003713 // Annotate typenames and C++ scope specifiers. If we get one, just
3714 // recurse to handle whatever we get.
3715 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003716 return true;
3717 if (Tok.is(tok::identifier))
3718 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003719
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003720 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00003721 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003722 // expression is permitted, then this is probably a class message send
3723 // missing the initial '['. In this case, we won't consider this to be
3724 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00003725 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003726 isStartOfObjCClassMessageMissingOpenBracket())
3727 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003728
John McCall1f476a12010-02-26 08:45:28 +00003729 return isDeclarationSpecifier();
3730
Chris Lattner020bab92009-01-04 23:41:41 +00003731 case tok::coloncolon: // ::foo::bar
3732 if (NextToken().is(tok::kw_new) || // ::new
3733 NextToken().is(tok::kw_delete)) // ::delete
3734 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003735
Chris Lattner020bab92009-01-04 23:41:41 +00003736 // Annotate typenames and C++ scope specifiers. If we get one, just
3737 // recurse to handle whatever we get.
3738 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003739 return true;
3740 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003741
Chris Lattneracd58a32006-08-06 17:24:14 +00003742 // storage-class-specifier
3743 case tok::kw_typedef:
3744 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003745 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003746 case tok::kw_static:
3747 case tok::kw_auto:
3748 case tok::kw_register:
3749 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003750
Douglas Gregor26701a42011-09-09 02:06:17 +00003751 // Modules
3752 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00003753
Chris Lattneracd58a32006-08-06 17:24:14 +00003754 // type-specifiers
3755 case tok::kw_short:
3756 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003757 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003758 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00003759 case tok::kw_signed:
3760 case tok::kw_unsigned:
3761 case tok::kw__Complex:
3762 case tok::kw__Imaginary:
3763 case tok::kw_void:
3764 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003765 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003766 case tok::kw_char16_t:
3767 case tok::kw_char32_t:
3768
Chris Lattneracd58a32006-08-06 17:24:14 +00003769 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003770 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00003771 case tok::kw_float:
3772 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003773 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003774 case tok::kw__Bool:
3775 case tok::kw__Decimal32:
3776 case tok::kw__Decimal64:
3777 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003778 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003779
Chris Lattner861a2262008-04-13 18:59:07 +00003780 // struct-or-union-specifier (C99) or class-specifier (C++)
3781 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003782 case tok::kw_struct:
3783 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00003784 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00003785 // enum-specifier
3786 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003787
Chris Lattneracd58a32006-08-06 17:24:14 +00003788 // type-qualifier
3789 case tok::kw_const:
3790 case tok::kw_volatile:
3791 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003792
Chris Lattneracd58a32006-08-06 17:24:14 +00003793 // function-specifier
3794 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003795 case tok::kw_virtual:
3796 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003797
Richard Smithd16fe122012-10-25 00:00:53 +00003798 // friend keyword.
3799 case tok::kw_friend:
3800
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003801 // static_assert-declaration
3802 case tok::kw__Static_assert:
3803
Chris Lattner599e47e2007-08-09 17:01:07 +00003804 // GNU typeof support.
3805 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003806
Chris Lattner599e47e2007-08-09 17:01:07 +00003807 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003808 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00003809
Richard Smithd16fe122012-10-25 00:00:53 +00003810 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00003811 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00003812 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00003813
Benjamin Kramere56f3932011-12-23 17:00:35 +00003814 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003815 case tok::kw__Atomic:
3816 return true;
3817
Chris Lattner8b2ec162008-07-26 03:38:44 +00003818 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3819 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003820 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003821
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003822 // typedef-name
3823 case tok::annot_typename:
3824 return !DisambiguatingWithExpression ||
3825 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00003826
Steve Narofff192fab2009-01-06 19:34:12 +00003827 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003828 case tok::kw___cdecl:
3829 case tok::kw___stdcall:
3830 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003831 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003832 case tok::kw___w64:
3833 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003834 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003835 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003836 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003837 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003838
3839 case tok::kw___private:
3840 case tok::kw___local:
3841 case tok::kw___global:
3842 case tok::kw___constant:
3843 case tok::kw___read_only:
3844 case tok::kw___read_write:
3845 case tok::kw___write_only:
3846
Eli Friedman53339e02009-06-08 23:27:34 +00003847 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003848 }
3849}
3850
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003851bool Parser::isConstructorDeclarator() {
3852 TentativeParsingAction TPA(*this);
3853
3854 // Parse the C++ scope specifier.
3855 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00003856 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003857 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00003858 TPA.Revert();
3859 return false;
3860 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003861
3862 // Parse the constructor name.
3863 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3864 // We already know that we have a constructor name; just consume
3865 // the token.
3866 ConsumeToken();
3867 } else {
3868 TPA.Revert();
3869 return false;
3870 }
3871
Richard Smith43f340f2012-03-27 23:05:05 +00003872 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003873 if (Tok.isNot(tok::l_paren)) {
3874 TPA.Revert();
3875 return false;
3876 }
3877 ConsumeParen();
3878
Richard Smith43f340f2012-03-27 23:05:05 +00003879 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3880 // that we have a constructor.
3881 if (Tok.is(tok::r_paren) ||
3882 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003883 TPA.Revert();
3884 return true;
3885 }
3886
3887 // If we need to, enter the specified scope.
3888 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003889 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003890 DeclScopeObj.EnterDeclaratorScope();
3891
Francois Pichet79f3a872011-01-31 04:54:32 +00003892 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003893 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003894 MaybeParseMicrosoftAttributes(Attrs);
3895
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003896 // Check whether the next token(s) are part of a declaration
3897 // specifier, in which case we have the start of a parameter and,
3898 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00003899 bool IsConstructor = false;
3900 if (isDeclarationSpecifier())
3901 IsConstructor = true;
3902 else if (Tok.is(tok::identifier) ||
3903 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3904 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3905 // This might be a parenthesized member name, but is more likely to
3906 // be a constructor declaration with an invalid argument type. Keep
3907 // looking.
3908 if (Tok.is(tok::annot_cxxscope))
3909 ConsumeToken();
3910 ConsumeToken();
3911
3912 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00003913 // which must have one of the following syntactic forms (see the
3914 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00003915 switch (Tok.getKind()) {
3916 case tok::l_paren:
3917 // C(X ( int));
3918 case tok::l_square:
3919 // C(X [ 5]);
3920 // C(X [ [attribute]]);
3921 case tok::coloncolon:
3922 // C(X :: Y);
3923 // C(X :: *p);
3924 case tok::r_paren:
3925 // C(X )
3926 // Assume this isn't a constructor, rather than assuming it's a
3927 // constructor with an unnamed parameter of an ill-formed type.
3928 break;
3929
3930 default:
3931 IsConstructor = true;
3932 break;
3933 }
3934 }
3935
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003936 TPA.Revert();
3937 return IsConstructor;
3938}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003939
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003940/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003941/// type-qualifier-list: [C99 6.7.5]
3942/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00003943/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00003944/// [ only if VendorAttributesAllowed=true ]
3945/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00003946/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00003947/// [ only if VendorAttributesAllowed=true ]
3948/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3949/// [ only if CXX0XAttributesAllowed=true ]
3950/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003951///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003952void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3953 bool VendorAttributesAllowed,
Richard Smith3dff2512012-04-10 03:25:07 +00003954 bool CXX11AttributesAllowed) {
3955 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003956 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00003957 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00003958 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003959 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003960 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003961
3962 SourceLocation EndLoc;
3963
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003964 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003965 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003966 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003967 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003968 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003969
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003970 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003971 case tok::code_completion:
3972 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003973 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00003974
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003975 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003976 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003977 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003978 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003979 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003980 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003981 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003982 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003983 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003984 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003985 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003986 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003987
3988 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003989 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003990 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003991 goto DoneWithTypeQuals;
3992 case tok::kw___private:
3993 case tok::kw___global:
3994 case tok::kw___local:
3995 case tok::kw___constant:
3996 case tok::kw___read_only:
3997 case tok::kw___write_only:
3998 case tok::kw___read_write:
3999 ParseOpenCLQualifiers(DS);
4000 break;
4001
Eli Friedman53339e02009-06-08 23:27:34 +00004002 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004003 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004004 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004005 case tok::kw___cdecl:
4006 case tok::kw___stdcall:
4007 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004008 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004009 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004010 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004011 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004012 continue;
4013 }
4014 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004015 case tok::kw___pascal:
4016 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004017 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004018 continue;
4019 }
4020 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004021 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004022 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004023 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004024 continue; // do *not* consume the next token!
4025 }
4026 // otherwise, FALL THROUGH!
4027 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004028 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004029 // If this is not a type-qualifier token, we're done reading type
4030 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004031 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004032 if (EndLoc.isValid())
4033 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004034 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004035 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004036
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004037 // If the specifier combination wasn't legal, issue a diagnostic.
4038 if (isInvalid) {
4039 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004040 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004041 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004042 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004043 }
4044}
4045
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004046
4047/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4048///
4049void Parser::ParseDeclarator(Declarator &D) {
4050 /// This implements the 'declarator' production in the C grammar, then checks
4051 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004052 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004053}
4054
Richard Smith0efa75c2012-03-29 01:16:42 +00004055static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4056 if (Kind == tok::star || Kind == tok::caret)
4057 return true;
4058
4059 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4060 if (!Lang.CPlusPlus)
4061 return false;
4062
4063 return Kind == tok::amp || Kind == tok::ampamp;
4064}
4065
Sebastian Redlbd150f42008-11-21 19:14:01 +00004066/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4067/// is parsed by the function passed to it. Pass null, and the direct-declarator
4068/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004069/// ptr-operator production.
4070///
Richard Smith09f76ee2011-10-19 21:33:05 +00004071/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004072/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4073/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004074///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004075/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4076/// [C] pointer[opt] direct-declarator
4077/// [C++] direct-declarator
4078/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004079///
4080/// pointer: [C99 6.7.5]
4081/// '*' type-qualifier-list[opt]
4082/// '*' type-qualifier-list[opt] pointer
4083///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004084/// ptr-operator:
4085/// '*' cv-qualifier-seq[opt]
4086/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004087/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004088/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004089/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004090/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004091void Parser::ParseDeclaratorInternal(Declarator &D,
4092 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004093 if (Diags.hasAllExtensionsSilenced())
4094 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004095
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004096 // C++ member pointers start with a '::' or a nested-name.
4097 // Member pointers get special handling, since there's no place for the
4098 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004099 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004100 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4101 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004102 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4103 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004104 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004105 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004106
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004107 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004108 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004109 // The scope spec really belongs to the direct-declarator.
4110 D.getCXXScopeSpec() = SS;
4111 if (DirectDeclParser)
4112 (this->*DirectDeclParser)(D);
4113 return;
4114 }
4115
4116 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004117 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004118 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004119 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004120 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004121
4122 // Recurse to parse whatever is left.
4123 ParseDeclaratorInternal(D, DirectDeclParser);
4124
4125 // Sema will have to catch (syntactically invalid) pointers into global
4126 // scope. It has to catch pointers into namespace scope anyway.
4127 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004128 Loc),
4129 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004130 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004131 return;
4132 }
4133 }
4134
4135 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004136 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004137 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004138 if (DirectDeclParser)
4139 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004140 return;
4141 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004142
Sebastian Redled0f3b02009-03-15 22:02:01 +00004143 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4144 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004145 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004146 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004147
Chris Lattner9eac9312009-03-27 04:18:06 +00004148 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004149 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004150 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004151
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004152 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004153 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004154 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004155
Bill Wendling3708c182007-05-27 10:15:43 +00004156 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004157 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004158 if (Kind == tok::star)
4159 // Remember that we parsed a pointer type, and remember the type-quals.
4160 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004161 DS.getConstSpecLoc(),
4162 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004163 DS.getRestrictSpecLoc()),
4164 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004165 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004166 else
4167 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004168 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004169 Loc),
4170 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004171 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004172 } else {
4173 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004174 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004175
Sebastian Redl3b27be62009-03-23 00:00:23 +00004176 // Complain about rvalue references in C++03, but then go on and build
4177 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004178 if (Kind == tok::ampamp)
David Blaikiebbafb8a2012-03-11 07:00:24 +00004179 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004180 diag::warn_cxx98_compat_rvalue_reference :
4181 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004182
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004183 // GNU-style and C++11 attributes are allowed here, as is restrict.
4184 ParseTypeQualifierListOpt(DS);
4185 D.ExtendWithDeclSpec(DS);
4186
Bill Wendling93efb222007-06-02 23:28:54 +00004187 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4188 // cv-qualifiers are introduced through the use of a typedef or of a
4189 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004190 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4191 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4192 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004193 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004194 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4195 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004196 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00004197 }
Bill Wendling3708c182007-05-27 10:15:43 +00004198
4199 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004200 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004201
Douglas Gregor66583c52008-11-03 15:51:28 +00004202 if (D.getNumTypeObjects() > 0) {
4203 // C++ [dcl.ref]p4: There shall be no references to references.
4204 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4205 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004206 if (const IdentifierInfo *II = D.getIdentifier())
4207 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4208 << II;
4209 else
4210 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4211 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004212
Sebastian Redlbd150f42008-11-21 19:14:01 +00004213 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004214 // can go ahead and build the (technically ill-formed)
4215 // declarator: reference collapsing will take care of it.
4216 }
4217 }
4218
Bill Wendling3708c182007-05-27 10:15:43 +00004219 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00004220 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004221 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004222 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004223 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004224 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004225}
4226
Richard Smith0efa75c2012-03-29 01:16:42 +00004227static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4228 SourceLocation EllipsisLoc) {
4229 if (EllipsisLoc.isValid()) {
4230 FixItHint Insertion;
4231 if (!D.getEllipsisLoc().isValid()) {
4232 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4233 D.setEllipsisLoc(EllipsisLoc);
4234 }
4235 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4236 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4237 }
4238}
4239
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004240/// ParseDirectDeclarator
4241/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004242/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004243/// '(' declarator ')'
4244/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004245/// [C90] direct-declarator '[' constant-expression[opt] ']'
4246/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4247/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4248/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4249/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004250/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4251/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004252/// direct-declarator '(' parameter-type-list ')'
4253/// direct-declarator '(' identifier-list[opt] ')'
4254/// [GNU] direct-declarator '(' parameter-forward-declarations
4255/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004256/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4257/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004258/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4259/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4260/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004261/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004262/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004263///
4264/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004265/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004266/// '::'[opt] nested-name-specifier[opt] type-name
4267///
4268/// id-expression: [C++ 5.1]
4269/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004270/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004271///
4272/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004273/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004274/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004275/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004276/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004277/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004278///
Richard Smith1453e312012-03-27 01:42:32 +00004279/// Note, any additional constructs added here may need corresponding changes
4280/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004281void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004282 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004283
David Blaikiebbafb8a2012-03-11 07:00:24 +00004284 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004285 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004286 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004287 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4288 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004289 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004290 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004291 }
4292
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004293 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004294 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004295 // Change the declaration context for name lookup, until this function
4296 // is exited (and the declarator has been parsed).
4297 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004298 }
4299
Douglas Gregor27b4c162010-12-23 22:44:42 +00004300 // C++0x [dcl.fct]p14:
4301 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004302 // of a parameter-declaration-clause without a preceding comma. In
4303 // this case, the ellipsis is parsed as part of the
4304 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004305 // parameter pack that has not been expanded; otherwise, it is parsed
4306 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004307 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004308 !((D.getContext() == Declarator::PrototypeContext ||
4309 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004310 NextToken().is(tok::r_paren) &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004311 !Actions.containsUnexpandedParameterPacks(D))) {
4312 SourceLocation EllipsisLoc = ConsumeToken();
4313 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4314 // The ellipsis was put in the wrong place. Recover, and explain to
4315 // the user what they should have done.
4316 ParseDeclarator(D);
4317 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4318 return;
4319 } else
4320 D.setEllipsisLoc(EllipsisLoc);
4321
4322 // The ellipsis can't be followed by a parenthesized declarator. We
4323 // check for that in ParseParenDeclarator, after we have disambiguated
4324 // the l_paren token.
4325 }
4326
Douglas Gregor7861a802009-11-03 01:35:08 +00004327 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4328 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4329 // We found something that indicates the start of an unqualified-id.
4330 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004331 bool AllowConstructorName;
4332 if (D.getDeclSpec().hasTypeSpecifier())
4333 AllowConstructorName = false;
4334 else if (D.getCXXScopeSpec().isSet())
4335 AllowConstructorName =
4336 (D.getContext() == Declarator::FileContext ||
4337 (D.getContext() == Declarator::MemberContext &&
4338 D.getDeclSpec().isFriendSpecified()));
4339 else
4340 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4341
Abramo Bagnara7945c982012-01-27 09:46:47 +00004342 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004343 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4344 /*EnteringContext=*/true,
4345 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004346 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004347 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004348 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004349 D.getName()) ||
4350 // Once we're past the identifier, if the scope was bad, mark the
4351 // whole declarator bad.
4352 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004353 D.SetIdentifier(0, Tok.getLocation());
4354 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004355 } else {
4356 // Parsed the unqualified-id; update range information and move along.
4357 if (D.getSourceRange().getBegin().isInvalid())
4358 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4359 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004360 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004361 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004362 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004363 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004364 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004365 "There's a C++-specific check for tok::identifier above");
4366 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4367 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4368 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004369 goto PastIdentifier;
4370 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004371
Douglas Gregor7861a802009-11-03 01:35:08 +00004372 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004373 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004374 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004375 // Example: 'char (*X)' or 'int (*XX)(void)'
4376 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004377
4378 // If the declarator was parenthesized, we entered the declarator
4379 // scope when parsing the parenthesized declarator, then exited
4380 // the scope already. Re-enter the scope, if we need to.
4381 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004382 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004383 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004384 if (!D.isInvalidType() &&
4385 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004386 // Change the declaration context for name lookup, until this function
4387 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004388 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004389 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004390 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004391 // This could be something simple like "int" (in which case the declarator
4392 // portion is empty), if an abstract-declarator is allowed.
4393 D.SetIdentifier(0, Tok.getLocation());
4394 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004395 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004396 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004397 if (D.getContext() == Declarator::MemberContext)
4398 Diag(Tok, diag::err_expected_member_name_or_semi)
4399 << D.getDeclSpec().getSourceRange();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004400 else if (getLangOpts().CPlusPlus)
4401 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004402 else
Chris Lattner6d29c102008-11-18 07:48:38 +00004403 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004404 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004405 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004406 }
Mike Stump11289f42009-09-09 15:08:12 +00004407
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004408 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004409 assert(D.isPastIdentifier() &&
4410 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004411
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004412 // Don't parse attributes unless we have parsed an unparenthesized name.
4413 if (D.hasName() && !D.getNumTypeObjects())
John McCall53fa7142010-12-24 02:08:15 +00004414 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004415
Chris Lattneracd58a32006-08-06 17:24:14 +00004416 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004417 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004418 // Enter function-declaration scope, limiting any declarators to the
4419 // function prototype scope, including parameter declarators.
4420 ParseScope PrototypeScope(this,
4421 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004422 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4423 // In such a case, check if we actually have a function declarator; if it
4424 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004425 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004426 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4427 // The name of the declarator, if any, is tentatively declared within
4428 // a possible direct initializer.
4429 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4430 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4431 TentativelyDeclaredIdentifiers.pop_back();
4432 if (!IsFunctionDecl)
4433 break;
4434 }
John McCall084e83d2011-03-24 11:26:52 +00004435 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004436 BalancedDelimiterTracker T(*this, tok::l_paren);
4437 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004438 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004439 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004440 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004441 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004442 } else {
4443 break;
4444 }
4445 }
Chad Rosierc1183952012-06-26 22:30:43 +00004446}
Chris Lattneracd58a32006-08-06 17:24:14 +00004447
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004448/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4449/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004450/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004451/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4452///
4453/// direct-declarator:
4454/// '(' declarator ')'
4455/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004456/// direct-declarator '(' parameter-type-list ')'
4457/// direct-declarator '(' identifier-list[opt] ')'
4458/// [GNU] direct-declarator '(' parameter-forward-declarations
4459/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004460///
4461void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004462 BalancedDelimiterTracker T(*this, tok::l_paren);
4463 T.consumeOpen();
4464
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004465 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004466
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004467 // Eat any attributes before we look at whether this is a grouping or function
4468 // declarator paren. If this is a grouping paren, the attribute applies to
4469 // the type being built up, for example:
4470 // int (__attribute__(()) *x)(long y)
4471 // If this ends up not being a grouping paren, the attribute applies to the
4472 // first argument, for example:
4473 // int (__attribute__(()) int x)
4474 // In either case, we need to eat any attributes to be able to determine what
4475 // sort of paren this is.
4476 //
John McCall084e83d2011-03-24 11:26:52 +00004477 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004478 bool RequiresArg = false;
4479 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004480 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004481
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004482 // We require that the argument list (if this is a non-grouping paren) be
4483 // present even if the attribute list was empty.
4484 RequiresArg = true;
4485 }
Steve Naroff44ac7772008-12-25 14:16:32 +00004486 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00004487 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00004488 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet17ed0202011-08-18 09:59:55 +00004489 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +00004490 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall53fa7142010-12-24 02:08:15 +00004491 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00004492 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00004493 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004494 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004495 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004496
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004497 // If we haven't past the identifier yet (or where the identifier would be
4498 // stored, if this is an abstract declarator), then this is probably just
4499 // grouping parens. However, if this could be an abstract-declarator, then
4500 // this could also be the start of function arguments (consider 'void()').
4501 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004502
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004503 if (!D.mayOmitIdentifier()) {
4504 // If this can't be an abstract-declarator, this *must* be a grouping
4505 // paren, because we haven't seen the identifier yet.
4506 isGrouping = true;
4507 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004508 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4509 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004510 isDeclarationSpecifier() || // 'int(int)' is a function.
4511 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004512 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4513 // considered to be a type, not a K&R identifier-list.
4514 isGrouping = false;
4515 } else {
4516 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4517 isGrouping = true;
4518 }
Mike Stump11289f42009-09-09 15:08:12 +00004519
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004520 // If this is a grouping paren, handle:
4521 // direct-declarator: '(' declarator ')'
4522 // direct-declarator: '(' attributes declarator ')'
4523 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004524 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4525 D.setEllipsisLoc(SourceLocation());
4526
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004527 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004528 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004529 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004530 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004531 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004532 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004533 T.getCloseLocation()),
4534 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004535
4536 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004537
4538 // An ellipsis cannot be placed outside parentheses.
4539 if (EllipsisLoc.isValid())
4540 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4541
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004542 return;
4543 }
Mike Stump11289f42009-09-09 15:08:12 +00004544
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004545 // Okay, if this wasn't a grouping paren, it must be the start of a function
4546 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004547 // identifier (and remember where it would have been), then call into
4548 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004549 D.SetIdentifier(0, Tok.getLocation());
4550
David Blaikie15a430a2011-12-04 05:04:18 +00004551 // Enter function-declaration scope, limiting any declarators to the
4552 // function prototype scope, including parameter declarators.
4553 ParseScope PrototypeScope(this,
4554 Scope::FunctionPrototypeScope|Scope::DeclScope);
Richard Smith943c4402012-07-30 21:30:52 +00004555 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004556 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004557}
4558
4559/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4560/// declarator D up to a paren, which indicates that we are parsing function
4561/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004562///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004563/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4564/// immediately after the open paren - they should be considered to be the
4565/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004566///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004567/// If RequiresArg is true, then the first argument of the function is required
4568/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004569///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004570/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4571/// (C++11) ref-qualifier[opt], exception-specification[opt],
4572/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4573///
4574/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004575/// dynamic-exception-specification
4576/// noexcept-specification
4577///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004578void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004579 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004580 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004581 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004582 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004583 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004584 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004585 // lparen is already consumed!
4586 assert(D.isPastIdentifier() && "Should not call before identifier!");
4587
4588 // This should be true when the function has typed arguments.
4589 // Otherwise, it is treated as a K&R-style function.
4590 bool HasProto = false;
4591 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004592 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004593 // Remember where we see an ellipsis, if any.
4594 SourceLocation EllipsisLoc;
4595
4596 DeclSpec DS(AttrFactory);
4597 bool RefQualifierIsLValueRef = true;
4598 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004599 SourceLocation ConstQualifierLoc;
4600 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004601 ExceptionSpecificationType ESpecType = EST_None;
4602 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004603 SmallVector<ParsedType, 2> DynamicExceptions;
4604 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004605 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004606 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004607 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004608
James Molloy6f8780b2012-02-29 10:24:19 +00004609 Actions.ActOnStartFunctionDeclarator();
4610
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004611 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4612 EndLoc is the end location for the function declarator.
4613 They differ for trailing return types. */
4614 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004615 SourceLocation LParenLoc, RParenLoc;
4616 LParenLoc = Tracker.getOpenLocation();
4617 StartLoc = LParenLoc;
4618
Douglas Gregor9e66af42011-07-05 16:44:18 +00004619 if (isFunctionDeclaratorIdentifierList()) {
4620 if (RequiresArg)
4621 Diag(Tok, diag::err_argument_required_after_attribute);
4622
4623 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4624
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004625 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004626 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004627 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004628 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004629 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004630 if (Tok.isNot(tok::r_paren))
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004631 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004632 else if (RequiresArg)
4633 Diag(Tok, diag::err_argument_required_after_attribute);
4634
David Blaikiebbafb8a2012-03-11 07:00:24 +00004635 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004636
4637 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004638 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004639 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004640 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004641 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004642
David Blaikiebbafb8a2012-03-11 07:00:24 +00004643 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004644 // FIXME: Accept these components in any order, and produce fixits to
4645 // correct the order if the user gets it wrong. Ideally we should deal
4646 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004647
4648 // Parse cv-qualifier-seq[opt].
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004649 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4650 if (!DS.getSourceRange().getEnd().isInvalid()) {
4651 EndLoc = DS.getSourceRange().getEnd();
4652 ConstQualifierLoc = DS.getConstSpecLoc();
4653 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4654 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004655
4656 // Parse ref-qualifier[opt].
4657 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004658 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004659 diag::warn_cxx98_compat_ref_qualifier :
4660 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004661
Douglas Gregor9e66af42011-07-05 16:44:18 +00004662 RefQualifierIsLValueRef = Tok.is(tok::amp);
4663 RefQualifierLoc = ConsumeToken();
4664 EndLoc = RefQualifierLoc;
4665 }
4666
Douglas Gregor3024f072012-04-16 07:05:22 +00004667 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00004668 // If a declaration declares a member function or member function
4669 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00004670 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00004671 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00004672 // declarator.
Chad Rosierc1183952012-06-26 22:30:43 +00004673 bool IsCXX11MemberFunction =
Douglas Gregor3024f072012-04-16 07:05:22 +00004674 getLangOpts().CPlusPlus0x &&
4675 (D.getContext() == Declarator::MemberContext ||
4676 (D.getContext() == Declarator::FileContext &&
Chad Rosierc1183952012-06-26 22:30:43 +00004677 D.getCXXScopeSpec().isValid() &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004678 Actions.CurContext->isRecord()));
4679 Sema::CXXThisScopeRAII ThisScope(Actions,
4680 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4681 DS.getTypeQualifiers(),
4682 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00004683
Douglas Gregor9e66af42011-07-05 16:44:18 +00004684 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00004685 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00004686 DynamicExceptions,
4687 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00004688 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004689 if (ESpecType != EST_None)
4690 EndLoc = ESpecRange.getEnd();
4691
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004692 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4693 // after the exception-specification.
4694 MaybeParseCXX0XAttributes(FnAttrs);
4695
Douglas Gregor9e66af42011-07-05 16:44:18 +00004696 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004697 LocalEndLoc = EndLoc;
David Blaikiebbafb8a2012-03-11 07:00:24 +00004698 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00004699 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004700 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4701 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004702 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004703 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00004704 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004705 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004706 }
4707 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004708 }
4709
4710 // Remember that we parsed a function type, and remember the attributes.
4711 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004712 IsAmbiguous,
4713 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004714 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004715 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004716 DS.getTypeQualifiers(),
4717 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00004718 RefQualifierLoc, ConstQualifierLoc,
4719 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00004720 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00004721 ESpecType, ESpecRange.getBegin(),
4722 DynamicExceptions.data(),
4723 DynamicExceptionRanges.data(),
4724 DynamicExceptions.size(),
4725 NoexceptExpr.isUsable() ?
4726 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004727 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004728 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004729 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00004730
4731 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004732}
4733
4734/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4735/// identifier list form for a K&R-style function: void foo(a,b,c)
4736///
4737/// Note that identifier-lists are only allowed for normal declarators, not for
4738/// abstract-declarators.
4739bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004740 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00004741 && Tok.is(tok::identifier)
4742 && !TryAltiVecVectorToken()
4743 // K&R identifier lists can't have typedefs as identifiers, per C99
4744 // 6.7.5.3p11.
4745 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4746 // Identifier lists follow a really simple grammar: the identifiers can
4747 // be followed *only* by a ", identifier" or ")". However, K&R
4748 // identifier lists are really rare in the brave new modern world, and
4749 // it is very common for someone to typo a type in a non-K&R style
4750 // list. If we are presented with something like: "void foo(intptr x,
4751 // float y)", we don't want to start parsing the function declarator as
4752 // though it is a K&R style declarator just because intptr is an
4753 // invalid type.
4754 //
4755 // To handle this, we check to see if the token after the first
4756 // identifier is a "," or ")". Only then do we parse it as an
4757 // identifier list.
4758 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4759}
4760
4761/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4762/// we found a K&R-style identifier list instead of a typed parameter list.
4763///
4764/// After returning, ParamInfo will hold the parsed parameters.
4765///
4766/// identifier-list: [C99 6.7.5]
4767/// identifier
4768/// identifier-list ',' identifier
4769///
4770void Parser::ParseFunctionDeclaratorIdentifierList(
4771 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004772 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004773 // If there was no identifier specified for the declarator, either we are in
4774 // an abstract-declarator, or we are in a parameter declarator which was found
4775 // to be abstract. In abstract-declarators, identifier lists are not valid:
4776 // diagnose this.
4777 if (!D.getIdentifier())
4778 Diag(Tok, diag::ext_ident_list_in_param);
4779
4780 // Maintain an efficient lookup of params we have seen so far.
4781 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4782
4783 while (1) {
4784 // If this isn't an identifier, report the error and skip until ')'.
4785 if (Tok.isNot(tok::identifier)) {
4786 Diag(Tok, diag::err_expected_ident);
4787 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4788 // Forget we parsed anything.
4789 ParamInfo.clear();
4790 return;
4791 }
4792
4793 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4794
4795 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4796 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4797 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4798
4799 // Verify that the argument identifier has not already been mentioned.
4800 if (!ParamsSoFar.insert(ParmII)) {
4801 Diag(Tok, diag::err_param_redefinition) << ParmII;
4802 } else {
4803 // Remember this identifier in ParamInfo.
4804 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4805 Tok.getLocation(),
4806 0));
4807 }
4808
4809 // Eat the identifier.
4810 ConsumeToken();
4811
4812 // The list continues if we see a comma.
4813 if (Tok.isNot(tok::comma))
4814 break;
4815 ConsumeToken();
4816 }
4817}
4818
4819/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4820/// after the opening parenthesis. This function will not parse a K&R-style
4821/// identifier list.
4822///
Richard Smith2620cd92012-04-11 04:01:28 +00004823/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4824/// caller parsed those arguments immediately after the open paren - they should
4825/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004826///
4827/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4828/// be the location of the ellipsis, if any was parsed.
4829///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004830/// parameter-type-list: [C99 6.7.5]
4831/// parameter-list
4832/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004833/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004834///
4835/// parameter-list: [C99 6.7.5]
4836/// parameter-declaration
4837/// parameter-list ',' parameter-declaration
4838///
4839/// parameter-declaration: [C99 6.7.5]
4840/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004841/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00004842/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00004843/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00004844/// declaration-specifiers abstract-declarator[opt]
4845/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00004846/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004847/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00004848/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004849///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004850void Parser::ParseParameterDeclarationClause(
4851 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00004852 ParsedAttributes &FirstArgAttrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004853 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004854 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00004855
Chris Lattner371ed4e2008-04-06 06:57:35 +00004856 while (1) {
4857 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00004858 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4859 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00004860 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00004861 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00004862 }
Mike Stump11289f42009-09-09 15:08:12 +00004863
Chris Lattner371ed4e2008-04-06 06:57:35 +00004864 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00004865 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00004866 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004867
Richard Smith2620cd92012-04-11 04:01:28 +00004868 // Parse any C++11 attributes.
4869 MaybeParseCXX0XAttributes(DS.getAttributes());
4870
John McCall53fa7142010-12-24 02:08:15 +00004871 // Skip any Microsoft attributes before a param.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004872 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall53fa7142010-12-24 02:08:15 +00004873 ParseMicrosoftAttributes(DS.getAttributes());
4874
4875 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004876
4877 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004878 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004879 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00004880 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4881 // too much hassle.
4882 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00004883
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004884 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004885
Chris Lattner371ed4e2008-04-06 06:57:35 +00004886 // Parse the declarator. This is "PrototypeContext", because we must
4887 // accept either 'declarator' or 'abstract-declarator' here.
4888 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4889 ParseDeclarator(ParmDecl);
4890
4891 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00004892 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00004893
Chris Lattner371ed4e2008-04-06 06:57:35 +00004894 // Remember this parsed parameter in ParamInfo.
4895 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00004896
Douglas Gregor4d87df52008-12-16 21:30:33 +00004897 // DefArgToks is used when the parsing of default arguments needs
4898 // to be delayed.
4899 CachedTokens *DefArgToks = 0;
4900
Chris Lattner371ed4e2008-04-06 06:57:35 +00004901 // If no parameter was specified, verify that *something* was specified,
4902 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004903 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4904 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00004905 // Completely missing, emit error.
4906 Diag(DSStart, diag::err_missing_param);
4907 } else {
4908 // Otherwise, we have something. Add it and let semantic analysis try
4909 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00004910
Chris Lattner371ed4e2008-04-06 06:57:35 +00004911 // Inform the actions module about the parameter declarator, so it gets
4912 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00004913 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004914
4915 // Parse the default argument, if any. We parse the default
4916 // arguments in all dialects; the semantic analysis in
4917 // ActOnParamDefaultArgument will reject the default argument in
4918 // C.
4919 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00004920 SourceLocation EqualLoc = Tok.getLocation();
4921
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004922 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00004923 if (D.getContext() == Declarator::MemberContext) {
4924 // If we're inside a class definition, cache the tokens
4925 // corresponding to the default argument. We'll actually parse
4926 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00004927 // FIXME: Can we use a smart pointer for Toks?
4928 DefArgToks = new CachedTokens;
4929
Mike Stump11289f42009-09-09 15:08:12 +00004930 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00004931 /*StopAtSemi=*/true,
4932 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004933 delete DefArgToks;
4934 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00004935 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004936 } else {
4937 // Mark the end of the default argument so that we know when to
4938 // stop when we parse it later on.
4939 Token DefArgEnd;
4940 DefArgEnd.startToken();
4941 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4942 DefArgEnd.setLocation(Tok.getLocation());
4943 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00004944 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00004945 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004946 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004947 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004948 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00004949 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004950
Chad Rosierc1183952012-06-26 22:30:43 +00004951 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004952 // used.
4953 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00004954 Sema::PotentiallyEvaluatedIfUsed,
4955 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004956
Sebastian Redldb63af22012-03-14 15:54:00 +00004957 ExprResult DefArgResult;
Sebastian Redl1678d5f2012-03-18 22:25:45 +00004958 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4959 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00004960 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00004961 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00004962 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00004963 if (DefArgResult.isInvalid()) {
4964 Actions.ActOnParamDefaultArgumentError(Param);
4965 SkipUntil(tok::comma, tok::r_paren, true, true);
4966 } else {
4967 // Inform the actions module about the default argument
4968 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00004969 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004970 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004971 }
4972 }
Mike Stump11289f42009-09-09 15:08:12 +00004973
4974 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4975 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00004976 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00004977 }
4978
4979 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004980 if (Tok.isNot(tok::comma)) {
4981 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004982 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00004983
David Blaikiebbafb8a2012-03-11 07:00:24 +00004984 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004985 // We have ellipsis without a preceding ',', which is ill-formed
4986 // in C. Complain and provide the fix.
4987 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00004988 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004989 }
4990 }
Chad Rosierc1183952012-06-26 22:30:43 +00004991
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004992 break;
4993 }
Mike Stump11289f42009-09-09 15:08:12 +00004994
Chris Lattner371ed4e2008-04-06 06:57:35 +00004995 // Consume the comma.
4996 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00004997 }
Mike Stump11289f42009-09-09 15:08:12 +00004998
Chris Lattner6c940e62008-04-06 06:34:08 +00004999}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005000
Chris Lattnere8074e62006-08-06 18:30:15 +00005001/// [C90] direct-declarator '[' constant-expression[opt] ']'
5002/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5003/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5004/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5005/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005006/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5007/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005008void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005009 if (CheckProhibitedCXX11Attribute())
5010 return;
5011
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005012 BalancedDelimiterTracker T(*this, tok::l_square);
5013 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005014
Chris Lattner84a11622008-12-18 07:27:21 +00005015 // C array syntax has many features, but by-far the most common is [] and [4].
5016 // This code does a fast path to handle some of the most obvious cases.
5017 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005018 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005019 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00005020 MaybeParseCXX0XAttributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005021
Chris Lattner84a11622008-12-18 07:27:21 +00005022 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005023 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005024 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005025 T.getOpenLocation(),
5026 T.getCloseLocation()),
5027 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005028 return;
5029 } else if (Tok.getKind() == tok::numeric_constant &&
5030 GetLookAheadToken(1).is(tok::r_square)) {
5031 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005032 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005033 ConsumeToken();
5034
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005035 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005036 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00005037 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005038
Chris Lattner84a11622008-12-18 07:27:21 +00005039 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005040 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00005041 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005042 T.getOpenLocation(),
5043 T.getCloseLocation()),
5044 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005045 return;
5046 }
Mike Stump11289f42009-09-09 15:08:12 +00005047
Chris Lattnere8074e62006-08-06 18:30:15 +00005048 // If valid, this location is the position where we read the 'static' keyword.
5049 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005050 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005051 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005052
Chris Lattnere8074e62006-08-06 18:30:15 +00005053 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005054 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005055 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005056 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005057
Chris Lattnere8074e62006-08-06 18:30:15 +00005058 // If we haven't already read 'static', check to see if there is one after the
5059 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005060 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005061 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005062
Chris Lattnere8074e62006-08-06 18:30:15 +00005063 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005064 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005065 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005066
Chris Lattner521ff2b2008-04-06 05:26:30 +00005067 // Handle the case where we have '[*]' as the array size. However, a leading
5068 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005069 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005070 // infrequent, use of lookahead is not costly here.
5071 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005072 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005073
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005074 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005075 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005076 StaticLoc = SourceLocation(); // Drop the static.
5077 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005078 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005079 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005080 // Note, in C89, this production uses the constant-expr production instead
5081 // of assignment-expr. The only difference is that assignment-expr allows
5082 // things like '=' and '*='. Sema rejects these in C89 mode because they
5083 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005084
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005085 // Parse the constant-expression or assignment-expression now (depending
5086 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005087 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005088 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005089 } else {
5090 EnterExpressionEvaluationContext Unevaluated(Actions,
5091 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005092 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005093 }
Chris Lattner62591722006-08-12 18:40:58 +00005094 }
Mike Stump11289f42009-09-09 15:08:12 +00005095
Chris Lattner62591722006-08-12 18:40:58 +00005096 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005097 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005098 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005099 // If the expression was invalid, skip it.
5100 SkipUntil(tok::r_square);
5101 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005102 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005103
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005104 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005105
John McCall084e83d2011-03-24 11:26:52 +00005106 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00005107 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005108
Chris Lattner84a11622008-12-18 07:27:21 +00005109 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005110 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005111 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005112 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005113 T.getOpenLocation(),
5114 T.getCloseLocation()),
5115 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005116}
5117
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005118/// [GNU] typeof-specifier:
5119/// typeof ( expressions )
5120/// typeof ( type-name )
5121/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005122///
5123void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005124 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005125 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005126 SourceLocation StartLoc = ConsumeToken();
5127
John McCalle8595032010-01-13 20:03:27 +00005128 const bool hasParens = Tok.is(tok::l_paren);
5129
Eli Friedman15681d62012-09-26 04:34:21 +00005130 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5131 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005132
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005133 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005134 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005135 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005136 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5137 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005138 if (hasParens)
5139 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005140
5141 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005142 // FIXME: Not accurate, the range gets one token more than it should.
5143 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005144 else
5145 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005146
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005147 if (isCastExpr) {
5148 if (!CastTy) {
5149 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005150 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005151 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005152
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005153 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005154 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005155 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5156 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005157 DiagID, CastTy))
5158 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005159 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005160 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005161
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005162 // If we get here, the operand to the typeof was an expresion.
5163 if (Operand.isInvalid()) {
5164 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005165 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005166 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005167
Eli Friedmane0afc982012-01-21 01:01:51 +00005168 // We might need to transform the operand if it is potentially evaluated.
5169 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5170 if (Operand.isInvalid()) {
5171 DS.SetTypeSpecError();
5172 return;
5173 }
5174
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005175 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005176 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005177 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5178 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005179 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005180 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005181}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005182
Benjamin Kramere56f3932011-12-23 17:00:35 +00005183/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005184/// _Atomic ( type-name )
5185///
5186void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5187 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5188
5189 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005190 BalancedDelimiterTracker T(*this, tok::l_paren);
5191 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedman0dfb8892011-10-06 23:00:33 +00005192 SkipUntil(tok::r_paren);
5193 return;
5194 }
5195
5196 TypeResult Result = ParseTypeName();
5197 if (Result.isInvalid()) {
5198 SkipUntil(tok::r_paren);
5199 return;
5200 }
5201
5202 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005203 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005204
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005205 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005206 return;
5207
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005208 DS.setTypeofParensRange(T.getRange());
5209 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005210
5211 const char *PrevSpec = 0;
5212 unsigned DiagID;
5213 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5214 DiagID, Result.release()))
5215 Diag(StartLoc, DiagID) << PrevSpec;
5216}
5217
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005218
5219/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5220/// from TryAltiVecVectorToken.
5221bool Parser::TryAltiVecVectorTokenOutOfLine() {
5222 Token Next = NextToken();
5223 switch (Next.getKind()) {
5224 default: return false;
5225 case tok::kw_short:
5226 case tok::kw_long:
5227 case tok::kw_signed:
5228 case tok::kw_unsigned:
5229 case tok::kw_void:
5230 case tok::kw_char:
5231 case tok::kw_int:
5232 case tok::kw_float:
5233 case tok::kw_double:
5234 case tok::kw_bool:
5235 case tok::kw___pixel:
5236 Tok.setKind(tok::kw___vector);
5237 return true;
5238 case tok::identifier:
5239 if (Next.getIdentifierInfo() == Ident_pixel) {
5240 Tok.setKind(tok::kw___vector);
5241 return true;
5242 }
5243 return false;
5244 }
5245}
5246
5247bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5248 const char *&PrevSpec, unsigned &DiagID,
5249 bool &isInvalid) {
5250 if (Tok.getIdentifierInfo() == Ident_vector) {
5251 Token Next = NextToken();
5252 switch (Next.getKind()) {
5253 case tok::kw_short:
5254 case tok::kw_long:
5255 case tok::kw_signed:
5256 case tok::kw_unsigned:
5257 case tok::kw_void:
5258 case tok::kw_char:
5259 case tok::kw_int:
5260 case tok::kw_float:
5261 case tok::kw_double:
5262 case tok::kw_bool:
5263 case tok::kw___pixel:
5264 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5265 return true;
5266 case tok::identifier:
5267 if (Next.getIdentifierInfo() == Ident_pixel) {
5268 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5269 return true;
5270 }
5271 break;
5272 default:
5273 break;
5274 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005275 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005276 DS.isTypeAltiVecVector()) {
5277 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5278 return true;
5279 }
5280 return false;
5281}