blob: 7c5b1531407ca8fec5175839c9f8757f6c83047e [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.
DeLesley Hutchins161db022012-11-02 21:44:32 +0000146 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000147 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()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000738 Diag(Tok, diag::err_expected_string_literal)
739 << /*Source='availability attribute'*/2;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000740 SkipUntil(tok::r_paren);
741 return;
742 }
743 MessageExpr = ParseStringLiteralExpression();
744 break;
745 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000746
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000747 SourceRange VersionRange;
748 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000749
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000750 if (Version.empty()) {
751 SkipUntil(tok::r_paren);
752 return;
753 }
754
755 unsigned Index;
756 if (Keyword == Ident_introduced)
757 Index = Introduced;
758 else if (Keyword == Ident_deprecated)
759 Index = Deprecated;
760 else if (Keyword == Ident_obsoleted)
761 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000762 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000763 Index = Unknown;
764
765 if (Index < Unknown) {
766 if (!Changes[Index].KeywordLoc.isInvalid()) {
767 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000768 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000769 << SourceRange(Changes[Index].KeywordLoc,
770 Changes[Index].VersionRange.getEnd());
771 }
772
773 Changes[Index].KeywordLoc = KeywordLoc;
774 Changes[Index].Version = Version;
775 Changes[Index].VersionRange = VersionRange;
776 } else {
777 Diag(KeywordLoc, diag::err_availability_unknown_change)
778 << Keyword << VersionRange;
779 }
780
781 if (Tok.isNot(tok::comma))
782 break;
783
784 ConsumeToken();
785 } while (true);
786
787 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000788 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000789 return;
790
791 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000792 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000793
Douglas Gregorb53e4172011-03-26 03:35:55 +0000794 // The 'unavailable' availability cannot be combined with any other
795 // availability changes. Make sure that hasn't happened.
796 if (UnavailableLoc.isValid()) {
797 bool Complained = false;
798 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
799 if (Changes[Index].KeywordLoc.isValid()) {
800 if (!Complained) {
801 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
802 << SourceRange(Changes[Index].KeywordLoc,
803 Changes[Index].VersionRange.getEnd());
804 Complained = true;
805 }
806
807 // Clear out the availability.
808 Changes[Index] = AvailabilityChange();
809 }
810 }
811 }
812
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000813 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000814 attrs.addNew(&Availability,
815 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000816 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000817 Platform, PlatformLoc,
818 Changes[Introduced],
819 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000820 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000821 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000822 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000823}
824
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000825
826// Late Parsed Attributes:
827// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
828
829void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
830
831void Parser::LateParsedClass::ParseLexedAttributes() {
832 Self->ParseLexedAttributes(*Class);
833}
834
835void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000836 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000837}
838
839/// Wrapper class which calls ParseLexedAttribute, after setting up the
840/// scope appropriately.
841void Parser::ParseLexedAttributes(ParsingClass &Class) {
842 // Deal with templates
843 // FIXME: Test cases to make sure this does the right thing for templates.
844 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
845 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
846 HasTemplateScope);
847 if (HasTemplateScope)
848 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
849
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000850 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000851 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000852 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000853 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
854 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
855
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000856 // Enter the scope of nested classes
857 if (!AlreadyHasClassScope)
858 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
859 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000860 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000861 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
862 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
863 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000864 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000865
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000866 if (!AlreadyHasClassScope)
867 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
868 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000869}
870
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000871
872/// \brief Parse all attributes in LAs, and attach them to Decl D.
873void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
874 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +0000875 assert(LAs.parseSoon() &&
876 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000877 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +0000878 if (D)
879 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000880 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000881 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000882 }
883 LAs.clear();
884}
885
886
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000887/// \brief Finish parsing an attribute for which parsing was delayed.
888/// This will be called at the end of parsing a class declaration
889/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +0000890/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000891/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000892void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
893 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000894 // Save the current token position.
895 SourceLocation OrigLoc = Tok.getLocation();
896
897 // Append the current token at the end of the new token stream so that it
898 // doesn't get lost.
899 LA.Toks.push_back(Tok);
900 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
901 // Consume the previously pushed token.
902 ConsumeAnyToken();
903
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000904 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
905 Diag(Tok, diag::warn_attribute_on_function_definition)
906 << LA.AttrName.getName();
907 }
908
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000909 ParsedAttributes Attrs(AttrFactory);
910 SourceLocation endLoc;
911
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000912 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000913 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000914 NamedDecl *ND = dyn_cast<NamedDecl>(D);
915 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000916
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000917 // Allow 'this' within late-parsed attributes.
918 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
919 /*TypeQuals=*/0,
920 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000921
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000922 if (LA.Decls.size() == 1) {
923 // If the Decl is templatized, add template parameters to scope.
924 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
925 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
926 if (HasTemplateScope)
927 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000928
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000929 // If the Decl is on a function, add function parameters to the scope.
930 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
931 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
932 if (HasFunScope)
933 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000934
Michael Han6880f492012-10-03 01:56:22 +0000935 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000936 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000937
938 if (HasFunScope) {
939 Actions.ActOnExitFunctionContext();
940 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
941 }
942 if (HasTemplateScope) {
943 TempScope.Exit();
944 }
945 } else {
946 // If there are multiple decls, then the decl cannot be within the
947 // function scope.
Michael Han6880f492012-10-03 01:56:22 +0000948 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000949 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000950 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000951 } else {
952 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000953 }
954
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000955 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
956 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
957 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000958
959 if (Tok.getLocation() != OrigLoc) {
960 // Due to a parsing error, we either went over the cached tokens or
961 // there are still cached tokens left, so we skip the leftover tokens.
962 // Since this is an uncommon situation that should be avoided, use the
963 // expensive isBeforeInTranslationUnit call.
964 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
965 OrigLoc))
966 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000967 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000968 }
969}
970
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000971/// \brief Wrapper around a case statement checking if AttrName is
972/// one of the thread safety attributes
973bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
974 return llvm::StringSwitch<bool>(AttrName)
975 .Case("guarded_by", true)
976 .Case("guarded_var", true)
977 .Case("pt_guarded_by", true)
978 .Case("pt_guarded_var", true)
979 .Case("lockable", true)
980 .Case("scoped_lockable", true)
981 .Case("no_thread_safety_analysis", true)
982 .Case("acquired_after", true)
983 .Case("acquired_before", true)
984 .Case("exclusive_lock_function", true)
985 .Case("shared_lock_function", true)
986 .Case("exclusive_trylock_function", true)
987 .Case("shared_trylock_function", true)
988 .Case("unlock_function", true)
989 .Case("lock_returned", true)
990 .Case("locks_excluded", true)
991 .Case("exclusive_locks_required", true)
992 .Case("shared_locks_required", true)
993 .Default(false);
994}
995
996/// \brief Parse the contents of thread safety attributes. These
997/// should always be parsed as an expression list.
998///
999/// We need to special case the parsing due to the fact that if the first token
1000/// of the first argument is an identifier, the main parse loop will store
1001/// that token as a "parameter" and the rest of
1002/// the arguments will be added to a list of "arguments". However,
1003/// subsequent tokens in the first argument are lost. We instead parse each
1004/// argument as an expression and add all arguments to the list of "arguments".
1005/// In future, we will take advantage of this special case to also
1006/// deal with some argument scoping issues here (for example, referring to a
1007/// function parameter in the attribute on that function).
1008void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1009 SourceLocation AttrNameLoc,
1010 ParsedAttributes &Attrs,
1011 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001012 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001013
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001014 BalancedDelimiterTracker T(*this, tok::l_paren);
1015 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001016
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001017 ExprVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001018 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001019
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001020 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001021 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001022 ExprResult ArgExpr(ParseAssignmentExpression());
1023 if (ArgExpr.isInvalid()) {
1024 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001025 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001026 break;
1027 } else {
1028 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001029 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001030 if (Tok.isNot(tok::comma))
1031 break;
1032 ConsumeToken(); // Eat the comma, move to the next argument
1033 }
1034 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001035 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001036 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001037 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001038 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001039 if (EndLoc)
1040 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001041}
1042
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001043void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1044 SourceLocation AttrNameLoc,
1045 ParsedAttributes &Attrs,
1046 SourceLocation *EndLoc) {
1047 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1048
1049 BalancedDelimiterTracker T(*this, tok::l_paren);
1050 T.consumeOpen();
1051
1052 if (Tok.isNot(tok::identifier)) {
1053 Diag(Tok, diag::err_expected_ident);
1054 T.skipToEnd();
1055 return;
1056 }
1057 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1058 SourceLocation ArgumentKindLoc = ConsumeToken();
1059
1060 if (Tok.isNot(tok::comma)) {
1061 Diag(Tok, diag::err_expected_comma);
1062 T.skipToEnd();
1063 return;
1064 }
1065 ConsumeToken();
1066
1067 SourceRange MatchingCTypeRange;
1068 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1069 if (MatchingCType.isInvalid()) {
1070 T.skipToEnd();
1071 return;
1072 }
1073
1074 bool LayoutCompatible = false;
1075 bool MustBeNull = false;
1076 while (Tok.is(tok::comma)) {
1077 ConsumeToken();
1078 if (Tok.isNot(tok::identifier)) {
1079 Diag(Tok, diag::err_expected_ident);
1080 T.skipToEnd();
1081 return;
1082 }
1083 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1084 if (Flag->isStr("layout_compatible"))
1085 LayoutCompatible = true;
1086 else if (Flag->isStr("must_be_null"))
1087 MustBeNull = true;
1088 else {
1089 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1090 T.skipToEnd();
1091 return;
1092 }
1093 ConsumeToken(); // consume flag
1094 }
1095
1096 if (!T.consumeClose()) {
1097 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1098 ArgumentKind, ArgumentKindLoc,
1099 MatchingCType.release(), LayoutCompatible,
1100 MustBeNull, AttributeList::AS_GNU);
1101 }
1102
1103 if (EndLoc)
1104 *EndLoc = T.getCloseLocation();
1105}
1106
Richard Smith6ee326a2012-04-10 01:32:12 +00001107/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1108/// of a C++11 attribute-specifier in a location where an attribute is not
1109/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1110/// situation.
1111///
1112/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1113/// this doesn't appear to actually be an attribute-specifier, and the caller
1114/// should try to parse it.
1115bool Parser::DiagnoseProhibitedCXX11Attribute() {
1116 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1117
1118 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1119 case CAK_NotAttributeSpecifier:
1120 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1121 return false;
1122
1123 case CAK_InvalidAttributeSpecifier:
1124 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1125 return false;
1126
1127 case CAK_AttributeSpecifier:
1128 // Parse and discard the attributes.
1129 SourceLocation BeginLoc = ConsumeBracket();
1130 ConsumeBracket();
1131 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1132 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1133 SourceLocation EndLoc = ConsumeBracket();
1134 Diag(BeginLoc, diag::err_attributes_not_allowed)
1135 << SourceRange(BeginLoc, EndLoc);
1136 return true;
1137 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001138 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001139}
1140
John McCall7f040a92010-12-24 02:08:15 +00001141void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1142 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1143 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001144}
1145
Michael Hanf64231e2012-11-06 19:34:54 +00001146void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1147 AttributeList *AttrList = attrs.getList();
1148 while (AttrList) {
1149 if (AttrList->isCXX0XAttribute()) {
1150 Diag(AttrList->getLoc(), diag::warn_attribute_no_decl)
1151 << AttrList->getName();
1152 AttrList->setInvalid();
1153 }
1154 AttrList = AttrList->getNext();
1155 }
1156}
1157
Reid Spencer5f016e22007-07-11 17:01:13 +00001158/// ParseDeclaration - Parse a full 'declaration', which consists of
1159/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001160/// 'Context' should be a Declarator::TheContext value. This returns the
1161/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001162///
1163/// declaration: [C99 6.7]
1164/// block-declaration ->
1165/// simple-declaration
1166/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001167/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001168/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001169/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001170/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001171/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001172/// others... [FIXME]
1173///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001174Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1175 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001176 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001177 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001178 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001179 // Must temporarily exit the objective-c container scope for
1180 // parsing c none objective-c decls.
1181 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001182
John McCalld226f652010-08-21 09:40:31 +00001183 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001184 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001185 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001186 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001187 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001188 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001189 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001190 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001191 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001192 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001193 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001194 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001195 SourceLocation InlineLoc = ConsumeToken();
1196 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1197 break;
1198 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001199 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001200 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001201 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001202 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001203 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001204 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001205 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001206 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001207 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001208 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001209 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001210 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001211 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001212 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001213 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001214 default:
John McCall7f040a92010-12-24 02:08:15 +00001215 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001216 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001217
Chris Lattner682bf922009-03-29 16:50:03 +00001218 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001219 // single decl, convert it now. Alias declarations can also declare a type;
1220 // include that too if it is present.
1221 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001222}
1223
1224/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1225/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001226/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1227/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001228///[C90/C++]init-declarator-list ';' [TODO]
1229/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001230///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001231/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001232/// attribute-specifier-seq[opt] type-specifier-seq declarator
1233///
Chris Lattnercd147752009-03-29 17:27:48 +00001234/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001235/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001236///
1237/// If FRI is non-null, we might be parsing a for-range-declaration instead
1238/// of a simple-declaration. If we find that we are, we also parse the
1239/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001240Parser::DeclGroupPtrTy
1241Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1242 SourceLocation &DeclEnd,
1243 ParsedAttributesWithRange &attrs,
1244 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001246 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001247 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001248
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001249 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001250 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001251
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1253 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001254 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001255 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001256 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001257 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001258 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001259 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001260 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001262
1263 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001264}
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Richard Smith0706df42011-10-19 21:33:05 +00001266/// Returns true if this might be the start of a declarator, or a common typo
1267/// for a declarator.
1268bool Parser::MightBeDeclarator(unsigned Context) {
1269 switch (Tok.getKind()) {
1270 case tok::annot_cxxscope:
1271 case tok::annot_template_id:
1272 case tok::caret:
1273 case tok::code_completion:
1274 case tok::coloncolon:
1275 case tok::ellipsis:
1276 case tok::kw___attribute:
1277 case tok::kw_operator:
1278 case tok::l_paren:
1279 case tok::star:
1280 return true;
1281
1282 case tok::amp:
1283 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001284 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001285
Richard Smith1c94c162012-01-09 22:31:44 +00001286 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001287 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001288 NextToken().is(tok::l_square);
1289
1290 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001291 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001292
Richard Smith0706df42011-10-19 21:33:05 +00001293 case tok::identifier:
1294 switch (NextToken().getKind()) {
1295 case tok::code_completion:
1296 case tok::coloncolon:
1297 case tok::comma:
1298 case tok::equal:
1299 case tok::equalequal: // Might be a typo for '='.
1300 case tok::kw_alignas:
1301 case tok::kw_asm:
1302 case tok::kw___attribute:
1303 case tok::l_brace:
1304 case tok::l_paren:
1305 case tok::l_square:
1306 case tok::less:
1307 case tok::r_brace:
1308 case tok::r_paren:
1309 case tok::r_square:
1310 case tok::semi:
1311 return true;
1312
1313 case tok::colon:
1314 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001315 // and in block scope it's probably a label. Inside a class definition,
1316 // this is a bit-field.
1317 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001318 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001319
1320 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001321 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001322
1323 default:
1324 return false;
1325 }
1326
1327 default:
1328 return false;
1329 }
1330}
1331
Richard Smith994d73f2012-04-11 20:59:20 +00001332/// Skip until we reach something which seems like a sensible place to pick
1333/// up parsing after a malformed declaration. This will sometimes stop sooner
1334/// than SkipUntil(tok::r_brace) would, but will never stop later.
1335void Parser::SkipMalformedDecl() {
1336 while (true) {
1337 switch (Tok.getKind()) {
1338 case tok::l_brace:
1339 // Skip until matching }, then stop. We've probably skipped over
1340 // a malformed class or function definition or similar.
1341 ConsumeBrace();
1342 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1343 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1344 // This declaration isn't over yet. Keep skipping.
1345 continue;
1346 }
1347 if (Tok.is(tok::semi))
1348 ConsumeToken();
1349 return;
1350
1351 case tok::l_square:
1352 ConsumeBracket();
1353 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1354 continue;
1355
1356 case tok::l_paren:
1357 ConsumeParen();
1358 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1359 continue;
1360
1361 case tok::r_brace:
1362 return;
1363
1364 case tok::semi:
1365 ConsumeToken();
1366 return;
1367
1368 case tok::kw_inline:
1369 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001370 // a good place to pick back up parsing, except in an Objective-C
1371 // @interface context.
1372 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1373 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001374 return;
1375 break;
1376
1377 case tok::kw_namespace:
1378 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001379 // place to pick back up parsing, except in an Objective-C
1380 // @interface context.
1381 if (Tok.isAtStartOfLine() &&
1382 (!ParsingInObjCContainer || CurParsedObjCImpl))
1383 return;
1384 break;
1385
1386 case tok::at:
1387 // @end is very much like } in Objective-C contexts.
1388 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1389 ParsingInObjCContainer)
1390 return;
1391 break;
1392
1393 case tok::minus:
1394 case tok::plus:
1395 // - and + probably start new method declarations in Objective-C contexts.
1396 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001397 return;
1398 break;
1399
1400 case tok::eof:
1401 return;
1402
1403 default:
1404 break;
1405 }
1406
1407 ConsumeAnyToken();
1408 }
1409}
1410
John McCalld8ac0572009-11-03 19:26:08 +00001411/// ParseDeclGroup - Having concluded that this is either a function
1412/// definition or a group of object declarations, actually parse the
1413/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001414Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1415 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001416 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001417 SourceLocation *DeclEnd,
1418 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001419 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001420 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001421 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001422
John McCalld8ac0572009-11-03 19:26:08 +00001423 // Bail out if the first declarator didn't seem well-formed.
1424 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001425 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001426 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001427 }
Mike Stump1eb44332009-09-09 15:08:12 +00001428
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001429 // Save late-parsed attributes for now; they need to be parsed in the
1430 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001431 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1432 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001433 if (D.isFunctionDeclarator())
1434 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1435
Chris Lattnerc82daef2010-07-11 22:24:20 +00001436 // Check to see if we have a function *definition* which must have a body.
1437 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1438 // Look at the next token to make sure that this isn't a function
1439 // declaration. We have to check this because __attribute__ might be the
1440 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001441 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001442
Chris Lattner004659a2010-07-11 22:42:07 +00001443 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001444 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1445 Diag(Tok, diag::err_function_declared_typedef);
1446
1447 // Recover by treating the 'typedef' as spurious.
1448 DS.ClearStorageClassSpecs();
1449 }
1450
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001451 Decl *TheDecl =
1452 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001453 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001454 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001455
Chris Lattner004659a2010-07-11 22:42:07 +00001456 if (isDeclarationSpecifier()) {
1457 // If there is an invalid declaration specifier right after the function
1458 // prototype, then we must be in a missing semicolon case where this isn't
1459 // actually a body. Just fall through into the code that handles it as a
1460 // prototype, and let the top-level code handle the erroneous declspec
1461 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001462 } else {
1463 Diag(Tok, diag::err_expected_fn_body);
1464 SkipUntil(tok::semi);
1465 return DeclGroupPtrTy();
1466 }
1467 }
1468
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001469 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001470 return DeclGroupPtrTy();
1471
1472 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1473 // must parse and analyze the for-range-initializer before the declaration is
1474 // analyzed.
1475 if (FRI && Tok.is(tok::colon)) {
1476 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001477 if (Tok.is(tok::l_brace))
1478 FRI->RangeExpr = ParseBraceInitializer();
1479 else
1480 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001481 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1482 Actions.ActOnCXXForRangeDecl(ThisDecl);
1483 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001484 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001485 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1486 }
1487
Chris Lattner5f9e2722011-07-23 10:55:15 +00001488 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001489 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001490 if (LateParsedAttrs.size() > 0)
1491 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001492 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001493 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001494 DeclsInGroup.push_back(FirstDecl);
1495
Richard Smith0706df42011-10-19 21:33:05 +00001496 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001497
John McCalld8ac0572009-11-03 19:26:08 +00001498 // If we don't have a comma, it is either the end of the list (a ';') or an
1499 // error, bail out.
1500 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001501 SourceLocation CommaLoc = ConsumeToken();
1502
1503 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1504 // This comma was followed by a line-break and something which can't be
1505 // the start of a declarator. The comma was probably a typo for a
1506 // semicolon.
1507 Diag(CommaLoc, diag::err_expected_semi_declaration)
1508 << FixItHint::CreateReplacement(CommaLoc, ";");
1509 ExpectSemi = false;
1510 break;
1511 }
John McCalld8ac0572009-11-03 19:26:08 +00001512
1513 // Parse the next declarator.
1514 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001515 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001516
1517 // Accept attributes in an init-declarator. In the first declarator in a
1518 // declaration, these would be part of the declspec. In subsequent
1519 // declarators, they become part of the declarator itself, so that they
1520 // don't apply to declarators after *this* one. Examples:
1521 // short __attribute__((common)) var; -> declspec
1522 // short var __attribute__((common)); -> declarator
1523 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001524 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001525
1526 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001527 if (!D.isInvalidType()) {
1528 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1529 D.complete(ThisDecl);
1530 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001531 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001532 }
John McCalld8ac0572009-11-03 19:26:08 +00001533 }
1534
1535 if (DeclEnd)
1536 *DeclEnd = Tok.getLocation();
1537
Richard Smith0706df42011-10-19 21:33:05 +00001538 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001539 ExpectAndConsumeSemi(Context == Declarator::FileContext
1540 ? diag::err_invalid_token_after_toplevel_declarator
1541 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001542 // Okay, there was no semicolon and one was expected. If we see a
1543 // declaration specifier, just assume it was missing and continue parsing.
1544 // Otherwise things are very confused and we skip to recover.
1545 if (!isDeclarationSpecifier()) {
1546 SkipUntil(tok::r_brace, true, true);
1547 if (Tok.is(tok::semi))
1548 ConsumeToken();
1549 }
John McCalld8ac0572009-11-03 19:26:08 +00001550 }
1551
Douglas Gregor23c94db2010-07-02 17:43:08 +00001552 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001553 DeclsInGroup.data(),
1554 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001555}
1556
Richard Smithad762fc2011-04-14 22:09:26 +00001557/// Parse an optional simple-asm-expr and attributes, and attach them to a
1558/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001559bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001560 // If a simple-asm-expr is present, parse it.
1561 if (Tok.is(tok::kw_asm)) {
1562 SourceLocation Loc;
1563 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1564 if (AsmLabel.isInvalid()) {
1565 SkipUntil(tok::semi, true, true);
1566 return true;
1567 }
1568
1569 D.setAsmLabel(AsmLabel.release());
1570 D.SetRangeEnd(Loc);
1571 }
1572
1573 MaybeParseGNUAttributes(D);
1574 return false;
1575}
1576
Douglas Gregor1426e532009-05-12 21:31:51 +00001577/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1578/// declarator'. This method parses the remainder of the declaration
1579/// (including any attributes or initializer, among other things) and
1580/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001581///
Reid Spencer5f016e22007-07-11 17:01:13 +00001582/// init-declarator: [C99 6.7]
1583/// declarator
1584/// declarator '=' initializer
1585/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1586/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001587/// [C++] declarator initializer[opt]
1588///
1589/// [C++] initializer:
1590/// [C++] '=' initializer-clause
1591/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001592/// [C++0x] '=' 'default' [TODO]
1593/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001594/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001595///
1596/// According to the standard grammar, =default and =delete are function
1597/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001598///
John McCalld226f652010-08-21 09:40:31 +00001599Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001600 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001601 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001602 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Richard Smithad762fc2011-04-14 22:09:26 +00001604 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1605}
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Richard Smithad762fc2011-04-14 22:09:26 +00001607Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1608 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001609 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001610 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001611 switch (TemplateInfo.Kind) {
1612 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001613 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001614 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001615
Douglas Gregord5a423b2009-09-25 18:43:00 +00001616 case ParsedTemplateInfo::Template:
1617 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001618 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001619 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001620 D);
1621 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001622
Douglas Gregord5a423b2009-09-25 18:43:00 +00001623 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001624 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001625 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001626 TemplateInfo.ExternLoc,
1627 TemplateInfo.TemplateLoc,
1628 D);
1629 if (ThisRes.isInvalid()) {
1630 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001631 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001632 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001633
Douglas Gregord5a423b2009-09-25 18:43:00 +00001634 ThisDecl = ThisRes.get();
1635 break;
1636 }
1637 }
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Richard Smith34b41d92011-02-20 03:19:35 +00001639 bool TypeContainsAuto =
1640 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1641
Douglas Gregor1426e532009-05-12 21:31:51 +00001642 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001643 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001644 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001645 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001646 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001647 if (D.isFunctionDeclarator())
1648 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1649 << 1 /* delete */;
1650 else
1651 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001652 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001653 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001654 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1655 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001656 else
1657 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001658 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001659 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001660 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001661 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001662 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001663
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001664 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001665 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001666 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001667 cutOffParsing();
1668 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001669 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001670
John McCall60d7b3a2010-08-24 06:29:42 +00001671 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001672
David Blaikie4e4d0842012-03-11 07:00:24 +00001673 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001674 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001675 ExitScope();
1676 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001677
Douglas Gregor1426e532009-05-12 21:31:51 +00001678 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001679 SkipUntil(tok::comma, true, true);
1680 Actions.ActOnInitializerError(ThisDecl);
1681 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001682 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1683 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001684 }
1685 } else if (Tok.is(tok::l_paren)) {
1686 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001687 BalancedDelimiterTracker T(*this, tok::l_paren);
1688 T.consumeOpen();
1689
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001690 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001691 CommaLocsTy CommaLocs;
1692
David Blaikie4e4d0842012-03-11 07:00:24 +00001693 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001694 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001695 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001696 }
1697
Douglas Gregor1426e532009-05-12 21:31:51 +00001698 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001699 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001700 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001701
David Blaikie4e4d0842012-03-11 07:00:24 +00001702 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001703 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001704 ExitScope();
1705 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001706 } else {
1707 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001708 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001709
1710 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1711 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001712
David Blaikie4e4d0842012-03-11 07:00:24 +00001713 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001714 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001715 ExitScope();
1716 }
1717
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001718 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1719 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001720 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001721 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1722 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001723 }
Fariborz Jahanian3b5f9dc2012-07-03 22:54:28 +00001724 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001725 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001726 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001727 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1728
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001729 if (D.getCXXScopeSpec().isSet()) {
1730 EnterScope(0);
1731 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1732 }
1733
1734 ExprResult Init(ParseBraceInitializer());
1735
1736 if (D.getCXXScopeSpec().isSet()) {
1737 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1738 ExitScope();
1739 }
1740
1741 if (Init.isInvalid()) {
1742 Actions.ActOnInitializerError(ThisDecl);
1743 } else
1744 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1745 /*DirectInit=*/true, TypeContainsAuto);
1746
Douglas Gregor1426e532009-05-12 21:31:51 +00001747 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001748 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001749 }
1750
Richard Smith483b9f32011-02-21 20:05:19 +00001751 Actions.FinalizeDeclaration(ThisDecl);
1752
Douglas Gregor1426e532009-05-12 21:31:51 +00001753 return ThisDecl;
1754}
1755
Reid Spencer5f016e22007-07-11 17:01:13 +00001756/// ParseSpecifierQualifierList
1757/// specifier-qualifier-list:
1758/// type-specifier specifier-qualifier-list[opt]
1759/// type-qualifier specifier-qualifier-list[opt]
1760/// [GNU] attributes specifier-qualifier-list[opt]
1761///
Richard Smith69730c12012-03-12 07:56:15 +00001762void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1763 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001764 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1765 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001766 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001767 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Reid Spencer5f016e22007-07-11 17:01:13 +00001769 // Validate declspec for type-name.
1770 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001771 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1772 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001773 Diag(Tok, diag::err_expected_type);
1774 DS.SetTypeSpecError();
1775 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1776 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001778 if (!DS.hasTypeSpecifier())
1779 DS.SetTypeSpecError();
1780 }
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Reid Spencer5f016e22007-07-11 17:01:13 +00001782 // Issue diagnostic and remove storage class if present.
1783 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1784 if (DS.getStorageClassSpecLoc().isValid())
1785 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1786 else
1787 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1788 DS.ClearStorageClassSpecs();
1789 }
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Reid Spencer5f016e22007-07-11 17:01:13 +00001791 // Issue diagnostic and remove function specfier if present.
1792 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001793 if (DS.isInlineSpecified())
1794 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1795 if (DS.isVirtualSpecified())
1796 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1797 if (DS.isExplicitSpecified())
1798 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001799 DS.ClearFunctionSpecs();
1800 }
Richard Smith69730c12012-03-12 07:56:15 +00001801
1802 // Issue diagnostic and remove constexpr specfier if present.
1803 if (DS.isConstexprSpecified()) {
1804 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1805 DS.ClearConstexprSpec();
1806 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001807}
1808
Chris Lattnerc199ab32009-04-12 20:42:31 +00001809/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1810/// specified token is valid after the identifier in a declarator which
1811/// immediately follows the declspec. For example, these things are valid:
1812///
1813/// int x [ 4]; // direct-declarator
1814/// int x ( int y); // direct-declarator
1815/// int(int x ) // direct-declarator
1816/// int x ; // simple-declaration
1817/// int x = 17; // init-declarator-list
1818/// int x , y; // init-declarator-list
1819/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001820/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001821/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001822///
1823/// This is not, because 'x' does not immediately follow the declspec (though
1824/// ')' happens to be valid anyway).
1825/// int (x)
1826///
1827static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1828 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1829 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001830 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001831}
1832
Chris Lattnere40c2952009-04-14 21:34:55 +00001833
1834/// ParseImplicitInt - This method is called when we have an non-typename
1835/// identifier in a declspec (which normally terminates the decl spec) when
1836/// the declspec has no type specifier. In this case, the declspec is either
1837/// malformed or is "implicit int" (in K&R and C89).
1838///
1839/// This method handles diagnosing this prettily and returns false if the
1840/// declspec is done being processed. If it recovers and thinks there may be
1841/// other pieces of declspec after it, it returns true.
1842///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001843bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001844 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001845 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001846 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001847
Chris Lattnere40c2952009-04-14 21:34:55 +00001848 SourceLocation Loc = Tok.getLocation();
1849 // If we see an identifier that is not a type name, we normally would
1850 // parse it as the identifer being declared. However, when a typename
1851 // is typo'd or the definition is not included, this will incorrectly
1852 // parse the typename as the identifier name and fall over misparsing
1853 // later parts of the diagnostic.
1854 //
1855 // As such, we try to do some look-ahead in cases where this would
1856 // otherwise be an "implicit-int" case to see if this is invalid. For
1857 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1858 // an identifier with implicit int, we'd get a parse error because the
1859 // next token is obviously invalid for a type. Parse these as a case
1860 // with an invalid type specifier.
1861 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Chris Lattnere40c2952009-04-14 21:34:55 +00001863 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001864 // error, do lookahead to try to do better recovery. This never applies
1865 // within a type specifier. Outside of C++, we allow this even if the
1866 // language doesn't "officially" support implicit int -- we support
1867 // implicit int as an extension in C99 and C11. Allegedly, MS also
1868 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001869 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001870 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001871 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001872 // If this token is valid for implicit int, e.g. "static x = 4", then
1873 // we just avoid eating the identifier, so it will be parsed as the
1874 // identifier in the declarator.
1875 return false;
1876 }
Mike Stump1eb44332009-09-09 15:08:12 +00001877
Richard Smith827adaf2012-05-15 21:01:51 +00001878 if (getLangOpts().CPlusPlus &&
1879 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1880 // Don't require a type specifier if we have the 'auto' storage class
1881 // specifier in C++98 -- we'll promote it to a type specifier.
1882 return false;
1883 }
1884
Chris Lattnere40c2952009-04-14 21:34:55 +00001885 // Otherwise, if we don't consume this token, we are going to emit an
1886 // error anyway. Try to recover from various common problems. Check
1887 // to see if this was a reference to a tag name without a tag specified.
1888 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001889 //
1890 // C++ doesn't need this, and isTagName doesn't take SS.
1891 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001892 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001893 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Douglas Gregor23c94db2010-07-02 17:43:08 +00001895 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001896 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001897 case DeclSpec::TST_enum:
1898 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1899 case DeclSpec::TST_union:
1900 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1901 case DeclSpec::TST_struct:
1902 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00001903 case DeclSpec::TST_interface:
1904 TagName="__interface"; FixitTagName = "__interface ";
1905 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001906 case DeclSpec::TST_class:
1907 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001908 }
Mike Stump1eb44332009-09-09 15:08:12 +00001909
Chris Lattnerf4382f52009-04-14 22:17:06 +00001910 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001911 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1912 LookupResult R(Actions, TokenName, SourceLocation(),
1913 Sema::LookupOrdinaryName);
1914
Chris Lattnerf4382f52009-04-14 22:17:06 +00001915 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001916 << TokenName << TagName << getLangOpts().CPlusPlus
1917 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1918
1919 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1920 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1921 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001922 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001923 << TokenName << TagName;
1924 }
Mike Stump1eb44332009-09-09 15:08:12 +00001925
Chris Lattnerf4382f52009-04-14 22:17:06 +00001926 // Parse this as a tag as if the missing tag were present.
1927 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001928 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001929 else
Richard Smith69730c12012-03-12 07:56:15 +00001930 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1931 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001932 return true;
1933 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001934 }
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Richard Smith8f0a7e72012-05-15 21:29:55 +00001936 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001937 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001938 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1939 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001940 // Look ahead to the next token to try to figure out what this declaration
1941 // was supposed to be.
1942 switch (NextToken().getKind()) {
1943 case tok::comma:
1944 case tok::equal:
1945 case tok::kw_asm:
1946 case tok::l_brace:
1947 case tok::l_square:
1948 case tok::semi:
1949 // This looks like a variable declaration. The type is probably missing.
1950 // We're done parsing decl-specifiers.
1951 return false;
1952
1953 case tok::l_paren: {
1954 // static x(4); // 'x' is not a type
1955 // x(int n); // 'x' is not a type
1956 // x (*p)[]; // 'x' is a type
1957 //
1958 // Since we're in an error case (or the rare 'implicit int in C++' MS
1959 // extension), we can afford to perform a tentative parse to determine
1960 // which case we're in.
1961 TentativeParsingAction PA(*this);
1962 ConsumeToken();
1963 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1964 PA.Revert();
1965 if (TPR == TPResult::False())
1966 return false;
1967 // The identifier is followed by a parenthesized declarator.
1968 // It's supposed to be a type.
1969 break;
1970 }
1971
1972 default:
1973 // This is probably supposed to be a type. This includes cases like:
1974 // int f(itn);
1975 // struct S { unsinged : 4; };
1976 break;
1977 }
1978 }
1979
Chad Rosier8decdee2012-06-26 22:30:43 +00001980 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00001981 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001982 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001983 IdentifierInfo *II = Tok.getIdentifierInfo();
1984 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001985 // The action emitted a diagnostic, so we don't have to.
1986 if (T) {
1987 // The action has suggested that the type T could be used. Set that as
1988 // the type in the declaration specifiers, consume the would-be type
1989 // name token, and we're done.
1990 const char *PrevSpec;
1991 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001992 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001993 DS.SetRangeEnd(Tok.getLocation());
1994 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001995 // There may be other declaration specifiers after this.
1996 return true;
1997 } else if (II != Tok.getIdentifierInfo()) {
1998 // If no type was suggested, the correction is to a keyword
1999 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002000 // There may be other declaration specifiers after this.
2001 return true;
2002 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002003
Douglas Gregora786fdb2009-10-13 23:27:22 +00002004 // Fall through; the action had no suggestion for us.
2005 } else {
2006 // The action did not emit a diagnostic, so emit one now.
2007 SourceRange R;
2008 if (SS) R = SS->getRange();
2009 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2010 }
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Douglas Gregora786fdb2009-10-13 23:27:22 +00002012 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002013 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002014 DS.SetRangeEnd(Tok.getLocation());
2015 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Chris Lattnere40c2952009-04-14 21:34:55 +00002017 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2018 // avoid rippling error messages on subsequent uses of the same type,
2019 // could be useful if #include was forgotten.
2020 return false;
2021}
2022
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002023/// \brief Determine the declaration specifier context from the declarator
2024/// context.
2025///
2026/// \param Context the declarator context, which is one of the
2027/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002028Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002029Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2030 if (Context == Declarator::MemberContext)
2031 return DSC_class;
2032 if (Context == Declarator::FileContext)
2033 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002034 if (Context == Declarator::TrailingReturnContext)
2035 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002036 return DSC_normal;
2037}
2038
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002039/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2040///
2041/// FIXME: Simply returns an alignof() expression if the argument is a
2042/// type. Ideally, the type should be propagated directly into Sema.
2043///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002044/// [C11] type-id
2045/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002046/// [C++0x] type-id ...[opt]
2047/// [C++0x] assignment-expression ...[opt]
2048ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2049 SourceLocation &EllipsisLoc) {
2050 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002051 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002052 SourceLocation TypeLoc = Tok.getLocation();
2053 ParsedType Ty = ParseTypeName().get();
2054 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002055 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2056 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002057 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002058 ER = ParseConstantExpression();
2059
David Blaikie4e4d0842012-03-11 07:00:24 +00002060 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002061 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002062
2063 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002064}
2065
2066/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2067/// attribute to Attrs.
2068///
2069/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002070/// [C11] '_Alignas' '(' type-id ')'
2071/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002072/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
2073/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002074void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2075 SourceLocation *endLoc) {
2076 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2077 "Not an alignment-specifier!");
2078
2079 SourceLocation KWLoc = Tok.getLocation();
2080 ConsumeToken();
2081
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002082 BalancedDelimiterTracker T(*this, tok::l_paren);
2083 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002084 return;
2085
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002086 SourceLocation EllipsisLoc;
2087 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002088 if (ArgExpr.isInvalid()) {
2089 SkipUntil(tok::r_paren);
2090 return;
2091 }
2092
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002093 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002094 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002095 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002096
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002097 // FIXME: Handle pack-expansions here.
2098 if (EllipsisLoc.isValid()) {
2099 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2100 return;
2101 }
2102
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002103 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002104 ArgExprs.push_back(ArgExpr.release());
Sean Hunt8e083e72012-06-19 23:57:03 +00002105 // FIXME: This should not be GNU, but we since the attribute used is
2106 // based on the spelling, and there is no true spelling for
2107 // C++11 attributes, this isn't accepted.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002108 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002109 0, T.getOpenLocation(), ArgExprs.data(), 1,
Sean Hunt8e083e72012-06-19 23:57:03 +00002110 AttributeList::AS_GNU);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002111}
2112
Reid Spencer5f016e22007-07-11 17:01:13 +00002113/// ParseDeclarationSpecifiers
2114/// declaration-specifiers: [C99 6.7]
2115/// storage-class-specifier declaration-specifiers[opt]
2116/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002117/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002118/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002119/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002120/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002121///
2122/// storage-class-specifier: [C99 6.7.1]
2123/// 'typedef'
2124/// 'extern'
2125/// 'static'
2126/// 'auto'
2127/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002128/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002129/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002130/// function-specifier: [C99 6.7.4]
2131/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002132/// [C++] 'virtual'
2133/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002134/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002135/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002136/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002137
Reid Spencer5f016e22007-07-11 17:01:13 +00002138///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002139void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002140 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002141 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002142 DeclSpecContext DSContext,
2143 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002144 if (DS.getSourceRange().isInvalid()) {
2145 DS.SetRangeStart(Tok.getLocation());
2146 DS.SetRangeEnd(Tok.getLocation());
2147 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002148
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002149 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002150 bool AttrsLastTime = false;
2151 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002152 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002153 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002154 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002155 unsigned DiagID = 0;
2156
Reid Spencer5f016e22007-07-11 17:01:13 +00002157 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002158
Reid Spencer5f016e22007-07-11 17:01:13 +00002159 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002160 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002161 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002162 if (!AttrsLastTime)
2163 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002164 else {
2165 // Reject C++11 attributes that appertain to decl specifiers as
2166 // we don't support any C++11 attributes that appertain to decl
2167 // specifiers. This also conforms to what g++ 4.8 is doing.
2168 ProhibitCXX11Attributes(attrs);
2169
Sean Hunt2edf0a22012-06-23 05:07:58 +00002170 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002171 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002172
Reid Spencer5f016e22007-07-11 17:01:13 +00002173 // If this is not a declaration specifier token, we're done reading decl
2174 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002175 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002176 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002177
Sean Hunt2edf0a22012-06-23 05:07:58 +00002178 case tok::l_square:
2179 case tok::kw_alignas:
2180 if (!isCXX11AttributeSpecifier())
2181 goto DoneWithDeclSpec;
2182
2183 ProhibitAttributes(attrs);
2184 // FIXME: It would be good to recover by accepting the attributes,
2185 // but attempting to do that now would cause serious
2186 // madness in terms of diagnostics.
2187 attrs.clear();
2188 attrs.Range = SourceRange();
2189
2190 ParseCXX11Attributes(attrs);
2191 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002192 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002193
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002194 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002195 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002196 if (DS.hasTypeSpecifier()) {
2197 bool AllowNonIdentifiers
2198 = (getCurScope()->getFlags() & (Scope::ControlScope |
2199 Scope::BlockScope |
2200 Scope::TemplateParamScope |
2201 Scope::FunctionPrototypeScope |
2202 Scope::AtCatchScope)) == 0;
2203 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002204 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002205 (DSContext == DSC_class && DS.isFriendSpecified());
2206
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002207 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002208 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002209 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002210 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002211 }
2212
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002213 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2214 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2215 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002216 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002217 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002218 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002219 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002220 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002221 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002222
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002223 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002224 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002225 }
2226
Chris Lattner5e02c472009-01-05 00:07:25 +00002227 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002228 // C++ scope specifier. Annotate and loop, or bail out on error.
2229 if (TryAnnotateCXXScopeToken(true)) {
2230 if (!DS.hasTypeSpecifier())
2231 DS.SetTypeSpecError();
2232 goto DoneWithDeclSpec;
2233 }
John McCall2e0a7152010-03-01 18:20:46 +00002234 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2235 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002236 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002237
2238 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002239 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002240 goto DoneWithDeclSpec;
2241
John McCallaa87d332009-12-12 11:40:51 +00002242 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002243 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2244 Tok.getAnnotationRange(),
2245 SS);
John McCallaa87d332009-12-12 11:40:51 +00002246
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002247 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002248 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002249 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002250 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002251 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002252 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002253
2254 // C++ [class.qual]p2:
2255 // In a lookup in which the constructor is an acceptable lookup
2256 // result and the nested-name-specifier nominates a class C:
2257 //
2258 // - if the name specified after the
2259 // nested-name-specifier, when looked up in C, is the
2260 // injected-class-name of C (Clause 9), or
2261 //
2262 // - if the name specified after the nested-name-specifier
2263 // is the same as the identifier or the
2264 // simple-template-id's template-name in the last
2265 // component of the nested-name-specifier,
2266 //
2267 // the name is instead considered to name the constructor of
2268 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002269 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002270 // Thus, if the template-name is actually the constructor
2271 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002272 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002273 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002274 if ((DSContext == DSC_top_level ||
2275 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2276 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002277 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002278 if (isConstructorDeclarator()) {
2279 // The user meant this to be an out-of-line constructor
2280 // definition, but template arguments are not allowed
2281 // there. Just allow this as a constructor; we'll
2282 // complain about it later.
2283 goto DoneWithDeclSpec;
2284 }
2285
2286 // The user meant this to name a type, but it actually names
2287 // a constructor with some extraneous template
2288 // arguments. Complain, then parse it as a type as the user
2289 // intended.
2290 Diag(TemplateId->TemplateNameLoc,
2291 diag::err_out_of_line_template_id_names_constructor)
2292 << TemplateId->Name;
2293 }
2294
John McCallaa87d332009-12-12 11:40:51 +00002295 DS.getTypeSpecScope() = SS;
2296 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002297 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002298 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002299 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002300 continue;
2301 }
2302
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002303 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002304 DS.getTypeSpecScope() = SS;
2305 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002306 if (Tok.getAnnotationValue()) {
2307 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002308 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002309 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002310 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002311 if (isInvalid)
2312 break;
John McCallb3d87482010-08-24 05:47:05 +00002313 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002314 else
2315 DS.SetTypeSpecError();
2316 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2317 ConsumeToken(); // The typename
2318 }
2319
Douglas Gregor9135c722009-03-25 15:40:00 +00002320 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002321 goto DoneWithDeclSpec;
2322
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002323 // If we're in a context where the identifier could be a class name,
2324 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002325 if ((DSContext == DSC_top_level ||
2326 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002327 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002328 &SS)) {
2329 if (isConstructorDeclarator())
2330 goto DoneWithDeclSpec;
2331
2332 // As noted in C++ [class.qual]p2 (cited above), when the name
2333 // of the class is qualified in a context where it could name
2334 // a constructor, its a constructor name. However, we've
2335 // looked at the declarator, and the user probably meant this
2336 // to be a type. Complain that it isn't supposed to be treated
2337 // as a type, then proceed to parse it as a type.
2338 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2339 << Next.getIdentifierInfo();
2340 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002341
John McCallb3d87482010-08-24 05:47:05 +00002342 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2343 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002344 getCurScope(), &SS,
2345 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002346 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002347 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002348
Chris Lattnerf4382f52009-04-14 22:17:06 +00002349 // If the referenced identifier is not a type, then this declspec is
2350 // erroneous: We already checked about that it has no type specifier, and
2351 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002352 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002353 if (TypeRep == 0) {
2354 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002355 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002356 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002357 }
Mike Stump1eb44332009-09-09 15:08:12 +00002358
John McCallaa87d332009-12-12 11:40:51 +00002359 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002360 ConsumeToken(); // The C++ scope.
2361
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002362 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002363 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002364 if (isInvalid)
2365 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002366
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002367 DS.SetRangeEnd(Tok.getLocation());
2368 ConsumeToken(); // The typename.
2369
2370 continue;
2371 }
Mike Stump1eb44332009-09-09 15:08:12 +00002372
Chris Lattner80d0c892009-01-21 19:48:37 +00002373 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002374 if (Tok.getAnnotationValue()) {
2375 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002376 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002377 DiagID, T);
2378 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002379 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002380
Chris Lattner5c5db552010-04-05 18:18:31 +00002381 if (isInvalid)
2382 break;
2383
Chris Lattner80d0c892009-01-21 19:48:37 +00002384 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2385 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002386
Chris Lattner80d0c892009-01-21 19:48:37 +00002387 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2388 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002389 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002390 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002391 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002392
Chris Lattner80d0c892009-01-21 19:48:37 +00002393 continue;
2394 }
Mike Stump1eb44332009-09-09 15:08:12 +00002395
Douglas Gregorbfad9152011-04-28 15:48:45 +00002396 case tok::kw___is_signed:
2397 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2398 // typically treats it as a trait. If we see __is_signed as it appears
2399 // in libstdc++, e.g.,
2400 //
2401 // static const bool __is_signed;
2402 //
2403 // then treat __is_signed as an identifier rather than as a keyword.
2404 if (DS.getTypeSpecType() == TST_bool &&
2405 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2406 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2407 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2408 Tok.setKind(tok::identifier);
2409 }
2410
2411 // We're done with the declaration-specifiers.
2412 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002413
Chris Lattner3bd934a2008-07-26 01:18:38 +00002414 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002415 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002416 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002417 // In C++, check to see if this is a scope specifier like foo::bar::, if
2418 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002419 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002420 if (TryAnnotateCXXScopeToken(true)) {
2421 if (!DS.hasTypeSpecifier())
2422 DS.SetTypeSpecError();
2423 goto DoneWithDeclSpec;
2424 }
2425 if (!Tok.is(tok::identifier))
2426 continue;
2427 }
Mike Stump1eb44332009-09-09 15:08:12 +00002428
Chris Lattner3bd934a2008-07-26 01:18:38 +00002429 // This identifier can only be a typedef name if we haven't already seen
2430 // a type-specifier. Without this check we misparse:
2431 // typedef int X; struct Y { short X; }; as 'short int'.
2432 if (DS.hasTypeSpecifier())
2433 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002434
John Thompson82287d12010-02-05 00:12:22 +00002435 // Check for need to substitute AltiVec keyword tokens.
2436 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2437 break;
2438
Richard Smithf63eee72012-05-09 18:56:43 +00002439 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2440 // allow the use of a typedef name as a type specifier.
2441 if (DS.isTypeAltiVecVector())
2442 goto DoneWithDeclSpec;
2443
John McCallb3d87482010-08-24 05:47:05 +00002444 ParsedType TypeRep =
2445 Actions.getTypeName(*Tok.getIdentifierInfo(),
2446 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002447
Chris Lattnerc199ab32009-04-12 20:42:31 +00002448 // If this is not a typedef name, don't parse it as part of the declspec,
2449 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002450 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002451 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002452 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002453 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002454
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002455 // If we're in a context where the identifier could be a class name,
2456 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002457 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002458 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002459 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002460 goto DoneWithDeclSpec;
2461
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002462 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002463 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002464 if (isInvalid)
2465 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002466
Chris Lattner3bd934a2008-07-26 01:18:38 +00002467 DS.SetRangeEnd(Tok.getLocation());
2468 ConsumeToken(); // The identifier
2469
2470 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2471 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002472 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002473 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002474 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002475
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002476 // Need to support trailing type qualifiers (e.g. "id<p> const").
2477 // If a type specifier follows, it will be diagnosed elsewhere.
2478 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002479 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002480
2481 // type-name
2482 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002483 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002484 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002485 // This template-id does not refer to a type name, so we're
2486 // done with the type-specifiers.
2487 goto DoneWithDeclSpec;
2488 }
2489
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002490 // If we're in a context where the template-id could be a
2491 // constructor name or specialization, check whether this is a
2492 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002493 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002494 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002495 isConstructorDeclarator())
2496 goto DoneWithDeclSpec;
2497
Douglas Gregor39a8de12009-02-25 19:37:18 +00002498 // Turn the template-id annotation token into a type annotation
2499 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002500 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002501 continue;
2502 }
2503
Reid Spencer5f016e22007-07-11 17:01:13 +00002504 // GNU attributes support.
2505 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002506 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002507 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002508
2509 // Microsoft declspec support.
2510 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002511 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002512 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002513
Steve Naroff239f0732008-12-25 14:16:32 +00002514 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002515 case tok::kw___forceinline: {
2516 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2517 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002518 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002519 // FIXME: This does not work correctly if it is set to be a declspec
2520 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002521 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002522 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002523 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002524 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002525
2526 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002527 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002528 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002529 case tok::kw___cdecl:
2530 case tok::kw___stdcall:
2531 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002532 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002533 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002534 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002535 continue;
2536
Dawn Perchik52fc3142010-09-03 01:29:35 +00002537 // Borland single token adornments.
2538 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002539 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002540 continue;
2541
Peter Collingbournef315fa82011-02-14 01:42:53 +00002542 // OpenCL single token adornments.
2543 case tok::kw___kernel:
2544 ParseOpenCLAttributes(DS.getAttributes());
2545 continue;
2546
Reid Spencer5f016e22007-07-11 17:01:13 +00002547 // storage-class-specifier
2548 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002549 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2550 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002551 break;
2552 case tok::kw_extern:
2553 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002554 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002555 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2556 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002557 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002558 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002559 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2560 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002561 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002562 case tok::kw_static:
2563 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002564 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002565 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2566 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002567 break;
2568 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002569 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002570 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002571 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2572 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002573 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002574 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002575 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002576 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002577 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2578 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002579 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002580 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2581 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002582 break;
2583 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002584 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2585 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002586 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002587 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002588 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2589 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002590 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002591 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002592 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002593 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002594
Reid Spencer5f016e22007-07-11 17:01:13 +00002595 // function-specifier
2596 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002597 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002598 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002599 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002600 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002601 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002602 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002603 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002604 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002605
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002606 // alignment-specifier
2607 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002608 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002609 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002610 ParseAlignmentSpecifier(DS.getAttributes());
2611 continue;
2612
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002613 // friend
2614 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002615 if (DSContext == DSC_class)
2616 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2617 else {
2618 PrevSpec = ""; // not actually used by the diagnostic
2619 DiagID = diag::err_friend_invalid_in_context;
2620 isInvalid = true;
2621 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002622 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002623
Douglas Gregor8d267c52011-09-09 02:06:17 +00002624 // Modules
2625 case tok::kw___module_private__:
2626 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2627 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002628
Sebastian Redl2ac67232009-11-05 15:47:02 +00002629 // constexpr
2630 case tok::kw_constexpr:
2631 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2632 break;
2633
Chris Lattner80d0c892009-01-21 19:48:37 +00002634 // type-specifier
2635 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002636 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2637 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002638 break;
2639 case tok::kw_long:
2640 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002641 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2642 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002643 else
John McCallfec54012009-08-03 20:12:06 +00002644 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2645 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002646 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002647 case tok::kw___int64:
2648 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2649 DiagID);
2650 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002651 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002652 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2653 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002654 break;
2655 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002656 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2657 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002658 break;
2659 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002660 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2661 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002662 break;
2663 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002664 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2665 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002666 break;
2667 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002668 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2669 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002670 break;
2671 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002672 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2673 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002674 break;
2675 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002676 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2677 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002678 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002679 case tok::kw___int128:
2680 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2681 DiagID);
2682 break;
2683 case tok::kw_half:
2684 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2685 DiagID);
2686 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002687 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002688 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2689 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002690 break;
2691 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002692 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2693 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002694 break;
2695 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002696 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2697 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002698 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002699 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002700 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2701 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002702 break;
2703 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002704 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2705 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002706 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002707 case tok::kw_bool:
2708 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002709 if (Tok.is(tok::kw_bool) &&
2710 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2711 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2712 PrevSpec = ""; // Not used by the diagnostic.
2713 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002714 // For better error recovery.
2715 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002716 isInvalid = true;
2717 } else {
2718 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2719 DiagID);
2720 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002721 break;
2722 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002723 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2724 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002725 break;
2726 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002727 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2728 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002729 break;
2730 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002731 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2732 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002733 break;
John Thompson82287d12010-02-05 00:12:22 +00002734 case tok::kw___vector:
2735 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2736 break;
2737 case tok::kw___pixel:
2738 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2739 break;
John McCalla5fc4722011-04-09 22:50:59 +00002740 case tok::kw___unknown_anytype:
2741 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2742 PrevSpec, DiagID);
2743 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002744
2745 // class-specifier:
2746 case tok::kw_class:
2747 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00002748 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00002749 case tok::kw_union: {
2750 tok::TokenKind Kind = Tok.getKind();
2751 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002752 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2753 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002754 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002755 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002756
2757 // enum-specifier:
2758 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002759 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002760 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002761 continue;
2762
2763 // cv-qualifier:
2764 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002765 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002766 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002767 break;
2768 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002769 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002770 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002771 break;
2772 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002773 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002774 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002775 break;
2776
Douglas Gregord57959a2009-03-27 23:10:48 +00002777 // C++ typename-specifier:
2778 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002779 if (TryAnnotateTypeOrScopeToken()) {
2780 DS.SetTypeSpecError();
2781 goto DoneWithDeclSpec;
2782 }
2783 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002784 continue;
2785 break;
2786
Chris Lattner80d0c892009-01-21 19:48:37 +00002787 // GNU typeof support.
2788 case tok::kw_typeof:
2789 ParseTypeofSpecifier(DS);
2790 continue;
2791
David Blaikie42d6d0c2011-12-04 05:04:18 +00002792 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002793 ParseDecltypeSpecifier(DS);
2794 continue;
2795
Sean Huntdb5d44b2011-05-19 05:37:45 +00002796 case tok::kw___underlying_type:
2797 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002798 continue;
2799
2800 case tok::kw__Atomic:
2801 ParseAtomicSpecifier(DS);
2802 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002803
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002804 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00002805 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002806 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002807 goto DoneWithDeclSpec;
2808 case tok::kw___private:
2809 case tok::kw___global:
2810 case tok::kw___local:
2811 case tok::kw___constant:
2812 case tok::kw___read_only:
2813 case tok::kw___write_only:
2814 case tok::kw___read_write:
2815 ParseOpenCLQualifiers(DS);
2816 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002817
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002818 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002819 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002820 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2821 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002822 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002823 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002824
Douglas Gregor46f936e2010-11-19 17:10:50 +00002825 if (!ParseObjCProtocolQualifiers(DS))
2826 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2827 << FixItHint::CreateInsertion(Loc, "id")
2828 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00002829
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002830 // Need to support trailing type qualifiers (e.g. "id<p> const").
2831 // If a type specifier follows, it will be diagnosed elsewhere.
2832 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002833 }
John McCallfec54012009-08-03 20:12:06 +00002834 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002835 if (isInvalid) {
2836 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002837 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00002838
Douglas Gregorae2fb142010-08-23 14:34:43 +00002839 if (DiagID == diag::ext_duplicate_declspec)
2840 Diag(Tok, DiagID)
2841 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2842 else
2843 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002844 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002845
Chris Lattner81c018d2008-03-13 06:29:04 +00002846 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002847 if (DiagID != diag::err_bool_redeclaration)
2848 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002849
2850 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002851 }
2852}
Douglas Gregoradcac882008-12-01 23:54:00 +00002853
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002854/// ParseStructDeclaration - Parse a struct declaration without the terminating
2855/// semicolon.
2856///
Reid Spencer5f016e22007-07-11 17:01:13 +00002857/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002858/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002859/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002860/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002861/// struct-declarator-list:
2862/// struct-declarator
2863/// struct-declarator-list ',' struct-declarator
2864/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2865/// struct-declarator:
2866/// declarator
2867/// [GNU] declarator attributes[opt]
2868/// declarator[opt] ':' constant-expression
2869/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2870///
Chris Lattnere1359422008-04-10 06:46:29 +00002871void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002872ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00002873
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002874 if (Tok.is(tok::kw___extension__)) {
2875 // __extension__ silences extension warnings in the subexpression.
2876 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002877 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002878 return ParseStructDeclaration(DS, Fields);
2879 }
Mike Stump1eb44332009-09-09 15:08:12 +00002880
Steve Naroff28a7ca82007-08-20 22:28:22 +00002881 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002882 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002883
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002884 // If there are no declarators, this is a free-standing declaration
2885 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002886 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002887 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2888 DS);
2889 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002890 return;
2891 }
2892
2893 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002894 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002895 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002896 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002897 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00002898 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002899
2900 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002901 if (!FirstDeclarator)
2902 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002903
Steve Naroff28a7ca82007-08-20 22:28:22 +00002904 /// struct-declarator: declarator
2905 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002906 if (Tok.isNot(tok::colon)) {
2907 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2908 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002909 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002910 }
Mike Stump1eb44332009-09-09 15:08:12 +00002911
Chris Lattner04d66662007-10-09 17:33:22 +00002912 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002913 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002914 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002915 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002916 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002917 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002918 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002919 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002920
Steve Naroff28a7ca82007-08-20 22:28:22 +00002921 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002922 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002923
John McCallbdd563e2009-11-03 02:38:08 +00002924 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00002925 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00002926
Steve Naroff28a7ca82007-08-20 22:28:22 +00002927 // If we don't have a comma, it is either the end of the list (a ';')
2928 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002929 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002930 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002931
Steve Naroff28a7ca82007-08-20 22:28:22 +00002932 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002933 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002934
John McCallbdd563e2009-11-03 02:38:08 +00002935 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002936 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002937}
2938
2939/// ParseStructUnionBody
2940/// struct-contents:
2941/// struct-declaration-list
2942/// [EXT] empty
2943/// [GNU] "struct-declaration-list" without terminatoring ';'
2944/// struct-declaration-list:
2945/// struct-declaration
2946/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002947/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002948///
Reid Spencer5f016e22007-07-11 17:01:13 +00002949void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002950 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002951 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2952 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002953
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002954 BalancedDelimiterTracker T(*this, tok::l_brace);
2955 if (T.consumeOpen())
2956 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002957
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002958 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002959 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002960
Reid Spencer5f016e22007-07-11 17:01:13 +00002961 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2962 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002963 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002964 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2965 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2966 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002967
Chris Lattner5f9e2722011-07-23 10:55:15 +00002968 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002969
Reid Spencer5f016e22007-07-11 17:01:13 +00002970 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002971 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002972 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002973
Reid Spencer5f016e22007-07-11 17:01:13 +00002974 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002975 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002976 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00002977 continue;
2978 }
Chris Lattnere1359422008-04-10 06:46:29 +00002979
John McCallbdd563e2009-11-03 02:38:08 +00002980 if (!Tok.is(tok::at)) {
2981 struct CFieldCallback : FieldCallback {
2982 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002983 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002984 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002985
John McCalld226f652010-08-21 09:40:31 +00002986 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002987 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002988 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2989
Eli Friedmandcdff462012-08-08 23:53:27 +00002990 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002991 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002992 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002993 FD.D.getDeclSpec().getSourceRange().getBegin(),
2994 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002995 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002996 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00002997 }
John McCallbdd563e2009-11-03 02:38:08 +00002998 } Callback(*this, TagDecl, FieldDecls);
2999
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003000 // Parse all the comma separated declarators.
3001 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003002 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003003 } else { // Handle @defs
3004 ConsumeToken();
3005 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3006 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003007 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003008 continue;
3009 }
3010 ConsumeToken();
3011 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3012 if (!Tok.is(tok::identifier)) {
3013 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003014 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003015 continue;
3016 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003017 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003018 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003019 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003020 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3021 ConsumeToken();
3022 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003023 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003024
Chris Lattner04d66662007-10-09 17:33:22 +00003025 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003026 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003027 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003028 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003029 break;
3030 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003031 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3032 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003033 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003034 // If we stopped at a ';', eat it.
3035 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003036 }
3037 }
Mike Stump1eb44332009-09-09 15:08:12 +00003038
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003039 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003040
John McCall0b7e6782011-03-24 11:26:52 +00003041 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003042 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003043 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003044
Douglas Gregor23c94db2010-07-02 17:43:08 +00003045 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003046 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003047 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003048 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003049 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003050 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3051 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003052}
3053
Reid Spencer5f016e22007-07-11 17:01:13 +00003054/// ParseEnumSpecifier
3055/// enum-specifier: [C99 6.7.2.2]
3056/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003057///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003058/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3059/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003060/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3061/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003062/// 'enum' identifier
3063/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003064///
Richard Smith1af83c42012-03-23 03:33:32 +00003065/// [C++11] enum-head '{' enumerator-list[opt] '}'
3066/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003067///
Richard Smith1af83c42012-03-23 03:33:32 +00003068/// enum-head: [C++11]
3069/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3070/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3071/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003072///
Richard Smith1af83c42012-03-23 03:33:32 +00003073/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003074/// 'enum'
3075/// 'enum' 'class'
3076/// 'enum' 'struct'
3077///
Richard Smith1af83c42012-03-23 03:33:32 +00003078/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003079/// ':' type-specifier-seq
3080///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003081/// [C++] elaborated-type-specifier:
3082/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3083///
Chris Lattner4c97d762009-04-12 21:49:30 +00003084void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003085 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003086 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003087 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003088 if (Tok.is(tok::code_completion)) {
3089 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003090 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003091 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003092 }
John McCall57c13002011-07-06 05:58:41 +00003093
Sean Hunt2edf0a22012-06-23 05:07:58 +00003094 // If attributes exist after tag, parse them.
3095 ParsedAttributesWithRange attrs(AttrFactory);
3096 MaybeParseGNUAttributes(attrs);
3097 MaybeParseCXX0XAttributes(attrs);
3098
3099 // If declspecs exist after tag, parse them.
3100 while (Tok.is(tok::kw___declspec))
3101 ParseMicrosoftDeclSpec(attrs);
3102
Richard Smithbdad7a22012-01-10 01:33:14 +00003103 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003104 bool IsScopedUsingClassTag = false;
3105
John McCall1e12b3d2012-06-23 22:30:04 +00003106 // In C++11, recognize 'enum class' and 'enum struct'.
David Blaikie4e4d0842012-03-11 07:00:24 +00003107 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00003108 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00003109 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003110 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003111 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003112
John McCall1e12b3d2012-06-23 22:30:04 +00003113 // Attributes are not allowed between these keywords. Diagnose,
3114 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003115 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003116
3117 // They are allowed afterwards, though.
3118 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003119 MaybeParseCXX0XAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003120 while (Tok.is(tok::kw___declspec))
3121 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003122 }
Richard Smith1af83c42012-03-23 03:33:32 +00003123
John McCall13489672012-05-07 06:16:58 +00003124 // C++11 [temp.explicit]p12:
3125 // The usual access controls do not apply to names used to specify
3126 // explicit instantiations.
3127 // We extend this to also cover explicit specializations. Note that
3128 // we don't suppress if this turns out to be an elaborated type
3129 // specifier.
3130 bool shouldDelayDiagsInTag =
3131 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3132 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3133 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003134
Richard Smith7796eb52012-03-12 08:56:40 +00003135 // Enum definitions should not be parsed in a trailing-return-type.
3136 bool AllowDeclaration = DSC != DSC_trailing;
3137
3138 bool AllowFixedUnderlyingType = AllowDeclaration &&
3139 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
3140 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003141
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003142 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003143 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003144 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3145 // if a fixed underlying type is allowed.
3146 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003147
3148 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003149 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00003150 return;
3151
3152 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003153 Diag(Tok, diag::err_expected_ident);
3154 if (Tok.isNot(tok::l_brace)) {
3155 // Has no name and is not a definition.
3156 // Skip the rest of this declarator, up until the comma or semicolon.
3157 SkipUntil(tok::comma, true);
3158 return;
3159 }
3160 }
3161 }
Mike Stump1eb44332009-09-09 15:08:12 +00003162
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003163 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003164 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003165 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003166 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003167
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003168 // Skip the rest of this declarator, up until the comma or semicolon.
3169 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003170 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003171 }
Mike Stump1eb44332009-09-09 15:08:12 +00003172
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003173 // If an identifier is present, consume and remember it.
3174 IdentifierInfo *Name = 0;
3175 SourceLocation NameLoc;
3176 if (Tok.is(tok::identifier)) {
3177 Name = Tok.getIdentifierInfo();
3178 NameLoc = ConsumeToken();
3179 }
Mike Stump1eb44332009-09-09 15:08:12 +00003180
Richard Smithbdad7a22012-01-10 01:33:14 +00003181 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003182 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3183 // declaration of a scoped enumeration.
3184 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003185 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003186 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003187 }
3188
John McCall13489672012-05-07 06:16:58 +00003189 // Okay, end the suppression area. We'll decide whether to emit the
3190 // diagnostics in a second.
3191 if (shouldDelayDiagsInTag)
3192 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003193
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003194 TypeResult BaseType;
3195
Douglas Gregora61b3e72010-12-01 17:42:47 +00003196 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003197 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003198 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003199 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003200 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003201 // If we're in class scope, this can either be an enum declaration with
3202 // an underlying type, or a declaration of a bitfield member. We try to
3203 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003204 // (integer literal, sizeof); if it's still ambiguous, we then consider
3205 // anything that's a simple-type-specifier followed by '(' as an
3206 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003207 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003208 EnterExpressionEvaluationContext Unevaluated(Actions,
3209 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003210 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003211 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003212 // bit-field. This is the common case.
3213 if (TPR == TPResult::True())
3214 PossibleBitfield = true;
3215 // If the next token starts a type-specifier-seq, it may be either a
3216 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003217 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003218 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003219 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003220 GetLookAheadToken(2).getKind() == tok::semi) {
3221 // Consume the ':'.
3222 ConsumeToken();
3223 } else {
3224 // We have the start of a type-specifier-seq, so we have to perform
3225 // tentative parsing to determine whether we have an expression or a
3226 // type.
3227 TentativeParsingAction TPA(*this);
3228
3229 // Consume the ':'.
3230 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003231
3232 // If we see a type specifier followed by an open-brace, we have an
3233 // ambiguity between an underlying type and a C++11 braced
3234 // function-style cast. Resolve this by always treating it as an
3235 // underlying type.
3236 // FIXME: The standard is not entirely clear on how to disambiguate in
3237 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003238 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003239 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003240 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003241 // We'll parse this as a bitfield later.
3242 PossibleBitfield = true;
3243 TPA.Revert();
3244 } else {
3245 // We have a type-specifier-seq.
3246 TPA.Commit();
3247 }
3248 }
3249 } else {
3250 // Consume the ':'.
3251 ConsumeToken();
3252 }
3253
3254 if (!PossibleBitfield) {
3255 SourceRange Range;
3256 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003257
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003258 if (getLangOpts().CPlusPlus0x) {
Richard Smith7fe62082011-10-15 05:09:34 +00003259 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003260 } else if (!getLangOpts().ObjC2) {
3261 if (getLangOpts().CPlusPlus)
3262 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3263 else
3264 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3265 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003266 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003267 }
3268
Richard Smithbdad7a22012-01-10 01:33:14 +00003269 // There are four options here. If we have 'friend enum foo;' then this is a
3270 // friend declaration, and cannot have an accompanying definition. If we have
3271 // 'enum foo;', then this is a forward declaration. If we have
3272 // 'enum foo {...' then this is a definition. Otherwise we have something
3273 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003274 //
3275 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3276 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3277 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3278 //
John McCallf312b1e2010-08-26 23:41:50 +00003279 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003280 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003281 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003282 } else if (Tok.is(tok::l_brace)) {
3283 if (DS.isFriendSpecified()) {
3284 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3285 << SourceRange(DS.getFriendSpecLoc());
3286 ConsumeBrace();
3287 SkipUntil(tok::r_brace);
3288 TUK = Sema::TUK_Friend;
3289 } else {
3290 TUK = Sema::TUK_Definition;
3291 }
Richard Smithc9f35172012-06-25 21:37:02 +00003292 } else if (DSC != DSC_type_specifier &&
3293 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003294 (Tok.isAtStartOfLine() &&
3295 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003296 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3297 if (Tok.isNot(tok::semi)) {
3298 // A semicolon was missing after this declaration. Diagnose and recover.
3299 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3300 "enum");
3301 PP.EnterToken(Tok);
3302 Tok.setKind(tok::semi);
3303 }
John McCall13489672012-05-07 06:16:58 +00003304 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003305 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003306 }
3307
3308 // If this is an elaborated type specifier, and we delayed
3309 // diagnostics before, just merge them into the current pool.
3310 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3311 diagsFromTag.redelay();
3312 }
Richard Smith1af83c42012-03-23 03:33:32 +00003313
3314 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003315 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003316 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003317 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3318 // Skip the rest of this declarator, up until the comma or semicolon.
3319 Diag(Tok, diag::err_enum_template);
3320 SkipUntil(tok::comma, true);
3321 return;
3322 }
3323
3324 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3325 // Enumerations can't be explicitly instantiated.
3326 DS.SetTypeSpecError();
3327 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3328 return;
3329 }
3330
3331 assert(TemplateInfo.TemplateParams && "no template parameters");
3332 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3333 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003334 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003335
Sean Hunt2edf0a22012-06-23 05:07:58 +00003336 if (TUK == Sema::TUK_Reference)
3337 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003338
Douglas Gregorb9075602011-02-22 02:55:24 +00003339 if (!Name && TUK != Sema::TUK_Definition) {
3340 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003341
Douglas Gregorb9075602011-02-22 02:55:24 +00003342 // Skip the rest of this declarator, up until the comma or semicolon.
3343 SkipUntil(tok::comma, true);
3344 return;
3345 }
Richard Smith1af83c42012-03-23 03:33:32 +00003346
Douglas Gregor402abb52009-05-28 23:31:59 +00003347 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003348 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003349 const char *PrevSpec = 0;
3350 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003351 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003352 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003353 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003354 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003355 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003356
Douglas Gregor48c89f42010-04-24 16:38:41 +00003357 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003358 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003359 // dependent tag.
3360 if (!Name) {
3361 DS.SetTypeSpecError();
3362 Diag(Tok, diag::err_expected_type_name_after_typename);
3363 return;
3364 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003365
Douglas Gregor23c94db2010-07-02 17:43:08 +00003366 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003367 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003368 NameLoc);
3369 if (Type.isInvalid()) {
3370 DS.SetTypeSpecError();
3371 return;
3372 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003373
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003374 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3375 NameLoc.isValid() ? NameLoc : StartLoc,
3376 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003377 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003378
Douglas Gregor48c89f42010-04-24 16:38:41 +00003379 return;
3380 }
Mike Stump1eb44332009-09-09 15:08:12 +00003381
John McCalld226f652010-08-21 09:40:31 +00003382 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003383 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003384 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003385 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003386 ConsumeBrace();
3387 SkipUntil(tok::r_brace);
3388 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003389
Douglas Gregor48c89f42010-04-24 16:38:41 +00003390 DS.SetTypeSpecError();
3391 return;
3392 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003393
Richard Smithc9f35172012-06-25 21:37:02 +00003394 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003395 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003396
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003397 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3398 NameLoc.isValid() ? NameLoc : StartLoc,
3399 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003400 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003401}
3402
3403/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3404/// enumerator-list:
3405/// enumerator
3406/// enumerator-list ',' enumerator
3407/// enumerator:
3408/// enumeration-constant
3409/// enumeration-constant '=' constant-expression
3410/// enumeration-constant:
3411/// identifier
3412///
John McCalld226f652010-08-21 09:40:31 +00003413void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003414 // Enter the scope of the enum body and start the definition.
3415 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003416 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003417
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003418 BalancedDelimiterTracker T(*this, tok::l_brace);
3419 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003420
Chris Lattner7946dd32007-08-27 17:24:30 +00003421 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003422 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003423 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003424
Chris Lattner5f9e2722011-07-23 10:55:15 +00003425 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003426
John McCalld226f652010-08-21 09:40:31 +00003427 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003428
Reid Spencer5f016e22007-07-11 17:01:13 +00003429 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003430 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003431 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3432 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003433
John McCall5b629aa2010-10-22 23:36:17 +00003434 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003435 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003436 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003437 MaybeParseCXX0XAttributes(attrs);
3438 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003439
Reid Spencer5f016e22007-07-11 17:01:13 +00003440 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003441 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003442 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003443
Chris Lattner04d66662007-10-09 17:33:22 +00003444 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003445 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003446 AssignedVal = ParseConstantExpression();
3447 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003448 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003449 }
Mike Stump1eb44332009-09-09 15:08:12 +00003450
Reid Spencer5f016e22007-07-11 17:01:13 +00003451 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003452 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3453 LastEnumConstDecl,
3454 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003455 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003456 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003457 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003458
Reid Spencer5f016e22007-07-11 17:01:13 +00003459 EnumConstantDecls.push_back(EnumConstDecl);
3460 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003461
Douglas Gregor751f6922010-09-07 14:51:08 +00003462 if (Tok.is(tok::identifier)) {
3463 // We're missing a comma between enumerators.
3464 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003465 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003466 << FixItHint::CreateInsertion(Loc, ", ");
3467 continue;
3468 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003469
Chris Lattner04d66662007-10-09 17:33:22 +00003470 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003471 break;
3472 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003473
Richard Smith7fe62082011-10-15 05:09:34 +00003474 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003475 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003476 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3477 diag::ext_enumerator_list_comma_cxx :
3478 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003479 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003480 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003481 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3482 << FixItHint::CreateRemoval(CommaLoc);
3483 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003484 }
Mike Stump1eb44332009-09-09 15:08:12 +00003485
Reid Spencer5f016e22007-07-11 17:01:13 +00003486 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003487 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003488
Reid Spencer5f016e22007-07-11 17:01:13 +00003489 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003490 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003491 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003492
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003493 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3494 EnumDecl, EnumConstantDecls.data(),
3495 EnumConstantDecls.size(), getCurScope(),
3496 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003497
Douglas Gregor72de6672009-01-08 20:45:30 +00003498 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003499 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3500 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003501
3502 // The next token must be valid after an enum definition. If not, a ';'
3503 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003504 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3505 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003506 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3507 // Push this token back into the preprocessor and change our current token
3508 // to ';' so that the rest of the code recovers as though there were an
3509 // ';' after the definition.
3510 PP.EnterToken(Tok);
3511 Tok.setKind(tok::semi);
3512 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003513}
3514
3515/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003516/// start of a type-qualifier-list.
3517bool Parser::isTypeQualifier() const {
3518 switch (Tok.getKind()) {
3519 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003520
3521 // type-qualifier only in OpenCL
3522 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003523 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003524
Steve Naroff5f8aa692008-02-11 23:15:56 +00003525 // type-qualifier
3526 case tok::kw_const:
3527 case tok::kw_volatile:
3528 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003529 case tok::kw___private:
3530 case tok::kw___local:
3531 case tok::kw___global:
3532 case tok::kw___constant:
3533 case tok::kw___read_only:
3534 case tok::kw___read_write:
3535 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003536 return true;
3537 }
3538}
3539
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003540/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3541/// is definitely a type-specifier. Return false if it isn't part of a type
3542/// specifier or if we're not sure.
3543bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3544 switch (Tok.getKind()) {
3545 default: return false;
3546 // type-specifiers
3547 case tok::kw_short:
3548 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003549 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003550 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003551 case tok::kw_signed:
3552 case tok::kw_unsigned:
3553 case tok::kw__Complex:
3554 case tok::kw__Imaginary:
3555 case tok::kw_void:
3556 case tok::kw_char:
3557 case tok::kw_wchar_t:
3558 case tok::kw_char16_t:
3559 case tok::kw_char32_t:
3560 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003561 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003562 case tok::kw_float:
3563 case tok::kw_double:
3564 case tok::kw_bool:
3565 case tok::kw__Bool:
3566 case tok::kw__Decimal32:
3567 case tok::kw__Decimal64:
3568 case tok::kw__Decimal128:
3569 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003570
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003571 // struct-or-union-specifier (C99) or class-specifier (C++)
3572 case tok::kw_class:
3573 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003574 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003575 case tok::kw_union:
3576 // enum-specifier
3577 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003578
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003579 // typedef-name
3580 case tok::annot_typename:
3581 return true;
3582 }
3583}
3584
Steve Naroff5f8aa692008-02-11 23:15:56 +00003585/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003586/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003587bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003588 switch (Tok.getKind()) {
3589 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003590
Chris Lattner166a8fc2009-01-04 23:41:41 +00003591 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003592 if (TryAltiVecVectorToken())
3593 return true;
3594 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003595 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003596 // Annotate typenames and C++ scope specifiers. If we get one, just
3597 // recurse to handle whatever we get.
3598 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003599 return true;
3600 if (Tok.is(tok::identifier))
3601 return false;
3602 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003603
Chris Lattner166a8fc2009-01-04 23:41:41 +00003604 case tok::coloncolon: // ::foo::bar
3605 if (NextToken().is(tok::kw_new) || // ::new
3606 NextToken().is(tok::kw_delete)) // ::delete
3607 return false;
3608
Chris Lattner166a8fc2009-01-04 23:41:41 +00003609 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003610 return true;
3611 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003612
Reid Spencer5f016e22007-07-11 17:01:13 +00003613 // GNU attributes support.
3614 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003615 // GNU typeof support.
3616 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003617
Reid Spencer5f016e22007-07-11 17:01:13 +00003618 // type-specifiers
3619 case tok::kw_short:
3620 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003621 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003622 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003623 case tok::kw_signed:
3624 case tok::kw_unsigned:
3625 case tok::kw__Complex:
3626 case tok::kw__Imaginary:
3627 case tok::kw_void:
3628 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003629 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003630 case tok::kw_char16_t:
3631 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003632 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003633 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003634 case tok::kw_float:
3635 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003636 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003637 case tok::kw__Bool:
3638 case tok::kw__Decimal32:
3639 case tok::kw__Decimal64:
3640 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003641 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003642
Chris Lattner99dc9142008-04-13 18:59:07 +00003643 // struct-or-union-specifier (C99) or class-specifier (C++)
3644 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003645 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003646 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003647 case tok::kw_union:
3648 // enum-specifier
3649 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003650
Reid Spencer5f016e22007-07-11 17:01:13 +00003651 // type-qualifier
3652 case tok::kw_const:
3653 case tok::kw_volatile:
3654 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003655
John McCallb8a8de32012-11-14 00:49:39 +00003656 // Debugger support.
3657 case tok::kw___unknown_anytype:
3658
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003659 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003660 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003661 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003662
Chris Lattner7c186be2008-10-20 00:25:30 +00003663 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3664 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003665 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003666
Steve Naroff239f0732008-12-25 14:16:32 +00003667 case tok::kw___cdecl:
3668 case tok::kw___stdcall:
3669 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003670 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003671 case tok::kw___w64:
3672 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003673 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003674 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003675 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003676
3677 case tok::kw___private:
3678 case tok::kw___local:
3679 case tok::kw___global:
3680 case tok::kw___constant:
3681 case tok::kw___read_only:
3682 case tok::kw___read_write:
3683 case tok::kw___write_only:
3684
Eli Friedman290eeb02009-06-08 23:27:34 +00003685 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003686
3687 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003688 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003689
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003690 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003691 case tok::kw__Atomic:
3692 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003693 }
3694}
3695
3696/// isDeclarationSpecifier() - Return true if the current token is part of a
3697/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003698///
3699/// \param DisambiguatingWithExpression True to indicate that the purpose of
3700/// this check is to disambiguate between an expression and a declaration.
3701bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003702 switch (Tok.getKind()) {
3703 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003704
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003705 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003706 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003707
Chris Lattner166a8fc2009-01-04 23:41:41 +00003708 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003709 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003710 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003711 return false;
John Thompson82287d12010-02-05 00:12:22 +00003712 if (TryAltiVecVectorToken())
3713 return true;
3714 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003715 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003716 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003717 // Annotate typenames and C++ scope specifiers. If we get one, just
3718 // recurse to handle whatever we get.
3719 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003720 return true;
3721 if (Tok.is(tok::identifier))
3722 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003723
Douglas Gregor9497a732010-09-16 01:51:54 +00003724 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00003725 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00003726 // expression is permitted, then this is probably a class message send
3727 // missing the initial '['. In this case, we won't consider this to be
3728 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00003729 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00003730 isStartOfObjCClassMessageMissingOpenBracket())
3731 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003732
John McCall9ba61662010-02-26 08:45:28 +00003733 return isDeclarationSpecifier();
3734
Chris Lattner166a8fc2009-01-04 23:41:41 +00003735 case tok::coloncolon: // ::foo::bar
3736 if (NextToken().is(tok::kw_new) || // ::new
3737 NextToken().is(tok::kw_delete)) // ::delete
3738 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003739
Chris Lattner166a8fc2009-01-04 23:41:41 +00003740 // Annotate typenames and C++ scope specifiers. If we get one, just
3741 // recurse to handle whatever we get.
3742 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003743 return true;
3744 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003745
Reid Spencer5f016e22007-07-11 17:01:13 +00003746 // storage-class-specifier
3747 case tok::kw_typedef:
3748 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003749 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003750 case tok::kw_static:
3751 case tok::kw_auto:
3752 case tok::kw_register:
3753 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003754
Douglas Gregor8d267c52011-09-09 02:06:17 +00003755 // Modules
3756 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00003757
John McCallb8a8de32012-11-14 00:49:39 +00003758 // Debugger support
3759 case tok::kw___unknown_anytype:
3760
Reid Spencer5f016e22007-07-11 17:01:13 +00003761 // type-specifiers
3762 case tok::kw_short:
3763 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003764 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003765 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003766 case tok::kw_signed:
3767 case tok::kw_unsigned:
3768 case tok::kw__Complex:
3769 case tok::kw__Imaginary:
3770 case tok::kw_void:
3771 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003772 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003773 case tok::kw_char16_t:
3774 case tok::kw_char32_t:
3775
Reid Spencer5f016e22007-07-11 17:01:13 +00003776 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003777 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003778 case tok::kw_float:
3779 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003780 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003781 case tok::kw__Bool:
3782 case tok::kw__Decimal32:
3783 case tok::kw__Decimal64:
3784 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003785 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003786
Chris Lattner99dc9142008-04-13 18:59:07 +00003787 // struct-or-union-specifier (C99) or class-specifier (C++)
3788 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003789 case tok::kw_struct:
3790 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00003791 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003792 // enum-specifier
3793 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003794
Reid Spencer5f016e22007-07-11 17:01:13 +00003795 // type-qualifier
3796 case tok::kw_const:
3797 case tok::kw_volatile:
3798 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003799
Reid Spencer5f016e22007-07-11 17:01:13 +00003800 // function-specifier
3801 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003802 case tok::kw_virtual:
3803 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003804
Richard Smith53aec2a2012-10-25 00:00:53 +00003805 // friend keyword.
3806 case tok::kw_friend:
3807
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003808 // static_assert-declaration
3809 case tok::kw__Static_assert:
3810
Chris Lattner1ef08762007-08-09 17:01:07 +00003811 // GNU typeof support.
3812 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003813
Chris Lattner1ef08762007-08-09 17:01:07 +00003814 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003815 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00003816
Richard Smith53aec2a2012-10-25 00:00:53 +00003817 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003818 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00003819 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00003820
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003821 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003822 case tok::kw__Atomic:
3823 return true;
3824
Chris Lattnerf3948c42008-07-26 03:38:44 +00003825 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3826 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003827 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003828
Douglas Gregord9d75e52011-04-27 05:41:15 +00003829 // typedef-name
3830 case tok::annot_typename:
3831 return !DisambiguatingWithExpression ||
3832 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00003833
Steve Naroff47f52092009-01-06 19:34:12 +00003834 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003835 case tok::kw___cdecl:
3836 case tok::kw___stdcall:
3837 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003838 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003839 case tok::kw___w64:
3840 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003841 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003842 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003843 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003844 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003845
3846 case tok::kw___private:
3847 case tok::kw___local:
3848 case tok::kw___global:
3849 case tok::kw___constant:
3850 case tok::kw___read_only:
3851 case tok::kw___read_write:
3852 case tok::kw___write_only:
3853
Eli Friedman290eeb02009-06-08 23:27:34 +00003854 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003855 }
3856}
3857
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003858bool Parser::isConstructorDeclarator() {
3859 TentativeParsingAction TPA(*this);
3860
3861 // Parse the C++ scope specifier.
3862 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00003863 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003864 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003865 TPA.Revert();
3866 return false;
3867 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003868
3869 // Parse the constructor name.
3870 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3871 // We already know that we have a constructor name; just consume
3872 // the token.
3873 ConsumeToken();
3874 } else {
3875 TPA.Revert();
3876 return false;
3877 }
3878
Richard Smith22592862012-03-27 23:05:05 +00003879 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003880 if (Tok.isNot(tok::l_paren)) {
3881 TPA.Revert();
3882 return false;
3883 }
3884 ConsumeParen();
3885
Richard Smith22592862012-03-27 23:05:05 +00003886 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3887 // that we have a constructor.
3888 if (Tok.is(tok::r_paren) ||
3889 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003890 TPA.Revert();
3891 return true;
3892 }
3893
3894 // If we need to, enter the specified scope.
3895 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003896 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003897 DeclScopeObj.EnterDeclaratorScope();
3898
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003899 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003900 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003901 MaybeParseMicrosoftAttributes(Attrs);
3902
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003903 // Check whether the next token(s) are part of a declaration
3904 // specifier, in which case we have the start of a parameter and,
3905 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003906 bool IsConstructor = false;
3907 if (isDeclarationSpecifier())
3908 IsConstructor = true;
3909 else if (Tok.is(tok::identifier) ||
3910 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3911 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3912 // This might be a parenthesized member name, but is more likely to
3913 // be a constructor declaration with an invalid argument type. Keep
3914 // looking.
3915 if (Tok.is(tok::annot_cxxscope))
3916 ConsumeToken();
3917 ConsumeToken();
3918
3919 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003920 // which must have one of the following syntactic forms (see the
3921 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003922 switch (Tok.getKind()) {
3923 case tok::l_paren:
3924 // C(X ( int));
3925 case tok::l_square:
3926 // C(X [ 5]);
3927 // C(X [ [attribute]]);
3928 case tok::coloncolon:
3929 // C(X :: Y);
3930 // C(X :: *p);
3931 case tok::r_paren:
3932 // C(X )
3933 // Assume this isn't a constructor, rather than assuming it's a
3934 // constructor with an unnamed parameter of an ill-formed type.
3935 break;
3936
3937 default:
3938 IsConstructor = true;
3939 break;
3940 }
3941 }
3942
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003943 TPA.Revert();
3944 return IsConstructor;
3945}
Reid Spencer5f016e22007-07-11 17:01:13 +00003946
3947/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003948/// type-qualifier-list: [C99 6.7.5]
3949/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00003950/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00003951/// [ only if VendorAttributesAllowed=true ]
3952/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00003953/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00003954/// [ only if VendorAttributesAllowed=true ]
3955/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3956/// [ only if CXX0XAttributesAllowed=true ]
3957/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003958///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003959void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3960 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003961 bool CXX11AttributesAllowed) {
3962 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003963 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003964 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003965 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003966 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003967 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003968
3969 SourceLocation EndLoc;
3970
Reid Spencer5f016e22007-07-11 17:01:13 +00003971 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003972 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003973 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003974 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003975 SourceLocation Loc = Tok.getLocation();
3976
3977 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003978 case tok::code_completion:
3979 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003980 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00003981
Reid Spencer5f016e22007-07-11 17:01:13 +00003982 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003983 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003984 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003985 break;
3986 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003987 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003988 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003989 break;
3990 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003991 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003992 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003993 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003994
3995 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00003996 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003997 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003998 goto DoneWithTypeQuals;
3999 case tok::kw___private:
4000 case tok::kw___global:
4001 case tok::kw___local:
4002 case tok::kw___constant:
4003 case tok::kw___read_only:
4004 case tok::kw___write_only:
4005 case tok::kw___read_write:
4006 ParseOpenCLQualifiers(DS);
4007 break;
4008
Eli Friedman290eeb02009-06-08 23:27:34 +00004009 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004010 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004011 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004012 case tok::kw___cdecl:
4013 case tok::kw___stdcall:
4014 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004015 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004016 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004017 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004018 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004019 continue;
4020 }
4021 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004022 case tok::kw___pascal:
4023 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004024 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004025 continue;
4026 }
4027 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004028 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004029 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004030 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004031 continue; // do *not* consume the next token!
4032 }
4033 // otherwise, FALL THROUGH!
4034 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004035 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004036 // If this is not a type-qualifier token, we're done reading type
4037 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004038 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004039 if (EndLoc.isValid())
4040 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004041 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004042 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004043
Reid Spencer5f016e22007-07-11 17:01:13 +00004044 // If the specifier combination wasn't legal, issue a diagnostic.
4045 if (isInvalid) {
4046 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004047 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004048 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004049 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004050 }
4051}
4052
4053
4054/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4055///
4056void Parser::ParseDeclarator(Declarator &D) {
4057 /// This implements the 'declarator' production in the C grammar, then checks
4058 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004059 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004060}
4061
Richard Smith9988f282012-03-29 01:16:42 +00004062static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4063 if (Kind == tok::star || Kind == tok::caret)
4064 return true;
4065
4066 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4067 if (!Lang.CPlusPlus)
4068 return false;
4069
4070 return Kind == tok::amp || Kind == tok::ampamp;
4071}
4072
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004073/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4074/// is parsed by the function passed to it. Pass null, and the direct-declarator
4075/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004076/// ptr-operator production.
4077///
Richard Smith0706df42011-10-19 21:33:05 +00004078/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004079/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4080/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004081///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004082/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4083/// [C] pointer[opt] direct-declarator
4084/// [C++] direct-declarator
4085/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004086///
4087/// pointer: [C99 6.7.5]
4088/// '*' type-qualifier-list[opt]
4089/// '*' type-qualifier-list[opt] pointer
4090///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004091/// ptr-operator:
4092/// '*' cv-qualifier-seq[opt]
4093/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004094/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004095/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004096/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004097/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004098void Parser::ParseDeclaratorInternal(Declarator &D,
4099 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004100 if (Diags.hasAllExtensionsSilenced())
4101 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004102
Sebastian Redlf30208a2009-01-24 21:16:55 +00004103 // C++ member pointers start with a '::' or a nested-name.
4104 // Member pointers get special handling, since there's no place for the
4105 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004106 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004107 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4108 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004109 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4110 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004111 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004112 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004113
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004114 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004115 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004116 // The scope spec really belongs to the direct-declarator.
4117 D.getCXXScopeSpec() = SS;
4118 if (DirectDeclParser)
4119 (this->*DirectDeclParser)(D);
4120 return;
4121 }
4122
4123 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004124 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004125 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004126 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004127 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004128
4129 // Recurse to parse whatever is left.
4130 ParseDeclaratorInternal(D, DirectDeclParser);
4131
4132 // Sema will have to catch (syntactically invalid) pointers into global
4133 // scope. It has to catch pointers into namespace scope anyway.
4134 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004135 Loc),
4136 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004137 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004138 return;
4139 }
4140 }
4141
4142 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004143 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004144 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004145 if (DirectDeclParser)
4146 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004147 return;
4148 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004149
Sebastian Redl05532f22009-03-15 22:02:01 +00004150 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4151 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004152 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004153 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004154
Chris Lattner9af55002009-03-27 04:18:06 +00004155 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004156 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004157 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004158
Richard Smith6ee326a2012-04-10 01:32:12 +00004159 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004160 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004161 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004162
Reid Spencer5f016e22007-07-11 17:01:13 +00004163 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004164 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004165 if (Kind == tok::star)
4166 // Remember that we parsed a pointer type, and remember the type-quals.
4167 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004168 DS.getConstSpecLoc(),
4169 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004170 DS.getRestrictSpecLoc()),
4171 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004172 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004173 else
4174 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004175 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004176 Loc),
4177 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004178 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004179 } else {
4180 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004181 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004182
Sebastian Redl743de1f2009-03-23 00:00:23 +00004183 // Complain about rvalue references in C++03, but then go on and build
4184 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004185 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00004186 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004187 diag::warn_cxx98_compat_rvalue_reference :
4188 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004189
Richard Smith6ee326a2012-04-10 01:32:12 +00004190 // GNU-style and C++11 attributes are allowed here, as is restrict.
4191 ParseTypeQualifierListOpt(DS);
4192 D.ExtendWithDeclSpec(DS);
4193
Reid Spencer5f016e22007-07-11 17:01:13 +00004194 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4195 // cv-qualifiers are introduced through the use of a typedef or of a
4196 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004197 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4198 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4199 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004200 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004201 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4202 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004203 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00004204 }
4205
4206 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004207 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004208
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004209 if (D.getNumTypeObjects() > 0) {
4210 // C++ [dcl.ref]p4: There shall be no references to references.
4211 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4212 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004213 if (const IdentifierInfo *II = D.getIdentifier())
4214 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4215 << II;
4216 else
4217 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4218 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004219
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004220 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004221 // can go ahead and build the (technically ill-formed)
4222 // declarator: reference collapsing will take care of it.
4223 }
4224 }
4225
Reid Spencer5f016e22007-07-11 17:01:13 +00004226 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004227 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004228 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004229 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004230 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004231 }
4232}
4233
Richard Smith9988f282012-03-29 01:16:42 +00004234static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4235 SourceLocation EllipsisLoc) {
4236 if (EllipsisLoc.isValid()) {
4237 FixItHint Insertion;
4238 if (!D.getEllipsisLoc().isValid()) {
4239 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4240 D.setEllipsisLoc(EllipsisLoc);
4241 }
4242 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4243 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4244 }
4245}
4246
Reid Spencer5f016e22007-07-11 17:01:13 +00004247/// ParseDirectDeclarator
4248/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004249/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004250/// '(' declarator ')'
4251/// [GNU] '(' attributes declarator ')'
4252/// [C90] direct-declarator '[' constant-expression[opt] ']'
4253/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4254/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4255/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4256/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004257/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4258/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004259/// direct-declarator '(' parameter-type-list ')'
4260/// direct-declarator '(' identifier-list[opt] ')'
4261/// [GNU] direct-declarator '(' parameter-forward-declarations
4262/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004263/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4264/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004265/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4266/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4267/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004268/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004269/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004270///
4271/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004272/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004273/// '::'[opt] nested-name-specifier[opt] type-name
4274///
4275/// id-expression: [C++ 5.1]
4276/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004277/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004278///
4279/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004280/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004281/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004282/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004283/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004284/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004285///
Richard Smith5d8388c2012-03-27 01:42:32 +00004286/// Note, any additional constructs added here may need corresponding changes
4287/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004288void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004289 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004290
David Blaikie4e4d0842012-03-11 07:00:24 +00004291 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004292 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004293 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004294 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4295 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004296 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004297 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004298 }
4299
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004300 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004301 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004302 // Change the declaration context for name lookup, until this function
4303 // is exited (and the declarator has been parsed).
4304 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004305 }
4306
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004307 // C++0x [dcl.fct]p14:
4308 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004309 // of a parameter-declaration-clause without a preceding comma. In
4310 // this case, the ellipsis is parsed as part of the
4311 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004312 // parameter pack that has not been expanded; otherwise, it is parsed
4313 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004314 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004315 !((D.getContext() == Declarator::PrototypeContext ||
4316 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004317 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00004318 !Actions.containsUnexpandedParameterPacks(D))) {
4319 SourceLocation EllipsisLoc = ConsumeToken();
4320 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4321 // The ellipsis was put in the wrong place. Recover, and explain to
4322 // the user what they should have done.
4323 ParseDeclarator(D);
4324 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4325 return;
4326 } else
4327 D.setEllipsisLoc(EllipsisLoc);
4328
4329 // The ellipsis can't be followed by a parenthesized declarator. We
4330 // check for that in ParseParenDeclarator, after we have disambiguated
4331 // the l_paren token.
4332 }
4333
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004334 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4335 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4336 // We found something that indicates the start of an unqualified-id.
4337 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004338 bool AllowConstructorName;
4339 if (D.getDeclSpec().hasTypeSpecifier())
4340 AllowConstructorName = false;
4341 else if (D.getCXXScopeSpec().isSet())
4342 AllowConstructorName =
4343 (D.getContext() == Declarator::FileContext ||
4344 (D.getContext() == Declarator::MemberContext &&
4345 D.getDeclSpec().isFriendSpecified()));
4346 else
4347 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4348
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004349 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004350 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4351 /*EnteringContext=*/true,
4352 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004353 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004354 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004355 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004356 D.getName()) ||
4357 // Once we're past the identifier, if the scope was bad, mark the
4358 // whole declarator bad.
4359 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004360 D.SetIdentifier(0, Tok.getLocation());
4361 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004362 } else {
4363 // Parsed the unqualified-id; update range information and move along.
4364 if (D.getSourceRange().getBegin().isInvalid())
4365 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4366 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004367 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004368 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004369 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004370 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004371 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004372 "There's a C++-specific check for tok::identifier above");
4373 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4374 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4375 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004376 goto PastIdentifier;
4377 }
Richard Smith9988f282012-03-29 01:16:42 +00004378
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004379 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004380 // direct-declarator: '(' declarator ')'
4381 // direct-declarator: '(' attributes declarator ')'
4382 // Example: 'char (*X)' or 'int (*XX)(void)'
4383 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004384
4385 // If the declarator was parenthesized, we entered the declarator
4386 // scope when parsing the parenthesized declarator, then exited
4387 // the scope already. Re-enter the scope, if we need to.
4388 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004389 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004390 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004391 if (!D.isInvalidType() &&
4392 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004393 // Change the declaration context for name lookup, until this function
4394 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004395 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004396 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004397 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004398 // This could be something simple like "int" (in which case the declarator
4399 // portion is empty), if an abstract-declarator is allowed.
4400 D.SetIdentifier(0, Tok.getLocation());
4401 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004402 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004403 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004404 if (D.getContext() == Declarator::MemberContext)
4405 Diag(Tok, diag::err_expected_member_name_or_semi)
4406 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004407 else if (getLangOpts().CPlusPlus)
4408 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004409 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004410 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004411 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004412 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004413 }
Mike Stump1eb44332009-09-09 15:08:12 +00004414
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004415 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004416 assert(D.isPastIdentifier() &&
4417 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004418
Richard Smith6ee326a2012-04-10 01:32:12 +00004419 // Don't parse attributes unless we have parsed an unparenthesized name.
4420 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004421 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004422
Reid Spencer5f016e22007-07-11 17:01:13 +00004423 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004424 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004425 // Enter function-declaration scope, limiting any declarators to the
4426 // function prototype scope, including parameter declarators.
4427 ParseScope PrototypeScope(this,
4428 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004429 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4430 // In such a case, check if we actually have a function declarator; if it
4431 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004432 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004433 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4434 // The name of the declarator, if any, is tentatively declared within
4435 // a possible direct initializer.
4436 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4437 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4438 TentativelyDeclaredIdentifiers.pop_back();
4439 if (!IsFunctionDecl)
4440 break;
4441 }
John McCall0b7e6782011-03-24 11:26:52 +00004442 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004443 BalancedDelimiterTracker T(*this, tok::l_paren);
4444 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004445 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004446 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004447 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004448 ParseBracketDeclarator(D);
4449 } else {
4450 break;
4451 }
4452 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004453}
Reid Spencer5f016e22007-07-11 17:01:13 +00004454
Chris Lattneref4715c2008-04-06 05:45:57 +00004455/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4456/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004457/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004458/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4459///
4460/// direct-declarator:
4461/// '(' declarator ')'
4462/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004463/// direct-declarator '(' parameter-type-list ')'
4464/// direct-declarator '(' identifier-list[opt] ')'
4465/// [GNU] direct-declarator '(' parameter-forward-declarations
4466/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004467///
4468void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004469 BalancedDelimiterTracker T(*this, tok::l_paren);
4470 T.consumeOpen();
4471
Chris Lattneref4715c2008-04-06 05:45:57 +00004472 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004473
Chris Lattner7399ee02008-10-20 02:05:46 +00004474 // Eat any attributes before we look at whether this is a grouping or function
4475 // declarator paren. If this is a grouping paren, the attribute applies to
4476 // the type being built up, for example:
4477 // int (__attribute__(()) *x)(long y)
4478 // If this ends up not being a grouping paren, the attribute applies to the
4479 // first argument, for example:
4480 // int (__attribute__(()) int x)
4481 // In either case, we need to eat any attributes to be able to determine what
4482 // sort of paren this is.
4483 //
John McCall0b7e6782011-03-24 11:26:52 +00004484 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004485 bool RequiresArg = false;
4486 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004487 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004488
Chris Lattner7399ee02008-10-20 02:05:46 +00004489 // We require that the argument list (if this is a non-grouping paren) be
4490 // present even if the attribute list was empty.
4491 RequiresArg = true;
4492 }
Steve Naroff239f0732008-12-25 14:16:32 +00004493 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004494 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004495 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004496 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004497 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004498 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004499 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004500 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004501 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004502 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004503
Chris Lattneref4715c2008-04-06 05:45:57 +00004504 // If we haven't past the identifier yet (or where the identifier would be
4505 // stored, if this is an abstract declarator), then this is probably just
4506 // grouping parens. However, if this could be an abstract-declarator, then
4507 // this could also be the start of function arguments (consider 'void()').
4508 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004509
Chris Lattneref4715c2008-04-06 05:45:57 +00004510 if (!D.mayOmitIdentifier()) {
4511 // If this can't be an abstract-declarator, this *must* be a grouping
4512 // paren, because we haven't seen the identifier yet.
4513 isGrouping = true;
4514 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004515 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4516 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004517 isDeclarationSpecifier() || // 'int(int)' is a function.
4518 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004519 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4520 // considered to be a type, not a K&R identifier-list.
4521 isGrouping = false;
4522 } else {
4523 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4524 isGrouping = true;
4525 }
Mike Stump1eb44332009-09-09 15:08:12 +00004526
Chris Lattneref4715c2008-04-06 05:45:57 +00004527 // If this is a grouping paren, handle:
4528 // direct-declarator: '(' declarator ')'
4529 // direct-declarator: '(' attributes declarator ')'
4530 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004531 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4532 D.setEllipsisLoc(SourceLocation());
4533
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004534 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004535 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004536 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004537 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004538 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004539 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004540 T.getCloseLocation()),
4541 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004542
4543 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004544
4545 // An ellipsis cannot be placed outside parentheses.
4546 if (EllipsisLoc.isValid())
4547 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4548
Chris Lattneref4715c2008-04-06 05:45:57 +00004549 return;
4550 }
Mike Stump1eb44332009-09-09 15:08:12 +00004551
Chris Lattneref4715c2008-04-06 05:45:57 +00004552 // Okay, if this wasn't a grouping paren, it must be the start of a function
4553 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004554 // identifier (and remember where it would have been), then call into
4555 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004556 D.SetIdentifier(0, Tok.getLocation());
4557
David Blaikie42d6d0c2011-12-04 05:04:18 +00004558 // Enter function-declaration scope, limiting any declarators to the
4559 // function prototype scope, including parameter declarators.
4560 ParseScope PrototypeScope(this,
4561 Scope::FunctionPrototypeScope|Scope::DeclScope);
Richard Smithb9c62612012-07-30 21:30:52 +00004562 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004563 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004564}
4565
4566/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4567/// declarator D up to a paren, which indicates that we are parsing function
4568/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004569///
Richard Smith6ee326a2012-04-10 01:32:12 +00004570/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4571/// immediately after the open paren - they should be considered to be the
4572/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004573///
Richard Smith6ee326a2012-04-10 01:32:12 +00004574/// If RequiresArg is true, then the first argument of the function is required
4575/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004576///
Richard Smith6ee326a2012-04-10 01:32:12 +00004577/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4578/// (C++11) ref-qualifier[opt], exception-specification[opt],
4579/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4580///
4581/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004582/// dynamic-exception-specification
4583/// noexcept-specification
4584///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004585void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004586 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004587 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004588 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004589 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004590 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004591 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004592 // lparen is already consumed!
4593 assert(D.isPastIdentifier() && "Should not call before identifier!");
4594
4595 // This should be true when the function has typed arguments.
4596 // Otherwise, it is treated as a K&R-style function.
4597 bool HasProto = false;
4598 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004599 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004600 // Remember where we see an ellipsis, if any.
4601 SourceLocation EllipsisLoc;
4602
4603 DeclSpec DS(AttrFactory);
4604 bool RefQualifierIsLValueRef = true;
4605 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004606 SourceLocation ConstQualifierLoc;
4607 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004608 ExceptionSpecificationType ESpecType = EST_None;
4609 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004610 SmallVector<ParsedType, 2> DynamicExceptions;
4611 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004612 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004613 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004614 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004615
James Molloy16f1f712012-02-29 10:24:19 +00004616 Actions.ActOnStartFunctionDeclarator();
4617
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004618 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4619 EndLoc is the end location for the function declarator.
4620 They differ for trailing return types. */
4621 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004622 SourceLocation LParenLoc, RParenLoc;
4623 LParenLoc = Tracker.getOpenLocation();
4624 StartLoc = LParenLoc;
4625
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004626 if (isFunctionDeclaratorIdentifierList()) {
4627 if (RequiresArg)
4628 Diag(Tok, diag::err_argument_required_after_attribute);
4629
4630 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4631
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004632 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004633 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004634 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004635 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004636 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004637 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004638 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004639 else if (RequiresArg)
4640 Diag(Tok, diag::err_argument_required_after_attribute);
4641
David Blaikie4e4d0842012-03-11 07:00:24 +00004642 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004643
4644 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004645 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004646 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004647 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004648 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004649
David Blaikie4e4d0842012-03-11 07:00:24 +00004650 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004651 // FIXME: Accept these components in any order, and produce fixits to
4652 // correct the order if the user gets it wrong. Ideally we should deal
4653 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004654
4655 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004656 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4657 if (!DS.getSourceRange().getEnd().isInvalid()) {
4658 EndLoc = DS.getSourceRange().getEnd();
4659 ConstQualifierLoc = DS.getConstSpecLoc();
4660 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4661 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004662
4663 // Parse ref-qualifier[opt].
4664 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004665 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004666 diag::warn_cxx98_compat_ref_qualifier :
4667 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004668
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004669 RefQualifierIsLValueRef = Tok.is(tok::amp);
4670 RefQualifierLoc = ConsumeToken();
4671 EndLoc = RefQualifierLoc;
4672 }
4673
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004674 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00004675 // If a declaration declares a member function or member function
4676 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004677 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00004678 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004679 // declarator.
Chad Rosier8decdee2012-06-26 22:30:43 +00004680 bool IsCXX11MemberFunction =
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004681 getLangOpts().CPlusPlus0x &&
4682 (D.getContext() == Declarator::MemberContext ||
4683 (D.getContext() == Declarator::FileContext &&
Chad Rosier8decdee2012-06-26 22:30:43 +00004684 D.getCXXScopeSpec().isValid() &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004685 Actions.CurContext->isRecord()));
4686 Sema::CXXThisScopeRAII ThisScope(Actions,
4687 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4688 DS.getTypeQualifiers(),
4689 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004690
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004691 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004692 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004693 DynamicExceptions,
4694 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004695 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004696 if (ESpecType != EST_None)
4697 EndLoc = ESpecRange.getEnd();
4698
Richard Smith6ee326a2012-04-10 01:32:12 +00004699 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4700 // after the exception-specification.
4701 MaybeParseCXX0XAttributes(FnAttrs);
4702
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004703 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004704 LocalEndLoc = EndLoc;
David Blaikie4e4d0842012-03-11 07:00:24 +00004705 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004706 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004707 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4708 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004709 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00004710 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004711 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004712 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004713 }
4714 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004715 }
4716
4717 // Remember that we parsed a function type, and remember the attributes.
4718 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004719 IsAmbiguous,
4720 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004721 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004722 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004723 DS.getTypeQualifiers(),
4724 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004725 RefQualifierLoc, ConstQualifierLoc,
4726 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004727 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004728 ESpecType, ESpecRange.getBegin(),
4729 DynamicExceptions.data(),
4730 DynamicExceptionRanges.data(),
4731 DynamicExceptions.size(),
4732 NoexceptExpr.isUsable() ?
4733 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004734 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004735 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004736 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004737
4738 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004739}
4740
4741/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4742/// identifier list form for a K&R-style function: void foo(a,b,c)
4743///
4744/// Note that identifier-lists are only allowed for normal declarators, not for
4745/// abstract-declarators.
4746bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004747 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004748 && Tok.is(tok::identifier)
4749 && !TryAltiVecVectorToken()
4750 // K&R identifier lists can't have typedefs as identifiers, per C99
4751 // 6.7.5.3p11.
4752 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4753 // Identifier lists follow a really simple grammar: the identifiers can
4754 // be followed *only* by a ", identifier" or ")". However, K&R
4755 // identifier lists are really rare in the brave new modern world, and
4756 // it is very common for someone to typo a type in a non-K&R style
4757 // list. If we are presented with something like: "void foo(intptr x,
4758 // float y)", we don't want to start parsing the function declarator as
4759 // though it is a K&R style declarator just because intptr is an
4760 // invalid type.
4761 //
4762 // To handle this, we check to see if the token after the first
4763 // identifier is a "," or ")". Only then do we parse it as an
4764 // identifier list.
4765 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4766}
4767
4768/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4769/// we found a K&R-style identifier list instead of a typed parameter list.
4770///
4771/// After returning, ParamInfo will hold the parsed parameters.
4772///
4773/// identifier-list: [C99 6.7.5]
4774/// identifier
4775/// identifier-list ',' identifier
4776///
4777void Parser::ParseFunctionDeclaratorIdentifierList(
4778 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004779 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004780 // If there was no identifier specified for the declarator, either we are in
4781 // an abstract-declarator, or we are in a parameter declarator which was found
4782 // to be abstract. In abstract-declarators, identifier lists are not valid:
4783 // diagnose this.
4784 if (!D.getIdentifier())
4785 Diag(Tok, diag::ext_ident_list_in_param);
4786
4787 // Maintain an efficient lookup of params we have seen so far.
4788 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4789
4790 while (1) {
4791 // If this isn't an identifier, report the error and skip until ')'.
4792 if (Tok.isNot(tok::identifier)) {
4793 Diag(Tok, diag::err_expected_ident);
4794 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4795 // Forget we parsed anything.
4796 ParamInfo.clear();
4797 return;
4798 }
4799
4800 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4801
4802 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4803 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4804 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4805
4806 // Verify that the argument identifier has not already been mentioned.
4807 if (!ParamsSoFar.insert(ParmII)) {
4808 Diag(Tok, diag::err_param_redefinition) << ParmII;
4809 } else {
4810 // Remember this identifier in ParamInfo.
4811 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4812 Tok.getLocation(),
4813 0));
4814 }
4815
4816 // Eat the identifier.
4817 ConsumeToken();
4818
4819 // The list continues if we see a comma.
4820 if (Tok.isNot(tok::comma))
4821 break;
4822 ConsumeToken();
4823 }
4824}
4825
4826/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4827/// after the opening parenthesis. This function will not parse a K&R-style
4828/// identifier list.
4829///
Richard Smith6ce48a72012-04-11 04:01:28 +00004830/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4831/// caller parsed those arguments immediately after the open paren - they should
4832/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004833///
4834/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4835/// be the location of the ellipsis, if any was parsed.
4836///
Reid Spencer5f016e22007-07-11 17:01:13 +00004837/// parameter-type-list: [C99 6.7.5]
4838/// parameter-list
4839/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004840/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004841///
4842/// parameter-list: [C99 6.7.5]
4843/// parameter-declaration
4844/// parameter-list ',' parameter-declaration
4845///
4846/// parameter-declaration: [C99 6.7.5]
4847/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004848/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004849/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004850/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004851/// declaration-specifiers abstract-declarator[opt]
4852/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004853/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004854/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004855/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004856///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004857void Parser::ParseParameterDeclarationClause(
4858 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004859 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004860 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004861 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004862
Chris Lattnerf97409f2008-04-06 06:57:35 +00004863 while (1) {
4864 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004865 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4866 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004867 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004868 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004869 }
Mike Stump1eb44332009-09-09 15:08:12 +00004870
Chris Lattnerf97409f2008-04-06 06:57:35 +00004871 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004872 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004873 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004874
Richard Smith6ce48a72012-04-11 04:01:28 +00004875 // Parse any C++11 attributes.
4876 MaybeParseCXX0XAttributes(DS.getAttributes());
4877
John McCall7f040a92010-12-24 02:08:15 +00004878 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004879 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004880 ParseMicrosoftAttributes(DS.getAttributes());
4881
4882 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004883
4884 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004885 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004886 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004887 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4888 // too much hassle.
4889 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004890
Chris Lattnere64c5492009-02-27 18:38:20 +00004891 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004892
Chris Lattnerf97409f2008-04-06 06:57:35 +00004893 // Parse the declarator. This is "PrototypeContext", because we must
4894 // accept either 'declarator' or 'abstract-declarator' here.
4895 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4896 ParseDeclarator(ParmDecl);
4897
4898 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004899 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004900
Chris Lattnerf97409f2008-04-06 06:57:35 +00004901 // Remember this parsed parameter in ParamInfo.
4902 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004903
Douglas Gregor72b505b2008-12-16 21:30:33 +00004904 // DefArgToks is used when the parsing of default arguments needs
4905 // to be delayed.
4906 CachedTokens *DefArgToks = 0;
4907
Chris Lattnerf97409f2008-04-06 06:57:35 +00004908 // If no parameter was specified, verify that *something* was specified,
4909 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004910 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4911 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004912 // Completely missing, emit error.
4913 Diag(DSStart, diag::err_missing_param);
4914 } else {
4915 // Otherwise, we have something. Add it and let semantic analysis try
4916 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004917
Chris Lattnerf97409f2008-04-06 06:57:35 +00004918 // Inform the actions module about the parameter declarator, so it gets
4919 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004920 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004921
4922 // Parse the default argument, if any. We parse the default
4923 // arguments in all dialects; the semantic analysis in
4924 // ActOnParamDefaultArgument will reject the default argument in
4925 // C.
4926 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004927 SourceLocation EqualLoc = Tok.getLocation();
4928
Chris Lattner04421082008-04-08 04:40:51 +00004929 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004930 if (D.getContext() == Declarator::MemberContext) {
4931 // If we're inside a class definition, cache the tokens
4932 // corresponding to the default argument. We'll actually parse
4933 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004934 // FIXME: Can we use a smart pointer for Toks?
4935 DefArgToks = new CachedTokens;
4936
Mike Stump1eb44332009-09-09 15:08:12 +00004937 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004938 /*StopAtSemi=*/true,
4939 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004940 delete DefArgToks;
4941 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004942 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004943 } else {
4944 // Mark the end of the default argument so that we know when to
4945 // stop when we parse it later on.
4946 Token DefArgEnd;
4947 DefArgEnd.startToken();
4948 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4949 DefArgEnd.setLocation(Tok.getLocation());
4950 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004951 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004952 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004953 }
Chris Lattner04421082008-04-08 04:40:51 +00004954 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004955 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004956 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004957
Chad Rosier8decdee2012-06-26 22:30:43 +00004958 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004959 // used.
4960 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004961 Sema::PotentiallyEvaluatedIfUsed,
4962 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004963
Sebastian Redl84407ba2012-03-14 15:54:00 +00004964 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004965 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4966 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004967 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004968 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004969 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004970 if (DefArgResult.isInvalid()) {
4971 Actions.ActOnParamDefaultArgumentError(Param);
4972 SkipUntil(tok::comma, tok::r_paren, true, true);
4973 } else {
4974 // Inform the actions module about the default argument
4975 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004976 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004977 }
Chris Lattner04421082008-04-08 04:40:51 +00004978 }
4979 }
Mike Stump1eb44332009-09-09 15:08:12 +00004980
4981 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4982 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004983 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004984 }
4985
4986 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004987 if (Tok.isNot(tok::comma)) {
4988 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004989 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00004990
David Blaikie4e4d0842012-03-11 07:00:24 +00004991 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004992 // We have ellipsis without a preceding ',', which is ill-formed
4993 // in C. Complain and provide the fix.
4994 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004995 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004996 }
4997 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004998
Douglas Gregored5d6512009-09-22 21:41:40 +00004999 break;
5000 }
Mike Stump1eb44332009-09-09 15:08:12 +00005001
Chris Lattnerf97409f2008-04-06 06:57:35 +00005002 // Consume the comma.
5003 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005004 }
Mike Stump1eb44332009-09-09 15:08:12 +00005005
Chris Lattner66d28652008-04-06 06:34:08 +00005006}
Chris Lattneref4715c2008-04-06 05:45:57 +00005007
Reid Spencer5f016e22007-07-11 17:01:13 +00005008/// [C90] direct-declarator '[' constant-expression[opt] ']'
5009/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5010/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5011/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5012/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005013/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5014/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005015void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005016 if (CheckProhibitedCXX11Attribute())
5017 return;
5018
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005019 BalancedDelimiterTracker T(*this, tok::l_square);
5020 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005021
Chris Lattner378c7e42008-12-18 07:27:21 +00005022 // C array syntax has many features, but by-far the most common is [] and [4].
5023 // This code does a fast path to handle some of the most obvious cases.
5024 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005025 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005026 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00005027 MaybeParseCXX0XAttributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005028
Chris Lattner378c7e42008-12-18 07:27:21 +00005029 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005030 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005031 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005032 T.getOpenLocation(),
5033 T.getCloseLocation()),
5034 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005035 return;
5036 } else if (Tok.getKind() == tok::numeric_constant &&
5037 GetLookAheadToken(1).is(tok::r_square)) {
5038 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005039 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005040 ConsumeToken();
5041
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005042 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005043 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00005044 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005045
Chris Lattner378c7e42008-12-18 07:27:21 +00005046 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005047 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00005048 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005049 T.getOpenLocation(),
5050 T.getCloseLocation()),
5051 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005052 return;
5053 }
Mike Stump1eb44332009-09-09 15:08:12 +00005054
Reid Spencer5f016e22007-07-11 17:01:13 +00005055 // If valid, this location is the position where we read the 'static' keyword.
5056 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005057 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005058 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005059
Reid Spencer5f016e22007-07-11 17:01:13 +00005060 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005061 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005062 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005063 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005064
Reid Spencer5f016e22007-07-11 17:01:13 +00005065 // If we haven't already read 'static', check to see if there is one after the
5066 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005067 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005068 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005069
Reid Spencer5f016e22007-07-11 17:01:13 +00005070 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5071 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005072 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005073
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005074 // Handle the case where we have '[*]' as the array size. However, a leading
5075 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005076 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005077 // infrequent, use of lookahead is not costly here.
5078 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005079 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005080
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005081 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005082 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005083 StaticLoc = SourceLocation(); // Drop the static.
5084 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005085 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005086 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005087 // Note, in C89, this production uses the constant-expr production instead
5088 // of assignment-expr. The only difference is that assignment-expr allows
5089 // things like '=' and '*='. Sema rejects these in C89 mode because they
5090 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005091
Douglas Gregore0762c92009-06-19 23:52:42 +00005092 // Parse the constant-expression or assignment-expression now (depending
5093 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005094 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005095 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005096 } else {
5097 EnterExpressionEvaluationContext Unevaluated(Actions,
5098 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005099 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005100 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005101 }
Mike Stump1eb44332009-09-09 15:08:12 +00005102
Reid Spencer5f016e22007-07-11 17:01:13 +00005103 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005104 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005105 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005106 // If the expression was invalid, skip it.
5107 SkipUntil(tok::r_square);
5108 return;
5109 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005110
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005111 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005112
John McCall0b7e6782011-03-24 11:26:52 +00005113 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00005114 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005115
Chris Lattner378c7e42008-12-18 07:27:21 +00005116 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005117 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005118 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005119 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005120 T.getOpenLocation(),
5121 T.getCloseLocation()),
5122 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005123}
5124
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005125/// [GNU] typeof-specifier:
5126/// typeof ( expressions )
5127/// typeof ( type-name )
5128/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005129///
5130void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005131 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005132 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005133 SourceLocation StartLoc = ConsumeToken();
5134
John McCallcfb708c2010-01-13 20:03:27 +00005135 const bool hasParens = Tok.is(tok::l_paren);
5136
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005137 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5138 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005139
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005140 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005141 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005142 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005143 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5144 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005145 if (hasParens)
5146 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005147
5148 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005149 // FIXME: Not accurate, the range gets one token more than it should.
5150 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005151 else
5152 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005153
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005154 if (isCastExpr) {
5155 if (!CastTy) {
5156 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005157 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005158 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005159
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005160 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005161 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005162 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5163 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005164 DiagID, CastTy))
5165 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005166 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005167 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005168
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005169 // If we get here, the operand to the typeof was an expresion.
5170 if (Operand.isInvalid()) {
5171 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005172 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005173 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005174
Eli Friedman71b8fb52012-01-21 01:01:51 +00005175 // We might need to transform the operand if it is potentially evaluated.
5176 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5177 if (Operand.isInvalid()) {
5178 DS.SetTypeSpecError();
5179 return;
5180 }
5181
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005182 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005183 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005184 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5185 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005186 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005187 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005188}
Chris Lattner1b492422010-02-28 18:33:55 +00005189
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005190/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005191/// _Atomic ( type-name )
5192///
5193void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5194 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5195
5196 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005197 BalancedDelimiterTracker T(*this, tok::l_paren);
5198 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00005199 SkipUntil(tok::r_paren);
5200 return;
5201 }
5202
5203 TypeResult Result = ParseTypeName();
5204 if (Result.isInvalid()) {
5205 SkipUntil(tok::r_paren);
5206 return;
5207 }
5208
5209 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005210 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005211
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005212 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005213 return;
5214
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005215 DS.setTypeofParensRange(T.getRange());
5216 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005217
5218 const char *PrevSpec = 0;
5219 unsigned DiagID;
5220 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5221 DiagID, Result.release()))
5222 Diag(StartLoc, DiagID) << PrevSpec;
5223}
5224
Chris Lattner1b492422010-02-28 18:33:55 +00005225
5226/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5227/// from TryAltiVecVectorToken.
5228bool Parser::TryAltiVecVectorTokenOutOfLine() {
5229 Token Next = NextToken();
5230 switch (Next.getKind()) {
5231 default: return false;
5232 case tok::kw_short:
5233 case tok::kw_long:
5234 case tok::kw_signed:
5235 case tok::kw_unsigned:
5236 case tok::kw_void:
5237 case tok::kw_char:
5238 case tok::kw_int:
5239 case tok::kw_float:
5240 case tok::kw_double:
5241 case tok::kw_bool:
5242 case tok::kw___pixel:
5243 Tok.setKind(tok::kw___vector);
5244 return true;
5245 case tok::identifier:
5246 if (Next.getIdentifierInfo() == Ident_pixel) {
5247 Tok.setKind(tok::kw___vector);
5248 return true;
5249 }
5250 return false;
5251 }
5252}
5253
5254bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5255 const char *&PrevSpec, unsigned &DiagID,
5256 bool &isInvalid) {
5257 if (Tok.getIdentifierInfo() == Ident_vector) {
5258 Token Next = NextToken();
5259 switch (Next.getKind()) {
5260 case tok::kw_short:
5261 case tok::kw_long:
5262 case tok::kw_signed:
5263 case tok::kw_unsigned:
5264 case tok::kw_void:
5265 case tok::kw_char:
5266 case tok::kw_int:
5267 case tok::kw_float:
5268 case tok::kw_double:
5269 case tok::kw_bool:
5270 case tok::kw___pixel:
5271 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5272 return true;
5273 case tok::identifier:
5274 if (Next.getIdentifierInfo() == Ident_pixel) {
5275 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5276 return true;
5277 }
5278 break;
5279 default:
5280 break;
5281 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005282 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005283 DS.isTypeAltiVecVector()) {
5284 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5285 return true;
5286 }
5287 return false;
5288}