blob: 52bd4166bb89d28f9888384134da2ad542e6e3d5 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000017#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000018#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000024#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// C99 6.7: Declarations.
29//===----------------------------------------------------------------------===//
30
31/// ParseTypeName
32/// type-name: [C99 6.7.6]
33/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000034///
35/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000036TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000037 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000038 AccessSpecifier AS,
39 Decl **OwnedType) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000040 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000041 if (DSC == DSC_normal)
42 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000043
Reid Spencer5f016e22007-07-11 17:01:13 +000044 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000045 DeclSpec DS(AttrFactory);
Richard Smith7796eb52012-03-12 08:56:40 +000046 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000047 if (OwnedType)
48 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000049
Reid Spencer5f016e22007-07-11 17:01:13 +000050 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000051 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000052 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000053 if (Range)
54 *Range = DeclaratorInfo.getSourceRange();
55
Chris Lattnereaaebc72009-04-25 08:06:05 +000056 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000057 return true;
58
Douglas Gregor23c94db2010-07-02 17:43:08 +000059 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000060}
61
Caitlin Sadowskieff98fc2011-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
Sean Huntbbd37c62009-11-21 08:43:09 +000071/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +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
86/// attrib-name
87/// attrib-name '(' identifier ')'
88/// attrib-name '(' identifier ',' nonempty-expr-list ')'
89/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
90///
91/// [GNU] attrib-name:
92/// identifier
93/// typespec
94/// typequal
95/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000096///
Reid Spencer5f016e22007-07-11 17:01:13 +000097/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000100/// start with that identifier; otherwise they are an expression list."
101///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000102/// GCC does not require the ',' between attribs in an attribute-list.
103///
Reid Spencer5f016e22007-07-11 17:01:13 +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.
108
John McCall7f040a92010-12-24 02:08:15 +0000109void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000110 SourceLocation *endLoc,
111 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000112 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Chris Lattner04d66662007-10-09 17:33:22 +0000114 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 McCall7f040a92010-12-24 02:08:15 +0000119 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 }
121 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
122 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000123 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 }
125 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000126 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
127 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000128 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Stump1eb44332009-09-09 15:08:12 +0000136
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000137 if (Tok.is(tok::l_paren)) {
138 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000139 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000140 LateParsedAttribute *LA =
141 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
142 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000143
144 // Attributes in a class are parsed at the end of the class, along
145 // with other late-parsed declarations.
146 if (!ClassStack.empty())
147 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Caitlin Sadowskieff98fc2011-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 Stump1eb44332009-09-09 15:08:12 +0000151
Caitlin Sadowskieff98fc2011-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 Han6880f492012-10-03 01:56:22 +0000157 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000158 0, SourceLocation(), AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 }
160 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000161 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000162 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 }
164 }
165 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000167 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000168 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
169 SkipUntil(tok::r_paren, false);
170 }
John McCall7f040a92010-12-24 02:08:15 +0000171 if (endLoc)
172 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000174}
175
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000176
Michael Han6880f492012-10-03 01:56:22 +0000177/// Parse the arguments to a parameterized GNU attribute or
178/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000179void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
180 SourceLocation AttrNameLoc,
181 ParsedAttributes &Attrs,
Michael Han6880f492012-10-03 01:56:22 +0000182 SourceLocation *EndLoc,
183 IdentifierInfo *ScopeName,
184 SourceLocation ScopeLoc,
185 AttributeList::Syntax Syntax) {
Caitlin Sadowskieff98fc2011-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 Gribenko0d5a0692012-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 Sadowskieff98fc2011-09-08 17:42:22 +0000205
206 ConsumeParen(); // ignore the left paren loc for now
207
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000208 IdentifierInfo *ParmName = 0;
209 SourceLocation ParmLoc;
210 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000211
Richard Smithfe0a0fb2011-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 Smith5a5a9712012-04-04 06:24:32 +0000222 case tok::kw___int128:
Richard Smithfe0a0fb2011-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 Kramer4e28d9e2012-08-23 22:51:59 +0000244 ExprVector ArgExprs;
Richard Smithfe0a0fb2011-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 Sadowskieff98fc2011-09-08 17:42:22 +0000250 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000251
Richard Smithfe0a0fb2011-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 Sadowskieff98fc2011-09-08 17:42:22 +0000258 }
Richard Smithfe0a0fb2011-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 Sadowskieff98fc2011-09-08 17:42:22 +0000263 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000264 }
Fariborz Jahanian7a81e412011-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 Jahanianb2243432011-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 Jahanian7a81e412011-10-18 17:11:10 +0000279 SkipUntil(tok::r_paren, false, true); // skip until ')'
280 }
281 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000282
283 SourceLocation RParen = Tok.getLocation();
284 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han45bed132012-10-04 16:42:52 +0000285 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000286 AttributeList *attr =
Michael Han45bed132012-10-04 16:42:52 +0000287 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen),
Michael Han6880f492012-10-03 01:56:22 +0000288 ScopeName, ScopeLoc, ParmName, ParmLoc,
289 ArgExprs.data(), ArgExprs.size(), Syntax);
Sean Hunt8e083e72012-06-19 23:57:03 +0000290 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000291 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000292 }
293}
294
Chad Rosier8decdee2012-06-26 22:30:43 +0000295/// \brief Parses a single argument for a declspec, including the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000296/// surrounding parens.
Chad Rosier8decdee2012-06-26 22:30:43 +0000297void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000298 SourceLocation AttrNameLoc,
299 ParsedAttributes &Attrs)
300{
301 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000302 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000303 AttrName->getNameStart(), tok::r_paren))
304 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000305
Aaron Ballmanfc685ac2012-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 Rosier8decdee2012-06-26 22:30:43 +0000312 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000313 &ExprList, 1, AttributeList::AS_Declspec);
314
315 T.consumeClose();
316}
317
Chad Rosier8decdee2012-06-26 22:30:43 +0000318/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballmanfc685ac2012-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 Rosier8decdee2012-06-26 22:30:43 +0000339/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballmanfc685ac2012-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 Rosier8decdee2012-06-26 22:30:43 +0000342void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballmanfc685ac2012-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 Rosier8decdee2012-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 Ballmanfc685ac2012-06-19 22:09:27 +0000356 // not.
357 if (Tok.getKind() == tok::l_paren)
358 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
359 else
Chad Rosier8decdee2012-06-26 22:30:43 +0000360 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballmanfc685ac2012-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 Rosier8decdee2012-06-26 22:30:43 +0000364 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000365 // must be named get or put.
366 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000367 // For right now, we will just skip to the closing right paren of the
Aaron Ballmanfc685ac2012-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 Rosier8decdee2012-06-26 22:30:43 +0000374 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-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 Rosier8decdee2012-06-26 22:30:43 +0000383 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballmanfc685ac2012-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 Friedmana23b4852009-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 Ballmanfc685ac2012-06-19 22:09:27 +0000401void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000402 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000403
Steve Narofff59e17e2008-12-24 20:59:21 +0000404 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000405 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000406 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000407 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000408 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000409
Chad Rosier8decdee2012-06-26 22:30:43 +0000410 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-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 Rosier8decdee2012-06-26 22:30:43 +0000416 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-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 Olesen35329362012-06-19 21:48:43 +0000421 }
Aaron Ballmanfc685ac2012-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 Olesen35329362012-06-19 21:48:43 +0000432 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000433 AttrName = PP.getIdentifierInfo(Str);
434 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000435 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000436 AttrName = Tok.getIdentifierInfo();
437 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000438 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000439
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000440 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosier8decdee2012-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 Ballmanfc685ac2012-06-19 22:09:27 +0000443 // (for instance, SAL declspecs in older versions of MSVC).
444 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000445 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000446 // arguments and can be turned into an attribute directly.
Chad Rosier8decdee2012-06-26 22:30:43 +0000447 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000448 0, 0, AttributeList::AS_Declspec);
449 else
450 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000451 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000452 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000453}
454
John McCall7f040a92010-12-24 02:08:15 +0000455void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000456 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000457 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000458 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000459 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000460 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000461 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000462 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
463 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000464 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000465 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Eli Friedman290eeb02009-06-08 23:27:34 +0000466 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000467}
468
John McCall7f040a92010-12-24 02:08:15 +0000469void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-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 McCall0b7e6782011-03-24 11:26:52 +0000474 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000475 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000476 }
John McCall7f040a92010-12-24 02:08:15 +0000477}
478
Peter Collingbournef315fa82011-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 McCall0b7e6782011-03-24 11:26:52 +0000483 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
484 AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000485 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000486 }
487}
488
Peter Collingbourne207f4d82011-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 Rosier8decdee2012-06-26 22:30:43 +0000494 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000495 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000496 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000497 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000498 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000499
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000500 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000501 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000502 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000503 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000504 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000505
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000506 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000507 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000508 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000509 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000510 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000511
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000512 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000513 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000514 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000515 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000516 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000517
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000518 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000519 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000520 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000521 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000522 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000523
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000524 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000525 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000526 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000527 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000528 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000529
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000530 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000531 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000532 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000533 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000534 break;
535 default: break;
536 }
537}
538
Douglas Gregor0a0d2b12011-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 Noblesmithf7ccbad2012-02-05 02:13:05 +0000558 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-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 Rosier8decdee2012-06-26 22:30:43 +0000610
Douglas Gregor0a0d2b12011-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 Rosier8decdee2012-06-26 22:30:43 +0000617 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-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 Rosier8decdee2012-06-26 22:30:43 +0000624 return VersionTuple();
Douglas Gregor0a0d2b12011-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 Jahanian006e42f2011-12-10 00:28:41 +0000647/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-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 Gregor93a70672012-03-11 04:53:21 +0000659/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000660/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000661/// opt-message:
662/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-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 Jahanian006e42f2011-12-10 00:28:41 +0000672 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000673
674 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000675 BalancedDelimiterTracker T(*this, tok::l_paren);
676 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000677 Diag(Tok, diag::err_expected_lparen);
678 return;
679 }
Douglas Gregor0a0d2b12011-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 Gregorb53e4172011-03-26 03:35:55 +0000700 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000701 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000702 }
703
704 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000705 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-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 Gregorb53e4172011-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 Rosier8decdee2012-06-26 22:30:43 +0000719 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000720 UnavailableLoc = KeywordLoc;
721
722 if (Tok.isNot(tok::comma))
723 break;
724
725 ConsumeToken();
726 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000727 }
728
Douglas Gregor0a0d2b12011-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 Jahanian006e42f2011-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 Rosier8decdee2012-06-26 22:30:43 +0000745
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000746 SourceRange VersionRange;
747 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000748
Douglas Gregor0a0d2b12011-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 Rosier8decdee2012-06-26 22:30:43 +0000761 else
Douglas Gregor0a0d2b12011-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 Rosier8decdee2012-06-26 22:30:43 +0000767 << Keyword
Douglas Gregor0a0d2b12011-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 Gregor4a8dfb52011-10-12 16:37:45 +0000787 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000788 return;
789
790 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000791 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000792
Douglas Gregorb53e4172011-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 Gregor0a0d2b12011-03-23 00:50:03 +0000812 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000813 attrs.addNew(&Availability,
814 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000815 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000816 Platform, PlatformLoc,
817 Changes[Introduced],
818 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000819 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000820 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000821 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000822}
823
Caitlin Sadowskieff98fc2011-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 Hutchinsc24a2332012-02-16 16:50:43 +0000835 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-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 Gregorcefc3af2012-04-16 07:05:22 +0000849 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000850 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000851 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000852 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
853 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
854
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000855 // Enter the scope of nested classes
856 if (!AlreadyHasClassScope)
857 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
858 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000859 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000860 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
861 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
862 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000863 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000864
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000865 if (!AlreadyHasClassScope)
866 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
867 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000868}
869
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000870
871/// \brief Parse all attributes in LAs, and attach them to Decl D.
872void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
873 bool EnterScope, bool OnDefinition) {
874 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +0000875 if (D)
876 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000877 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000878 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000879 }
880 LAs.clear();
881}
882
883
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000884/// \brief Finish parsing an attribute for which parsing was delayed.
885/// This will be called at the end of parsing a class declaration
886/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +0000887/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000888/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000889void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
890 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000891 // Save the current token position.
892 SourceLocation OrigLoc = Tok.getLocation();
893
894 // Append the current token at the end of the new token stream so that it
895 // doesn't get lost.
896 LA.Toks.push_back(Tok);
897 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
898 // Consume the previously pushed token.
899 ConsumeAnyToken();
900
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000901 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
902 Diag(Tok, diag::warn_attribute_on_function_definition)
903 << LA.AttrName.getName();
904 }
905
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000906 ParsedAttributes Attrs(AttrFactory);
907 SourceLocation endLoc;
908
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000909 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000910 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000911 NamedDecl *ND = dyn_cast<NamedDecl>(D);
912 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000913
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000914 // Allow 'this' within late-parsed attributes.
915 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
916 /*TypeQuals=*/0,
917 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000918
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000919 if (LA.Decls.size() == 1) {
920 // If the Decl is templatized, add template parameters to scope.
921 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
922 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
923 if (HasTemplateScope)
924 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000925
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000926 // If the Decl is on a function, add function parameters to the scope.
927 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
928 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
929 if (HasFunScope)
930 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000931
Michael Han6880f492012-10-03 01:56:22 +0000932 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000933 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000934
935 if (HasFunScope) {
936 Actions.ActOnExitFunctionContext();
937 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
938 }
939 if (HasTemplateScope) {
940 TempScope.Exit();
941 }
942 } else {
943 // If there are multiple decls, then the decl cannot be within the
944 // function scope.
Michael Han6880f492012-10-03 01:56:22 +0000945 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000946 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000947 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000948 } else {
949 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000950 }
951
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000952 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
953 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
954 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000955
956 if (Tok.getLocation() != OrigLoc) {
957 // Due to a parsing error, we either went over the cached tokens or
958 // there are still cached tokens left, so we skip the leftover tokens.
959 // Since this is an uncommon situation that should be avoided, use the
960 // expensive isBeforeInTranslationUnit call.
961 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
962 OrigLoc))
963 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000964 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000965 }
966}
967
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000968/// \brief Wrapper around a case statement checking if AttrName is
969/// one of the thread safety attributes
970bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
971 return llvm::StringSwitch<bool>(AttrName)
972 .Case("guarded_by", true)
973 .Case("guarded_var", true)
974 .Case("pt_guarded_by", true)
975 .Case("pt_guarded_var", true)
976 .Case("lockable", true)
977 .Case("scoped_lockable", true)
978 .Case("no_thread_safety_analysis", true)
979 .Case("acquired_after", true)
980 .Case("acquired_before", true)
981 .Case("exclusive_lock_function", true)
982 .Case("shared_lock_function", true)
983 .Case("exclusive_trylock_function", true)
984 .Case("shared_trylock_function", true)
985 .Case("unlock_function", true)
986 .Case("lock_returned", true)
987 .Case("locks_excluded", true)
988 .Case("exclusive_locks_required", true)
989 .Case("shared_locks_required", true)
990 .Default(false);
991}
992
993/// \brief Parse the contents of thread safety attributes. These
994/// should always be parsed as an expression list.
995///
996/// We need to special case the parsing due to the fact that if the first token
997/// of the first argument is an identifier, the main parse loop will store
998/// that token as a "parameter" and the rest of
999/// the arguments will be added to a list of "arguments". However,
1000/// subsequent tokens in the first argument are lost. We instead parse each
1001/// argument as an expression and add all arguments to the list of "arguments".
1002/// In future, we will take advantage of this special case to also
1003/// deal with some argument scoping issues here (for example, referring to a
1004/// function parameter in the attribute on that function).
1005void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1006 SourceLocation AttrNameLoc,
1007 ParsedAttributes &Attrs,
1008 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001009 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001010
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001011 BalancedDelimiterTracker T(*this, tok::l_paren);
1012 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001013
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001014 ExprVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001015 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001016
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001017 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001018 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001019 ExprResult ArgExpr(ParseAssignmentExpression());
1020 if (ArgExpr.isInvalid()) {
1021 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001022 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001023 break;
1024 } else {
1025 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001026 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001027 if (Tok.isNot(tok::comma))
1028 break;
1029 ConsumeToken(); // Eat the comma, move to the next argument
1030 }
1031 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001032 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001033 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001034 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001035 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001036 if (EndLoc)
1037 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001038}
1039
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001040void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1041 SourceLocation AttrNameLoc,
1042 ParsedAttributes &Attrs,
1043 SourceLocation *EndLoc) {
1044 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1045
1046 BalancedDelimiterTracker T(*this, tok::l_paren);
1047 T.consumeOpen();
1048
1049 if (Tok.isNot(tok::identifier)) {
1050 Diag(Tok, diag::err_expected_ident);
1051 T.skipToEnd();
1052 return;
1053 }
1054 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1055 SourceLocation ArgumentKindLoc = ConsumeToken();
1056
1057 if (Tok.isNot(tok::comma)) {
1058 Diag(Tok, diag::err_expected_comma);
1059 T.skipToEnd();
1060 return;
1061 }
1062 ConsumeToken();
1063
1064 SourceRange MatchingCTypeRange;
1065 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1066 if (MatchingCType.isInvalid()) {
1067 T.skipToEnd();
1068 return;
1069 }
1070
1071 bool LayoutCompatible = false;
1072 bool MustBeNull = false;
1073 while (Tok.is(tok::comma)) {
1074 ConsumeToken();
1075 if (Tok.isNot(tok::identifier)) {
1076 Diag(Tok, diag::err_expected_ident);
1077 T.skipToEnd();
1078 return;
1079 }
1080 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1081 if (Flag->isStr("layout_compatible"))
1082 LayoutCompatible = true;
1083 else if (Flag->isStr("must_be_null"))
1084 MustBeNull = true;
1085 else {
1086 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1087 T.skipToEnd();
1088 return;
1089 }
1090 ConsumeToken(); // consume flag
1091 }
1092
1093 if (!T.consumeClose()) {
1094 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1095 ArgumentKind, ArgumentKindLoc,
1096 MatchingCType.release(), LayoutCompatible,
1097 MustBeNull, AttributeList::AS_GNU);
1098 }
1099
1100 if (EndLoc)
1101 *EndLoc = T.getCloseLocation();
1102}
1103
Richard Smith6ee326a2012-04-10 01:32:12 +00001104/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1105/// of a C++11 attribute-specifier in a location where an attribute is not
1106/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1107/// situation.
1108///
1109/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1110/// this doesn't appear to actually be an attribute-specifier, and the caller
1111/// should try to parse it.
1112bool Parser::DiagnoseProhibitedCXX11Attribute() {
1113 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1114
1115 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1116 case CAK_NotAttributeSpecifier:
1117 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1118 return false;
1119
1120 case CAK_InvalidAttributeSpecifier:
1121 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1122 return false;
1123
1124 case CAK_AttributeSpecifier:
1125 // Parse and discard the attributes.
1126 SourceLocation BeginLoc = ConsumeBracket();
1127 ConsumeBracket();
1128 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1129 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1130 SourceLocation EndLoc = ConsumeBracket();
1131 Diag(BeginLoc, diag::err_attributes_not_allowed)
1132 << SourceRange(BeginLoc, EndLoc);
1133 return true;
1134 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001135 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001136}
1137
John McCall7f040a92010-12-24 02:08:15 +00001138void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1139 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1140 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001141}
1142
Reid Spencer5f016e22007-07-11 17:01:13 +00001143/// ParseDeclaration - Parse a full 'declaration', which consists of
1144/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001145/// 'Context' should be a Declarator::TheContext value. This returns the
1146/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001147///
1148/// declaration: [C99 6.7]
1149/// block-declaration ->
1150/// simple-declaration
1151/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001152/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001153/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001154/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001155/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001156/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001157/// others... [FIXME]
1158///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001159Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1160 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001161 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001162 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001163 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001164 // Must temporarily exit the objective-c container scope for
1165 // parsing c none objective-c decls.
1166 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001167
John McCalld226f652010-08-21 09:40:31 +00001168 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001169 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001170 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001171 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001172 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001173 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001174 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001175 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001176 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001177 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001178 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001179 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001180 SourceLocation InlineLoc = ConsumeToken();
1181 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1182 break;
1183 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001184 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001185 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001186 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001187 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001188 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001189 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001190 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001191 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001192 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001193 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001194 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001195 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001196 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001197 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001198 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001199 default:
John McCall7f040a92010-12-24 02:08:15 +00001200 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001201 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001202
Chris Lattner682bf922009-03-29 16:50:03 +00001203 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001204 // single decl, convert it now. Alias declarations can also declare a type;
1205 // include that too if it is present.
1206 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001207}
1208
1209/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1210/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001211/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1212/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001213///[C90/C++]init-declarator-list ';' [TODO]
1214/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001215///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001216/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001217/// attribute-specifier-seq[opt] type-specifier-seq declarator
1218///
Chris Lattnercd147752009-03-29 17:27:48 +00001219/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001220/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001221///
1222/// If FRI is non-null, we might be parsing a for-range-declaration instead
1223/// of a simple-declaration. If we find that we are, we also parse the
1224/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001225Parser::DeclGroupPtrTy
1226Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1227 SourceLocation &DeclEnd,
1228 ParsedAttributesWithRange &attrs,
1229 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001231 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001232 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001233
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001234 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001235 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001236
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1238 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001239 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001240 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001241 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001242 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001243 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001244 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001245 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001247
1248 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001249}
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Richard Smith0706df42011-10-19 21:33:05 +00001251/// Returns true if this might be the start of a declarator, or a common typo
1252/// for a declarator.
1253bool Parser::MightBeDeclarator(unsigned Context) {
1254 switch (Tok.getKind()) {
1255 case tok::annot_cxxscope:
1256 case tok::annot_template_id:
1257 case tok::caret:
1258 case tok::code_completion:
1259 case tok::coloncolon:
1260 case tok::ellipsis:
1261 case tok::kw___attribute:
1262 case tok::kw_operator:
1263 case tok::l_paren:
1264 case tok::star:
1265 return true;
1266
1267 case tok::amp:
1268 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001269 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001270
Richard Smith1c94c162012-01-09 22:31:44 +00001271 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001272 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001273 NextToken().is(tok::l_square);
1274
1275 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001276 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001277
Richard Smith0706df42011-10-19 21:33:05 +00001278 case tok::identifier:
1279 switch (NextToken().getKind()) {
1280 case tok::code_completion:
1281 case tok::coloncolon:
1282 case tok::comma:
1283 case tok::equal:
1284 case tok::equalequal: // Might be a typo for '='.
1285 case tok::kw_alignas:
1286 case tok::kw_asm:
1287 case tok::kw___attribute:
1288 case tok::l_brace:
1289 case tok::l_paren:
1290 case tok::l_square:
1291 case tok::less:
1292 case tok::r_brace:
1293 case tok::r_paren:
1294 case tok::r_square:
1295 case tok::semi:
1296 return true;
1297
1298 case tok::colon:
1299 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001300 // and in block scope it's probably a label. Inside a class definition,
1301 // this is a bit-field.
1302 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001303 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001304
1305 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001306 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001307
1308 default:
1309 return false;
1310 }
1311
1312 default:
1313 return false;
1314 }
1315}
1316
Richard Smith994d73f2012-04-11 20:59:20 +00001317/// Skip until we reach something which seems like a sensible place to pick
1318/// up parsing after a malformed declaration. This will sometimes stop sooner
1319/// than SkipUntil(tok::r_brace) would, but will never stop later.
1320void Parser::SkipMalformedDecl() {
1321 while (true) {
1322 switch (Tok.getKind()) {
1323 case tok::l_brace:
1324 // Skip until matching }, then stop. We've probably skipped over
1325 // a malformed class or function definition or similar.
1326 ConsumeBrace();
1327 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1328 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1329 // This declaration isn't over yet. Keep skipping.
1330 continue;
1331 }
1332 if (Tok.is(tok::semi))
1333 ConsumeToken();
1334 return;
1335
1336 case tok::l_square:
1337 ConsumeBracket();
1338 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1339 continue;
1340
1341 case tok::l_paren:
1342 ConsumeParen();
1343 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1344 continue;
1345
1346 case tok::r_brace:
1347 return;
1348
1349 case tok::semi:
1350 ConsumeToken();
1351 return;
1352
1353 case tok::kw_inline:
1354 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001355 // a good place to pick back up parsing, except in an Objective-C
1356 // @interface context.
1357 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1358 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001359 return;
1360 break;
1361
1362 case tok::kw_namespace:
1363 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001364 // place to pick back up parsing, except in an Objective-C
1365 // @interface context.
1366 if (Tok.isAtStartOfLine() &&
1367 (!ParsingInObjCContainer || CurParsedObjCImpl))
1368 return;
1369 break;
1370
1371 case tok::at:
1372 // @end is very much like } in Objective-C contexts.
1373 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1374 ParsingInObjCContainer)
1375 return;
1376 break;
1377
1378 case tok::minus:
1379 case tok::plus:
1380 // - and + probably start new method declarations in Objective-C contexts.
1381 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001382 return;
1383 break;
1384
1385 case tok::eof:
1386 return;
1387
1388 default:
1389 break;
1390 }
1391
1392 ConsumeAnyToken();
1393 }
1394}
1395
John McCalld8ac0572009-11-03 19:26:08 +00001396/// ParseDeclGroup - Having concluded that this is either a function
1397/// definition or a group of object declarations, actually parse the
1398/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001399Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1400 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001401 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001402 SourceLocation *DeclEnd,
1403 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001404 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001405 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001406 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001407
John McCalld8ac0572009-11-03 19:26:08 +00001408 // Bail out if the first declarator didn't seem well-formed.
1409 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001410 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001411 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001412 }
Mike Stump1eb44332009-09-09 15:08:12 +00001413
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001414 // Save late-parsed attributes for now; they need to be parsed in the
1415 // appropriate function scope after the function Decl has been constructed.
1416 LateParsedAttrList LateParsedAttrs;
1417 if (D.isFunctionDeclarator())
1418 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1419
Chris Lattnerc82daef2010-07-11 22:24:20 +00001420 // Check to see if we have a function *definition* which must have a body.
1421 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1422 // Look at the next token to make sure that this isn't a function
1423 // declaration. We have to check this because __attribute__ might be the
1424 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001425 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001426
Chris Lattner004659a2010-07-11 22:42:07 +00001427 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001428 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1429 Diag(Tok, diag::err_function_declared_typedef);
1430
1431 // Recover by treating the 'typedef' as spurious.
1432 DS.ClearStorageClassSpecs();
1433 }
1434
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001435 Decl *TheDecl =
1436 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001437 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001438 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001439
Chris Lattner004659a2010-07-11 22:42:07 +00001440 if (isDeclarationSpecifier()) {
1441 // If there is an invalid declaration specifier right after the function
1442 // prototype, then we must be in a missing semicolon case where this isn't
1443 // actually a body. Just fall through into the code that handles it as a
1444 // prototype, and let the top-level code handle the erroneous declspec
1445 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001446 } else {
1447 Diag(Tok, diag::err_expected_fn_body);
1448 SkipUntil(tok::semi);
1449 return DeclGroupPtrTy();
1450 }
1451 }
1452
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001453 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001454 return DeclGroupPtrTy();
1455
1456 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1457 // must parse and analyze the for-range-initializer before the declaration is
1458 // analyzed.
1459 if (FRI && Tok.is(tok::colon)) {
1460 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001461 if (Tok.is(tok::l_brace))
1462 FRI->RangeExpr = ParseBraceInitializer();
1463 else
1464 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001465 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1466 Actions.ActOnCXXForRangeDecl(ThisDecl);
1467 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001468 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001469 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1470 }
1471
Chris Lattner5f9e2722011-07-23 10:55:15 +00001472 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001473 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001474 if (LateParsedAttrs.size() > 0)
1475 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001476 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001477 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001478 DeclsInGroup.push_back(FirstDecl);
1479
Richard Smith0706df42011-10-19 21:33:05 +00001480 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001481
John McCalld8ac0572009-11-03 19:26:08 +00001482 // If we don't have a comma, it is either the end of the list (a ';') or an
1483 // error, bail out.
1484 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001485 SourceLocation CommaLoc = ConsumeToken();
1486
1487 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1488 // This comma was followed by a line-break and something which can't be
1489 // the start of a declarator. The comma was probably a typo for a
1490 // semicolon.
1491 Diag(CommaLoc, diag::err_expected_semi_declaration)
1492 << FixItHint::CreateReplacement(CommaLoc, ";");
1493 ExpectSemi = false;
1494 break;
1495 }
John McCalld8ac0572009-11-03 19:26:08 +00001496
1497 // Parse the next declarator.
1498 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001499 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001500
1501 // Accept attributes in an init-declarator. In the first declarator in a
1502 // declaration, these would be part of the declspec. In subsequent
1503 // declarators, they become part of the declarator itself, so that they
1504 // don't apply to declarators after *this* one. Examples:
1505 // short __attribute__((common)) var; -> declspec
1506 // short var __attribute__((common)); -> declarator
1507 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001508 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001509
1510 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001511 if (!D.isInvalidType()) {
1512 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1513 D.complete(ThisDecl);
1514 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001515 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001516 }
John McCalld8ac0572009-11-03 19:26:08 +00001517 }
1518
1519 if (DeclEnd)
1520 *DeclEnd = Tok.getLocation();
1521
Richard Smith0706df42011-10-19 21:33:05 +00001522 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001523 ExpectAndConsumeSemi(Context == Declarator::FileContext
1524 ? diag::err_invalid_token_after_toplevel_declarator
1525 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001526 // Okay, there was no semicolon and one was expected. If we see a
1527 // declaration specifier, just assume it was missing and continue parsing.
1528 // Otherwise things are very confused and we skip to recover.
1529 if (!isDeclarationSpecifier()) {
1530 SkipUntil(tok::r_brace, true, true);
1531 if (Tok.is(tok::semi))
1532 ConsumeToken();
1533 }
John McCalld8ac0572009-11-03 19:26:08 +00001534 }
1535
Douglas Gregor23c94db2010-07-02 17:43:08 +00001536 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001537 DeclsInGroup.data(),
1538 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001539}
1540
Richard Smithad762fc2011-04-14 22:09:26 +00001541/// Parse an optional simple-asm-expr and attributes, and attach them to a
1542/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001543bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001544 // If a simple-asm-expr is present, parse it.
1545 if (Tok.is(tok::kw_asm)) {
1546 SourceLocation Loc;
1547 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1548 if (AsmLabel.isInvalid()) {
1549 SkipUntil(tok::semi, true, true);
1550 return true;
1551 }
1552
1553 D.setAsmLabel(AsmLabel.release());
1554 D.SetRangeEnd(Loc);
1555 }
1556
1557 MaybeParseGNUAttributes(D);
1558 return false;
1559}
1560
Douglas Gregor1426e532009-05-12 21:31:51 +00001561/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1562/// declarator'. This method parses the remainder of the declaration
1563/// (including any attributes or initializer, among other things) and
1564/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001565///
Reid Spencer5f016e22007-07-11 17:01:13 +00001566/// init-declarator: [C99 6.7]
1567/// declarator
1568/// declarator '=' initializer
1569/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1570/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001571/// [C++] declarator initializer[opt]
1572///
1573/// [C++] initializer:
1574/// [C++] '=' initializer-clause
1575/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001576/// [C++0x] '=' 'default' [TODO]
1577/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001578/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001579///
1580/// According to the standard grammar, =default and =delete are function
1581/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001582///
John McCalld226f652010-08-21 09:40:31 +00001583Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001584 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001585 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001586 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001587
Richard Smithad762fc2011-04-14 22:09:26 +00001588 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1589}
Mike Stump1eb44332009-09-09 15:08:12 +00001590
Richard Smithad762fc2011-04-14 22:09:26 +00001591Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1592 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001593 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001594 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001595 switch (TemplateInfo.Kind) {
1596 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001597 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001598 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001599
Douglas Gregord5a423b2009-09-25 18:43:00 +00001600 case ParsedTemplateInfo::Template:
1601 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001602 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001603 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001604 D);
1605 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001606
Douglas Gregord5a423b2009-09-25 18:43:00 +00001607 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001608 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001609 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001610 TemplateInfo.ExternLoc,
1611 TemplateInfo.TemplateLoc,
1612 D);
1613 if (ThisRes.isInvalid()) {
1614 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001615 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001616 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001617
Douglas Gregord5a423b2009-09-25 18:43:00 +00001618 ThisDecl = ThisRes.get();
1619 break;
1620 }
1621 }
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Richard Smith34b41d92011-02-20 03:19:35 +00001623 bool TypeContainsAuto =
1624 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1625
Douglas Gregor1426e532009-05-12 21:31:51 +00001626 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001627 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001628 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001629 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001630 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001631 if (D.isFunctionDeclarator())
1632 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1633 << 1 /* delete */;
1634 else
1635 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001636 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001637 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001638 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1639 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001640 else
1641 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001642 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001643 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001644 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001645 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001646 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001647
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001648 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001649 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001650 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001651 cutOffParsing();
1652 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001653 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001654
John McCall60d7b3a2010-08-24 06:29:42 +00001655 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001656
David Blaikie4e4d0842012-03-11 07:00:24 +00001657 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001658 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001659 ExitScope();
1660 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001661
Douglas Gregor1426e532009-05-12 21:31:51 +00001662 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001663 SkipUntil(tok::comma, true, true);
1664 Actions.ActOnInitializerError(ThisDecl);
1665 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001666 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1667 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001668 }
1669 } else if (Tok.is(tok::l_paren)) {
1670 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001671 BalancedDelimiterTracker T(*this, tok::l_paren);
1672 T.consumeOpen();
1673
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001674 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001675 CommaLocsTy CommaLocs;
1676
David Blaikie4e4d0842012-03-11 07:00:24 +00001677 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001678 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001679 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001680 }
1681
Douglas Gregor1426e532009-05-12 21:31:51 +00001682 if (ParseExpressionList(Exprs, CommaLocs)) {
1683 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001684
David Blaikie4e4d0842012-03-11 07:00:24 +00001685 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001686 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001687 ExitScope();
1688 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001689 } else {
1690 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001691 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001692
1693 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1694 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001695
David Blaikie4e4d0842012-03-11 07:00:24 +00001696 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001697 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001698 ExitScope();
1699 }
1700
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001701 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1702 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001703 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001704 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1705 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001706 }
Fariborz Jahanian3b5f9dc2012-07-03 22:54:28 +00001707 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001708 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001709 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001710 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1711
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001712 if (D.getCXXScopeSpec().isSet()) {
1713 EnterScope(0);
1714 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1715 }
1716
1717 ExprResult Init(ParseBraceInitializer());
1718
1719 if (D.getCXXScopeSpec().isSet()) {
1720 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1721 ExitScope();
1722 }
1723
1724 if (Init.isInvalid()) {
1725 Actions.ActOnInitializerError(ThisDecl);
1726 } else
1727 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1728 /*DirectInit=*/true, TypeContainsAuto);
1729
Douglas Gregor1426e532009-05-12 21:31:51 +00001730 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001731 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001732 }
1733
Richard Smith483b9f32011-02-21 20:05:19 +00001734 Actions.FinalizeDeclaration(ThisDecl);
1735
Douglas Gregor1426e532009-05-12 21:31:51 +00001736 return ThisDecl;
1737}
1738
Reid Spencer5f016e22007-07-11 17:01:13 +00001739/// ParseSpecifierQualifierList
1740/// specifier-qualifier-list:
1741/// type-specifier specifier-qualifier-list[opt]
1742/// type-qualifier specifier-qualifier-list[opt]
1743/// [GNU] attributes specifier-qualifier-list[opt]
1744///
Richard Smith69730c12012-03-12 07:56:15 +00001745void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1746 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001747 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1748 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001749 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001750 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001751
Reid Spencer5f016e22007-07-11 17:01:13 +00001752 // Validate declspec for type-name.
1753 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001754 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1755 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001756 Diag(Tok, diag::err_expected_type);
1757 DS.SetTypeSpecError();
1758 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1759 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001760 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001761 if (!DS.hasTypeSpecifier())
1762 DS.SetTypeSpecError();
1763 }
Mike Stump1eb44332009-09-09 15:08:12 +00001764
Reid Spencer5f016e22007-07-11 17:01:13 +00001765 // Issue diagnostic and remove storage class if present.
1766 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1767 if (DS.getStorageClassSpecLoc().isValid())
1768 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1769 else
1770 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1771 DS.ClearStorageClassSpecs();
1772 }
Mike Stump1eb44332009-09-09 15:08:12 +00001773
Reid Spencer5f016e22007-07-11 17:01:13 +00001774 // Issue diagnostic and remove function specfier if present.
1775 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001776 if (DS.isInlineSpecified())
1777 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1778 if (DS.isVirtualSpecified())
1779 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1780 if (DS.isExplicitSpecified())
1781 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001782 DS.ClearFunctionSpecs();
1783 }
Richard Smith69730c12012-03-12 07:56:15 +00001784
1785 // Issue diagnostic and remove constexpr specfier if present.
1786 if (DS.isConstexprSpecified()) {
1787 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1788 DS.ClearConstexprSpec();
1789 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001790}
1791
Chris Lattnerc199ab32009-04-12 20:42:31 +00001792/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1793/// specified token is valid after the identifier in a declarator which
1794/// immediately follows the declspec. For example, these things are valid:
1795///
1796/// int x [ 4]; // direct-declarator
1797/// int x ( int y); // direct-declarator
1798/// int(int x ) // direct-declarator
1799/// int x ; // simple-declaration
1800/// int x = 17; // init-declarator-list
1801/// int x , y; // init-declarator-list
1802/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001803/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001804/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001805///
1806/// This is not, because 'x' does not immediately follow the declspec (though
1807/// ')' happens to be valid anyway).
1808/// int (x)
1809///
1810static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1811 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1812 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001813 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001814}
1815
Chris Lattnere40c2952009-04-14 21:34:55 +00001816
1817/// ParseImplicitInt - This method is called when we have an non-typename
1818/// identifier in a declspec (which normally terminates the decl spec) when
1819/// the declspec has no type specifier. In this case, the declspec is either
1820/// malformed or is "implicit int" (in K&R and C89).
1821///
1822/// This method handles diagnosing this prettily and returns false if the
1823/// declspec is done being processed. If it recovers and thinks there may be
1824/// other pieces of declspec after it, it returns true.
1825///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001826bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001827 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001828 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001829 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001830
Chris Lattnere40c2952009-04-14 21:34:55 +00001831 SourceLocation Loc = Tok.getLocation();
1832 // If we see an identifier that is not a type name, we normally would
1833 // parse it as the identifer being declared. However, when a typename
1834 // is typo'd or the definition is not included, this will incorrectly
1835 // parse the typename as the identifier name and fall over misparsing
1836 // later parts of the diagnostic.
1837 //
1838 // As such, we try to do some look-ahead in cases where this would
1839 // otherwise be an "implicit-int" case to see if this is invalid. For
1840 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1841 // an identifier with implicit int, we'd get a parse error because the
1842 // next token is obviously invalid for a type. Parse these as a case
1843 // with an invalid type specifier.
1844 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001845
Chris Lattnere40c2952009-04-14 21:34:55 +00001846 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001847 // error, do lookahead to try to do better recovery. This never applies
1848 // within a type specifier. Outside of C++, we allow this even if the
1849 // language doesn't "officially" support implicit int -- we support
1850 // implicit int as an extension in C99 and C11. Allegedly, MS also
1851 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001852 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001853 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001854 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001855 // If this token is valid for implicit int, e.g. "static x = 4", then
1856 // we just avoid eating the identifier, so it will be parsed as the
1857 // identifier in the declarator.
1858 return false;
1859 }
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Richard Smith827adaf2012-05-15 21:01:51 +00001861 if (getLangOpts().CPlusPlus &&
1862 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1863 // Don't require a type specifier if we have the 'auto' storage class
1864 // specifier in C++98 -- we'll promote it to a type specifier.
1865 return false;
1866 }
1867
Chris Lattnere40c2952009-04-14 21:34:55 +00001868 // Otherwise, if we don't consume this token, we are going to emit an
1869 // error anyway. Try to recover from various common problems. Check
1870 // to see if this was a reference to a tag name without a tag specified.
1871 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001872 //
1873 // C++ doesn't need this, and isTagName doesn't take SS.
1874 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001875 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001876 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001877
Douglas Gregor23c94db2010-07-02 17:43:08 +00001878 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001879 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001880 case DeclSpec::TST_enum:
1881 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1882 case DeclSpec::TST_union:
1883 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1884 case DeclSpec::TST_struct:
1885 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00001886 case DeclSpec::TST_interface:
1887 TagName="__interface"; FixitTagName = "__interface ";
1888 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001889 case DeclSpec::TST_class:
1890 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001891 }
Mike Stump1eb44332009-09-09 15:08:12 +00001892
Chris Lattnerf4382f52009-04-14 22:17:06 +00001893 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001894 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1895 LookupResult R(Actions, TokenName, SourceLocation(),
1896 Sema::LookupOrdinaryName);
1897
Chris Lattnerf4382f52009-04-14 22:17:06 +00001898 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001899 << TokenName << TagName << getLangOpts().CPlusPlus
1900 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1901
1902 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1903 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1904 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001905 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001906 << TokenName << TagName;
1907 }
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Chris Lattnerf4382f52009-04-14 22:17:06 +00001909 // Parse this as a tag as if the missing tag were present.
1910 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001911 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001912 else
Richard Smith69730c12012-03-12 07:56:15 +00001913 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1914 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001915 return true;
1916 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001917 }
Mike Stump1eb44332009-09-09 15:08:12 +00001918
Richard Smith8f0a7e72012-05-15 21:29:55 +00001919 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001920 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001921 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1922 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001923 // Look ahead to the next token to try to figure out what this declaration
1924 // was supposed to be.
1925 switch (NextToken().getKind()) {
1926 case tok::comma:
1927 case tok::equal:
1928 case tok::kw_asm:
1929 case tok::l_brace:
1930 case tok::l_square:
1931 case tok::semi:
1932 // This looks like a variable declaration. The type is probably missing.
1933 // We're done parsing decl-specifiers.
1934 return false;
1935
1936 case tok::l_paren: {
1937 // static x(4); // 'x' is not a type
1938 // x(int n); // 'x' is not a type
1939 // x (*p)[]; // 'x' is a type
1940 //
1941 // Since we're in an error case (or the rare 'implicit int in C++' MS
1942 // extension), we can afford to perform a tentative parse to determine
1943 // which case we're in.
1944 TentativeParsingAction PA(*this);
1945 ConsumeToken();
1946 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1947 PA.Revert();
1948 if (TPR == TPResult::False())
1949 return false;
1950 // The identifier is followed by a parenthesized declarator.
1951 // It's supposed to be a type.
1952 break;
1953 }
1954
1955 default:
1956 // This is probably supposed to be a type. This includes cases like:
1957 // int f(itn);
1958 // struct S { unsinged : 4; };
1959 break;
1960 }
1961 }
1962
Chad Rosier8decdee2012-06-26 22:30:43 +00001963 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00001964 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001965 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001966 IdentifierInfo *II = Tok.getIdentifierInfo();
1967 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001968 // The action emitted a diagnostic, so we don't have to.
1969 if (T) {
1970 // The action has suggested that the type T could be used. Set that as
1971 // the type in the declaration specifiers, consume the would-be type
1972 // name token, and we're done.
1973 const char *PrevSpec;
1974 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001975 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001976 DS.SetRangeEnd(Tok.getLocation());
1977 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001978 // There may be other declaration specifiers after this.
1979 return true;
1980 } else if (II != Tok.getIdentifierInfo()) {
1981 // If no type was suggested, the correction is to a keyword
1982 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00001983 // There may be other declaration specifiers after this.
1984 return true;
1985 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001986
Douglas Gregora786fdb2009-10-13 23:27:22 +00001987 // Fall through; the action had no suggestion for us.
1988 } else {
1989 // The action did not emit a diagnostic, so emit one now.
1990 SourceRange R;
1991 if (SS) R = SS->getRange();
1992 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1993 }
Mike Stump1eb44332009-09-09 15:08:12 +00001994
Douglas Gregora786fdb2009-10-13 23:27:22 +00001995 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001996 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001997 DS.SetRangeEnd(Tok.getLocation());
1998 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001999
Chris Lattnere40c2952009-04-14 21:34:55 +00002000 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2001 // avoid rippling error messages on subsequent uses of the same type,
2002 // could be useful if #include was forgotten.
2003 return false;
2004}
2005
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002006/// \brief Determine the declaration specifier context from the declarator
2007/// context.
2008///
2009/// \param Context the declarator context, which is one of the
2010/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002011Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002012Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2013 if (Context == Declarator::MemberContext)
2014 return DSC_class;
2015 if (Context == Declarator::FileContext)
2016 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002017 if (Context == Declarator::TrailingReturnContext)
2018 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002019 return DSC_normal;
2020}
2021
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002022/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2023///
2024/// FIXME: Simply returns an alignof() expression if the argument is a
2025/// type. Ideally, the type should be propagated directly into Sema.
2026///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002027/// [C11] type-id
2028/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002029/// [C++0x] type-id ...[opt]
2030/// [C++0x] assignment-expression ...[opt]
2031ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2032 SourceLocation &EllipsisLoc) {
2033 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002034 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002035 SourceLocation TypeLoc = Tok.getLocation();
2036 ParsedType Ty = ParseTypeName().get();
2037 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002038 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2039 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002040 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002041 ER = ParseConstantExpression();
2042
David Blaikie4e4d0842012-03-11 07:00:24 +00002043 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002044 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002045
2046 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002047}
2048
2049/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2050/// attribute to Attrs.
2051///
2052/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002053/// [C11] '_Alignas' '(' type-id ')'
2054/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002055/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
2056/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002057void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2058 SourceLocation *endLoc) {
2059 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2060 "Not an alignment-specifier!");
2061
2062 SourceLocation KWLoc = Tok.getLocation();
2063 ConsumeToken();
2064
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002065 BalancedDelimiterTracker T(*this, tok::l_paren);
2066 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002067 return;
2068
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002069 SourceLocation EllipsisLoc;
2070 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002071 if (ArgExpr.isInvalid()) {
2072 SkipUntil(tok::r_paren);
2073 return;
2074 }
2075
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002076 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002077 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002078 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002079
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002080 // FIXME: Handle pack-expansions here.
2081 if (EllipsisLoc.isValid()) {
2082 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2083 return;
2084 }
2085
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002086 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002087 ArgExprs.push_back(ArgExpr.release());
Sean Hunt8e083e72012-06-19 23:57:03 +00002088 // FIXME: This should not be GNU, but we since the attribute used is
2089 // based on the spelling, and there is no true spelling for
2090 // C++11 attributes, this isn't accepted.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002091 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002092 0, T.getOpenLocation(), ArgExprs.data(), 1,
Sean Hunt8e083e72012-06-19 23:57:03 +00002093 AttributeList::AS_GNU);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002094}
2095
Reid Spencer5f016e22007-07-11 17:01:13 +00002096/// ParseDeclarationSpecifiers
2097/// declaration-specifiers: [C99 6.7]
2098/// storage-class-specifier declaration-specifiers[opt]
2099/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002100/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002101/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002102/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002103/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002104///
2105/// storage-class-specifier: [C99 6.7.1]
2106/// 'typedef'
2107/// 'extern'
2108/// 'static'
2109/// 'auto'
2110/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002111/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002112/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002113/// function-specifier: [C99 6.7.4]
2114/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002115/// [C++] 'virtual'
2116/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002117/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002118/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002119/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002120
Reid Spencer5f016e22007-07-11 17:01:13 +00002121///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002122void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002123 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002124 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002125 DeclSpecContext DSContext,
2126 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002127 if (DS.getSourceRange().isInvalid()) {
2128 DS.SetRangeStart(Tok.getLocation());
2129 DS.SetRangeEnd(Tok.getLocation());
2130 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002131
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002132 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002133 bool AttrsLastTime = false;
2134 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002135 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002136 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002137 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002138 unsigned DiagID = 0;
2139
Reid Spencer5f016e22007-07-11 17:01:13 +00002140 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002141
Reid Spencer5f016e22007-07-11 17:01:13 +00002142 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002143 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002144 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002145 if (!AttrsLastTime)
2146 ProhibitAttributes(attrs);
2147 else
2148 DS.takeAttributesFrom(attrs);
Peter Collingbournef1907682011-09-29 18:03:57 +00002149
Reid Spencer5f016e22007-07-11 17:01:13 +00002150 // If this is not a declaration specifier token, we're done reading decl
2151 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002152 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002153 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002154
Sean Hunt2edf0a22012-06-23 05:07:58 +00002155 case tok::l_square:
2156 case tok::kw_alignas:
2157 if (!isCXX11AttributeSpecifier())
2158 goto DoneWithDeclSpec;
2159
2160 ProhibitAttributes(attrs);
2161 // FIXME: It would be good to recover by accepting the attributes,
2162 // but attempting to do that now would cause serious
2163 // madness in terms of diagnostics.
2164 attrs.clear();
2165 attrs.Range = SourceRange();
2166
2167 ParseCXX11Attributes(attrs);
2168 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002169 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002170
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002171 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002172 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002173 if (DS.hasTypeSpecifier()) {
2174 bool AllowNonIdentifiers
2175 = (getCurScope()->getFlags() & (Scope::ControlScope |
2176 Scope::BlockScope |
2177 Scope::TemplateParamScope |
2178 Scope::FunctionPrototypeScope |
2179 Scope::AtCatchScope)) == 0;
2180 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002181 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002182 (DSContext == DSC_class && DS.isFriendSpecified());
2183
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002184 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002185 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002186 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002187 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002188 }
2189
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002190 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2191 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2192 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002193 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002194 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002195 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002196 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002197 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002198 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002199
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002200 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002201 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002202 }
2203
Chris Lattner5e02c472009-01-05 00:07:25 +00002204 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002205 // C++ scope specifier. Annotate and loop, or bail out on error.
2206 if (TryAnnotateCXXScopeToken(true)) {
2207 if (!DS.hasTypeSpecifier())
2208 DS.SetTypeSpecError();
2209 goto DoneWithDeclSpec;
2210 }
John McCall2e0a7152010-03-01 18:20:46 +00002211 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2212 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002213 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002214
2215 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002216 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002217 goto DoneWithDeclSpec;
2218
John McCallaa87d332009-12-12 11:40:51 +00002219 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002220 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2221 Tok.getAnnotationRange(),
2222 SS);
John McCallaa87d332009-12-12 11:40:51 +00002223
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002224 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002225 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002226 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002227 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002228 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002229 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002230
2231 // C++ [class.qual]p2:
2232 // In a lookup in which the constructor is an acceptable lookup
2233 // result and the nested-name-specifier nominates a class C:
2234 //
2235 // - if the name specified after the
2236 // nested-name-specifier, when looked up in C, is the
2237 // injected-class-name of C (Clause 9), or
2238 //
2239 // - if the name specified after the nested-name-specifier
2240 // is the same as the identifier or the
2241 // simple-template-id's template-name in the last
2242 // component of the nested-name-specifier,
2243 //
2244 // the name is instead considered to name the constructor of
2245 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002246 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002247 // Thus, if the template-name is actually the constructor
2248 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002249 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002250 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002251 if ((DSContext == DSC_top_level ||
2252 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2253 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002254 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002255 if (isConstructorDeclarator()) {
2256 // The user meant this to be an out-of-line constructor
2257 // definition, but template arguments are not allowed
2258 // there. Just allow this as a constructor; we'll
2259 // complain about it later.
2260 goto DoneWithDeclSpec;
2261 }
2262
2263 // The user meant this to name a type, but it actually names
2264 // a constructor with some extraneous template
2265 // arguments. Complain, then parse it as a type as the user
2266 // intended.
2267 Diag(TemplateId->TemplateNameLoc,
2268 diag::err_out_of_line_template_id_names_constructor)
2269 << TemplateId->Name;
2270 }
2271
John McCallaa87d332009-12-12 11:40:51 +00002272 DS.getTypeSpecScope() = SS;
2273 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002274 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002275 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002276 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002277 continue;
2278 }
2279
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002280 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002281 DS.getTypeSpecScope() = SS;
2282 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002283 if (Tok.getAnnotationValue()) {
2284 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002285 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002286 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002287 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002288 if (isInvalid)
2289 break;
John McCallb3d87482010-08-24 05:47:05 +00002290 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002291 else
2292 DS.SetTypeSpecError();
2293 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2294 ConsumeToken(); // The typename
2295 }
2296
Douglas Gregor9135c722009-03-25 15:40:00 +00002297 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002298 goto DoneWithDeclSpec;
2299
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002300 // If we're in a context where the identifier could be a class name,
2301 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002302 if ((DSContext == DSC_top_level ||
2303 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002304 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002305 &SS)) {
2306 if (isConstructorDeclarator())
2307 goto DoneWithDeclSpec;
2308
2309 // As noted in C++ [class.qual]p2 (cited above), when the name
2310 // of the class is qualified in a context where it could name
2311 // a constructor, its a constructor name. However, we've
2312 // looked at the declarator, and the user probably meant this
2313 // to be a type. Complain that it isn't supposed to be treated
2314 // as a type, then proceed to parse it as a type.
2315 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2316 << Next.getIdentifierInfo();
2317 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002318
John McCallb3d87482010-08-24 05:47:05 +00002319 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2320 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002321 getCurScope(), &SS,
2322 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002323 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002324 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002325
Chris Lattnerf4382f52009-04-14 22:17:06 +00002326 // If the referenced identifier is not a type, then this declspec is
2327 // erroneous: We already checked about that it has no type specifier, and
2328 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002329 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002330 if (TypeRep == 0) {
2331 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002332 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002333 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002334 }
Mike Stump1eb44332009-09-09 15:08:12 +00002335
John McCallaa87d332009-12-12 11:40:51 +00002336 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002337 ConsumeToken(); // The C++ scope.
2338
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002339 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002340 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002341 if (isInvalid)
2342 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002343
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002344 DS.SetRangeEnd(Tok.getLocation());
2345 ConsumeToken(); // The typename.
2346
2347 continue;
2348 }
Mike Stump1eb44332009-09-09 15:08:12 +00002349
Chris Lattner80d0c892009-01-21 19:48:37 +00002350 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002351 if (Tok.getAnnotationValue()) {
2352 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002353 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002354 DiagID, T);
2355 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002356 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002357
Chris Lattner5c5db552010-04-05 18:18:31 +00002358 if (isInvalid)
2359 break;
2360
Chris Lattner80d0c892009-01-21 19:48:37 +00002361 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2362 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002363
Chris Lattner80d0c892009-01-21 19:48:37 +00002364 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2365 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002366 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002367 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002368 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002369
Chris Lattner80d0c892009-01-21 19:48:37 +00002370 continue;
2371 }
Mike Stump1eb44332009-09-09 15:08:12 +00002372
Douglas Gregorbfad9152011-04-28 15:48:45 +00002373 case tok::kw___is_signed:
2374 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2375 // typically treats it as a trait. If we see __is_signed as it appears
2376 // in libstdc++, e.g.,
2377 //
2378 // static const bool __is_signed;
2379 //
2380 // then treat __is_signed as an identifier rather than as a keyword.
2381 if (DS.getTypeSpecType() == TST_bool &&
2382 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2383 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2384 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2385 Tok.setKind(tok::identifier);
2386 }
2387
2388 // We're done with the declaration-specifiers.
2389 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002390
Chris Lattner3bd934a2008-07-26 01:18:38 +00002391 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002392 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002393 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002394 // In C++, check to see if this is a scope specifier like foo::bar::, if
2395 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002396 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002397 if (TryAnnotateCXXScopeToken(true)) {
2398 if (!DS.hasTypeSpecifier())
2399 DS.SetTypeSpecError();
2400 goto DoneWithDeclSpec;
2401 }
2402 if (!Tok.is(tok::identifier))
2403 continue;
2404 }
Mike Stump1eb44332009-09-09 15:08:12 +00002405
Chris Lattner3bd934a2008-07-26 01:18:38 +00002406 // This identifier can only be a typedef name if we haven't already seen
2407 // a type-specifier. Without this check we misparse:
2408 // typedef int X; struct Y { short X; }; as 'short int'.
2409 if (DS.hasTypeSpecifier())
2410 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002411
John Thompson82287d12010-02-05 00:12:22 +00002412 // Check for need to substitute AltiVec keyword tokens.
2413 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2414 break;
2415
Richard Smithf63eee72012-05-09 18:56:43 +00002416 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2417 // allow the use of a typedef name as a type specifier.
2418 if (DS.isTypeAltiVecVector())
2419 goto DoneWithDeclSpec;
2420
John McCallb3d87482010-08-24 05:47:05 +00002421 ParsedType TypeRep =
2422 Actions.getTypeName(*Tok.getIdentifierInfo(),
2423 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002424
Chris Lattnerc199ab32009-04-12 20:42:31 +00002425 // If this is not a typedef name, don't parse it as part of the declspec,
2426 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002427 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002428 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002429 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002430 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002431
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002432 // If we're in a context where the identifier could be a class name,
2433 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002434 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002435 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002436 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002437 goto DoneWithDeclSpec;
2438
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002439 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002440 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002441 if (isInvalid)
2442 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002443
Chris Lattner3bd934a2008-07-26 01:18:38 +00002444 DS.SetRangeEnd(Tok.getLocation());
2445 ConsumeToken(); // The identifier
2446
2447 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2448 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002449 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002450 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002451 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002452
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002453 // Need to support trailing type qualifiers (e.g. "id<p> const").
2454 // If a type specifier follows, it will be diagnosed elsewhere.
2455 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002456 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002457
2458 // type-name
2459 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002460 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002461 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002462 // This template-id does not refer to a type name, so we're
2463 // done with the type-specifiers.
2464 goto DoneWithDeclSpec;
2465 }
2466
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002467 // If we're in a context where the template-id could be a
2468 // constructor name or specialization, check whether this is a
2469 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002470 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002471 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002472 isConstructorDeclarator())
2473 goto DoneWithDeclSpec;
2474
Douglas Gregor39a8de12009-02-25 19:37:18 +00002475 // Turn the template-id annotation token into a type annotation
2476 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002477 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002478 continue;
2479 }
2480
Reid Spencer5f016e22007-07-11 17:01:13 +00002481 // GNU attributes support.
2482 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002483 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002484 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002485
2486 // Microsoft declspec support.
2487 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002488 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002489 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002490
Steve Naroff239f0732008-12-25 14:16:32 +00002491 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002492 case tok::kw___forceinline: {
2493 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2494 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002495 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002496 // FIXME: This does not work correctly if it is set to be a declspec
2497 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002498 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002499 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002500 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002501 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002502
2503 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002504 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002505 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002506 case tok::kw___cdecl:
2507 case tok::kw___stdcall:
2508 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002509 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002510 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002511 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002512 continue;
2513
Dawn Perchik52fc3142010-09-03 01:29:35 +00002514 // Borland single token adornments.
2515 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002516 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002517 continue;
2518
Peter Collingbournef315fa82011-02-14 01:42:53 +00002519 // OpenCL single token adornments.
2520 case tok::kw___kernel:
2521 ParseOpenCLAttributes(DS.getAttributes());
2522 continue;
2523
Reid Spencer5f016e22007-07-11 17:01:13 +00002524 // storage-class-specifier
2525 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002526 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2527 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002528 break;
2529 case tok::kw_extern:
2530 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002531 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002532 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2533 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002534 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002535 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002536 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2537 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002538 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002539 case tok::kw_static:
2540 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002541 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002542 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2543 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002544 break;
2545 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002546 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002547 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002548 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2549 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002550 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002551 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002552 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002553 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002554 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2555 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002556 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002557 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2558 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002559 break;
2560 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002561 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2562 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002563 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002564 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002565 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2566 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002567 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002568 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002569 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002570 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002571
Reid Spencer5f016e22007-07-11 17:01:13 +00002572 // function-specifier
2573 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002574 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002575 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002576 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002577 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002578 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002579 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002580 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002581 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002582
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002583 // alignment-specifier
2584 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002585 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002586 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002587 ParseAlignmentSpecifier(DS.getAttributes());
2588 continue;
2589
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002590 // friend
2591 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002592 if (DSContext == DSC_class)
2593 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2594 else {
2595 PrevSpec = ""; // not actually used by the diagnostic
2596 DiagID = diag::err_friend_invalid_in_context;
2597 isInvalid = true;
2598 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002599 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002600
Douglas Gregor8d267c52011-09-09 02:06:17 +00002601 // Modules
2602 case tok::kw___module_private__:
2603 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2604 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002605
Sebastian Redl2ac67232009-11-05 15:47:02 +00002606 // constexpr
2607 case tok::kw_constexpr:
2608 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2609 break;
2610
Chris Lattner80d0c892009-01-21 19:48:37 +00002611 // type-specifier
2612 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002613 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2614 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002615 break;
2616 case tok::kw_long:
2617 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002618 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2619 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002620 else
John McCallfec54012009-08-03 20:12:06 +00002621 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2622 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002623 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002624 case tok::kw___int64:
2625 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2626 DiagID);
2627 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002628 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002629 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2630 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002631 break;
2632 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002633 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2634 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002635 break;
2636 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002637 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2638 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002639 break;
2640 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002641 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2642 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002643 break;
2644 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002645 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2646 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002647 break;
2648 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002649 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2650 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002651 break;
2652 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002653 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2654 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002655 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002656 case tok::kw___int128:
2657 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2658 DiagID);
2659 break;
2660 case tok::kw_half:
2661 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2662 DiagID);
2663 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002664 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002665 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2666 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002667 break;
2668 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002669 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2670 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002671 break;
2672 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002673 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2674 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002675 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002676 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002677 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2678 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002679 break;
2680 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002681 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2682 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002683 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002684 case tok::kw_bool:
2685 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002686 if (Tok.is(tok::kw_bool) &&
2687 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2688 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2689 PrevSpec = ""; // Not used by the diagnostic.
2690 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002691 // For better error recovery.
2692 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002693 isInvalid = true;
2694 } else {
2695 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2696 DiagID);
2697 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002698 break;
2699 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002700 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2701 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002702 break;
2703 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002704 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2705 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002706 break;
2707 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002708 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2709 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002710 break;
John Thompson82287d12010-02-05 00:12:22 +00002711 case tok::kw___vector:
2712 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2713 break;
2714 case tok::kw___pixel:
2715 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2716 break;
John McCalla5fc4722011-04-09 22:50:59 +00002717 case tok::kw___unknown_anytype:
2718 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2719 PrevSpec, DiagID);
2720 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002721
2722 // class-specifier:
2723 case tok::kw_class:
2724 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00002725 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00002726 case tok::kw_union: {
2727 tok::TokenKind Kind = Tok.getKind();
2728 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002729 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2730 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002731 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002732 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002733
2734 // enum-specifier:
2735 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002736 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002737 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002738 continue;
2739
2740 // cv-qualifier:
2741 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002742 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith42926a02012-07-24 20:24:58 +00002743 getLangOpts(), /*IsTypeSpec*/true);
Chris Lattner80d0c892009-01-21 19:48:37 +00002744 break;
2745 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002746 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith42926a02012-07-24 20:24:58 +00002747 getLangOpts(), /*IsTypeSpec*/true);
Chris Lattner80d0c892009-01-21 19:48:37 +00002748 break;
2749 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002750 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith42926a02012-07-24 20:24:58 +00002751 getLangOpts(), /*IsTypeSpec*/true);
Chris Lattner80d0c892009-01-21 19:48:37 +00002752 break;
2753
Douglas Gregord57959a2009-03-27 23:10:48 +00002754 // C++ typename-specifier:
2755 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002756 if (TryAnnotateTypeOrScopeToken()) {
2757 DS.SetTypeSpecError();
2758 goto DoneWithDeclSpec;
2759 }
2760 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002761 continue;
2762 break;
2763
Chris Lattner80d0c892009-01-21 19:48:37 +00002764 // GNU typeof support.
2765 case tok::kw_typeof:
2766 ParseTypeofSpecifier(DS);
2767 continue;
2768
David Blaikie42d6d0c2011-12-04 05:04:18 +00002769 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002770 ParseDecltypeSpecifier(DS);
2771 continue;
2772
Sean Huntdb5d44b2011-05-19 05:37:45 +00002773 case tok::kw___underlying_type:
2774 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002775 continue;
2776
2777 case tok::kw__Atomic:
2778 ParseAtomicSpecifier(DS);
2779 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002780
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002781 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00002782 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002783 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002784 goto DoneWithDeclSpec;
2785 case tok::kw___private:
2786 case tok::kw___global:
2787 case tok::kw___local:
2788 case tok::kw___constant:
2789 case tok::kw___read_only:
2790 case tok::kw___write_only:
2791 case tok::kw___read_write:
2792 ParseOpenCLQualifiers(DS);
2793 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002794
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002795 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002796 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002797 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2798 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002799 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002800 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002801
Douglas Gregor46f936e2010-11-19 17:10:50 +00002802 if (!ParseObjCProtocolQualifiers(DS))
2803 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2804 << FixItHint::CreateInsertion(Loc, "id")
2805 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00002806
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002807 // Need to support trailing type qualifiers (e.g. "id<p> const").
2808 // If a type specifier follows, it will be diagnosed elsewhere.
2809 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002810 }
John McCallfec54012009-08-03 20:12:06 +00002811 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002812 if (isInvalid) {
2813 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002814 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00002815
Douglas Gregorae2fb142010-08-23 14:34:43 +00002816 if (DiagID == diag::ext_duplicate_declspec)
2817 Diag(Tok, DiagID)
2818 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2819 else
2820 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002821 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002822
Chris Lattner81c018d2008-03-13 06:29:04 +00002823 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002824 if (DiagID != diag::err_bool_redeclaration)
2825 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002826
2827 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002828 }
2829}
Douglas Gregoradcac882008-12-01 23:54:00 +00002830
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002831/// ParseStructDeclaration - Parse a struct declaration without the terminating
2832/// semicolon.
2833///
Reid Spencer5f016e22007-07-11 17:01:13 +00002834/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002835/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002836/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002837/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002838/// struct-declarator-list:
2839/// struct-declarator
2840/// struct-declarator-list ',' struct-declarator
2841/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2842/// struct-declarator:
2843/// declarator
2844/// [GNU] declarator attributes[opt]
2845/// declarator[opt] ':' constant-expression
2846/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2847///
Chris Lattnere1359422008-04-10 06:46:29 +00002848void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002849ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00002850
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002851 if (Tok.is(tok::kw___extension__)) {
2852 // __extension__ silences extension warnings in the subexpression.
2853 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002854 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002855 return ParseStructDeclaration(DS, Fields);
2856 }
Mike Stump1eb44332009-09-09 15:08:12 +00002857
Steve Naroff28a7ca82007-08-20 22:28:22 +00002858 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002859 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002860
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002861 // If there are no declarators, this is a free-standing declaration
2862 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002863 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002864 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2865 DS);
2866 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002867 return;
2868 }
2869
2870 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002871 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002872 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002873 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002874 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00002875 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002876
2877 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002878 if (!FirstDeclarator)
2879 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002880
Steve Naroff28a7ca82007-08-20 22:28:22 +00002881 /// struct-declarator: declarator
2882 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002883 if (Tok.isNot(tok::colon)) {
2884 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2885 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002886 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002887 }
Mike Stump1eb44332009-09-09 15:08:12 +00002888
Chris Lattner04d66662007-10-09 17:33:22 +00002889 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002890 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002891 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002892 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002893 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002894 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002895 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002896 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002897
Steve Naroff28a7ca82007-08-20 22:28:22 +00002898 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002899 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002900
John McCallbdd563e2009-11-03 02:38:08 +00002901 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00002902 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00002903
Steve Naroff28a7ca82007-08-20 22:28:22 +00002904 // If we don't have a comma, it is either the end of the list (a ';')
2905 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002906 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002907 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002908
Steve Naroff28a7ca82007-08-20 22:28:22 +00002909 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002910 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002911
John McCallbdd563e2009-11-03 02:38:08 +00002912 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002913 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002914}
2915
2916/// ParseStructUnionBody
2917/// struct-contents:
2918/// struct-declaration-list
2919/// [EXT] empty
2920/// [GNU] "struct-declaration-list" without terminatoring ';'
2921/// struct-declaration-list:
2922/// struct-declaration
2923/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002924/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002925///
Reid Spencer5f016e22007-07-11 17:01:13 +00002926void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002927 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002928 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2929 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002930
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002931 BalancedDelimiterTracker T(*this, tok::l_brace);
2932 if (T.consumeOpen())
2933 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002934
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002935 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002936 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002937
Reid Spencer5f016e22007-07-11 17:01:13 +00002938 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2939 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002940 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002941 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2942 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2943 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002944
Chris Lattner5f9e2722011-07-23 10:55:15 +00002945 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002946
Reid Spencer5f016e22007-07-11 17:01:13 +00002947 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002948 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002949 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002950
Reid Spencer5f016e22007-07-11 17:01:13 +00002951 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002952 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002953 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00002954 continue;
2955 }
Chris Lattnere1359422008-04-10 06:46:29 +00002956
John McCallbdd563e2009-11-03 02:38:08 +00002957 if (!Tok.is(tok::at)) {
2958 struct CFieldCallback : FieldCallback {
2959 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002960 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002961 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002962
John McCalld226f652010-08-21 09:40:31 +00002963 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002964 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002965 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2966
Eli Friedmandcdff462012-08-08 23:53:27 +00002967 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002968 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002969 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002970 FD.D.getDeclSpec().getSourceRange().getBegin(),
2971 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002972 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002973 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00002974 }
John McCallbdd563e2009-11-03 02:38:08 +00002975 } Callback(*this, TagDecl, FieldDecls);
2976
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002977 // Parse all the comma separated declarators.
2978 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002979 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002980 } else { // Handle @defs
2981 ConsumeToken();
2982 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2983 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002984 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002985 continue;
2986 }
2987 ConsumeToken();
2988 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2989 if (!Tok.is(tok::identifier)) {
2990 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002991 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002992 continue;
2993 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002994 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002995 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002996 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002997 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2998 ConsumeToken();
2999 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003000 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003001
Chris Lattner04d66662007-10-09 17:33:22 +00003002 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003003 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003004 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003005 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003006 break;
3007 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003008 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3009 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003010 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003011 // If we stopped at a ';', eat it.
3012 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003013 }
3014 }
Mike Stump1eb44332009-09-09 15:08:12 +00003015
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003016 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003017
John McCall0b7e6782011-03-24 11:26:52 +00003018 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003019 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003020 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003021
Douglas Gregor23c94db2010-07-02 17:43:08 +00003022 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003023 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003024 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003025 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003026 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003027 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3028 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003029}
3030
Reid Spencer5f016e22007-07-11 17:01:13 +00003031/// ParseEnumSpecifier
3032/// enum-specifier: [C99 6.7.2.2]
3033/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003034///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003035/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3036/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003037/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3038/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003039/// 'enum' identifier
3040/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003041///
Richard Smith1af83c42012-03-23 03:33:32 +00003042/// [C++11] enum-head '{' enumerator-list[opt] '}'
3043/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003044///
Richard Smith1af83c42012-03-23 03:33:32 +00003045/// enum-head: [C++11]
3046/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3047/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3048/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003049///
Richard Smith1af83c42012-03-23 03:33:32 +00003050/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003051/// 'enum'
3052/// 'enum' 'class'
3053/// 'enum' 'struct'
3054///
Richard Smith1af83c42012-03-23 03:33:32 +00003055/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003056/// ':' type-specifier-seq
3057///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003058/// [C++] elaborated-type-specifier:
3059/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3060///
Chris Lattner4c97d762009-04-12 21:49:30 +00003061void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003062 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003063 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003064 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003065 if (Tok.is(tok::code_completion)) {
3066 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003067 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003068 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003069 }
John McCall57c13002011-07-06 05:58:41 +00003070
Sean Hunt2edf0a22012-06-23 05:07:58 +00003071 // If attributes exist after tag, parse them.
3072 ParsedAttributesWithRange attrs(AttrFactory);
3073 MaybeParseGNUAttributes(attrs);
3074 MaybeParseCXX0XAttributes(attrs);
3075
3076 // If declspecs exist after tag, parse them.
3077 while (Tok.is(tok::kw___declspec))
3078 ParseMicrosoftDeclSpec(attrs);
3079
Richard Smithbdad7a22012-01-10 01:33:14 +00003080 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003081 bool IsScopedUsingClassTag = false;
3082
John McCall1e12b3d2012-06-23 22:30:04 +00003083 // In C++11, recognize 'enum class' and 'enum struct'.
David Blaikie4e4d0842012-03-11 07:00:24 +00003084 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00003085 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00003086 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003087 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003088 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003089
John McCall1e12b3d2012-06-23 22:30:04 +00003090 // Attributes are not allowed between these keywords. Diagnose,
3091 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003092 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003093
3094 // They are allowed afterwards, though.
3095 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003096 MaybeParseCXX0XAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003097 while (Tok.is(tok::kw___declspec))
3098 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003099 }
Richard Smith1af83c42012-03-23 03:33:32 +00003100
John McCall13489672012-05-07 06:16:58 +00003101 // C++11 [temp.explicit]p12:
3102 // The usual access controls do not apply to names used to specify
3103 // explicit instantiations.
3104 // We extend this to also cover explicit specializations. Note that
3105 // we don't suppress if this turns out to be an elaborated type
3106 // specifier.
3107 bool shouldDelayDiagsInTag =
3108 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3109 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3110 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003111
Richard Smith7796eb52012-03-12 08:56:40 +00003112 // Enum definitions should not be parsed in a trailing-return-type.
3113 bool AllowDeclaration = DSC != DSC_trailing;
3114
3115 bool AllowFixedUnderlyingType = AllowDeclaration &&
3116 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
3117 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003118
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003119 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003120 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003121 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3122 // if a fixed underlying type is allowed.
3123 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003124
3125 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003126 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00003127 return;
3128
3129 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003130 Diag(Tok, diag::err_expected_ident);
3131 if (Tok.isNot(tok::l_brace)) {
3132 // Has no name and is not a definition.
3133 // Skip the rest of this declarator, up until the comma or semicolon.
3134 SkipUntil(tok::comma, true);
3135 return;
3136 }
3137 }
3138 }
Mike Stump1eb44332009-09-09 15:08:12 +00003139
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003140 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003141 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003142 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003143 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003144
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003145 // Skip the rest of this declarator, up until the comma or semicolon.
3146 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003147 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003148 }
Mike Stump1eb44332009-09-09 15:08:12 +00003149
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003150 // If an identifier is present, consume and remember it.
3151 IdentifierInfo *Name = 0;
3152 SourceLocation NameLoc;
3153 if (Tok.is(tok::identifier)) {
3154 Name = Tok.getIdentifierInfo();
3155 NameLoc = ConsumeToken();
3156 }
Mike Stump1eb44332009-09-09 15:08:12 +00003157
Richard Smithbdad7a22012-01-10 01:33:14 +00003158 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003159 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3160 // declaration of a scoped enumeration.
3161 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003162 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003163 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003164 }
3165
John McCall13489672012-05-07 06:16:58 +00003166 // Okay, end the suppression area. We'll decide whether to emit the
3167 // diagnostics in a second.
3168 if (shouldDelayDiagsInTag)
3169 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003170
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003171 TypeResult BaseType;
3172
Douglas Gregora61b3e72010-12-01 17:42:47 +00003173 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003174 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003175 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003176 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003177 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003178 // If we're in class scope, this can either be an enum declaration with
3179 // an underlying type, or a declaration of a bitfield member. We try to
3180 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003181 // (integer literal, sizeof); if it's still ambiguous, we then consider
3182 // anything that's a simple-type-specifier followed by '(' as an
3183 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003184 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003185 EnterExpressionEvaluationContext Unevaluated(Actions,
3186 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003187 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003188 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003189 // bit-field. This is the common case.
3190 if (TPR == TPResult::True())
3191 PossibleBitfield = true;
3192 // If the next token starts a type-specifier-seq, it may be either a
3193 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003194 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003195 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003196 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003197 GetLookAheadToken(2).getKind() == tok::semi) {
3198 // Consume the ':'.
3199 ConsumeToken();
3200 } else {
3201 // We have the start of a type-specifier-seq, so we have to perform
3202 // tentative parsing to determine whether we have an expression or a
3203 // type.
3204 TentativeParsingAction TPA(*this);
3205
3206 // Consume the ':'.
3207 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003208
3209 // If we see a type specifier followed by an open-brace, we have an
3210 // ambiguity between an underlying type and a C++11 braced
3211 // function-style cast. Resolve this by always treating it as an
3212 // underlying type.
3213 // FIXME: The standard is not entirely clear on how to disambiguate in
3214 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003215 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003216 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003217 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003218 // We'll parse this as a bitfield later.
3219 PossibleBitfield = true;
3220 TPA.Revert();
3221 } else {
3222 // We have a type-specifier-seq.
3223 TPA.Commit();
3224 }
3225 }
3226 } else {
3227 // Consume the ':'.
3228 ConsumeToken();
3229 }
3230
3231 if (!PossibleBitfield) {
3232 SourceRange Range;
3233 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003234
David Blaikie4e4d0842012-03-11 07:00:24 +00003235 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00003236 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
3237 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00003238 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003239 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003240 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003241 }
3242
Richard Smithbdad7a22012-01-10 01:33:14 +00003243 // There are four options here. If we have 'friend enum foo;' then this is a
3244 // friend declaration, and cannot have an accompanying definition. If we have
3245 // 'enum foo;', then this is a forward declaration. If we have
3246 // 'enum foo {...' then this is a definition. Otherwise we have something
3247 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003248 //
3249 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3250 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3251 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3252 //
John McCallf312b1e2010-08-26 23:41:50 +00003253 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003254 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003255 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003256 } else if (Tok.is(tok::l_brace)) {
3257 if (DS.isFriendSpecified()) {
3258 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3259 << SourceRange(DS.getFriendSpecLoc());
3260 ConsumeBrace();
3261 SkipUntil(tok::r_brace);
3262 TUK = Sema::TUK_Friend;
3263 } else {
3264 TUK = Sema::TUK_Definition;
3265 }
Richard Smithc9f35172012-06-25 21:37:02 +00003266 } else if (DSC != DSC_type_specifier &&
3267 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003268 (Tok.isAtStartOfLine() &&
3269 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003270 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3271 if (Tok.isNot(tok::semi)) {
3272 // A semicolon was missing after this declaration. Diagnose and recover.
3273 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3274 "enum");
3275 PP.EnterToken(Tok);
3276 Tok.setKind(tok::semi);
3277 }
John McCall13489672012-05-07 06:16:58 +00003278 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003279 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003280 }
3281
3282 // If this is an elaborated type specifier, and we delayed
3283 // diagnostics before, just merge them into the current pool.
3284 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3285 diagsFromTag.redelay();
3286 }
Richard Smith1af83c42012-03-23 03:33:32 +00003287
3288 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003289 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003290 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003291 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3292 // Skip the rest of this declarator, up until the comma or semicolon.
3293 Diag(Tok, diag::err_enum_template);
3294 SkipUntil(tok::comma, true);
3295 return;
3296 }
3297
3298 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3299 // Enumerations can't be explicitly instantiated.
3300 DS.SetTypeSpecError();
3301 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3302 return;
3303 }
3304
3305 assert(TemplateInfo.TemplateParams && "no template parameters");
3306 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3307 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003308 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003309
Sean Hunt2edf0a22012-06-23 05:07:58 +00003310 if (TUK == Sema::TUK_Reference)
3311 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003312
Douglas Gregorb9075602011-02-22 02:55:24 +00003313 if (!Name && TUK != Sema::TUK_Definition) {
3314 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003315
Douglas Gregorb9075602011-02-22 02:55:24 +00003316 // Skip the rest of this declarator, up until the comma or semicolon.
3317 SkipUntil(tok::comma, true);
3318 return;
3319 }
Richard Smith1af83c42012-03-23 03:33:32 +00003320
Douglas Gregor402abb52009-05-28 23:31:59 +00003321 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003322 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003323 const char *PrevSpec = 0;
3324 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003325 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003326 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003327 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003328 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003329 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003330
Douglas Gregor48c89f42010-04-24 16:38:41 +00003331 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003332 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003333 // dependent tag.
3334 if (!Name) {
3335 DS.SetTypeSpecError();
3336 Diag(Tok, diag::err_expected_type_name_after_typename);
3337 return;
3338 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003339
Douglas Gregor23c94db2010-07-02 17:43:08 +00003340 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003341 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003342 NameLoc);
3343 if (Type.isInvalid()) {
3344 DS.SetTypeSpecError();
3345 return;
3346 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003347
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003348 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3349 NameLoc.isValid() ? NameLoc : StartLoc,
3350 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003351 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003352
Douglas Gregor48c89f42010-04-24 16:38:41 +00003353 return;
3354 }
Mike Stump1eb44332009-09-09 15:08:12 +00003355
John McCalld226f652010-08-21 09:40:31 +00003356 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003357 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003358 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003359 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003360 ConsumeBrace();
3361 SkipUntil(tok::r_brace);
3362 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003363
Douglas Gregor48c89f42010-04-24 16:38:41 +00003364 DS.SetTypeSpecError();
3365 return;
3366 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003367
Richard Smithc9f35172012-06-25 21:37:02 +00003368 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003369 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003370
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003371 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3372 NameLoc.isValid() ? NameLoc : StartLoc,
3373 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003374 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003375}
3376
3377/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3378/// enumerator-list:
3379/// enumerator
3380/// enumerator-list ',' enumerator
3381/// enumerator:
3382/// enumeration-constant
3383/// enumeration-constant '=' constant-expression
3384/// enumeration-constant:
3385/// identifier
3386///
John McCalld226f652010-08-21 09:40:31 +00003387void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003388 // Enter the scope of the enum body and start the definition.
3389 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003390 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003391
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003392 BalancedDelimiterTracker T(*this, tok::l_brace);
3393 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003394
Chris Lattner7946dd32007-08-27 17:24:30 +00003395 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003396 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003397 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003398
Chris Lattner5f9e2722011-07-23 10:55:15 +00003399 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003400
John McCalld226f652010-08-21 09:40:31 +00003401 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003402
Reid Spencer5f016e22007-07-11 17:01:13 +00003403 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003404 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003405 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3406 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003407
John McCall5b629aa2010-10-22 23:36:17 +00003408 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003409 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003410 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003411 MaybeParseCXX0XAttributes(attrs);
3412 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003413
Reid Spencer5f016e22007-07-11 17:01:13 +00003414 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003415 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003416 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003417
Chris Lattner04d66662007-10-09 17:33:22 +00003418 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003419 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003420 AssignedVal = ParseConstantExpression();
3421 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003422 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003423 }
Mike Stump1eb44332009-09-09 15:08:12 +00003424
Reid Spencer5f016e22007-07-11 17:01:13 +00003425 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003426 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3427 LastEnumConstDecl,
3428 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003429 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003430 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003431 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003432
Reid Spencer5f016e22007-07-11 17:01:13 +00003433 EnumConstantDecls.push_back(EnumConstDecl);
3434 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003435
Douglas Gregor751f6922010-09-07 14:51:08 +00003436 if (Tok.is(tok::identifier)) {
3437 // We're missing a comma between enumerators.
3438 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003439 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003440 << FixItHint::CreateInsertion(Loc, ", ");
3441 continue;
3442 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003443
Chris Lattner04d66662007-10-09 17:33:22 +00003444 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003445 break;
3446 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003447
Richard Smith7fe62082011-10-15 05:09:34 +00003448 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003449 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003450 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3451 diag::ext_enumerator_list_comma_cxx :
3452 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003453 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003454 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003455 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3456 << FixItHint::CreateRemoval(CommaLoc);
3457 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003458 }
Mike Stump1eb44332009-09-09 15:08:12 +00003459
Reid Spencer5f016e22007-07-11 17:01:13 +00003460 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003461 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003462
Reid Spencer5f016e22007-07-11 17:01:13 +00003463 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003464 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003465 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003466
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003467 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3468 EnumDecl, EnumConstantDecls.data(),
3469 EnumConstantDecls.size(), getCurScope(),
3470 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003471
Douglas Gregor72de6672009-01-08 20:45:30 +00003472 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003473 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3474 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003475
3476 // The next token must be valid after an enum definition. If not, a ';'
3477 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003478 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3479 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003480 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3481 // Push this token back into the preprocessor and change our current token
3482 // to ';' so that the rest of the code recovers as though there were an
3483 // ';' after the definition.
3484 PP.EnterToken(Tok);
3485 Tok.setKind(tok::semi);
3486 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003487}
3488
3489/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003490/// start of a type-qualifier-list.
3491bool Parser::isTypeQualifier() const {
3492 switch (Tok.getKind()) {
3493 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003494
3495 // type-qualifier only in OpenCL
3496 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003497 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003498
Steve Naroff5f8aa692008-02-11 23:15:56 +00003499 // type-qualifier
3500 case tok::kw_const:
3501 case tok::kw_volatile:
3502 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003503 case tok::kw___private:
3504 case tok::kw___local:
3505 case tok::kw___global:
3506 case tok::kw___constant:
3507 case tok::kw___read_only:
3508 case tok::kw___read_write:
3509 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003510 return true;
3511 }
3512}
3513
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003514/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3515/// is definitely a type-specifier. Return false if it isn't part of a type
3516/// specifier or if we're not sure.
3517bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3518 switch (Tok.getKind()) {
3519 default: return false;
3520 // type-specifiers
3521 case tok::kw_short:
3522 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003523 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003524 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003525 case tok::kw_signed:
3526 case tok::kw_unsigned:
3527 case tok::kw__Complex:
3528 case tok::kw__Imaginary:
3529 case tok::kw_void:
3530 case tok::kw_char:
3531 case tok::kw_wchar_t:
3532 case tok::kw_char16_t:
3533 case tok::kw_char32_t:
3534 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003535 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003536 case tok::kw_float:
3537 case tok::kw_double:
3538 case tok::kw_bool:
3539 case tok::kw__Bool:
3540 case tok::kw__Decimal32:
3541 case tok::kw__Decimal64:
3542 case tok::kw__Decimal128:
3543 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003544
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003545 // struct-or-union-specifier (C99) or class-specifier (C++)
3546 case tok::kw_class:
3547 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003548 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003549 case tok::kw_union:
3550 // enum-specifier
3551 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003552
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003553 // typedef-name
3554 case tok::annot_typename:
3555 return true;
3556 }
3557}
3558
Steve Naroff5f8aa692008-02-11 23:15:56 +00003559/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003560/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003561bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003562 switch (Tok.getKind()) {
3563 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003564
Chris Lattner166a8fc2009-01-04 23:41:41 +00003565 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003566 if (TryAltiVecVectorToken())
3567 return true;
3568 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003569 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003570 // Annotate typenames and C++ scope specifiers. If we get one, just
3571 // recurse to handle whatever we get.
3572 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003573 return true;
3574 if (Tok.is(tok::identifier))
3575 return false;
3576 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003577
Chris Lattner166a8fc2009-01-04 23:41:41 +00003578 case tok::coloncolon: // ::foo::bar
3579 if (NextToken().is(tok::kw_new) || // ::new
3580 NextToken().is(tok::kw_delete)) // ::delete
3581 return false;
3582
Chris Lattner166a8fc2009-01-04 23:41:41 +00003583 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003584 return true;
3585 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003586
Reid Spencer5f016e22007-07-11 17:01:13 +00003587 // GNU attributes support.
3588 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003589 // GNU typeof support.
3590 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003591
Reid Spencer5f016e22007-07-11 17:01:13 +00003592 // type-specifiers
3593 case tok::kw_short:
3594 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003595 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003596 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003597 case tok::kw_signed:
3598 case tok::kw_unsigned:
3599 case tok::kw__Complex:
3600 case tok::kw__Imaginary:
3601 case tok::kw_void:
3602 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003603 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003604 case tok::kw_char16_t:
3605 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003606 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003607 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003608 case tok::kw_float:
3609 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003610 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003611 case tok::kw__Bool:
3612 case tok::kw__Decimal32:
3613 case tok::kw__Decimal64:
3614 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003615 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003616
Chris Lattner99dc9142008-04-13 18:59:07 +00003617 // struct-or-union-specifier (C99) or class-specifier (C++)
3618 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003619 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003620 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003621 case tok::kw_union:
3622 // enum-specifier
3623 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003624
Reid Spencer5f016e22007-07-11 17:01:13 +00003625 // type-qualifier
3626 case tok::kw_const:
3627 case tok::kw_volatile:
3628 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003629
3630 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003631 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003632 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003633
Chris Lattner7c186be2008-10-20 00:25:30 +00003634 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3635 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003636 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003637
Steve Naroff239f0732008-12-25 14:16:32 +00003638 case tok::kw___cdecl:
3639 case tok::kw___stdcall:
3640 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003641 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003642 case tok::kw___w64:
3643 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003644 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003645 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003646 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003647
3648 case tok::kw___private:
3649 case tok::kw___local:
3650 case tok::kw___global:
3651 case tok::kw___constant:
3652 case tok::kw___read_only:
3653 case tok::kw___read_write:
3654 case tok::kw___write_only:
3655
Eli Friedman290eeb02009-06-08 23:27:34 +00003656 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003657
3658 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003659 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003660
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003661 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003662 case tok::kw__Atomic:
3663 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003664 }
3665}
3666
3667/// isDeclarationSpecifier() - Return true if the current token is part of a
3668/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003669///
3670/// \param DisambiguatingWithExpression True to indicate that the purpose of
3671/// this check is to disambiguate between an expression and a declaration.
3672bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003673 switch (Tok.getKind()) {
3674 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003675
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003676 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003677 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003678
Chris Lattner166a8fc2009-01-04 23:41:41 +00003679 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003680 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003681 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003682 return false;
John Thompson82287d12010-02-05 00:12:22 +00003683 if (TryAltiVecVectorToken())
3684 return true;
3685 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003686 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003687 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003688 // Annotate typenames and C++ scope specifiers. If we get one, just
3689 // recurse to handle whatever we get.
3690 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003691 return true;
3692 if (Tok.is(tok::identifier))
3693 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003694
Douglas Gregor9497a732010-09-16 01:51:54 +00003695 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00003696 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00003697 // expression is permitted, then this is probably a class message send
3698 // missing the initial '['. In this case, we won't consider this to be
3699 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00003700 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00003701 isStartOfObjCClassMessageMissingOpenBracket())
3702 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003703
John McCall9ba61662010-02-26 08:45:28 +00003704 return isDeclarationSpecifier();
3705
Chris Lattner166a8fc2009-01-04 23:41:41 +00003706 case tok::coloncolon: // ::foo::bar
3707 if (NextToken().is(tok::kw_new) || // ::new
3708 NextToken().is(tok::kw_delete)) // ::delete
3709 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003710
Chris Lattner166a8fc2009-01-04 23:41:41 +00003711 // Annotate typenames and C++ scope specifiers. If we get one, just
3712 // recurse to handle whatever we get.
3713 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003714 return true;
3715 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003716
Reid Spencer5f016e22007-07-11 17:01:13 +00003717 // storage-class-specifier
3718 case tok::kw_typedef:
3719 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003720 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003721 case tok::kw_static:
3722 case tok::kw_auto:
3723 case tok::kw_register:
3724 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003725
Douglas Gregor8d267c52011-09-09 02:06:17 +00003726 // Modules
3727 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00003728
Reid Spencer5f016e22007-07-11 17:01:13 +00003729 // type-specifiers
3730 case tok::kw_short:
3731 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003732 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003733 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003734 case tok::kw_signed:
3735 case tok::kw_unsigned:
3736 case tok::kw__Complex:
3737 case tok::kw__Imaginary:
3738 case tok::kw_void:
3739 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003740 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003741 case tok::kw_char16_t:
3742 case tok::kw_char32_t:
3743
Reid Spencer5f016e22007-07-11 17:01:13 +00003744 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003745 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003746 case tok::kw_float:
3747 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003748 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003749 case tok::kw__Bool:
3750 case tok::kw__Decimal32:
3751 case tok::kw__Decimal64:
3752 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003753 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003754
Chris Lattner99dc9142008-04-13 18:59:07 +00003755 // struct-or-union-specifier (C99) or class-specifier (C++)
3756 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003757 case tok::kw_struct:
3758 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00003759 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003760 // enum-specifier
3761 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003762
Reid Spencer5f016e22007-07-11 17:01:13 +00003763 // type-qualifier
3764 case tok::kw_const:
3765 case tok::kw_volatile:
3766 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003767
Reid Spencer5f016e22007-07-11 17:01:13 +00003768 // function-specifier
3769 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003770 case tok::kw_virtual:
3771 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003772
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003773 // static_assert-declaration
3774 case tok::kw__Static_assert:
3775
Chris Lattner1ef08762007-08-09 17:01:07 +00003776 // GNU typeof support.
3777 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003778
Chris Lattner1ef08762007-08-09 17:01:07 +00003779 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003780 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003781 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003782
Francois Pichete3d49b42011-06-19 08:02:06 +00003783 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003784 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003785 return true;
3786
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003787 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003788 case tok::kw__Atomic:
3789 return true;
3790
Chris Lattnerf3948c42008-07-26 03:38:44 +00003791 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3792 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003793 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003794
Douglas Gregord9d75e52011-04-27 05:41:15 +00003795 // typedef-name
3796 case tok::annot_typename:
3797 return !DisambiguatingWithExpression ||
3798 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00003799
Steve Naroff47f52092009-01-06 19:34:12 +00003800 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003801 case tok::kw___cdecl:
3802 case tok::kw___stdcall:
3803 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003804 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003805 case tok::kw___w64:
3806 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003807 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003808 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003809 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003810 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003811
3812 case tok::kw___private:
3813 case tok::kw___local:
3814 case tok::kw___global:
3815 case tok::kw___constant:
3816 case tok::kw___read_only:
3817 case tok::kw___read_write:
3818 case tok::kw___write_only:
3819
Eli Friedman290eeb02009-06-08 23:27:34 +00003820 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003821 }
3822}
3823
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003824bool Parser::isConstructorDeclarator() {
3825 TentativeParsingAction TPA(*this);
3826
3827 // Parse the C++ scope specifier.
3828 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00003829 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003830 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003831 TPA.Revert();
3832 return false;
3833 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003834
3835 // Parse the constructor name.
3836 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3837 // We already know that we have a constructor name; just consume
3838 // the token.
3839 ConsumeToken();
3840 } else {
3841 TPA.Revert();
3842 return false;
3843 }
3844
Richard Smith22592862012-03-27 23:05:05 +00003845 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003846 if (Tok.isNot(tok::l_paren)) {
3847 TPA.Revert();
3848 return false;
3849 }
3850 ConsumeParen();
3851
Richard Smith22592862012-03-27 23:05:05 +00003852 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3853 // that we have a constructor.
3854 if (Tok.is(tok::r_paren) ||
3855 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003856 TPA.Revert();
3857 return true;
3858 }
3859
3860 // If we need to, enter the specified scope.
3861 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003862 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003863 DeclScopeObj.EnterDeclaratorScope();
3864
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003865 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003866 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003867 MaybeParseMicrosoftAttributes(Attrs);
3868
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003869 // Check whether the next token(s) are part of a declaration
3870 // specifier, in which case we have the start of a parameter and,
3871 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003872 bool IsConstructor = false;
3873 if (isDeclarationSpecifier())
3874 IsConstructor = true;
3875 else if (Tok.is(tok::identifier) ||
3876 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3877 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3878 // This might be a parenthesized member name, but is more likely to
3879 // be a constructor declaration with an invalid argument type. Keep
3880 // looking.
3881 if (Tok.is(tok::annot_cxxscope))
3882 ConsumeToken();
3883 ConsumeToken();
3884
3885 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003886 // which must have one of the following syntactic forms (see the
3887 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003888 switch (Tok.getKind()) {
3889 case tok::l_paren:
3890 // C(X ( int));
3891 case tok::l_square:
3892 // C(X [ 5]);
3893 // C(X [ [attribute]]);
3894 case tok::coloncolon:
3895 // C(X :: Y);
3896 // C(X :: *p);
3897 case tok::r_paren:
3898 // C(X )
3899 // Assume this isn't a constructor, rather than assuming it's a
3900 // constructor with an unnamed parameter of an ill-formed type.
3901 break;
3902
3903 default:
3904 IsConstructor = true;
3905 break;
3906 }
3907 }
3908
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003909 TPA.Revert();
3910 return IsConstructor;
3911}
Reid Spencer5f016e22007-07-11 17:01:13 +00003912
3913/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003914/// type-qualifier-list: [C99 6.7.5]
3915/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00003916/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00003917/// [ only if VendorAttributesAllowed=true ]
3918/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00003919/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00003920/// [ only if VendorAttributesAllowed=true ]
3921/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3922/// [ only if CXX0XAttributesAllowed=true ]
3923/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003924///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003925void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3926 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003927 bool CXX11AttributesAllowed) {
3928 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003929 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003930 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003931 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003932 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003933 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003934
3935 SourceLocation EndLoc;
3936
Reid Spencer5f016e22007-07-11 17:01:13 +00003937 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003938 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003939 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003940 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003941 SourceLocation Loc = Tok.getLocation();
3942
3943 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003944 case tok::code_completion:
3945 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003946 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00003947
Reid Spencer5f016e22007-07-11 17:01:13 +00003948 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003949 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith42926a02012-07-24 20:24:58 +00003950 getLangOpts(), /*IsTypeSpec*/false);
Reid Spencer5f016e22007-07-11 17:01:13 +00003951 break;
3952 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003953 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith42926a02012-07-24 20:24:58 +00003954 getLangOpts(), /*IsTypeSpec*/false);
Reid Spencer5f016e22007-07-11 17:01:13 +00003955 break;
3956 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003957 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith42926a02012-07-24 20:24:58 +00003958 getLangOpts(), /*IsTypeSpec*/false);
Reid Spencer5f016e22007-07-11 17:01:13 +00003959 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003960
3961 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00003962 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003963 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003964 goto DoneWithTypeQuals;
3965 case tok::kw___private:
3966 case tok::kw___global:
3967 case tok::kw___local:
3968 case tok::kw___constant:
3969 case tok::kw___read_only:
3970 case tok::kw___write_only:
3971 case tok::kw___read_write:
3972 ParseOpenCLQualifiers(DS);
3973 break;
3974
Eli Friedman290eeb02009-06-08 23:27:34 +00003975 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003976 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003977 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003978 case tok::kw___cdecl:
3979 case tok::kw___stdcall:
3980 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003981 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003982 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003983 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003984 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003985 continue;
3986 }
3987 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003988 case tok::kw___pascal:
3989 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003990 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003991 continue;
3992 }
3993 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003994 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003995 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003996 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003997 continue; // do *not* consume the next token!
3998 }
3999 // otherwise, FALL THROUGH!
4000 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004001 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004002 // If this is not a type-qualifier token, we're done reading type
4003 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004004 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004005 if (EndLoc.isValid())
4006 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004007 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004008 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004009
Reid Spencer5f016e22007-07-11 17:01:13 +00004010 // If the specifier combination wasn't legal, issue a diagnostic.
4011 if (isInvalid) {
4012 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004013 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004014 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004015 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004016 }
4017}
4018
4019
4020/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4021///
4022void Parser::ParseDeclarator(Declarator &D) {
4023 /// This implements the 'declarator' production in the C grammar, then checks
4024 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004025 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004026}
4027
Richard Smith9988f282012-03-29 01:16:42 +00004028static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4029 if (Kind == tok::star || Kind == tok::caret)
4030 return true;
4031
4032 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4033 if (!Lang.CPlusPlus)
4034 return false;
4035
4036 return Kind == tok::amp || Kind == tok::ampamp;
4037}
4038
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004039/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4040/// is parsed by the function passed to it. Pass null, and the direct-declarator
4041/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004042/// ptr-operator production.
4043///
Richard Smith0706df42011-10-19 21:33:05 +00004044/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004045/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4046/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004047///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004048/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4049/// [C] pointer[opt] direct-declarator
4050/// [C++] direct-declarator
4051/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004052///
4053/// pointer: [C99 6.7.5]
4054/// '*' type-qualifier-list[opt]
4055/// '*' type-qualifier-list[opt] pointer
4056///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004057/// ptr-operator:
4058/// '*' cv-qualifier-seq[opt]
4059/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004060/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004061/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004062/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004063/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004064void Parser::ParseDeclaratorInternal(Declarator &D,
4065 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004066 if (Diags.hasAllExtensionsSilenced())
4067 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004068
Sebastian Redlf30208a2009-01-24 21:16:55 +00004069 // C++ member pointers start with a '::' or a nested-name.
4070 // Member pointers get special handling, since there's no place for the
4071 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004072 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004073 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4074 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004075 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4076 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004077 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004078 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004079
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004080 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004081 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004082 // The scope spec really belongs to the direct-declarator.
4083 D.getCXXScopeSpec() = SS;
4084 if (DirectDeclParser)
4085 (this->*DirectDeclParser)(D);
4086 return;
4087 }
4088
4089 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004090 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004091 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004092 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004093 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004094
4095 // Recurse to parse whatever is left.
4096 ParseDeclaratorInternal(D, DirectDeclParser);
4097
4098 // Sema will have to catch (syntactically invalid) pointers into global
4099 // scope. It has to catch pointers into namespace scope anyway.
4100 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004101 Loc),
4102 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004103 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004104 return;
4105 }
4106 }
4107
4108 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004109 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004110 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004111 if (DirectDeclParser)
4112 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004113 return;
4114 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004115
Sebastian Redl05532f22009-03-15 22:02:01 +00004116 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4117 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004118 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004119 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004120
Chris Lattner9af55002009-03-27 04:18:06 +00004121 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004122 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004123 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004124
Richard Smith6ee326a2012-04-10 01:32:12 +00004125 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004126 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004127 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004128
Reid Spencer5f016e22007-07-11 17:01:13 +00004129 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004130 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004131 if (Kind == tok::star)
4132 // Remember that we parsed a pointer type, and remember the type-quals.
4133 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004134 DS.getConstSpecLoc(),
4135 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004136 DS.getRestrictSpecLoc()),
4137 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004138 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004139 else
4140 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004141 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004142 Loc),
4143 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004144 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004145 } else {
4146 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004147 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004148
Sebastian Redl743de1f2009-03-23 00:00:23 +00004149 // Complain about rvalue references in C++03, but then go on and build
4150 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004151 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00004152 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004153 diag::warn_cxx98_compat_rvalue_reference :
4154 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004155
Richard Smith6ee326a2012-04-10 01:32:12 +00004156 // GNU-style and C++11 attributes are allowed here, as is restrict.
4157 ParseTypeQualifierListOpt(DS);
4158 D.ExtendWithDeclSpec(DS);
4159
Reid Spencer5f016e22007-07-11 17:01:13 +00004160 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4161 // cv-qualifiers are introduced through the use of a typedef or of a
4162 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004163 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4164 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4165 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004166 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004167 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4168 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004169 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00004170 }
4171
4172 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004173 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004174
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004175 if (D.getNumTypeObjects() > 0) {
4176 // C++ [dcl.ref]p4: There shall be no references to references.
4177 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4178 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004179 if (const IdentifierInfo *II = D.getIdentifier())
4180 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4181 << II;
4182 else
4183 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4184 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004185
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004186 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004187 // can go ahead and build the (technically ill-formed)
4188 // declarator: reference collapsing will take care of it.
4189 }
4190 }
4191
Reid Spencer5f016e22007-07-11 17:01:13 +00004192 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004193 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004194 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004195 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004196 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004197 }
4198}
4199
Richard Smith9988f282012-03-29 01:16:42 +00004200static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4201 SourceLocation EllipsisLoc) {
4202 if (EllipsisLoc.isValid()) {
4203 FixItHint Insertion;
4204 if (!D.getEllipsisLoc().isValid()) {
4205 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4206 D.setEllipsisLoc(EllipsisLoc);
4207 }
4208 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4209 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4210 }
4211}
4212
Reid Spencer5f016e22007-07-11 17:01:13 +00004213/// ParseDirectDeclarator
4214/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004215/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004216/// '(' declarator ')'
4217/// [GNU] '(' attributes declarator ')'
4218/// [C90] direct-declarator '[' constant-expression[opt] ']'
4219/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4220/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4221/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4222/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004223/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4224/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004225/// direct-declarator '(' parameter-type-list ')'
4226/// direct-declarator '(' identifier-list[opt] ')'
4227/// [GNU] direct-declarator '(' parameter-forward-declarations
4228/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004229/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4230/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004231/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4232/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4233/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004234/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004235/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004236///
4237/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004238/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004239/// '::'[opt] nested-name-specifier[opt] type-name
4240///
4241/// id-expression: [C++ 5.1]
4242/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004243/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004244///
4245/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004246/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004247/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004248/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004249/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004250/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004251///
Richard Smith5d8388c2012-03-27 01:42:32 +00004252/// Note, any additional constructs added here may need corresponding changes
4253/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004254void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004255 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004256
David Blaikie4e4d0842012-03-11 07:00:24 +00004257 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004258 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004259 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004260 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4261 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004262 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004263 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004264 }
4265
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004266 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004267 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004268 // Change the declaration context for name lookup, until this function
4269 // is exited (and the declarator has been parsed).
4270 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004271 }
4272
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004273 // C++0x [dcl.fct]p14:
4274 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004275 // of a parameter-declaration-clause without a preceding comma. In
4276 // this case, the ellipsis is parsed as part of the
4277 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004278 // parameter pack that has not been expanded; otherwise, it is parsed
4279 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004280 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004281 !((D.getContext() == Declarator::PrototypeContext ||
4282 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004283 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00004284 !Actions.containsUnexpandedParameterPacks(D))) {
4285 SourceLocation EllipsisLoc = ConsumeToken();
4286 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4287 // The ellipsis was put in the wrong place. Recover, and explain to
4288 // the user what they should have done.
4289 ParseDeclarator(D);
4290 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4291 return;
4292 } else
4293 D.setEllipsisLoc(EllipsisLoc);
4294
4295 // The ellipsis can't be followed by a parenthesized declarator. We
4296 // check for that in ParseParenDeclarator, after we have disambiguated
4297 // the l_paren token.
4298 }
4299
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004300 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4301 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4302 // We found something that indicates the start of an unqualified-id.
4303 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004304 bool AllowConstructorName;
4305 if (D.getDeclSpec().hasTypeSpecifier())
4306 AllowConstructorName = false;
4307 else if (D.getCXXScopeSpec().isSet())
4308 AllowConstructorName =
4309 (D.getContext() == Declarator::FileContext ||
4310 (D.getContext() == Declarator::MemberContext &&
4311 D.getDeclSpec().isFriendSpecified()));
4312 else
4313 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4314
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004315 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004316 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4317 /*EnteringContext=*/true,
4318 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004319 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004320 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004321 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004322 D.getName()) ||
4323 // Once we're past the identifier, if the scope was bad, mark the
4324 // whole declarator bad.
4325 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004326 D.SetIdentifier(0, Tok.getLocation());
4327 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004328 } else {
4329 // Parsed the unqualified-id; update range information and move along.
4330 if (D.getSourceRange().getBegin().isInvalid())
4331 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4332 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004333 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004334 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004335 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004336 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004337 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004338 "There's a C++-specific check for tok::identifier above");
4339 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4340 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4341 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004342 goto PastIdentifier;
4343 }
Richard Smith9988f282012-03-29 01:16:42 +00004344
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004345 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004346 // direct-declarator: '(' declarator ')'
4347 // direct-declarator: '(' attributes declarator ')'
4348 // Example: 'char (*X)' or 'int (*XX)(void)'
4349 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004350
4351 // If the declarator was parenthesized, we entered the declarator
4352 // scope when parsing the parenthesized declarator, then exited
4353 // the scope already. Re-enter the scope, if we need to.
4354 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004355 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004356 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004357 if (!D.isInvalidType() &&
4358 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004359 // Change the declaration context for name lookup, until this function
4360 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004361 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004362 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004363 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004364 // This could be something simple like "int" (in which case the declarator
4365 // portion is empty), if an abstract-declarator is allowed.
4366 D.SetIdentifier(0, Tok.getLocation());
4367 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004368 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004369 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004370 if (D.getContext() == Declarator::MemberContext)
4371 Diag(Tok, diag::err_expected_member_name_or_semi)
4372 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004373 else if (getLangOpts().CPlusPlus)
4374 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004375 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004376 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004377 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004378 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004379 }
Mike Stump1eb44332009-09-09 15:08:12 +00004380
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004381 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004382 assert(D.isPastIdentifier() &&
4383 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004384
Richard Smith6ee326a2012-04-10 01:32:12 +00004385 // Don't parse attributes unless we have parsed an unparenthesized name.
4386 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004387 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004388
Reid Spencer5f016e22007-07-11 17:01:13 +00004389 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004390 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004391 // Enter function-declaration scope, limiting any declarators to the
4392 // function prototype scope, including parameter declarators.
4393 ParseScope PrototypeScope(this,
4394 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004395 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4396 // In such a case, check if we actually have a function declarator; if it
4397 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004398 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004399 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4400 // The name of the declarator, if any, is tentatively declared within
4401 // a possible direct initializer.
4402 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4403 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4404 TentativelyDeclaredIdentifiers.pop_back();
4405 if (!IsFunctionDecl)
4406 break;
4407 }
John McCall0b7e6782011-03-24 11:26:52 +00004408 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004409 BalancedDelimiterTracker T(*this, tok::l_paren);
4410 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004411 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004412 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004413 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004414 ParseBracketDeclarator(D);
4415 } else {
4416 break;
4417 }
4418 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004419}
Reid Spencer5f016e22007-07-11 17:01:13 +00004420
Chris Lattneref4715c2008-04-06 05:45:57 +00004421/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4422/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004423/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004424/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4425///
4426/// direct-declarator:
4427/// '(' declarator ')'
4428/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004429/// direct-declarator '(' parameter-type-list ')'
4430/// direct-declarator '(' identifier-list[opt] ')'
4431/// [GNU] direct-declarator '(' parameter-forward-declarations
4432/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004433///
4434void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004435 BalancedDelimiterTracker T(*this, tok::l_paren);
4436 T.consumeOpen();
4437
Chris Lattneref4715c2008-04-06 05:45:57 +00004438 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004439
Chris Lattner7399ee02008-10-20 02:05:46 +00004440 // Eat any attributes before we look at whether this is a grouping or function
4441 // declarator paren. If this is a grouping paren, the attribute applies to
4442 // the type being built up, for example:
4443 // int (__attribute__(()) *x)(long y)
4444 // If this ends up not being a grouping paren, the attribute applies to the
4445 // first argument, for example:
4446 // int (__attribute__(()) int x)
4447 // In either case, we need to eat any attributes to be able to determine what
4448 // sort of paren this is.
4449 //
John McCall0b7e6782011-03-24 11:26:52 +00004450 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004451 bool RequiresArg = false;
4452 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004453 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004454
Chris Lattner7399ee02008-10-20 02:05:46 +00004455 // We require that the argument list (if this is a non-grouping paren) be
4456 // present even if the attribute list was empty.
4457 RequiresArg = true;
4458 }
Steve Naroff239f0732008-12-25 14:16:32 +00004459 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004460 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004461 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004462 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004463 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004464 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004465 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004466 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004467 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004468 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004469
Chris Lattneref4715c2008-04-06 05:45:57 +00004470 // If we haven't past the identifier yet (or where the identifier would be
4471 // stored, if this is an abstract declarator), then this is probably just
4472 // grouping parens. However, if this could be an abstract-declarator, then
4473 // this could also be the start of function arguments (consider 'void()').
4474 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004475
Chris Lattneref4715c2008-04-06 05:45:57 +00004476 if (!D.mayOmitIdentifier()) {
4477 // If this can't be an abstract-declarator, this *must* be a grouping
4478 // paren, because we haven't seen the identifier yet.
4479 isGrouping = true;
4480 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004481 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4482 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004483 isDeclarationSpecifier() || // 'int(int)' is a function.
4484 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004485 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4486 // considered to be a type, not a K&R identifier-list.
4487 isGrouping = false;
4488 } else {
4489 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4490 isGrouping = true;
4491 }
Mike Stump1eb44332009-09-09 15:08:12 +00004492
Chris Lattneref4715c2008-04-06 05:45:57 +00004493 // If this is a grouping paren, handle:
4494 // direct-declarator: '(' declarator ')'
4495 // direct-declarator: '(' attributes declarator ')'
4496 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004497 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4498 D.setEllipsisLoc(SourceLocation());
4499
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004500 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004501 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004502 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004503 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004504 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004505 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004506 T.getCloseLocation()),
4507 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004508
4509 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004510
4511 // An ellipsis cannot be placed outside parentheses.
4512 if (EllipsisLoc.isValid())
4513 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4514
Chris Lattneref4715c2008-04-06 05:45:57 +00004515 return;
4516 }
Mike Stump1eb44332009-09-09 15:08:12 +00004517
Chris Lattneref4715c2008-04-06 05:45:57 +00004518 // Okay, if this wasn't a grouping paren, it must be the start of a function
4519 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004520 // identifier (and remember where it would have been), then call into
4521 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004522 D.SetIdentifier(0, Tok.getLocation());
4523
David Blaikie42d6d0c2011-12-04 05:04:18 +00004524 // Enter function-declaration scope, limiting any declarators to the
4525 // function prototype scope, including parameter declarators.
4526 ParseScope PrototypeScope(this,
4527 Scope::FunctionPrototypeScope|Scope::DeclScope);
Richard Smithb9c62612012-07-30 21:30:52 +00004528 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004529 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004530}
4531
4532/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4533/// declarator D up to a paren, which indicates that we are parsing function
4534/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004535///
Richard Smith6ee326a2012-04-10 01:32:12 +00004536/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4537/// immediately after the open paren - they should be considered to be the
4538/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004539///
Richard Smith6ee326a2012-04-10 01:32:12 +00004540/// If RequiresArg is true, then the first argument of the function is required
4541/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004542///
Richard Smith6ee326a2012-04-10 01:32:12 +00004543/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4544/// (C++11) ref-qualifier[opt], exception-specification[opt],
4545/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4546///
4547/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004548/// dynamic-exception-specification
4549/// noexcept-specification
4550///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004551void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004552 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004553 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004554 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004555 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004556 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004557 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004558 // lparen is already consumed!
4559 assert(D.isPastIdentifier() && "Should not call before identifier!");
4560
4561 // This should be true when the function has typed arguments.
4562 // Otherwise, it is treated as a K&R-style function.
4563 bool HasProto = false;
4564 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004565 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004566 // Remember where we see an ellipsis, if any.
4567 SourceLocation EllipsisLoc;
4568
4569 DeclSpec DS(AttrFactory);
4570 bool RefQualifierIsLValueRef = true;
4571 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004572 SourceLocation ConstQualifierLoc;
4573 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004574 ExceptionSpecificationType ESpecType = EST_None;
4575 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004576 SmallVector<ParsedType, 2> DynamicExceptions;
4577 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004578 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004579 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004580 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004581
James Molloy16f1f712012-02-29 10:24:19 +00004582 Actions.ActOnStartFunctionDeclarator();
4583
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004584 SourceLocation StartLoc, EndLoc;
4585 SourceLocation LParenLoc, RParenLoc;
4586 LParenLoc = Tracker.getOpenLocation();
4587 StartLoc = LParenLoc;
4588
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004589 if (isFunctionDeclaratorIdentifierList()) {
4590 if (RequiresArg)
4591 Diag(Tok, diag::err_argument_required_after_attribute);
4592
4593 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4594
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004595 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004596 RParenLoc = Tracker.getCloseLocation();
4597 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004598 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004599 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004600 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004601 else if (RequiresArg)
4602 Diag(Tok, diag::err_argument_required_after_attribute);
4603
David Blaikie4e4d0842012-03-11 07:00:24 +00004604 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004605
4606 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004607 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004608 RParenLoc = Tracker.getCloseLocation();
4609 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004610
David Blaikie4e4d0842012-03-11 07:00:24 +00004611 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004612 // FIXME: Accept these components in any order, and produce fixits to
4613 // correct the order if the user gets it wrong. Ideally we should deal
4614 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004615
4616 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004617 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4618 if (!DS.getSourceRange().getEnd().isInvalid()) {
4619 EndLoc = DS.getSourceRange().getEnd();
4620 ConstQualifierLoc = DS.getConstSpecLoc();
4621 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4622 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004623
4624 // Parse ref-qualifier[opt].
4625 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004626 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004627 diag::warn_cxx98_compat_ref_qualifier :
4628 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004629
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004630 RefQualifierIsLValueRef = Tok.is(tok::amp);
4631 RefQualifierLoc = ConsumeToken();
4632 EndLoc = RefQualifierLoc;
4633 }
4634
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004635 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00004636 // If a declaration declares a member function or member function
4637 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004638 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00004639 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004640 // declarator.
Chad Rosier8decdee2012-06-26 22:30:43 +00004641 bool IsCXX11MemberFunction =
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004642 getLangOpts().CPlusPlus0x &&
4643 (D.getContext() == Declarator::MemberContext ||
4644 (D.getContext() == Declarator::FileContext &&
Chad Rosier8decdee2012-06-26 22:30:43 +00004645 D.getCXXScopeSpec().isValid() &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004646 Actions.CurContext->isRecord()));
4647 Sema::CXXThisScopeRAII ThisScope(Actions,
4648 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4649 DS.getTypeQualifiers(),
4650 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004651
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004652 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004653 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004654 DynamicExceptions,
4655 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004656 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004657 if (ESpecType != EST_None)
4658 EndLoc = ESpecRange.getEnd();
4659
Richard Smith6ee326a2012-04-10 01:32:12 +00004660 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4661 // after the exception-specification.
4662 MaybeParseCXX0XAttributes(FnAttrs);
4663
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004664 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004665 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004666 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004667 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4668 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
4669 EndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00004670 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004671 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004672 }
4673 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004674 }
4675
4676 // Remember that we parsed a function type, and remember the attributes.
4677 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004678 IsAmbiguous,
4679 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004680 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004681 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004682 DS.getTypeQualifiers(),
4683 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004684 RefQualifierLoc, ConstQualifierLoc,
4685 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004686 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004687 ESpecType, ESpecRange.getBegin(),
4688 DynamicExceptions.data(),
4689 DynamicExceptionRanges.data(),
4690 DynamicExceptions.size(),
4691 NoexceptExpr.isUsable() ?
4692 NoexceptExpr.get() : 0,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004693 StartLoc, EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004694 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004695 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004696
4697 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004698}
4699
4700/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4701/// identifier list form for a K&R-style function: void foo(a,b,c)
4702///
4703/// Note that identifier-lists are only allowed for normal declarators, not for
4704/// abstract-declarators.
4705bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004706 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004707 && Tok.is(tok::identifier)
4708 && !TryAltiVecVectorToken()
4709 // K&R identifier lists can't have typedefs as identifiers, per C99
4710 // 6.7.5.3p11.
4711 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4712 // Identifier lists follow a really simple grammar: the identifiers can
4713 // be followed *only* by a ", identifier" or ")". However, K&R
4714 // identifier lists are really rare in the brave new modern world, and
4715 // it is very common for someone to typo a type in a non-K&R style
4716 // list. If we are presented with something like: "void foo(intptr x,
4717 // float y)", we don't want to start parsing the function declarator as
4718 // though it is a K&R style declarator just because intptr is an
4719 // invalid type.
4720 //
4721 // To handle this, we check to see if the token after the first
4722 // identifier is a "," or ")". Only then do we parse it as an
4723 // identifier list.
4724 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4725}
4726
4727/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4728/// we found a K&R-style identifier list instead of a typed parameter list.
4729///
4730/// After returning, ParamInfo will hold the parsed parameters.
4731///
4732/// identifier-list: [C99 6.7.5]
4733/// identifier
4734/// identifier-list ',' identifier
4735///
4736void Parser::ParseFunctionDeclaratorIdentifierList(
4737 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004738 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004739 // If there was no identifier specified for the declarator, either we are in
4740 // an abstract-declarator, or we are in a parameter declarator which was found
4741 // to be abstract. In abstract-declarators, identifier lists are not valid:
4742 // diagnose this.
4743 if (!D.getIdentifier())
4744 Diag(Tok, diag::ext_ident_list_in_param);
4745
4746 // Maintain an efficient lookup of params we have seen so far.
4747 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4748
4749 while (1) {
4750 // If this isn't an identifier, report the error and skip until ')'.
4751 if (Tok.isNot(tok::identifier)) {
4752 Diag(Tok, diag::err_expected_ident);
4753 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4754 // Forget we parsed anything.
4755 ParamInfo.clear();
4756 return;
4757 }
4758
4759 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4760
4761 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4762 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4763 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4764
4765 // Verify that the argument identifier has not already been mentioned.
4766 if (!ParamsSoFar.insert(ParmII)) {
4767 Diag(Tok, diag::err_param_redefinition) << ParmII;
4768 } else {
4769 // Remember this identifier in ParamInfo.
4770 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4771 Tok.getLocation(),
4772 0));
4773 }
4774
4775 // Eat the identifier.
4776 ConsumeToken();
4777
4778 // The list continues if we see a comma.
4779 if (Tok.isNot(tok::comma))
4780 break;
4781 ConsumeToken();
4782 }
4783}
4784
4785/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4786/// after the opening parenthesis. This function will not parse a K&R-style
4787/// identifier list.
4788///
Richard Smith6ce48a72012-04-11 04:01:28 +00004789/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4790/// caller parsed those arguments immediately after the open paren - they should
4791/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004792///
4793/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4794/// be the location of the ellipsis, if any was parsed.
4795///
Reid Spencer5f016e22007-07-11 17:01:13 +00004796/// parameter-type-list: [C99 6.7.5]
4797/// parameter-list
4798/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004799/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004800///
4801/// parameter-list: [C99 6.7.5]
4802/// parameter-declaration
4803/// parameter-list ',' parameter-declaration
4804///
4805/// parameter-declaration: [C99 6.7.5]
4806/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004807/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004808/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004809/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004810/// declaration-specifiers abstract-declarator[opt]
4811/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004812/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004813/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004814/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004815///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004816void Parser::ParseParameterDeclarationClause(
4817 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004818 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004819 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004820 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004821
Chris Lattnerf97409f2008-04-06 06:57:35 +00004822 while (1) {
4823 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004824 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4825 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004826 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004827 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004828 }
Mike Stump1eb44332009-09-09 15:08:12 +00004829
Chris Lattnerf97409f2008-04-06 06:57:35 +00004830 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004831 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004832 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004833
Richard Smith6ce48a72012-04-11 04:01:28 +00004834 // Parse any C++11 attributes.
4835 MaybeParseCXX0XAttributes(DS.getAttributes());
4836
John McCall7f040a92010-12-24 02:08:15 +00004837 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004838 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004839 ParseMicrosoftAttributes(DS.getAttributes());
4840
4841 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004842
4843 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004844 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004845 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004846 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4847 // too much hassle.
4848 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004849
Chris Lattnere64c5492009-02-27 18:38:20 +00004850 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004851
Chris Lattnerf97409f2008-04-06 06:57:35 +00004852 // Parse the declarator. This is "PrototypeContext", because we must
4853 // accept either 'declarator' or 'abstract-declarator' here.
4854 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4855 ParseDeclarator(ParmDecl);
4856
4857 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004858 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004859
Chris Lattnerf97409f2008-04-06 06:57:35 +00004860 // Remember this parsed parameter in ParamInfo.
4861 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004862
Douglas Gregor72b505b2008-12-16 21:30:33 +00004863 // DefArgToks is used when the parsing of default arguments needs
4864 // to be delayed.
4865 CachedTokens *DefArgToks = 0;
4866
Chris Lattnerf97409f2008-04-06 06:57:35 +00004867 // If no parameter was specified, verify that *something* was specified,
4868 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004869 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4870 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004871 // Completely missing, emit error.
4872 Diag(DSStart, diag::err_missing_param);
4873 } else {
4874 // Otherwise, we have something. Add it and let semantic analysis try
4875 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004876
Chris Lattnerf97409f2008-04-06 06:57:35 +00004877 // Inform the actions module about the parameter declarator, so it gets
4878 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004879 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004880
4881 // Parse the default argument, if any. We parse the default
4882 // arguments in all dialects; the semantic analysis in
4883 // ActOnParamDefaultArgument will reject the default argument in
4884 // C.
4885 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004886 SourceLocation EqualLoc = Tok.getLocation();
4887
Chris Lattner04421082008-04-08 04:40:51 +00004888 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004889 if (D.getContext() == Declarator::MemberContext) {
4890 // If we're inside a class definition, cache the tokens
4891 // corresponding to the default argument. We'll actually parse
4892 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004893 // FIXME: Can we use a smart pointer for Toks?
4894 DefArgToks = new CachedTokens;
4895
Mike Stump1eb44332009-09-09 15:08:12 +00004896 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004897 /*StopAtSemi=*/true,
4898 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004899 delete DefArgToks;
4900 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004901 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004902 } else {
4903 // Mark the end of the default argument so that we know when to
4904 // stop when we parse it later on.
4905 Token DefArgEnd;
4906 DefArgEnd.startToken();
4907 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4908 DefArgEnd.setLocation(Tok.getLocation());
4909 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004910 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004911 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004912 }
Chris Lattner04421082008-04-08 04:40:51 +00004913 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004914 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004915 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004916
Chad Rosier8decdee2012-06-26 22:30:43 +00004917 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004918 // used.
4919 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004920 Sema::PotentiallyEvaluatedIfUsed,
4921 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004922
Sebastian Redl84407ba2012-03-14 15:54:00 +00004923 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004924 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4925 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004926 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004927 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004928 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004929 if (DefArgResult.isInvalid()) {
4930 Actions.ActOnParamDefaultArgumentError(Param);
4931 SkipUntil(tok::comma, tok::r_paren, true, true);
4932 } else {
4933 // Inform the actions module about the default argument
4934 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004935 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004936 }
Chris Lattner04421082008-04-08 04:40:51 +00004937 }
4938 }
Mike Stump1eb44332009-09-09 15:08:12 +00004939
4940 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4941 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004942 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004943 }
4944
4945 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004946 if (Tok.isNot(tok::comma)) {
4947 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004948 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00004949
David Blaikie4e4d0842012-03-11 07:00:24 +00004950 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004951 // We have ellipsis without a preceding ',', which is ill-formed
4952 // in C. Complain and provide the fix.
4953 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004954 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004955 }
4956 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004957
Douglas Gregored5d6512009-09-22 21:41:40 +00004958 break;
4959 }
Mike Stump1eb44332009-09-09 15:08:12 +00004960
Chris Lattnerf97409f2008-04-06 06:57:35 +00004961 // Consume the comma.
4962 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004963 }
Mike Stump1eb44332009-09-09 15:08:12 +00004964
Chris Lattner66d28652008-04-06 06:34:08 +00004965}
Chris Lattneref4715c2008-04-06 05:45:57 +00004966
Reid Spencer5f016e22007-07-11 17:01:13 +00004967/// [C90] direct-declarator '[' constant-expression[opt] ']'
4968/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4969/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4970/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4971/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004972/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4973/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004974void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004975 if (CheckProhibitedCXX11Attribute())
4976 return;
4977
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004978 BalancedDelimiterTracker T(*this, tok::l_square);
4979 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004980
Chris Lattner378c7e42008-12-18 07:27:21 +00004981 // C array syntax has many features, but by-far the most common is [] and [4].
4982 // This code does a fast path to handle some of the most obvious cases.
4983 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004984 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004985 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004986 MaybeParseCXX0XAttributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00004987
Chris Lattner378c7e42008-12-18 07:27:21 +00004988 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004989 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004990 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004991 T.getOpenLocation(),
4992 T.getCloseLocation()),
4993 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004994 return;
4995 } else if (Tok.getKind() == tok::numeric_constant &&
4996 GetLookAheadToken(1).is(tok::r_square)) {
4997 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004998 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004999 ConsumeToken();
5000
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005001 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005002 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00005003 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005004
Chris Lattner378c7e42008-12-18 07:27:21 +00005005 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005006 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00005007 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005008 T.getOpenLocation(),
5009 T.getCloseLocation()),
5010 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005011 return;
5012 }
Mike Stump1eb44332009-09-09 15:08:12 +00005013
Reid Spencer5f016e22007-07-11 17:01:13 +00005014 // If valid, this location is the position where we read the 'static' keyword.
5015 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005016 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005017 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005018
Reid Spencer5f016e22007-07-11 17:01:13 +00005019 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005020 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005021 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005022 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005023
Reid Spencer5f016e22007-07-11 17:01:13 +00005024 // If we haven't already read 'static', check to see if there is one after the
5025 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005026 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005027 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005028
Reid Spencer5f016e22007-07-11 17:01:13 +00005029 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5030 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005031 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005032
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005033 // Handle the case where we have '[*]' as the array size. However, a leading
5034 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005035 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005036 // infrequent, use of lookahead is not costly here.
5037 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005038 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005039
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005040 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005041 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005042 StaticLoc = SourceLocation(); // Drop the static.
5043 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005044 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005045 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005046 // Note, in C89, this production uses the constant-expr production instead
5047 // of assignment-expr. The only difference is that assignment-expr allows
5048 // things like '=' and '*='. Sema rejects these in C89 mode because they
5049 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005050
Douglas Gregore0762c92009-06-19 23:52:42 +00005051 // Parse the constant-expression or assignment-expression now (depending
5052 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005053 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005054 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005055 } else {
5056 EnterExpressionEvaluationContext Unevaluated(Actions,
5057 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005058 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005059 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005060 }
Mike Stump1eb44332009-09-09 15:08:12 +00005061
Reid Spencer5f016e22007-07-11 17:01:13 +00005062 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005063 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005064 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005065 // If the expression was invalid, skip it.
5066 SkipUntil(tok::r_square);
5067 return;
5068 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005069
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005070 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005071
John McCall0b7e6782011-03-24 11:26:52 +00005072 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00005073 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005074
Chris Lattner378c7e42008-12-18 07:27:21 +00005075 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005076 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005077 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005078 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005079 T.getOpenLocation(),
5080 T.getCloseLocation()),
5081 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005082}
5083
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005084/// [GNU] typeof-specifier:
5085/// typeof ( expressions )
5086/// typeof ( type-name )
5087/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005088///
5089void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005090 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005091 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005092 SourceLocation StartLoc = ConsumeToken();
5093
John McCallcfb708c2010-01-13 20:03:27 +00005094 const bool hasParens = Tok.is(tok::l_paren);
5095
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005096 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5097 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005098
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005099 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005100 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005101 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005102 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5103 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005104 if (hasParens)
5105 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005106
5107 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005108 // FIXME: Not accurate, the range gets one token more than it should.
5109 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005110 else
5111 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005112
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005113 if (isCastExpr) {
5114 if (!CastTy) {
5115 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005116 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005117 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005118
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005119 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005120 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005121 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5122 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005123 DiagID, CastTy))
5124 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005125 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005126 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005127
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005128 // If we get here, the operand to the typeof was an expresion.
5129 if (Operand.isInvalid()) {
5130 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005131 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005132 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005133
Eli Friedman71b8fb52012-01-21 01:01:51 +00005134 // We might need to transform the operand if it is potentially evaluated.
5135 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5136 if (Operand.isInvalid()) {
5137 DS.SetTypeSpecError();
5138 return;
5139 }
5140
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005141 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005142 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005143 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5144 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005145 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005146 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005147}
Chris Lattner1b492422010-02-28 18:33:55 +00005148
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005149/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005150/// _Atomic ( type-name )
5151///
5152void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5153 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5154
5155 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005156 BalancedDelimiterTracker T(*this, tok::l_paren);
5157 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00005158 SkipUntil(tok::r_paren);
5159 return;
5160 }
5161
5162 TypeResult Result = ParseTypeName();
5163 if (Result.isInvalid()) {
5164 SkipUntil(tok::r_paren);
5165 return;
5166 }
5167
5168 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005169 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005170
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005171 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005172 return;
5173
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005174 DS.setTypeofParensRange(T.getRange());
5175 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005176
5177 const char *PrevSpec = 0;
5178 unsigned DiagID;
5179 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5180 DiagID, Result.release()))
5181 Diag(StartLoc, DiagID) << PrevSpec;
5182}
5183
Chris Lattner1b492422010-02-28 18:33:55 +00005184
5185/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5186/// from TryAltiVecVectorToken.
5187bool Parser::TryAltiVecVectorTokenOutOfLine() {
5188 Token Next = NextToken();
5189 switch (Next.getKind()) {
5190 default: return false;
5191 case tok::kw_short:
5192 case tok::kw_long:
5193 case tok::kw_signed:
5194 case tok::kw_unsigned:
5195 case tok::kw_void:
5196 case tok::kw_char:
5197 case tok::kw_int:
5198 case tok::kw_float:
5199 case tok::kw_double:
5200 case tok::kw_bool:
5201 case tok::kw___pixel:
5202 Tok.setKind(tok::kw___vector);
5203 return true;
5204 case tok::identifier:
5205 if (Next.getIdentifierInfo() == Ident_pixel) {
5206 Tok.setKind(tok::kw___vector);
5207 return true;
5208 }
5209 return false;
5210 }
5211}
5212
5213bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5214 const char *&PrevSpec, unsigned &DiagID,
5215 bool &isInvalid) {
5216 if (Tok.getIdentifierInfo() == Ident_vector) {
5217 Token Next = NextToken();
5218 switch (Next.getKind()) {
5219 case tok::kw_short:
5220 case tok::kw_long:
5221 case tok::kw_signed:
5222 case tok::kw_unsigned:
5223 case tok::kw_void:
5224 case tok::kw_char:
5225 case tok::kw_int:
5226 case tok::kw_float:
5227 case tok::kw_double:
5228 case tok::kw_bool:
5229 case tok::kw___pixel:
5230 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5231 return true;
5232 case tok::identifier:
5233 if (Next.getIdentifierInfo() == Ident_pixel) {
5234 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5235 return true;
5236 }
5237 break;
5238 default:
5239 break;
5240 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005241 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005242 DS.isTypeAltiVecVector()) {
5243 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5244 return true;
5245 }
5246 return false;
5247}