blob: 28d8811b9b394c1bc264f0debb53067e9dac6df8 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000017#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000018#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000024#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// C99 6.7: Declarations.
29//===----------------------------------------------------------------------===//
30
31/// ParseTypeName
32/// type-name: [C99 6.7.6]
33/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000034///
35/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000036TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000037 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000038 AccessSpecifier AS,
39 Decl **OwnedType) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000040 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000041 if (DSC == DSC_normal)
42 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000043
Reid Spencer5f016e22007-07-11 17:01:13 +000044 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000045 DeclSpec DS(AttrFactory);
Richard Smith7796eb52012-03-12 08:56:40 +000046 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000047 if (OwnedType)
48 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000049
Reid Spencer5f016e22007-07-11 17:01:13 +000050 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000051 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000052 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000053 if (Range)
54 *Range = DeclaratorInfo.getSourceRange();
55
Chris Lattnereaaebc72009-04-25 08:06:05 +000056 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000057 return true;
58
Douglas Gregor23c94db2010-07-02 17:43:08 +000059 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000060}
61
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000062
63/// isAttributeLateParsed - Return true if the attribute has arguments that
64/// require late parsing.
65static bool isAttributeLateParsed(const IdentifierInfo &II) {
66 return llvm::StringSwitch<bool>(II.getName())
67#include "clang/Parse/AttrLateParsed.inc"
68 .Default(false);
69}
70
Sean Huntbbd37c62009-11-21 08:43:09 +000071/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000072///
73/// [GNU] attributes:
74/// attribute
75/// attributes attribute
76///
77/// [GNU] attribute:
78/// '__attribute__' '(' '(' attribute-list ')' ')'
79///
80/// [GNU] attribute-list:
81/// attrib
82/// attribute_list ',' attrib
83///
84/// [GNU] attrib:
85/// empty
86/// attrib-name
87/// attrib-name '(' identifier ')'
88/// attrib-name '(' identifier ',' nonempty-expr-list ')'
89/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
90///
91/// [GNU] attrib-name:
92/// identifier
93/// typespec
94/// typequal
95/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000096///
Reid Spencer5f016e22007-07-11 17:01:13 +000097/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000098/// token lookahead. Comment from gcc: "If they start with an identifier
99/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +0000100/// start with that identifier; otherwise they are an expression list."
101///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000102/// GCC does not require the ',' between attribs in an attribute-list.
103///
Reid Spencer5f016e22007-07-11 17:01:13 +0000104/// At the moment, I am not doing 2 token lookahead. I am also unaware of
105/// any attributes that don't work (based on my limited testing). Most
106/// attributes are very simple in practice. Until we find a bug, I don't see
107/// a pressing need to implement the 2 token lookahead.
108
John McCall7f040a92010-12-24 02:08:15 +0000109void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000110 SourceLocation *endLoc,
111 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000112 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Chris Lattner04d66662007-10-09 17:33:22 +0000114 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 ConsumeToken();
116 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
117 "attribute")) {
118 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000119 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 }
121 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
122 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000123 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 }
125 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000126 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
127 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000128 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
130 ConsumeToken();
131 continue;
132 }
133 // we have an identifier or declaration specifier (const, int, etc.)
134 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
135 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000137 if (Tok.is(tok::l_paren)) {
138 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000139 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000140 LateParsedAttribute *LA =
141 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
142 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000143
144 // Attributes in a class are parsed at the end of the class, along
145 // with other late-parsed declarations.
146 if (!ClassStack.empty())
147 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000149 // consume everything up to and including the matching right parens
150 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000152 Token Eof;
153 Eof.startToken();
154 Eof.setLocation(Tok.getLocation());
155 LA->Toks.push_back(Eof);
156 } else {
157 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 }
159 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000160 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000161 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 }
163 }
164 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000166 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000167 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
168 SkipUntil(tok::r_paren, false);
169 }
John McCall7f040a92010-12-24 02:08:15 +0000170 if (endLoc)
171 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000173}
174
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000175
176/// Parse the arguments to a parameterized GNU attribute
177void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
178 SourceLocation AttrNameLoc,
179 ParsedAttributes &Attrs,
180 SourceLocation *EndLoc) {
181
182 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
183
184 // Availability attributes have their own grammar.
185 if (AttrName->isStr("availability")) {
186 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
187 return;
188 }
189 // Thread safety attributes fit into the FIXME case above, so we
190 // just parse the arguments as a list of expressions
191 if (IsThreadSafetyAttribute(AttrName->getName())) {
192 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
193 return;
194 }
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000195 // Type safety attributes have their own grammar.
196 if (AttrName->isStr("type_tag_for_datatype")) {
197 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
198 return;
199 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000200
201 ConsumeParen(); // ignore the left paren loc for now
202
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000203 IdentifierInfo *ParmName = 0;
204 SourceLocation ParmLoc;
205 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000206
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000207 switch (Tok.getKind()) {
208 case tok::kw_char:
209 case tok::kw_wchar_t:
210 case tok::kw_char16_t:
211 case tok::kw_char32_t:
212 case tok::kw_bool:
213 case tok::kw_short:
214 case tok::kw_int:
215 case tok::kw_long:
216 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000217 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000218 case tok::kw_signed:
219 case tok::kw_unsigned:
220 case tok::kw_float:
221 case tok::kw_double:
222 case tok::kw_void:
223 case tok::kw_typeof:
224 // __attribute__(( vec_type_hint(char) ))
225 // FIXME: Don't just discard the builtin type token.
226 ConsumeToken();
227 BuiltinType = true;
228 break;
229
230 case tok::identifier:
231 ParmName = Tok.getIdentifierInfo();
232 ParmLoc = ConsumeToken();
233 break;
234
235 default:
236 break;
237 }
238
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +0000239 ExprVector ArgExprs;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000240
241 if (!BuiltinType &&
242 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
243 // Eat the comma.
244 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000245 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000246
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000247 // Parse the non-empty comma-separated list of expressions.
248 while (1) {
249 ExprResult ArgExpr(ParseAssignmentExpression());
250 if (ArgExpr.isInvalid()) {
251 SkipUntil(tok::r_paren);
252 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000253 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000254 ArgExprs.push_back(ArgExpr.release());
255 if (Tok.isNot(tok::comma))
256 break;
257 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000258 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000259 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000260 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
261 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
262 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000263 while (Tok.is(tok::identifier)) {
264 ConsumeToken();
265 if (Tok.is(tok::greater))
266 break;
267 if (Tok.is(tok::comma)) {
268 ConsumeToken();
269 continue;
270 }
271 }
272 if (Tok.isNot(tok::greater))
273 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000274 SkipUntil(tok::r_paren, false, true); // skip until ')'
275 }
276 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000277
278 SourceLocation RParen = Tok.getLocation();
279 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
280 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000281 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +0000282 ParmName, ParmLoc, ArgExprs.data(), ArgExprs.size(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000283 AttributeList::AS_GNU);
Sean Hunt8e083e72012-06-19 23:57:03 +0000284 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000285 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000286 }
287}
288
Chad Rosier8decdee2012-06-26 22:30:43 +0000289/// \brief Parses a single argument for a declspec, including the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000290/// surrounding parens.
Chad Rosier8decdee2012-06-26 22:30:43 +0000291void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000292 SourceLocation AttrNameLoc,
293 ParsedAttributes &Attrs)
294{
295 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000296 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000297 AttrName->getNameStart(), tok::r_paren))
298 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000299
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000300 ExprResult ArgExpr(ParseConstantExpression());
301 if (ArgExpr.isInvalid()) {
302 T.skipToEnd();
303 return;
304 }
305 Expr *ExprList = ArgExpr.take();
Chad Rosier8decdee2012-06-26 22:30:43 +0000306 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000307 &ExprList, 1, AttributeList::AS_Declspec);
308
309 T.consumeClose();
310}
311
Chad Rosier8decdee2012-06-26 22:30:43 +0000312/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000313/// arguments.
314bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
315 return llvm::StringSwitch<bool>(Ident->getName())
316 .Case("dllimport", true)
317 .Case("dllexport", true)
318 .Case("noreturn", true)
319 .Case("nothrow", true)
320 .Case("noinline", true)
321 .Case("naked", true)
322 .Case("appdomain", true)
323 .Case("process", true)
324 .Case("jitintrinsic", true)
325 .Case("noalias", true)
326 .Case("restrict", true)
327 .Case("novtable", true)
328 .Case("selectany", true)
329 .Case("thread", true)
330 .Default(false);
331}
332
Chad Rosier8decdee2012-06-26 22:30:43 +0000333/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000334/// parameters). Will return false if we properly handled the declspec, or
335/// true if it is an unknown declspec.
Chad Rosier8decdee2012-06-26 22:30:43 +0000336void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000337 SourceLocation Loc,
338 ParsedAttributes &Attrs) {
339 // Try to handle the easy case first -- these declspecs all take a single
340 // parameter as their argument.
341 if (llvm::StringSwitch<bool>(Ident->getName())
342 .Case("uuid", true)
343 .Case("align", true)
344 .Case("allocate", true)
345 .Default(false)) {
346 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
347 } else if (Ident->getName() == "deprecated") {
Chad Rosier8decdee2012-06-26 22:30:43 +0000348 // The deprecated declspec has an optional single argument, so we will
349 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000350 // not.
351 if (Tok.getKind() == tok::l_paren)
352 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
353 else
Chad Rosier8decdee2012-06-26 22:30:43 +0000354 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000355 AttributeList::AS_Declspec);
356 } else if (Ident->getName() == "property") {
357 // The property declspec is more complex in that it can take one or two
Chad Rosier8decdee2012-06-26 22:30:43 +0000358 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000359 // must be named get or put.
360 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000361 // For right now, we will just skip to the closing right paren of the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000362 // property expression.
363 //
364 // FIXME: we should deal with __declspec(property) at some point because it
365 // is used in the platform SDK headers for the Parallel Patterns Library
366 // and ATL.
367 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000368 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000369 Ident->getNameStart(), tok::r_paren))
370 return;
371 T.skipToEnd();
372 } else {
373 // We don't recognize this as a valid declspec, but instead of creating the
374 // attribute and allowing sema to warn about it, we will warn here instead.
375 // This is because some attributes have multiple spellings, but we need to
376 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosier8decdee2012-06-26 22:30:43 +0000377 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000378 // both locations.
379 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
380
381 // If there's an open paren, we should eat the open and close parens under
382 // the assumption that this unknown declspec has parameters.
383 BalancedDelimiterTracker T(*this, tok::l_paren);
384 if (!T.consumeOpen())
385 T.skipToEnd();
386 }
387}
388
Eli Friedmana23b4852009-06-08 07:21:15 +0000389/// [MS] decl-specifier:
390/// __declspec ( extended-decl-modifier-seq )
391///
392/// [MS] extended-decl-modifier-seq:
393/// extended-decl-modifier[opt]
394/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000395void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000396 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000397
Steve Narofff59e17e2008-12-24 20:59:21 +0000398 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000399 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000400 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000401 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000402 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000403
Chad Rosier8decdee2012-06-26 22:30:43 +0000404 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000405 // you can specify multiple attributes per declspec.
406 while (Tok.getKind() != tok::r_paren) {
407 // We expect either a well-known identifier or a generic string. Anything
408 // else is a malformed declspec.
409 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosier8decdee2012-06-26 22:30:43 +0000410 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000411 Tok.getKind() != tok::kw_restrict) {
412 Diag(Tok, diag::err_ms_declspec_type);
413 T.skipToEnd();
414 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000415 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000416
417 IdentifierInfo *AttrName;
418 SourceLocation AttrNameLoc;
419 if (IsString) {
420 SmallString<8> StrBuffer;
421 bool Invalid = false;
422 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
423 if (Invalid) {
424 T.skipToEnd();
425 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000426 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000427 AttrName = PP.getIdentifierInfo(Str);
428 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000429 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000430 AttrName = Tok.getIdentifierInfo();
431 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000432 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000433
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000434 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosier8decdee2012-06-26 22:30:43 +0000435 // If we have a generic string, we will allow it because there is no
436 // documented list of allowable string declspecs, but we know they exist
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000437 // (for instance, SAL declspecs in older versions of MSVC).
438 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000439 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000440 // arguments and can be turned into an attribute directly.
Chad Rosier8decdee2012-06-26 22:30:43 +0000441 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000442 0, 0, AttributeList::AS_Declspec);
443 else
444 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000445 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000446 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000447}
448
John McCall7f040a92010-12-24 02:08:15 +0000449void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000450 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000451 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000452 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000453 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000454 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000455 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000456 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
457 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000458 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000459 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Eli Friedman290eeb02009-06-08 23:27:34 +0000460 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000461}
462
John McCall7f040a92010-12-24 02:08:15 +0000463void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000464 // Treat these like attributes
465 while (Tok.is(tok::kw___pascal)) {
466 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
467 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000468 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000469 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000470 }
John McCall7f040a92010-12-24 02:08:15 +0000471}
472
Peter Collingbournef315fa82011-02-14 01:42:53 +0000473void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
474 // Treat these like attributes
475 while (Tok.is(tok::kw___kernel)) {
476 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000477 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
478 AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000479 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000480 }
481}
482
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000483void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
484 SourceLocation Loc = Tok.getLocation();
485 switch(Tok.getKind()) {
486 // OpenCL qualifiers:
487 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000488 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000489 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000490 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000491 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000492 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000493
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000494 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000495 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000496 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000497 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
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___local:
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_local);
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___constant:
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_constant);
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___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000513 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000514 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000515 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
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___write_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_write_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___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000525 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000526 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000527 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000528 break;
529 default: break;
530 }
531}
532
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000533/// \brief Parse a version number.
534///
535/// version:
536/// simple-integer
537/// simple-integer ',' simple-integer
538/// simple-integer ',' simple-integer ',' simple-integer
539VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
540 Range = Tok.getLocation();
541
542 if (!Tok.is(tok::numeric_constant)) {
543 Diag(Tok, diag::err_expected_version);
544 SkipUntil(tok::comma, tok::r_paren, true, true, true);
545 return VersionTuple();
546 }
547
548 // Parse the major (and possibly minor and subminor) versions, which
549 // are stored in the numeric constant. We utilize a quirk of the
550 // lexer, which is that it handles something like 1.2.3 as a single
551 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000552 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000553 Buffer.resize(Tok.getLength()+1);
554 const char *ThisTokBegin = &Buffer[0];
555
556 // Get the spelling of the token, which eliminates trigraphs, etc.
557 bool Invalid = false;
558 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
559 if (Invalid)
560 return VersionTuple();
561
562 // Parse the major version.
563 unsigned AfterMajor = 0;
564 unsigned Major = 0;
565 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
566 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
567 ++AfterMajor;
568 }
569
570 if (AfterMajor == 0) {
571 Diag(Tok, diag::err_expected_version);
572 SkipUntil(tok::comma, tok::r_paren, true, true, true);
573 return VersionTuple();
574 }
575
576 if (AfterMajor == ActualLength) {
577 ConsumeToken();
578
579 // We only had a single version component.
580 if (Major == 0) {
581 Diag(Tok, diag::err_zero_version);
582 return VersionTuple();
583 }
584
585 return VersionTuple(Major);
586 }
587
588 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
589 Diag(Tok, diag::err_expected_version);
590 SkipUntil(tok::comma, tok::r_paren, true, true, true);
591 return VersionTuple();
592 }
593
594 // Parse the minor version.
595 unsigned AfterMinor = AfterMajor + 1;
596 unsigned Minor = 0;
597 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
598 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
599 ++AfterMinor;
600 }
601
602 if (AfterMinor == ActualLength) {
603 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000604
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000605 // We had major.minor.
606 if (Major == 0 && Minor == 0) {
607 Diag(Tok, diag::err_zero_version);
608 return VersionTuple();
609 }
610
Chad Rosier8decdee2012-06-26 22:30:43 +0000611 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000612 }
613
614 // If what follows is not a '.', we have a problem.
615 if (ThisTokBegin[AfterMinor] != '.') {
616 Diag(Tok, diag::err_expected_version);
617 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosier8decdee2012-06-26 22:30:43 +0000618 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000619 }
620
621 // Parse the subminor version.
622 unsigned AfterSubminor = AfterMinor + 1;
623 unsigned Subminor = 0;
624 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
625 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
626 ++AfterSubminor;
627 }
628
629 if (AfterSubminor != ActualLength) {
630 Diag(Tok, diag::err_expected_version);
631 SkipUntil(tok::comma, tok::r_paren, true, true, true);
632 return VersionTuple();
633 }
634 ConsumeToken();
635 return VersionTuple(Major, Minor, Subminor);
636}
637
638/// \brief Parse the contents of the "availability" attribute.
639///
640/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000641/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000642///
643/// platform:
644/// identifier
645///
646/// version-arg-list:
647/// version-arg
648/// version-arg ',' version-arg-list
649///
650/// version-arg:
651/// 'introduced' '=' version
652/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000653/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000654/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000655/// opt-message:
656/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000657void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
658 SourceLocation AvailabilityLoc,
659 ParsedAttributes &attrs,
660 SourceLocation *endLoc) {
661 SourceLocation PlatformLoc;
662 IdentifierInfo *Platform = 0;
663
664 enum { Introduced, Deprecated, Obsoleted, Unknown };
665 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000666 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000667
668 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000669 BalancedDelimiterTracker T(*this, tok::l_paren);
670 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000671 Diag(Tok, diag::err_expected_lparen);
672 return;
673 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000674
675 // Parse the platform name,
676 if (Tok.isNot(tok::identifier)) {
677 Diag(Tok, diag::err_availability_expected_platform);
678 SkipUntil(tok::r_paren);
679 return;
680 }
681 Platform = Tok.getIdentifierInfo();
682 PlatformLoc = ConsumeToken();
683
684 // Parse the ',' following the platform name.
685 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
686 return;
687
688 // If we haven't grabbed the pointers for the identifiers
689 // "introduced", "deprecated", and "obsoleted", do so now.
690 if (!Ident_introduced) {
691 Ident_introduced = PP.getIdentifierInfo("introduced");
692 Ident_deprecated = PP.getIdentifierInfo("deprecated");
693 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000694 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000695 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000696 }
697
698 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000699 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000700 do {
701 if (Tok.isNot(tok::identifier)) {
702 Diag(Tok, diag::err_availability_expected_change);
703 SkipUntil(tok::r_paren);
704 return;
705 }
706 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
707 SourceLocation KeywordLoc = ConsumeToken();
708
Douglas Gregorb53e4172011-03-26 03:35:55 +0000709 if (Keyword == Ident_unavailable) {
710 if (UnavailableLoc.isValid()) {
711 Diag(KeywordLoc, diag::err_availability_redundant)
712 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000713 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000714 UnavailableLoc = KeywordLoc;
715
716 if (Tok.isNot(tok::comma))
717 break;
718
719 ConsumeToken();
720 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000721 }
722
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000723 if (Tok.isNot(tok::equal)) {
724 Diag(Tok, diag::err_expected_equal_after)
725 << Keyword;
726 SkipUntil(tok::r_paren);
727 return;
728 }
729 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000730 if (Keyword == Ident_message) {
731 if (!isTokenStringLiteral()) {
732 Diag(Tok, diag::err_expected_string_literal);
733 SkipUntil(tok::r_paren);
734 return;
735 }
736 MessageExpr = ParseStringLiteralExpression();
737 break;
738 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000739
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000740 SourceRange VersionRange;
741 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000742
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000743 if (Version.empty()) {
744 SkipUntil(tok::r_paren);
745 return;
746 }
747
748 unsigned Index;
749 if (Keyword == Ident_introduced)
750 Index = Introduced;
751 else if (Keyword == Ident_deprecated)
752 Index = Deprecated;
753 else if (Keyword == Ident_obsoleted)
754 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000755 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000756 Index = Unknown;
757
758 if (Index < Unknown) {
759 if (!Changes[Index].KeywordLoc.isInvalid()) {
760 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000761 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000762 << SourceRange(Changes[Index].KeywordLoc,
763 Changes[Index].VersionRange.getEnd());
764 }
765
766 Changes[Index].KeywordLoc = KeywordLoc;
767 Changes[Index].Version = Version;
768 Changes[Index].VersionRange = VersionRange;
769 } else {
770 Diag(KeywordLoc, diag::err_availability_unknown_change)
771 << Keyword << VersionRange;
772 }
773
774 if (Tok.isNot(tok::comma))
775 break;
776
777 ConsumeToken();
778 } while (true);
779
780 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000781 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000782 return;
783
784 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000785 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000786
Douglas Gregorb53e4172011-03-26 03:35:55 +0000787 // The 'unavailable' availability cannot be combined with any other
788 // availability changes. Make sure that hasn't happened.
789 if (UnavailableLoc.isValid()) {
790 bool Complained = false;
791 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
792 if (Changes[Index].KeywordLoc.isValid()) {
793 if (!Complained) {
794 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
795 << SourceRange(Changes[Index].KeywordLoc,
796 Changes[Index].VersionRange.getEnd());
797 Complained = true;
798 }
799
800 // Clear out the availability.
801 Changes[Index] = AvailabilityChange();
802 }
803 }
804 }
805
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000806 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000807 attrs.addNew(&Availability,
808 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000809 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000810 Platform, PlatformLoc,
811 Changes[Introduced],
812 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000813 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000814 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000815 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000816}
817
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000818
819// Late Parsed Attributes:
820// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
821
822void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
823
824void Parser::LateParsedClass::ParseLexedAttributes() {
825 Self->ParseLexedAttributes(*Class);
826}
827
828void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000829 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000830}
831
832/// Wrapper class which calls ParseLexedAttribute, after setting up the
833/// scope appropriately.
834void Parser::ParseLexedAttributes(ParsingClass &Class) {
835 // Deal with templates
836 // FIXME: Test cases to make sure this does the right thing for templates.
837 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
838 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
839 HasTemplateScope);
840 if (HasTemplateScope)
841 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
842
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000843 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000844 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000845 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000846 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
847 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
848
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000849 // Enter the scope of nested classes
850 if (!AlreadyHasClassScope)
851 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
852 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000853 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000854 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
855 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
856 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000857 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000858
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000859 if (!AlreadyHasClassScope)
860 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
861 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000862}
863
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000864
865/// \brief Parse all attributes in LAs, and attach them to Decl D.
866void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
867 bool EnterScope, bool OnDefinition) {
868 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +0000869 if (D)
870 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000871 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000872 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000873 }
874 LAs.clear();
875}
876
877
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000878/// \brief Finish parsing an attribute for which parsing was delayed.
879/// This will be called at the end of parsing a class declaration
880/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +0000881/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000882/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000883void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
884 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000885 // Save the current token position.
886 SourceLocation OrigLoc = Tok.getLocation();
887
888 // Append the current token at the end of the new token stream so that it
889 // doesn't get lost.
890 LA.Toks.push_back(Tok);
891 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
892 // Consume the previously pushed token.
893 ConsumeAnyToken();
894
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000895 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
896 Diag(Tok, diag::warn_attribute_on_function_definition)
897 << LA.AttrName.getName();
898 }
899
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000900 ParsedAttributes Attrs(AttrFactory);
901 SourceLocation endLoc;
902
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000903 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000904 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000905 NamedDecl *ND = dyn_cast<NamedDecl>(D);
906 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000907
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000908 // Allow 'this' within late-parsed attributes.
909 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
910 /*TypeQuals=*/0,
911 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000912
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000913 if (LA.Decls.size() == 1) {
914 // If the Decl is templatized, add template parameters to scope.
915 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
916 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
917 if (HasTemplateScope)
918 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000919
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000920 // If the Decl is on a function, add function parameters to the scope.
921 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
922 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
923 if (HasFunScope)
924 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000925
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000926 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
927
928 if (HasFunScope) {
929 Actions.ActOnExitFunctionContext();
930 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
931 }
932 if (HasTemplateScope) {
933 TempScope.Exit();
934 }
935 } else {
936 // If there are multiple decls, then the decl cannot be within the
937 // function scope.
938 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000939 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000940 } else {
941 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000942 }
943
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000944 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
945 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
946 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000947
948 if (Tok.getLocation() != OrigLoc) {
949 // Due to a parsing error, we either went over the cached tokens or
950 // there are still cached tokens left, so we skip the leftover tokens.
951 // Since this is an uncommon situation that should be avoided, use the
952 // expensive isBeforeInTranslationUnit call.
953 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
954 OrigLoc))
955 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000956 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000957 }
958}
959
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000960/// \brief Wrapper around a case statement checking if AttrName is
961/// one of the thread safety attributes
962bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
963 return llvm::StringSwitch<bool>(AttrName)
964 .Case("guarded_by", true)
965 .Case("guarded_var", true)
966 .Case("pt_guarded_by", true)
967 .Case("pt_guarded_var", true)
968 .Case("lockable", true)
969 .Case("scoped_lockable", true)
970 .Case("no_thread_safety_analysis", true)
971 .Case("acquired_after", true)
972 .Case("acquired_before", true)
973 .Case("exclusive_lock_function", true)
974 .Case("shared_lock_function", true)
975 .Case("exclusive_trylock_function", true)
976 .Case("shared_trylock_function", true)
977 .Case("unlock_function", true)
978 .Case("lock_returned", true)
979 .Case("locks_excluded", true)
980 .Case("exclusive_locks_required", true)
981 .Case("shared_locks_required", true)
982 .Default(false);
983}
984
985/// \brief Parse the contents of thread safety attributes. These
986/// should always be parsed as an expression list.
987///
988/// We need to special case the parsing due to the fact that if the first token
989/// of the first argument is an identifier, the main parse loop will store
990/// that token as a "parameter" and the rest of
991/// the arguments will be added to a list of "arguments". However,
992/// subsequent tokens in the first argument are lost. We instead parse each
993/// argument as an expression and add all arguments to the list of "arguments".
994/// In future, we will take advantage of this special case to also
995/// deal with some argument scoping issues here (for example, referring to a
996/// function parameter in the attribute on that function).
997void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
998 SourceLocation AttrNameLoc,
999 ParsedAttributes &Attrs,
1000 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001001 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001002
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001003 BalancedDelimiterTracker T(*this, tok::l_paren);
1004 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001005
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001006 ExprVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001007 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001008
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001009 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001010 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001011 ExprResult ArgExpr(ParseAssignmentExpression());
1012 if (ArgExpr.isInvalid()) {
1013 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001014 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001015 break;
1016 } else {
1017 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001018 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001019 if (Tok.isNot(tok::comma))
1020 break;
1021 ConsumeToken(); // Eat the comma, move to the next argument
1022 }
1023 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001024 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001025 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001026 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001027 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001028 if (EndLoc)
1029 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001030}
1031
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001032void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1033 SourceLocation AttrNameLoc,
1034 ParsedAttributes &Attrs,
1035 SourceLocation *EndLoc) {
1036 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1037
1038 BalancedDelimiterTracker T(*this, tok::l_paren);
1039 T.consumeOpen();
1040
1041 if (Tok.isNot(tok::identifier)) {
1042 Diag(Tok, diag::err_expected_ident);
1043 T.skipToEnd();
1044 return;
1045 }
1046 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1047 SourceLocation ArgumentKindLoc = ConsumeToken();
1048
1049 if (Tok.isNot(tok::comma)) {
1050 Diag(Tok, diag::err_expected_comma);
1051 T.skipToEnd();
1052 return;
1053 }
1054 ConsumeToken();
1055
1056 SourceRange MatchingCTypeRange;
1057 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1058 if (MatchingCType.isInvalid()) {
1059 T.skipToEnd();
1060 return;
1061 }
1062
1063 bool LayoutCompatible = false;
1064 bool MustBeNull = false;
1065 while (Tok.is(tok::comma)) {
1066 ConsumeToken();
1067 if (Tok.isNot(tok::identifier)) {
1068 Diag(Tok, diag::err_expected_ident);
1069 T.skipToEnd();
1070 return;
1071 }
1072 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1073 if (Flag->isStr("layout_compatible"))
1074 LayoutCompatible = true;
1075 else if (Flag->isStr("must_be_null"))
1076 MustBeNull = true;
1077 else {
1078 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1079 T.skipToEnd();
1080 return;
1081 }
1082 ConsumeToken(); // consume flag
1083 }
1084
1085 if (!T.consumeClose()) {
1086 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1087 ArgumentKind, ArgumentKindLoc,
1088 MatchingCType.release(), LayoutCompatible,
1089 MustBeNull, AttributeList::AS_GNU);
1090 }
1091
1092 if (EndLoc)
1093 *EndLoc = T.getCloseLocation();
1094}
1095
Richard Smith6ee326a2012-04-10 01:32:12 +00001096/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1097/// of a C++11 attribute-specifier in a location where an attribute is not
1098/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1099/// situation.
1100///
1101/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1102/// this doesn't appear to actually be an attribute-specifier, and the caller
1103/// should try to parse it.
1104bool Parser::DiagnoseProhibitedCXX11Attribute() {
1105 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1106
1107 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1108 case CAK_NotAttributeSpecifier:
1109 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1110 return false;
1111
1112 case CAK_InvalidAttributeSpecifier:
1113 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1114 return false;
1115
1116 case CAK_AttributeSpecifier:
1117 // Parse and discard the attributes.
1118 SourceLocation BeginLoc = ConsumeBracket();
1119 ConsumeBracket();
1120 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1121 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1122 SourceLocation EndLoc = ConsumeBracket();
1123 Diag(BeginLoc, diag::err_attributes_not_allowed)
1124 << SourceRange(BeginLoc, EndLoc);
1125 return true;
1126 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001127 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001128}
1129
John McCall7f040a92010-12-24 02:08:15 +00001130void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1131 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1132 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001133}
1134
Reid Spencer5f016e22007-07-11 17:01:13 +00001135/// ParseDeclaration - Parse a full 'declaration', which consists of
1136/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001137/// 'Context' should be a Declarator::TheContext value. This returns the
1138/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001139///
1140/// declaration: [C99 6.7]
1141/// block-declaration ->
1142/// simple-declaration
1143/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001144/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001145/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001146/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001147/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001148/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001149/// others... [FIXME]
1150///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001151Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1152 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001153 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001154 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001155 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001156 // Must temporarily exit the objective-c container scope for
1157 // parsing c none objective-c decls.
1158 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001159
John McCalld226f652010-08-21 09:40:31 +00001160 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001161 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001162 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001163 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001164 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001165 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001166 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001167 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001168 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001169 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001170 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001171 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001172 SourceLocation InlineLoc = ConsumeToken();
1173 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1174 break;
1175 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001176 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001177 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001178 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001179 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001180 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001181 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001182 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001183 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001184 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001185 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001186 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001187 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001188 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001189 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001190 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001191 default:
John McCall7f040a92010-12-24 02:08:15 +00001192 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001193 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001194
Chris Lattner682bf922009-03-29 16:50:03 +00001195 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001196 // single decl, convert it now. Alias declarations can also declare a type;
1197 // include that too if it is present.
1198 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001199}
1200
1201/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1202/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001203/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1204/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001205///[C90/C++]init-declarator-list ';' [TODO]
1206/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001207///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001208/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001209/// attribute-specifier-seq[opt] type-specifier-seq declarator
1210///
Chris Lattnercd147752009-03-29 17:27:48 +00001211/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001212/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001213///
1214/// If FRI is non-null, we might be parsing a for-range-declaration instead
1215/// of a simple-declaration. If we find that we are, we also parse the
1216/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001217Parser::DeclGroupPtrTy
1218Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1219 SourceLocation &DeclEnd,
1220 ParsedAttributesWithRange &attrs,
1221 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001223 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001224 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001225
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001226 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001227 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001228
Reid Spencer5f016e22007-07-11 17:01:13 +00001229 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1230 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001231 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001232 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001233 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001234 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001235 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001236 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001237 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001239
1240 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001241}
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Richard Smith0706df42011-10-19 21:33:05 +00001243/// Returns true if this might be the start of a declarator, or a common typo
1244/// for a declarator.
1245bool Parser::MightBeDeclarator(unsigned Context) {
1246 switch (Tok.getKind()) {
1247 case tok::annot_cxxscope:
1248 case tok::annot_template_id:
1249 case tok::caret:
1250 case tok::code_completion:
1251 case tok::coloncolon:
1252 case tok::ellipsis:
1253 case tok::kw___attribute:
1254 case tok::kw_operator:
1255 case tok::l_paren:
1256 case tok::star:
1257 return true;
1258
1259 case tok::amp:
1260 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001261 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001262
Richard Smith1c94c162012-01-09 22:31:44 +00001263 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001264 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001265 NextToken().is(tok::l_square);
1266
1267 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001268 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001269
Richard Smith0706df42011-10-19 21:33:05 +00001270 case tok::identifier:
1271 switch (NextToken().getKind()) {
1272 case tok::code_completion:
1273 case tok::coloncolon:
1274 case tok::comma:
1275 case tok::equal:
1276 case tok::equalequal: // Might be a typo for '='.
1277 case tok::kw_alignas:
1278 case tok::kw_asm:
1279 case tok::kw___attribute:
1280 case tok::l_brace:
1281 case tok::l_paren:
1282 case tok::l_square:
1283 case tok::less:
1284 case tok::r_brace:
1285 case tok::r_paren:
1286 case tok::r_square:
1287 case tok::semi:
1288 return true;
1289
1290 case tok::colon:
1291 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001292 // and in block scope it's probably a label. Inside a class definition,
1293 // this is a bit-field.
1294 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001295 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001296
1297 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001298 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001299
1300 default:
1301 return false;
1302 }
1303
1304 default:
1305 return false;
1306 }
1307}
1308
Richard Smith994d73f2012-04-11 20:59:20 +00001309/// Skip until we reach something which seems like a sensible place to pick
1310/// up parsing after a malformed declaration. This will sometimes stop sooner
1311/// than SkipUntil(tok::r_brace) would, but will never stop later.
1312void Parser::SkipMalformedDecl() {
1313 while (true) {
1314 switch (Tok.getKind()) {
1315 case tok::l_brace:
1316 // Skip until matching }, then stop. We've probably skipped over
1317 // a malformed class or function definition or similar.
1318 ConsumeBrace();
1319 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1320 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1321 // This declaration isn't over yet. Keep skipping.
1322 continue;
1323 }
1324 if (Tok.is(tok::semi))
1325 ConsumeToken();
1326 return;
1327
1328 case tok::l_square:
1329 ConsumeBracket();
1330 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1331 continue;
1332
1333 case tok::l_paren:
1334 ConsumeParen();
1335 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1336 continue;
1337
1338 case tok::r_brace:
1339 return;
1340
1341 case tok::semi:
1342 ConsumeToken();
1343 return;
1344
1345 case tok::kw_inline:
1346 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001347 // a good place to pick back up parsing, except in an Objective-C
1348 // @interface context.
1349 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1350 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001351 return;
1352 break;
1353
1354 case tok::kw_namespace:
1355 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001356 // place to pick back up parsing, except in an Objective-C
1357 // @interface context.
1358 if (Tok.isAtStartOfLine() &&
1359 (!ParsingInObjCContainer || CurParsedObjCImpl))
1360 return;
1361 break;
1362
1363 case tok::at:
1364 // @end is very much like } in Objective-C contexts.
1365 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1366 ParsingInObjCContainer)
1367 return;
1368 break;
1369
1370 case tok::minus:
1371 case tok::plus:
1372 // - and + probably start new method declarations in Objective-C contexts.
1373 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001374 return;
1375 break;
1376
1377 case tok::eof:
1378 return;
1379
1380 default:
1381 break;
1382 }
1383
1384 ConsumeAnyToken();
1385 }
1386}
1387
John McCalld8ac0572009-11-03 19:26:08 +00001388/// ParseDeclGroup - Having concluded that this is either a function
1389/// definition or a group of object declarations, actually parse the
1390/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001391Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1392 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001393 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001394 SourceLocation *DeclEnd,
1395 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001396 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001397 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001398 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001399
John McCalld8ac0572009-11-03 19:26:08 +00001400 // Bail out if the first declarator didn't seem well-formed.
1401 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001402 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001403 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001404 }
Mike Stump1eb44332009-09-09 15:08:12 +00001405
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001406 // Save late-parsed attributes for now; they need to be parsed in the
1407 // appropriate function scope after the function Decl has been constructed.
1408 LateParsedAttrList LateParsedAttrs;
1409 if (D.isFunctionDeclarator())
1410 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1411
Chris Lattnerc82daef2010-07-11 22:24:20 +00001412 // Check to see if we have a function *definition* which must have a body.
1413 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1414 // Look at the next token to make sure that this isn't a function
1415 // declaration. We have to check this because __attribute__ might be the
1416 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001417 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001418
Chris Lattner004659a2010-07-11 22:42:07 +00001419 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001420 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1421 Diag(Tok, diag::err_function_declared_typedef);
1422
1423 // Recover by treating the 'typedef' as spurious.
1424 DS.ClearStorageClassSpecs();
1425 }
1426
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001427 Decl *TheDecl =
1428 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001429 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001430 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001431
Chris Lattner004659a2010-07-11 22:42:07 +00001432 if (isDeclarationSpecifier()) {
1433 // If there is an invalid declaration specifier right after the function
1434 // prototype, then we must be in a missing semicolon case where this isn't
1435 // actually a body. Just fall through into the code that handles it as a
1436 // prototype, and let the top-level code handle the erroneous declspec
1437 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001438 } else {
1439 Diag(Tok, diag::err_expected_fn_body);
1440 SkipUntil(tok::semi);
1441 return DeclGroupPtrTy();
1442 }
1443 }
1444
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001445 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001446 return DeclGroupPtrTy();
1447
1448 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1449 // must parse and analyze the for-range-initializer before the declaration is
1450 // analyzed.
1451 if (FRI && Tok.is(tok::colon)) {
1452 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001453 if (Tok.is(tok::l_brace))
1454 FRI->RangeExpr = ParseBraceInitializer();
1455 else
1456 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001457 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1458 Actions.ActOnCXXForRangeDecl(ThisDecl);
1459 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001460 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001461 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1462 }
1463
Chris Lattner5f9e2722011-07-23 10:55:15 +00001464 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001465 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001466 if (LateParsedAttrs.size() > 0)
1467 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001468 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001469 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001470 DeclsInGroup.push_back(FirstDecl);
1471
Richard Smith0706df42011-10-19 21:33:05 +00001472 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001473
John McCalld8ac0572009-11-03 19:26:08 +00001474 // If we don't have a comma, it is either the end of the list (a ';') or an
1475 // error, bail out.
1476 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001477 SourceLocation CommaLoc = ConsumeToken();
1478
1479 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1480 // This comma was followed by a line-break and something which can't be
1481 // the start of a declarator. The comma was probably a typo for a
1482 // semicolon.
1483 Diag(CommaLoc, diag::err_expected_semi_declaration)
1484 << FixItHint::CreateReplacement(CommaLoc, ";");
1485 ExpectSemi = false;
1486 break;
1487 }
John McCalld8ac0572009-11-03 19:26:08 +00001488
1489 // Parse the next declarator.
1490 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001491 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001492
1493 // Accept attributes in an init-declarator. In the first declarator in a
1494 // declaration, these would be part of the declspec. In subsequent
1495 // declarators, they become part of the declarator itself, so that they
1496 // don't apply to declarators after *this* one. Examples:
1497 // short __attribute__((common)) var; -> declspec
1498 // short var __attribute__((common)); -> declarator
1499 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001500 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001501
1502 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001503 if (!D.isInvalidType()) {
1504 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1505 D.complete(ThisDecl);
1506 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001507 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001508 }
John McCalld8ac0572009-11-03 19:26:08 +00001509 }
1510
1511 if (DeclEnd)
1512 *DeclEnd = Tok.getLocation();
1513
Richard Smith0706df42011-10-19 21:33:05 +00001514 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001515 ExpectAndConsumeSemi(Context == Declarator::FileContext
1516 ? diag::err_invalid_token_after_toplevel_declarator
1517 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001518 // Okay, there was no semicolon and one was expected. If we see a
1519 // declaration specifier, just assume it was missing and continue parsing.
1520 // Otherwise things are very confused and we skip to recover.
1521 if (!isDeclarationSpecifier()) {
1522 SkipUntil(tok::r_brace, true, true);
1523 if (Tok.is(tok::semi))
1524 ConsumeToken();
1525 }
John McCalld8ac0572009-11-03 19:26:08 +00001526 }
1527
Douglas Gregor23c94db2010-07-02 17:43:08 +00001528 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001529 DeclsInGroup.data(),
1530 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001531}
1532
Richard Smithad762fc2011-04-14 22:09:26 +00001533/// Parse an optional simple-asm-expr and attributes, and attach them to a
1534/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001535bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001536 // If a simple-asm-expr is present, parse it.
1537 if (Tok.is(tok::kw_asm)) {
1538 SourceLocation Loc;
1539 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1540 if (AsmLabel.isInvalid()) {
1541 SkipUntil(tok::semi, true, true);
1542 return true;
1543 }
1544
1545 D.setAsmLabel(AsmLabel.release());
1546 D.SetRangeEnd(Loc);
1547 }
1548
1549 MaybeParseGNUAttributes(D);
1550 return false;
1551}
1552
Douglas Gregor1426e532009-05-12 21:31:51 +00001553/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1554/// declarator'. This method parses the remainder of the declaration
1555/// (including any attributes or initializer, among other things) and
1556/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001557///
Reid Spencer5f016e22007-07-11 17:01:13 +00001558/// init-declarator: [C99 6.7]
1559/// declarator
1560/// declarator '=' initializer
1561/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1562/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001563/// [C++] declarator initializer[opt]
1564///
1565/// [C++] initializer:
1566/// [C++] '=' initializer-clause
1567/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001568/// [C++0x] '=' 'default' [TODO]
1569/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001570/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001571///
1572/// According to the standard grammar, =default and =delete are function
1573/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001574///
John McCalld226f652010-08-21 09:40:31 +00001575Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001576 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001577 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001578 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001579
Richard Smithad762fc2011-04-14 22:09:26 +00001580 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1581}
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Richard Smithad762fc2011-04-14 22:09:26 +00001583Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1584 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001585 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001586 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001587 switch (TemplateInfo.Kind) {
1588 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001589 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001590 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001591
Douglas Gregord5a423b2009-09-25 18:43:00 +00001592 case ParsedTemplateInfo::Template:
1593 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001594 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001595 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001596 D);
1597 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001598
Douglas Gregord5a423b2009-09-25 18:43:00 +00001599 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001600 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001601 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001602 TemplateInfo.ExternLoc,
1603 TemplateInfo.TemplateLoc,
1604 D);
1605 if (ThisRes.isInvalid()) {
1606 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001607 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001608 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001609
Douglas Gregord5a423b2009-09-25 18:43:00 +00001610 ThisDecl = ThisRes.get();
1611 break;
1612 }
1613 }
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Richard Smith34b41d92011-02-20 03:19:35 +00001615 bool TypeContainsAuto =
1616 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1617
Douglas Gregor1426e532009-05-12 21:31:51 +00001618 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001619 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001620 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001621 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001622 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001623 if (D.isFunctionDeclarator())
1624 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1625 << 1 /* delete */;
1626 else
1627 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001628 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001629 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001630 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1631 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001632 else
1633 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001634 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001635 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001636 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001637 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001638 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001639
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001640 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001641 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001642 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001643 cutOffParsing();
1644 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001645 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001646
John McCall60d7b3a2010-08-24 06:29:42 +00001647 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001648
David Blaikie4e4d0842012-03-11 07:00:24 +00001649 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001650 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001651 ExitScope();
1652 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001653
Douglas Gregor1426e532009-05-12 21:31:51 +00001654 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001655 SkipUntil(tok::comma, true, true);
1656 Actions.ActOnInitializerError(ThisDecl);
1657 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001658 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1659 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001660 }
1661 } else if (Tok.is(tok::l_paren)) {
1662 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001663 BalancedDelimiterTracker T(*this, tok::l_paren);
1664 T.consumeOpen();
1665
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001666 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001667 CommaLocsTy CommaLocs;
1668
David Blaikie4e4d0842012-03-11 07:00:24 +00001669 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001670 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001671 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001672 }
1673
Douglas Gregor1426e532009-05-12 21:31:51 +00001674 if (ParseExpressionList(Exprs, CommaLocs)) {
1675 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001676
David Blaikie4e4d0842012-03-11 07:00:24 +00001677 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001678 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001679 ExitScope();
1680 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001681 } else {
1682 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001683 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001684
1685 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1686 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001687
David Blaikie4e4d0842012-03-11 07:00:24 +00001688 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001689 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001690 ExitScope();
1691 }
1692
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001693 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1694 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001695 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001696 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1697 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001698 }
Fariborz Jahanian3b5f9dc2012-07-03 22:54:28 +00001699 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001700 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001701 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001702 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1703
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001704 if (D.getCXXScopeSpec().isSet()) {
1705 EnterScope(0);
1706 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1707 }
1708
1709 ExprResult Init(ParseBraceInitializer());
1710
1711 if (D.getCXXScopeSpec().isSet()) {
1712 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1713 ExitScope();
1714 }
1715
1716 if (Init.isInvalid()) {
1717 Actions.ActOnInitializerError(ThisDecl);
1718 } else
1719 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1720 /*DirectInit=*/true, TypeContainsAuto);
1721
Douglas Gregor1426e532009-05-12 21:31:51 +00001722 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001723 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001724 }
1725
Richard Smith483b9f32011-02-21 20:05:19 +00001726 Actions.FinalizeDeclaration(ThisDecl);
1727
Douglas Gregor1426e532009-05-12 21:31:51 +00001728 return ThisDecl;
1729}
1730
Reid Spencer5f016e22007-07-11 17:01:13 +00001731/// ParseSpecifierQualifierList
1732/// specifier-qualifier-list:
1733/// type-specifier specifier-qualifier-list[opt]
1734/// type-qualifier specifier-qualifier-list[opt]
1735/// [GNU] attributes specifier-qualifier-list[opt]
1736///
Richard Smith69730c12012-03-12 07:56:15 +00001737void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1738 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001739 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1740 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001741 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001742 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001743
Reid Spencer5f016e22007-07-11 17:01:13 +00001744 // Validate declspec for type-name.
1745 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001746 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1747 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001748 Diag(Tok, diag::err_expected_type);
1749 DS.SetTypeSpecError();
1750 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1751 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001752 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001753 if (!DS.hasTypeSpecifier())
1754 DS.SetTypeSpecError();
1755 }
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Reid Spencer5f016e22007-07-11 17:01:13 +00001757 // Issue diagnostic and remove storage class if present.
1758 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1759 if (DS.getStorageClassSpecLoc().isValid())
1760 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1761 else
1762 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1763 DS.ClearStorageClassSpecs();
1764 }
Mike Stump1eb44332009-09-09 15:08:12 +00001765
Reid Spencer5f016e22007-07-11 17:01:13 +00001766 // Issue diagnostic and remove function specfier if present.
1767 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001768 if (DS.isInlineSpecified())
1769 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1770 if (DS.isVirtualSpecified())
1771 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1772 if (DS.isExplicitSpecified())
1773 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001774 DS.ClearFunctionSpecs();
1775 }
Richard Smith69730c12012-03-12 07:56:15 +00001776
1777 // Issue diagnostic and remove constexpr specfier if present.
1778 if (DS.isConstexprSpecified()) {
1779 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1780 DS.ClearConstexprSpec();
1781 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001782}
1783
Chris Lattnerc199ab32009-04-12 20:42:31 +00001784/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1785/// specified token is valid after the identifier in a declarator which
1786/// immediately follows the declspec. For example, these things are valid:
1787///
1788/// int x [ 4]; // direct-declarator
1789/// int x ( int y); // direct-declarator
1790/// int(int x ) // direct-declarator
1791/// int x ; // simple-declaration
1792/// int x = 17; // init-declarator-list
1793/// int x , y; // init-declarator-list
1794/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001795/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001796/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001797///
1798/// This is not, because 'x' does not immediately follow the declspec (though
1799/// ')' happens to be valid anyway).
1800/// int (x)
1801///
1802static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1803 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1804 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001805 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001806}
1807
Chris Lattnere40c2952009-04-14 21:34:55 +00001808
1809/// ParseImplicitInt - This method is called when we have an non-typename
1810/// identifier in a declspec (which normally terminates the decl spec) when
1811/// the declspec has no type specifier. In this case, the declspec is either
1812/// malformed or is "implicit int" (in K&R and C89).
1813///
1814/// This method handles diagnosing this prettily and returns false if the
1815/// declspec is done being processed. If it recovers and thinks there may be
1816/// other pieces of declspec after it, it returns true.
1817///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001818bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001819 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001820 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001821 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Chris Lattnere40c2952009-04-14 21:34:55 +00001823 SourceLocation Loc = Tok.getLocation();
1824 // If we see an identifier that is not a type name, we normally would
1825 // parse it as the identifer being declared. However, when a typename
1826 // is typo'd or the definition is not included, this will incorrectly
1827 // parse the typename as the identifier name and fall over misparsing
1828 // later parts of the diagnostic.
1829 //
1830 // As such, we try to do some look-ahead in cases where this would
1831 // otherwise be an "implicit-int" case to see if this is invalid. For
1832 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1833 // an identifier with implicit int, we'd get a parse error because the
1834 // next token is obviously invalid for a type. Parse these as a case
1835 // with an invalid type specifier.
1836 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Chris Lattnere40c2952009-04-14 21:34:55 +00001838 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001839 // error, do lookahead to try to do better recovery. This never applies
1840 // within a type specifier. Outside of C++, we allow this even if the
1841 // language doesn't "officially" support implicit int -- we support
1842 // implicit int as an extension in C99 and C11. Allegedly, MS also
1843 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001844 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001845 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001846 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001847 // If this token is valid for implicit int, e.g. "static x = 4", then
1848 // we just avoid eating the identifier, so it will be parsed as the
1849 // identifier in the declarator.
1850 return false;
1851 }
Mike Stump1eb44332009-09-09 15:08:12 +00001852
Richard Smith827adaf2012-05-15 21:01:51 +00001853 if (getLangOpts().CPlusPlus &&
1854 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1855 // Don't require a type specifier if we have the 'auto' storage class
1856 // specifier in C++98 -- we'll promote it to a type specifier.
1857 return false;
1858 }
1859
Chris Lattnere40c2952009-04-14 21:34:55 +00001860 // Otherwise, if we don't consume this token, we are going to emit an
1861 // error anyway. Try to recover from various common problems. Check
1862 // to see if this was a reference to a tag name without a tag specified.
1863 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001864 //
1865 // C++ doesn't need this, and isTagName doesn't take SS.
1866 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001867 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001868 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Douglas Gregor23c94db2010-07-02 17:43:08 +00001870 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001871 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001872 case DeclSpec::TST_enum:
1873 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1874 case DeclSpec::TST_union:
1875 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1876 case DeclSpec::TST_struct:
1877 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1878 case DeclSpec::TST_class:
1879 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001880 }
Mike Stump1eb44332009-09-09 15:08:12 +00001881
Chris Lattnerf4382f52009-04-14 22:17:06 +00001882 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001883 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1884 LookupResult R(Actions, TokenName, SourceLocation(),
1885 Sema::LookupOrdinaryName);
1886
Chris Lattnerf4382f52009-04-14 22:17:06 +00001887 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001888 << TokenName << TagName << getLangOpts().CPlusPlus
1889 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1890
1891 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1892 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1893 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001894 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001895 << TokenName << TagName;
1896 }
Mike Stump1eb44332009-09-09 15:08:12 +00001897
Chris Lattnerf4382f52009-04-14 22:17:06 +00001898 // Parse this as a tag as if the missing tag were present.
1899 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001900 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001901 else
Richard Smith69730c12012-03-12 07:56:15 +00001902 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1903 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001904 return true;
1905 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001906 }
Mike Stump1eb44332009-09-09 15:08:12 +00001907
Richard Smith8f0a7e72012-05-15 21:29:55 +00001908 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001909 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001910 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1911 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001912 // Look ahead to the next token to try to figure out what this declaration
1913 // was supposed to be.
1914 switch (NextToken().getKind()) {
1915 case tok::comma:
1916 case tok::equal:
1917 case tok::kw_asm:
1918 case tok::l_brace:
1919 case tok::l_square:
1920 case tok::semi:
1921 // This looks like a variable declaration. The type is probably missing.
1922 // We're done parsing decl-specifiers.
1923 return false;
1924
1925 case tok::l_paren: {
1926 // static x(4); // 'x' is not a type
1927 // x(int n); // 'x' is not a type
1928 // x (*p)[]; // 'x' is a type
1929 //
1930 // Since we're in an error case (or the rare 'implicit int in C++' MS
1931 // extension), we can afford to perform a tentative parse to determine
1932 // which case we're in.
1933 TentativeParsingAction PA(*this);
1934 ConsumeToken();
1935 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1936 PA.Revert();
1937 if (TPR == TPResult::False())
1938 return false;
1939 // The identifier is followed by a parenthesized declarator.
1940 // It's supposed to be a type.
1941 break;
1942 }
1943
1944 default:
1945 // This is probably supposed to be a type. This includes cases like:
1946 // int f(itn);
1947 // struct S { unsinged : 4; };
1948 break;
1949 }
1950 }
1951
Chad Rosier8decdee2012-06-26 22:30:43 +00001952 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00001953 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001954 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001955 IdentifierInfo *II = Tok.getIdentifierInfo();
1956 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001957 // The action emitted a diagnostic, so we don't have to.
1958 if (T) {
1959 // The action has suggested that the type T could be used. Set that as
1960 // the type in the declaration specifiers, consume the would-be type
1961 // name token, and we're done.
1962 const char *PrevSpec;
1963 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001964 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001965 DS.SetRangeEnd(Tok.getLocation());
1966 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001967 // There may be other declaration specifiers after this.
1968 return true;
1969 } else if (II != Tok.getIdentifierInfo()) {
1970 // If no type was suggested, the correction is to a keyword
1971 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00001972 // There may be other declaration specifiers after this.
1973 return true;
1974 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001975
Douglas Gregora786fdb2009-10-13 23:27:22 +00001976 // Fall through; the action had no suggestion for us.
1977 } else {
1978 // The action did not emit a diagnostic, so emit one now.
1979 SourceRange R;
1980 if (SS) R = SS->getRange();
1981 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1982 }
Mike Stump1eb44332009-09-09 15:08:12 +00001983
Douglas Gregora786fdb2009-10-13 23:27:22 +00001984 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001985 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001986 DS.SetRangeEnd(Tok.getLocation());
1987 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001988
Chris Lattnere40c2952009-04-14 21:34:55 +00001989 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1990 // avoid rippling error messages on subsequent uses of the same type,
1991 // could be useful if #include was forgotten.
1992 return false;
1993}
1994
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001995/// \brief Determine the declaration specifier context from the declarator
1996/// context.
1997///
1998/// \param Context the declarator context, which is one of the
1999/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002000Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002001Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2002 if (Context == Declarator::MemberContext)
2003 return DSC_class;
2004 if (Context == Declarator::FileContext)
2005 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002006 if (Context == Declarator::TrailingReturnContext)
2007 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002008 return DSC_normal;
2009}
2010
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002011/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2012///
2013/// FIXME: Simply returns an alignof() expression if the argument is a
2014/// type. Ideally, the type should be propagated directly into Sema.
2015///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002016/// [C11] type-id
2017/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002018/// [C++0x] type-id ...[opt]
2019/// [C++0x] assignment-expression ...[opt]
2020ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2021 SourceLocation &EllipsisLoc) {
2022 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002023 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002024 SourceLocation TypeLoc = Tok.getLocation();
2025 ParsedType Ty = ParseTypeName().get();
2026 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002027 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2028 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002029 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002030 ER = ParseConstantExpression();
2031
David Blaikie4e4d0842012-03-11 07:00:24 +00002032 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002033 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002034
2035 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002036}
2037
2038/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2039/// attribute to Attrs.
2040///
2041/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002042/// [C11] '_Alignas' '(' type-id ')'
2043/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002044/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
2045/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002046void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2047 SourceLocation *endLoc) {
2048 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2049 "Not an alignment-specifier!");
2050
2051 SourceLocation KWLoc = Tok.getLocation();
2052 ConsumeToken();
2053
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002054 BalancedDelimiterTracker T(*this, tok::l_paren);
2055 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002056 return;
2057
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002058 SourceLocation EllipsisLoc;
2059 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002060 if (ArgExpr.isInvalid()) {
2061 SkipUntil(tok::r_paren);
2062 return;
2063 }
2064
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002065 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002066 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002067 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002068
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002069 // FIXME: Handle pack-expansions here.
2070 if (EllipsisLoc.isValid()) {
2071 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2072 return;
2073 }
2074
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002075 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002076 ArgExprs.push_back(ArgExpr.release());
Sean Hunt8e083e72012-06-19 23:57:03 +00002077 // FIXME: This should not be GNU, but we since the attribute used is
2078 // based on the spelling, and there is no true spelling for
2079 // C++11 attributes, this isn't accepted.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002080 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002081 0, T.getOpenLocation(), ArgExprs.data(), 1,
Sean Hunt8e083e72012-06-19 23:57:03 +00002082 AttributeList::AS_GNU);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002083}
2084
Reid Spencer5f016e22007-07-11 17:01:13 +00002085/// ParseDeclarationSpecifiers
2086/// declaration-specifiers: [C99 6.7]
2087/// storage-class-specifier declaration-specifiers[opt]
2088/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002089/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002090/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002091/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002092/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002093///
2094/// storage-class-specifier: [C99 6.7.1]
2095/// 'typedef'
2096/// 'extern'
2097/// 'static'
2098/// 'auto'
2099/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002100/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002101/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002102/// function-specifier: [C99 6.7.4]
2103/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002104/// [C++] 'virtual'
2105/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002106/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002107/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002108/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002109
Reid Spencer5f016e22007-07-11 17:01:13 +00002110///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002111void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002112 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002113 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002114 DeclSpecContext DSContext,
2115 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002116 if (DS.getSourceRange().isInvalid()) {
2117 DS.SetRangeStart(Tok.getLocation());
2118 DS.SetRangeEnd(Tok.getLocation());
2119 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002120
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002121 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002122 bool AttrsLastTime = false;
2123 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002124 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002125 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002126 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002127 unsigned DiagID = 0;
2128
Reid Spencer5f016e22007-07-11 17:01:13 +00002129 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002130
Reid Spencer5f016e22007-07-11 17:01:13 +00002131 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002132 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002133 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002134 if (!AttrsLastTime)
2135 ProhibitAttributes(attrs);
2136 else
2137 DS.takeAttributesFrom(attrs);
Peter Collingbournef1907682011-09-29 18:03:57 +00002138
Reid Spencer5f016e22007-07-11 17:01:13 +00002139 // If this is not a declaration specifier token, we're done reading decl
2140 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002141 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002142 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002143
Sean Hunt2edf0a22012-06-23 05:07:58 +00002144 case tok::l_square:
2145 case tok::kw_alignas:
2146 if (!isCXX11AttributeSpecifier())
2147 goto DoneWithDeclSpec;
2148
2149 ProhibitAttributes(attrs);
2150 // FIXME: It would be good to recover by accepting the attributes,
2151 // but attempting to do that now would cause serious
2152 // madness in terms of diagnostics.
2153 attrs.clear();
2154 attrs.Range = SourceRange();
2155
2156 ParseCXX11Attributes(attrs);
2157 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002158 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002159
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002160 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002161 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002162 if (DS.hasTypeSpecifier()) {
2163 bool AllowNonIdentifiers
2164 = (getCurScope()->getFlags() & (Scope::ControlScope |
2165 Scope::BlockScope |
2166 Scope::TemplateParamScope |
2167 Scope::FunctionPrototypeScope |
2168 Scope::AtCatchScope)) == 0;
2169 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002170 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002171 (DSContext == DSC_class && DS.isFriendSpecified());
2172
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002173 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002174 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002175 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002176 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002177 }
2178
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002179 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2180 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2181 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002182 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002183 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002184 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002185 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002186 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002187 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002188
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002189 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002190 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002191 }
2192
Chris Lattner5e02c472009-01-05 00:07:25 +00002193 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002194 // C++ scope specifier. Annotate and loop, or bail out on error.
2195 if (TryAnnotateCXXScopeToken(true)) {
2196 if (!DS.hasTypeSpecifier())
2197 DS.SetTypeSpecError();
2198 goto DoneWithDeclSpec;
2199 }
John McCall2e0a7152010-03-01 18:20:46 +00002200 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2201 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002202 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002203
2204 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002205 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002206 goto DoneWithDeclSpec;
2207
John McCallaa87d332009-12-12 11:40:51 +00002208 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002209 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2210 Tok.getAnnotationRange(),
2211 SS);
John McCallaa87d332009-12-12 11:40:51 +00002212
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002213 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002214 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002215 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002216 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002217 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002218 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002219
2220 // C++ [class.qual]p2:
2221 // In a lookup in which the constructor is an acceptable lookup
2222 // result and the nested-name-specifier nominates a class C:
2223 //
2224 // - if the name specified after the
2225 // nested-name-specifier, when looked up in C, is the
2226 // injected-class-name of C (Clause 9), or
2227 //
2228 // - if the name specified after the nested-name-specifier
2229 // is the same as the identifier or the
2230 // simple-template-id's template-name in the last
2231 // component of the nested-name-specifier,
2232 //
2233 // the name is instead considered to name the constructor of
2234 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002235 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002236 // Thus, if the template-name is actually the constructor
2237 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002238 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002239 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002240 if ((DSContext == DSC_top_level ||
2241 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2242 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002243 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002244 if (isConstructorDeclarator()) {
2245 // The user meant this to be an out-of-line constructor
2246 // definition, but template arguments are not allowed
2247 // there. Just allow this as a constructor; we'll
2248 // complain about it later.
2249 goto DoneWithDeclSpec;
2250 }
2251
2252 // The user meant this to name a type, but it actually names
2253 // a constructor with some extraneous template
2254 // arguments. Complain, then parse it as a type as the user
2255 // intended.
2256 Diag(TemplateId->TemplateNameLoc,
2257 diag::err_out_of_line_template_id_names_constructor)
2258 << TemplateId->Name;
2259 }
2260
John McCallaa87d332009-12-12 11:40:51 +00002261 DS.getTypeSpecScope() = SS;
2262 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002263 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002264 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002265 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002266 continue;
2267 }
2268
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002269 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002270 DS.getTypeSpecScope() = SS;
2271 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002272 if (Tok.getAnnotationValue()) {
2273 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002274 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002275 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002276 PrevSpec, DiagID, T);
2277 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002278 else
2279 DS.SetTypeSpecError();
2280 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2281 ConsumeToken(); // The typename
2282 }
2283
Douglas Gregor9135c722009-03-25 15:40:00 +00002284 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002285 goto DoneWithDeclSpec;
2286
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002287 // If we're in a context where the identifier could be a class name,
2288 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002289 if ((DSContext == DSC_top_level ||
2290 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002291 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002292 &SS)) {
2293 if (isConstructorDeclarator())
2294 goto DoneWithDeclSpec;
2295
2296 // As noted in C++ [class.qual]p2 (cited above), when the name
2297 // of the class is qualified in a context where it could name
2298 // a constructor, its a constructor name. However, we've
2299 // looked at the declarator, and the user probably meant this
2300 // to be a type. Complain that it isn't supposed to be treated
2301 // as a type, then proceed to parse it as a type.
2302 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2303 << Next.getIdentifierInfo();
2304 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002305
John McCallb3d87482010-08-24 05:47:05 +00002306 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2307 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002308 getCurScope(), &SS,
2309 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002310 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002311 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002312
Chris Lattnerf4382f52009-04-14 22:17:06 +00002313 // If the referenced identifier is not a type, then this declspec is
2314 // erroneous: We already checked about that it has no type specifier, and
2315 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002316 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002317 if (TypeRep == 0) {
2318 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002319 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002320 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002321 }
Mike Stump1eb44332009-09-09 15:08:12 +00002322
John McCallaa87d332009-12-12 11:40:51 +00002323 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002324 ConsumeToken(); // The C++ scope.
2325
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002326 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002327 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002328 if (isInvalid)
2329 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002330
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002331 DS.SetRangeEnd(Tok.getLocation());
2332 ConsumeToken(); // The typename.
2333
2334 continue;
2335 }
Mike Stump1eb44332009-09-09 15:08:12 +00002336
Chris Lattner80d0c892009-01-21 19:48:37 +00002337 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002338 if (Tok.getAnnotationValue()) {
2339 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002340 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002341 DiagID, T);
2342 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002343 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002344
Chris Lattner5c5db552010-04-05 18:18:31 +00002345 if (isInvalid)
2346 break;
2347
Chris Lattner80d0c892009-01-21 19:48:37 +00002348 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2349 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002350
Chris Lattner80d0c892009-01-21 19:48:37 +00002351 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2352 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002353 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002354 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002355 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002356
Chris Lattner80d0c892009-01-21 19:48:37 +00002357 continue;
2358 }
Mike Stump1eb44332009-09-09 15:08:12 +00002359
Douglas Gregorbfad9152011-04-28 15:48:45 +00002360 case tok::kw___is_signed:
2361 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2362 // typically treats it as a trait. If we see __is_signed as it appears
2363 // in libstdc++, e.g.,
2364 //
2365 // static const bool __is_signed;
2366 //
2367 // then treat __is_signed as an identifier rather than as a keyword.
2368 if (DS.getTypeSpecType() == TST_bool &&
2369 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2370 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2371 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2372 Tok.setKind(tok::identifier);
2373 }
2374
2375 // We're done with the declaration-specifiers.
2376 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002377
Chris Lattner3bd934a2008-07-26 01:18:38 +00002378 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002379 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002380 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002381 // In C++, check to see if this is a scope specifier like foo::bar::, if
2382 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002383 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002384 if (TryAnnotateCXXScopeToken(true)) {
2385 if (!DS.hasTypeSpecifier())
2386 DS.SetTypeSpecError();
2387 goto DoneWithDeclSpec;
2388 }
2389 if (!Tok.is(tok::identifier))
2390 continue;
2391 }
Mike Stump1eb44332009-09-09 15:08:12 +00002392
Chris Lattner3bd934a2008-07-26 01:18:38 +00002393 // This identifier can only be a typedef name if we haven't already seen
2394 // a type-specifier. Without this check we misparse:
2395 // typedef int X; struct Y { short X; }; as 'short int'.
2396 if (DS.hasTypeSpecifier())
2397 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002398
John Thompson82287d12010-02-05 00:12:22 +00002399 // Check for need to substitute AltiVec keyword tokens.
2400 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2401 break;
2402
Richard Smithf63eee72012-05-09 18:56:43 +00002403 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2404 // allow the use of a typedef name as a type specifier.
2405 if (DS.isTypeAltiVecVector())
2406 goto DoneWithDeclSpec;
2407
John McCallb3d87482010-08-24 05:47:05 +00002408 ParsedType TypeRep =
2409 Actions.getTypeName(*Tok.getIdentifierInfo(),
2410 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002411
Chris Lattnerc199ab32009-04-12 20:42:31 +00002412 // If this is not a typedef name, don't parse it as part of the declspec,
2413 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002414 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002415 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002416 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002417 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002418
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002419 // If we're in a context where the identifier could be a class name,
2420 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002421 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002422 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002423 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002424 goto DoneWithDeclSpec;
2425
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002426 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002427 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002428 if (isInvalid)
2429 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002430
Chris Lattner3bd934a2008-07-26 01:18:38 +00002431 DS.SetRangeEnd(Tok.getLocation());
2432 ConsumeToken(); // The identifier
2433
2434 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2435 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002436 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002437 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002438 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002439
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002440 // Need to support trailing type qualifiers (e.g. "id<p> const").
2441 // If a type specifier follows, it will be diagnosed elsewhere.
2442 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002443 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002444
2445 // type-name
2446 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002447 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002448 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002449 // This template-id does not refer to a type name, so we're
2450 // done with the type-specifiers.
2451 goto DoneWithDeclSpec;
2452 }
2453
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002454 // If we're in a context where the template-id could be a
2455 // constructor name or specialization, check whether this is a
2456 // 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(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002459 isConstructorDeclarator())
2460 goto DoneWithDeclSpec;
2461
Douglas Gregor39a8de12009-02-25 19:37:18 +00002462 // Turn the template-id annotation token into a type annotation
2463 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002464 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002465 continue;
2466 }
2467
Reid Spencer5f016e22007-07-11 17:01:13 +00002468 // GNU attributes support.
2469 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002470 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002471 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002472
2473 // Microsoft declspec support.
2474 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002475 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002476 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002477
Steve Naroff239f0732008-12-25 14:16:32 +00002478 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002479 case tok::kw___forceinline: {
2480 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2481 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
2482 SourceLocation AttrNameLoc = ConsumeToken();
Sean Hunt93f95f22012-06-18 16:13:52 +00002483 // FIXME: This does not work correctly if it is set to be a declspec
2484 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002485 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002486 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002487 continue;
2488 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002489
2490 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002491 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002492 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002493 case tok::kw___cdecl:
2494 case tok::kw___stdcall:
2495 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002496 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002497 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002498 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002499 continue;
2500
Dawn Perchik52fc3142010-09-03 01:29:35 +00002501 // Borland single token adornments.
2502 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002503 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002504 continue;
2505
Peter Collingbournef315fa82011-02-14 01:42:53 +00002506 // OpenCL single token adornments.
2507 case tok::kw___kernel:
2508 ParseOpenCLAttributes(DS.getAttributes());
2509 continue;
2510
Reid Spencer5f016e22007-07-11 17:01:13 +00002511 // storage-class-specifier
2512 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002513 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2514 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002515 break;
2516 case tok::kw_extern:
2517 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002518 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002519 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2520 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002521 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002522 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002523 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2524 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002525 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002526 case tok::kw_static:
2527 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002528 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002529 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2530 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002531 break;
2532 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002533 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002534 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002535 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2536 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002537 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002538 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002539 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002540 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002541 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2542 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002543 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002544 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2545 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002546 break;
2547 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002548 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2549 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002550 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002551 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002552 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2553 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002554 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002555 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002556 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002557 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002558
Reid Spencer5f016e22007-07-11 17:01:13 +00002559 // function-specifier
2560 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002561 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002562 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002563 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002564 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002565 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002566 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002567 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002568 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002569
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002570 // alignment-specifier
2571 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002572 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002573 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002574 ParseAlignmentSpecifier(DS.getAttributes());
2575 continue;
2576
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002577 // friend
2578 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002579 if (DSContext == DSC_class)
2580 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2581 else {
2582 PrevSpec = ""; // not actually used by the diagnostic
2583 DiagID = diag::err_friend_invalid_in_context;
2584 isInvalid = true;
2585 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002586 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002587
Douglas Gregor8d267c52011-09-09 02:06:17 +00002588 // Modules
2589 case tok::kw___module_private__:
2590 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2591 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002592
Sebastian Redl2ac67232009-11-05 15:47:02 +00002593 // constexpr
2594 case tok::kw_constexpr:
2595 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2596 break;
2597
Chris Lattner80d0c892009-01-21 19:48:37 +00002598 // type-specifier
2599 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002600 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2601 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002602 break;
2603 case tok::kw_long:
2604 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002605 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2606 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002607 else
John McCallfec54012009-08-03 20:12:06 +00002608 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2609 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002610 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002611 case tok::kw___int64:
2612 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2613 DiagID);
2614 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002615 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002616 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2617 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002618 break;
2619 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002620 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2621 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002622 break;
2623 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002624 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2625 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002626 break;
2627 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002628 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2629 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002630 break;
2631 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002632 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2633 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002634 break;
2635 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002636 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2637 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002638 break;
2639 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002640 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2641 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002642 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002643 case tok::kw___int128:
2644 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2645 DiagID);
2646 break;
2647 case tok::kw_half:
2648 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2649 DiagID);
2650 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002651 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002652 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2653 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002654 break;
2655 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002656 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2657 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002658 break;
2659 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002660 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2661 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002662 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002663 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002664 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2665 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002666 break;
2667 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002668 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2669 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002670 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002671 case tok::kw_bool:
2672 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002673 if (Tok.is(tok::kw_bool) &&
2674 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2675 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2676 PrevSpec = ""; // Not used by the diagnostic.
2677 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002678 // For better error recovery.
2679 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002680 isInvalid = true;
2681 } else {
2682 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2683 DiagID);
2684 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002685 break;
2686 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002687 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2688 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002689 break;
2690 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002691 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2692 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002693 break;
2694 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002695 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2696 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002697 break;
John Thompson82287d12010-02-05 00:12:22 +00002698 case tok::kw___vector:
2699 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2700 break;
2701 case tok::kw___pixel:
2702 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2703 break;
John McCalla5fc4722011-04-09 22:50:59 +00002704 case tok::kw___unknown_anytype:
2705 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2706 PrevSpec, DiagID);
2707 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002708
2709 // class-specifier:
2710 case tok::kw_class:
2711 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002712 case tok::kw_union: {
2713 tok::TokenKind Kind = Tok.getKind();
2714 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002715 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2716 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002717 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002718 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002719
2720 // enum-specifier:
2721 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002722 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002723 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002724 continue;
2725
2726 // cv-qualifier:
2727 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002728 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith42926a02012-07-24 20:24:58 +00002729 getLangOpts(), /*IsTypeSpec*/true);
Chris Lattner80d0c892009-01-21 19:48:37 +00002730 break;
2731 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002732 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith42926a02012-07-24 20:24:58 +00002733 getLangOpts(), /*IsTypeSpec*/true);
Chris Lattner80d0c892009-01-21 19:48:37 +00002734 break;
2735 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002736 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith42926a02012-07-24 20:24:58 +00002737 getLangOpts(), /*IsTypeSpec*/true);
Chris Lattner80d0c892009-01-21 19:48:37 +00002738 break;
2739
Douglas Gregord57959a2009-03-27 23:10:48 +00002740 // C++ typename-specifier:
2741 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002742 if (TryAnnotateTypeOrScopeToken()) {
2743 DS.SetTypeSpecError();
2744 goto DoneWithDeclSpec;
2745 }
2746 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002747 continue;
2748 break;
2749
Chris Lattner80d0c892009-01-21 19:48:37 +00002750 // GNU typeof support.
2751 case tok::kw_typeof:
2752 ParseTypeofSpecifier(DS);
2753 continue;
2754
David Blaikie42d6d0c2011-12-04 05:04:18 +00002755 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002756 ParseDecltypeSpecifier(DS);
2757 continue;
2758
Sean Huntdb5d44b2011-05-19 05:37:45 +00002759 case tok::kw___underlying_type:
2760 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002761 continue;
2762
2763 case tok::kw__Atomic:
2764 ParseAtomicSpecifier(DS);
2765 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002766
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002767 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00002768 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002769 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002770 goto DoneWithDeclSpec;
2771 case tok::kw___private:
2772 case tok::kw___global:
2773 case tok::kw___local:
2774 case tok::kw___constant:
2775 case tok::kw___read_only:
2776 case tok::kw___write_only:
2777 case tok::kw___read_write:
2778 ParseOpenCLQualifiers(DS);
2779 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002780
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002781 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002782 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002783 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2784 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002785 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002786 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002787
Douglas Gregor46f936e2010-11-19 17:10:50 +00002788 if (!ParseObjCProtocolQualifiers(DS))
2789 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2790 << FixItHint::CreateInsertion(Loc, "id")
2791 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00002792
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002793 // Need to support trailing type qualifiers (e.g. "id<p> const").
2794 // If a type specifier follows, it will be diagnosed elsewhere.
2795 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002796 }
John McCallfec54012009-08-03 20:12:06 +00002797 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002798 if (isInvalid) {
2799 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002800 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00002801
Douglas Gregorae2fb142010-08-23 14:34:43 +00002802 if (DiagID == diag::ext_duplicate_declspec)
2803 Diag(Tok, DiagID)
2804 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2805 else
2806 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002807 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002808
Chris Lattner81c018d2008-03-13 06:29:04 +00002809 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002810 if (DiagID != diag::err_bool_redeclaration)
2811 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002812
2813 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002814 }
2815}
Douglas Gregoradcac882008-12-01 23:54:00 +00002816
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002817/// ParseStructDeclaration - Parse a struct declaration without the terminating
2818/// semicolon.
2819///
Reid Spencer5f016e22007-07-11 17:01:13 +00002820/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002821/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002822/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002823/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002824/// struct-declarator-list:
2825/// struct-declarator
2826/// struct-declarator-list ',' struct-declarator
2827/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2828/// struct-declarator:
2829/// declarator
2830/// [GNU] declarator attributes[opt]
2831/// declarator[opt] ':' constant-expression
2832/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2833///
Chris Lattnere1359422008-04-10 06:46:29 +00002834void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002835ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00002836
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002837 if (Tok.is(tok::kw___extension__)) {
2838 // __extension__ silences extension warnings in the subexpression.
2839 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002840 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002841 return ParseStructDeclaration(DS, Fields);
2842 }
Mike Stump1eb44332009-09-09 15:08:12 +00002843
Steve Naroff28a7ca82007-08-20 22:28:22 +00002844 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002845 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002846
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002847 // If there are no declarators, this is a free-standing declaration
2848 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002849 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002850 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2851 DS);
2852 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002853 return;
2854 }
2855
2856 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002857 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002858 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002859 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002860 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00002861 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002862
2863 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002864 if (!FirstDeclarator)
2865 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002866
Steve Naroff28a7ca82007-08-20 22:28:22 +00002867 /// struct-declarator: declarator
2868 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002869 if (Tok.isNot(tok::colon)) {
2870 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2871 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002872 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002873 }
Mike Stump1eb44332009-09-09 15:08:12 +00002874
Chris Lattner04d66662007-10-09 17:33:22 +00002875 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002876 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002877 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002878 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002879 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002880 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002881 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002882 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002883
Steve Naroff28a7ca82007-08-20 22:28:22 +00002884 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002885 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002886
John McCallbdd563e2009-11-03 02:38:08 +00002887 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00002888 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00002889
Steve Naroff28a7ca82007-08-20 22:28:22 +00002890 // If we don't have a comma, it is either the end of the list (a ';')
2891 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002892 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002893 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002894
Steve Naroff28a7ca82007-08-20 22:28:22 +00002895 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002896 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002897
John McCallbdd563e2009-11-03 02:38:08 +00002898 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002899 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002900}
2901
2902/// ParseStructUnionBody
2903/// struct-contents:
2904/// struct-declaration-list
2905/// [EXT] empty
2906/// [GNU] "struct-declaration-list" without terminatoring ';'
2907/// struct-declaration-list:
2908/// struct-declaration
2909/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002910/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002911///
Reid Spencer5f016e22007-07-11 17:01:13 +00002912void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002913 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002914 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2915 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002916
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002917 BalancedDelimiterTracker T(*this, tok::l_brace);
2918 if (T.consumeOpen())
2919 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002920
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002921 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002922 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002923
Reid Spencer5f016e22007-07-11 17:01:13 +00002924 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2925 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002926 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002927 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2928 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2929 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002930
Chris Lattner5f9e2722011-07-23 10:55:15 +00002931 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002932
Reid Spencer5f016e22007-07-11 17:01:13 +00002933 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002934 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002935 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002936
Reid Spencer5f016e22007-07-11 17:01:13 +00002937 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002938 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002939 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00002940 continue;
2941 }
Chris Lattnere1359422008-04-10 06:46:29 +00002942
John McCallbdd563e2009-11-03 02:38:08 +00002943 if (!Tok.is(tok::at)) {
2944 struct CFieldCallback : FieldCallback {
2945 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002946 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002947 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002948
John McCalld226f652010-08-21 09:40:31 +00002949 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002950 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002951 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2952
Eli Friedmandcdff462012-08-08 23:53:27 +00002953 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002954 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002955 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002956 FD.D.getDeclSpec().getSourceRange().getBegin(),
2957 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002958 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002959 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00002960 }
John McCallbdd563e2009-11-03 02:38:08 +00002961 } Callback(*this, TagDecl, FieldDecls);
2962
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002963 // Parse all the comma separated declarators.
2964 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002965 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002966 } else { // Handle @defs
2967 ConsumeToken();
2968 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2969 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002970 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002971 continue;
2972 }
2973 ConsumeToken();
2974 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2975 if (!Tok.is(tok::identifier)) {
2976 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002977 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002978 continue;
2979 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002980 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002981 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002982 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002983 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2984 ConsumeToken();
2985 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002986 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002987
Chris Lattner04d66662007-10-09 17:33:22 +00002988 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002989 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002990 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002991 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002992 break;
2993 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002994 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2995 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002996 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002997 // If we stopped at a ';', eat it.
2998 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002999 }
3000 }
Mike Stump1eb44332009-09-09 15:08:12 +00003001
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003002 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003003
John McCall0b7e6782011-03-24 11:26:52 +00003004 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003005 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003006 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003007
Douglas Gregor23c94db2010-07-02 17:43:08 +00003008 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003009 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003010 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003011 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003012 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003013 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3014 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003015}
3016
Reid Spencer5f016e22007-07-11 17:01:13 +00003017/// ParseEnumSpecifier
3018/// enum-specifier: [C99 6.7.2.2]
3019/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003020///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003021/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3022/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003023/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3024/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003025/// 'enum' identifier
3026/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003027///
Richard Smith1af83c42012-03-23 03:33:32 +00003028/// [C++11] enum-head '{' enumerator-list[opt] '}'
3029/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003030///
Richard Smith1af83c42012-03-23 03:33:32 +00003031/// enum-head: [C++11]
3032/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3033/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3034/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003035///
Richard Smith1af83c42012-03-23 03:33:32 +00003036/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003037/// 'enum'
3038/// 'enum' 'class'
3039/// 'enum' 'struct'
3040///
Richard Smith1af83c42012-03-23 03:33:32 +00003041/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003042/// ':' type-specifier-seq
3043///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003044/// [C++] elaborated-type-specifier:
3045/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3046///
Chris Lattner4c97d762009-04-12 21:49:30 +00003047void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003048 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003049 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003050 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003051 if (Tok.is(tok::code_completion)) {
3052 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003053 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003054 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003055 }
John McCall57c13002011-07-06 05:58:41 +00003056
Sean Hunt2edf0a22012-06-23 05:07:58 +00003057 // If attributes exist after tag, parse them.
3058 ParsedAttributesWithRange attrs(AttrFactory);
3059 MaybeParseGNUAttributes(attrs);
3060 MaybeParseCXX0XAttributes(attrs);
3061
3062 // If declspecs exist after tag, parse them.
3063 while (Tok.is(tok::kw___declspec))
3064 ParseMicrosoftDeclSpec(attrs);
3065
Richard Smithbdad7a22012-01-10 01:33:14 +00003066 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003067 bool IsScopedUsingClassTag = false;
3068
John McCall1e12b3d2012-06-23 22:30:04 +00003069 // In C++11, recognize 'enum class' and 'enum struct'.
David Blaikie4e4d0842012-03-11 07:00:24 +00003070 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00003071 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00003072 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003073 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003074 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003075
John McCall1e12b3d2012-06-23 22:30:04 +00003076 // Attributes are not allowed between these keywords. Diagnose,
3077 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003078 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003079
3080 // They are allowed afterwards, though.
3081 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003082 MaybeParseCXX0XAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003083 while (Tok.is(tok::kw___declspec))
3084 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003085 }
Richard Smith1af83c42012-03-23 03:33:32 +00003086
John McCall13489672012-05-07 06:16:58 +00003087 // C++11 [temp.explicit]p12:
3088 // The usual access controls do not apply to names used to specify
3089 // explicit instantiations.
3090 // We extend this to also cover explicit specializations. Note that
3091 // we don't suppress if this turns out to be an elaborated type
3092 // specifier.
3093 bool shouldDelayDiagsInTag =
3094 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3095 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3096 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003097
Richard Smith7796eb52012-03-12 08:56:40 +00003098 // Enum definitions should not be parsed in a trailing-return-type.
3099 bool AllowDeclaration = DSC != DSC_trailing;
3100
3101 bool AllowFixedUnderlyingType = AllowDeclaration &&
3102 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
3103 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003104
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003105 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003106 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003107 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3108 // if a fixed underlying type is allowed.
3109 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003110
3111 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003112 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00003113 return;
3114
3115 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003116 Diag(Tok, diag::err_expected_ident);
3117 if (Tok.isNot(tok::l_brace)) {
3118 // Has no name and is not a definition.
3119 // Skip the rest of this declarator, up until the comma or semicolon.
3120 SkipUntil(tok::comma, true);
3121 return;
3122 }
3123 }
3124 }
Mike Stump1eb44332009-09-09 15:08:12 +00003125
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003126 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003127 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003128 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003129 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003130
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003131 // Skip the rest of this declarator, up until the comma or semicolon.
3132 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003133 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003134 }
Mike Stump1eb44332009-09-09 15:08:12 +00003135
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003136 // If an identifier is present, consume and remember it.
3137 IdentifierInfo *Name = 0;
3138 SourceLocation NameLoc;
3139 if (Tok.is(tok::identifier)) {
3140 Name = Tok.getIdentifierInfo();
3141 NameLoc = ConsumeToken();
3142 }
Mike Stump1eb44332009-09-09 15:08:12 +00003143
Richard Smithbdad7a22012-01-10 01:33:14 +00003144 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003145 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3146 // declaration of a scoped enumeration.
3147 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003148 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003149 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003150 }
3151
John McCall13489672012-05-07 06:16:58 +00003152 // Okay, end the suppression area. We'll decide whether to emit the
3153 // diagnostics in a second.
3154 if (shouldDelayDiagsInTag)
3155 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003156
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003157 TypeResult BaseType;
3158
Douglas Gregora61b3e72010-12-01 17:42:47 +00003159 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003160 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003161 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003162 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003163 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003164 // If we're in class scope, this can either be an enum declaration with
3165 // an underlying type, or a declaration of a bitfield member. We try to
3166 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003167 // (integer literal, sizeof); if it's still ambiguous, we then consider
3168 // anything that's a simple-type-specifier followed by '(' as an
3169 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003170 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003171 EnterExpressionEvaluationContext Unevaluated(Actions,
3172 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003173 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003174 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003175 // bit-field. This is the common case.
3176 if (TPR == TPResult::True())
3177 PossibleBitfield = true;
3178 // If the next token starts a type-specifier-seq, it may be either a
3179 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003180 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003181 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003182 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003183 GetLookAheadToken(2).getKind() == tok::semi) {
3184 // Consume the ':'.
3185 ConsumeToken();
3186 } else {
3187 // We have the start of a type-specifier-seq, so we have to perform
3188 // tentative parsing to determine whether we have an expression or a
3189 // type.
3190 TentativeParsingAction TPA(*this);
3191
3192 // Consume the ':'.
3193 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003194
3195 // If we see a type specifier followed by an open-brace, we have an
3196 // ambiguity between an underlying type and a C++11 braced
3197 // function-style cast. Resolve this by always treating it as an
3198 // underlying type.
3199 // FIXME: The standard is not entirely clear on how to disambiguate in
3200 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003201 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003202 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003203 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003204 // We'll parse this as a bitfield later.
3205 PossibleBitfield = true;
3206 TPA.Revert();
3207 } else {
3208 // We have a type-specifier-seq.
3209 TPA.Commit();
3210 }
3211 }
3212 } else {
3213 // Consume the ':'.
3214 ConsumeToken();
3215 }
3216
3217 if (!PossibleBitfield) {
3218 SourceRange Range;
3219 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003220
David Blaikie4e4d0842012-03-11 07:00:24 +00003221 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00003222 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
3223 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00003224 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003225 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003226 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003227 }
3228
Richard Smithbdad7a22012-01-10 01:33:14 +00003229 // There are four options here. If we have 'friend enum foo;' then this is a
3230 // friend declaration, and cannot have an accompanying definition. If we have
3231 // 'enum foo;', then this is a forward declaration. If we have
3232 // 'enum foo {...' then this is a definition. Otherwise we have something
3233 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003234 //
3235 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3236 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3237 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3238 //
John McCallf312b1e2010-08-26 23:41:50 +00003239 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003240 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003241 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003242 } else if (Tok.is(tok::l_brace)) {
3243 if (DS.isFriendSpecified()) {
3244 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3245 << SourceRange(DS.getFriendSpecLoc());
3246 ConsumeBrace();
3247 SkipUntil(tok::r_brace);
3248 TUK = Sema::TUK_Friend;
3249 } else {
3250 TUK = Sema::TUK_Definition;
3251 }
Richard Smithc9f35172012-06-25 21:37:02 +00003252 } else if (DSC != DSC_type_specifier &&
3253 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003254 (Tok.isAtStartOfLine() &&
3255 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003256 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3257 if (Tok.isNot(tok::semi)) {
3258 // A semicolon was missing after this declaration. Diagnose and recover.
3259 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3260 "enum");
3261 PP.EnterToken(Tok);
3262 Tok.setKind(tok::semi);
3263 }
John McCall13489672012-05-07 06:16:58 +00003264 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003265 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003266 }
3267
3268 // If this is an elaborated type specifier, and we delayed
3269 // diagnostics before, just merge them into the current pool.
3270 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3271 diagsFromTag.redelay();
3272 }
Richard Smith1af83c42012-03-23 03:33:32 +00003273
3274 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003275 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003276 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003277 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3278 // Skip the rest of this declarator, up until the comma or semicolon.
3279 Diag(Tok, diag::err_enum_template);
3280 SkipUntil(tok::comma, true);
3281 return;
3282 }
3283
3284 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3285 // Enumerations can't be explicitly instantiated.
3286 DS.SetTypeSpecError();
3287 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3288 return;
3289 }
3290
3291 assert(TemplateInfo.TemplateParams && "no template parameters");
3292 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3293 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003294 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003295
Sean Hunt2edf0a22012-06-23 05:07:58 +00003296 if (TUK == Sema::TUK_Reference)
3297 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003298
Douglas Gregorb9075602011-02-22 02:55:24 +00003299 if (!Name && TUK != Sema::TUK_Definition) {
3300 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003301
Douglas Gregorb9075602011-02-22 02:55:24 +00003302 // Skip the rest of this declarator, up until the comma or semicolon.
3303 SkipUntil(tok::comma, true);
3304 return;
3305 }
Richard Smith1af83c42012-03-23 03:33:32 +00003306
Douglas Gregor402abb52009-05-28 23:31:59 +00003307 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003308 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003309 const char *PrevSpec = 0;
3310 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003311 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003312 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003313 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003314 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003315 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003316
Douglas Gregor48c89f42010-04-24 16:38:41 +00003317 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003318 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003319 // dependent tag.
3320 if (!Name) {
3321 DS.SetTypeSpecError();
3322 Diag(Tok, diag::err_expected_type_name_after_typename);
3323 return;
3324 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003325
Douglas Gregor23c94db2010-07-02 17:43:08 +00003326 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003327 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003328 NameLoc);
3329 if (Type.isInvalid()) {
3330 DS.SetTypeSpecError();
3331 return;
3332 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003333
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003334 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3335 NameLoc.isValid() ? NameLoc : StartLoc,
3336 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003337 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003338
Douglas Gregor48c89f42010-04-24 16:38:41 +00003339 return;
3340 }
Mike Stump1eb44332009-09-09 15:08:12 +00003341
John McCalld226f652010-08-21 09:40:31 +00003342 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003343 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003344 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003345 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003346 ConsumeBrace();
3347 SkipUntil(tok::r_brace);
3348 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003349
Douglas Gregor48c89f42010-04-24 16:38:41 +00003350 DS.SetTypeSpecError();
3351 return;
3352 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003353
Richard Smithc9f35172012-06-25 21:37:02 +00003354 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003355 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003356
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003357 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3358 NameLoc.isValid() ? NameLoc : StartLoc,
3359 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003360 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003361}
3362
3363/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3364/// enumerator-list:
3365/// enumerator
3366/// enumerator-list ',' enumerator
3367/// enumerator:
3368/// enumeration-constant
3369/// enumeration-constant '=' constant-expression
3370/// enumeration-constant:
3371/// identifier
3372///
John McCalld226f652010-08-21 09:40:31 +00003373void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003374 // Enter the scope of the enum body and start the definition.
3375 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003376 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003377
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003378 BalancedDelimiterTracker T(*this, tok::l_brace);
3379 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003380
Chris Lattner7946dd32007-08-27 17:24:30 +00003381 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003382 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003383 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003384
Chris Lattner5f9e2722011-07-23 10:55:15 +00003385 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003386
John McCalld226f652010-08-21 09:40:31 +00003387 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003388
Reid Spencer5f016e22007-07-11 17:01:13 +00003389 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003390 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003391 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3392 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003393
John McCall5b629aa2010-10-22 23:36:17 +00003394 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003395 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003396 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003397 MaybeParseCXX0XAttributes(attrs);
3398 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003399
Reid Spencer5f016e22007-07-11 17:01:13 +00003400 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003401 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003402 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003403
Chris Lattner04d66662007-10-09 17:33:22 +00003404 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003405 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003406 AssignedVal = ParseConstantExpression();
3407 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003408 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003409 }
Mike Stump1eb44332009-09-09 15:08:12 +00003410
Reid Spencer5f016e22007-07-11 17:01:13 +00003411 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003412 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3413 LastEnumConstDecl,
3414 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003415 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003416 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003417 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003418
Reid Spencer5f016e22007-07-11 17:01:13 +00003419 EnumConstantDecls.push_back(EnumConstDecl);
3420 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003421
Douglas Gregor751f6922010-09-07 14:51:08 +00003422 if (Tok.is(tok::identifier)) {
3423 // We're missing a comma between enumerators.
3424 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003425 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003426 << FixItHint::CreateInsertion(Loc, ", ");
3427 continue;
3428 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003429
Chris Lattner04d66662007-10-09 17:33:22 +00003430 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003431 break;
3432 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003433
Richard Smith7fe62082011-10-15 05:09:34 +00003434 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003435 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003436 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3437 diag::ext_enumerator_list_comma_cxx :
3438 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003439 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003440 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003441 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3442 << FixItHint::CreateRemoval(CommaLoc);
3443 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003444 }
Mike Stump1eb44332009-09-09 15:08:12 +00003445
Reid Spencer5f016e22007-07-11 17:01:13 +00003446 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003447 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003448
Reid Spencer5f016e22007-07-11 17:01:13 +00003449 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003450 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003451 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003452
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003453 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3454 EnumDecl, EnumConstantDecls.data(),
3455 EnumConstantDecls.size(), getCurScope(),
3456 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003457
Douglas Gregor72de6672009-01-08 20:45:30 +00003458 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003459 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3460 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003461
3462 // The next token must be valid after an enum definition. If not, a ';'
3463 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003464 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3465 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003466 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3467 // Push this token back into the preprocessor and change our current token
3468 // to ';' so that the rest of the code recovers as though there were an
3469 // ';' after the definition.
3470 PP.EnterToken(Tok);
3471 Tok.setKind(tok::semi);
3472 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003473}
3474
3475/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003476/// start of a type-qualifier-list.
3477bool Parser::isTypeQualifier() const {
3478 switch (Tok.getKind()) {
3479 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003480
3481 // type-qualifier only in OpenCL
3482 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003483 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003484
Steve Naroff5f8aa692008-02-11 23:15:56 +00003485 // type-qualifier
3486 case tok::kw_const:
3487 case tok::kw_volatile:
3488 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003489 case tok::kw___private:
3490 case tok::kw___local:
3491 case tok::kw___global:
3492 case tok::kw___constant:
3493 case tok::kw___read_only:
3494 case tok::kw___read_write:
3495 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003496 return true;
3497 }
3498}
3499
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003500/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3501/// is definitely a type-specifier. Return false if it isn't part of a type
3502/// specifier or if we're not sure.
3503bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3504 switch (Tok.getKind()) {
3505 default: return false;
3506 // type-specifiers
3507 case tok::kw_short:
3508 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003509 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003510 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003511 case tok::kw_signed:
3512 case tok::kw_unsigned:
3513 case tok::kw__Complex:
3514 case tok::kw__Imaginary:
3515 case tok::kw_void:
3516 case tok::kw_char:
3517 case tok::kw_wchar_t:
3518 case tok::kw_char16_t:
3519 case tok::kw_char32_t:
3520 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003521 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003522 case tok::kw_float:
3523 case tok::kw_double:
3524 case tok::kw_bool:
3525 case tok::kw__Bool:
3526 case tok::kw__Decimal32:
3527 case tok::kw__Decimal64:
3528 case tok::kw__Decimal128:
3529 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003530
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003531 // struct-or-union-specifier (C99) or class-specifier (C++)
3532 case tok::kw_class:
3533 case tok::kw_struct:
3534 case tok::kw_union:
3535 // enum-specifier
3536 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003537
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003538 // typedef-name
3539 case tok::annot_typename:
3540 return true;
3541 }
3542}
3543
Steve Naroff5f8aa692008-02-11 23:15:56 +00003544/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003545/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003546bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003547 switch (Tok.getKind()) {
3548 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003549
Chris Lattner166a8fc2009-01-04 23:41:41 +00003550 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003551 if (TryAltiVecVectorToken())
3552 return true;
3553 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003554 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003555 // Annotate typenames and C++ scope specifiers. If we get one, just
3556 // recurse to handle whatever we get.
3557 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003558 return true;
3559 if (Tok.is(tok::identifier))
3560 return false;
3561 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003562
Chris Lattner166a8fc2009-01-04 23:41:41 +00003563 case tok::coloncolon: // ::foo::bar
3564 if (NextToken().is(tok::kw_new) || // ::new
3565 NextToken().is(tok::kw_delete)) // ::delete
3566 return false;
3567
Chris Lattner166a8fc2009-01-04 23:41:41 +00003568 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003569 return true;
3570 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003571
Reid Spencer5f016e22007-07-11 17:01:13 +00003572 // GNU attributes support.
3573 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003574 // GNU typeof support.
3575 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003576
Reid Spencer5f016e22007-07-11 17:01:13 +00003577 // type-specifiers
3578 case tok::kw_short:
3579 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003580 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003581 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003582 case tok::kw_signed:
3583 case tok::kw_unsigned:
3584 case tok::kw__Complex:
3585 case tok::kw__Imaginary:
3586 case tok::kw_void:
3587 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003588 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003589 case tok::kw_char16_t:
3590 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003591 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003592 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003593 case tok::kw_float:
3594 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003595 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003596 case tok::kw__Bool:
3597 case tok::kw__Decimal32:
3598 case tok::kw__Decimal64:
3599 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003600 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003601
Chris Lattner99dc9142008-04-13 18:59:07 +00003602 // struct-or-union-specifier (C99) or class-specifier (C++)
3603 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003604 case tok::kw_struct:
3605 case tok::kw_union:
3606 // enum-specifier
3607 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003608
Reid Spencer5f016e22007-07-11 17:01:13 +00003609 // type-qualifier
3610 case tok::kw_const:
3611 case tok::kw_volatile:
3612 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003613
3614 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003615 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003616 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003617
Chris Lattner7c186be2008-10-20 00:25:30 +00003618 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3619 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003620 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003621
Steve Naroff239f0732008-12-25 14:16:32 +00003622 case tok::kw___cdecl:
3623 case tok::kw___stdcall:
3624 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003625 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003626 case tok::kw___w64:
3627 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003628 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003629 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003630 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003631
3632 case tok::kw___private:
3633 case tok::kw___local:
3634 case tok::kw___global:
3635 case tok::kw___constant:
3636 case tok::kw___read_only:
3637 case tok::kw___read_write:
3638 case tok::kw___write_only:
3639
Eli Friedman290eeb02009-06-08 23:27:34 +00003640 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003641
3642 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003643 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003644
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003645 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003646 case tok::kw__Atomic:
3647 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003648 }
3649}
3650
3651/// isDeclarationSpecifier() - Return true if the current token is part of a
3652/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003653///
3654/// \param DisambiguatingWithExpression True to indicate that the purpose of
3655/// this check is to disambiguate between an expression and a declaration.
3656bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003657 switch (Tok.getKind()) {
3658 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003659
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003660 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003661 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003662
Chris Lattner166a8fc2009-01-04 23:41:41 +00003663 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003664 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003665 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003666 return false;
John Thompson82287d12010-02-05 00:12:22 +00003667 if (TryAltiVecVectorToken())
3668 return true;
3669 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003670 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003671 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003672 // Annotate typenames and C++ scope specifiers. If we get one, just
3673 // recurse to handle whatever we get.
3674 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003675 return true;
3676 if (Tok.is(tok::identifier))
3677 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003678
Douglas Gregor9497a732010-09-16 01:51:54 +00003679 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00003680 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00003681 // expression is permitted, then this is probably a class message send
3682 // missing the initial '['. In this case, we won't consider this to be
3683 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00003684 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00003685 isStartOfObjCClassMessageMissingOpenBracket())
3686 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003687
John McCall9ba61662010-02-26 08:45:28 +00003688 return isDeclarationSpecifier();
3689
Chris Lattner166a8fc2009-01-04 23:41:41 +00003690 case tok::coloncolon: // ::foo::bar
3691 if (NextToken().is(tok::kw_new) || // ::new
3692 NextToken().is(tok::kw_delete)) // ::delete
3693 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003694
Chris Lattner166a8fc2009-01-04 23:41:41 +00003695 // Annotate typenames and C++ scope specifiers. If we get one, just
3696 // recurse to handle whatever we get.
3697 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003698 return true;
3699 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003700
Reid Spencer5f016e22007-07-11 17:01:13 +00003701 // storage-class-specifier
3702 case tok::kw_typedef:
3703 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003704 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003705 case tok::kw_static:
3706 case tok::kw_auto:
3707 case tok::kw_register:
3708 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003709
Douglas Gregor8d267c52011-09-09 02:06:17 +00003710 // Modules
3711 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00003712
Reid Spencer5f016e22007-07-11 17:01:13 +00003713 // type-specifiers
3714 case tok::kw_short:
3715 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003716 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003717 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003718 case tok::kw_signed:
3719 case tok::kw_unsigned:
3720 case tok::kw__Complex:
3721 case tok::kw__Imaginary:
3722 case tok::kw_void:
3723 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003724 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003725 case tok::kw_char16_t:
3726 case tok::kw_char32_t:
3727
Reid Spencer5f016e22007-07-11 17:01:13 +00003728 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003729 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003730 case tok::kw_float:
3731 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003732 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003733 case tok::kw__Bool:
3734 case tok::kw__Decimal32:
3735 case tok::kw__Decimal64:
3736 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003737 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003738
Chris Lattner99dc9142008-04-13 18:59:07 +00003739 // struct-or-union-specifier (C99) or class-specifier (C++)
3740 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003741 case tok::kw_struct:
3742 case tok::kw_union:
3743 // enum-specifier
3744 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003745
Reid Spencer5f016e22007-07-11 17:01:13 +00003746 // type-qualifier
3747 case tok::kw_const:
3748 case tok::kw_volatile:
3749 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003750
Reid Spencer5f016e22007-07-11 17:01:13 +00003751 // function-specifier
3752 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003753 case tok::kw_virtual:
3754 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003755
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003756 // static_assert-declaration
3757 case tok::kw__Static_assert:
3758
Chris Lattner1ef08762007-08-09 17:01:07 +00003759 // GNU typeof support.
3760 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003761
Chris Lattner1ef08762007-08-09 17:01:07 +00003762 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003763 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003764 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003765
Francois Pichete3d49b42011-06-19 08:02:06 +00003766 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003767 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003768 return true;
3769
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003770 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003771 case tok::kw__Atomic:
3772 return true;
3773
Chris Lattnerf3948c42008-07-26 03:38:44 +00003774 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3775 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003776 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003777
Douglas Gregord9d75e52011-04-27 05:41:15 +00003778 // typedef-name
3779 case tok::annot_typename:
3780 return !DisambiguatingWithExpression ||
3781 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00003782
Steve Naroff47f52092009-01-06 19:34:12 +00003783 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003784 case tok::kw___cdecl:
3785 case tok::kw___stdcall:
3786 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003787 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003788 case tok::kw___w64:
3789 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003790 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003791 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003792 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003793 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003794
3795 case tok::kw___private:
3796 case tok::kw___local:
3797 case tok::kw___global:
3798 case tok::kw___constant:
3799 case tok::kw___read_only:
3800 case tok::kw___read_write:
3801 case tok::kw___write_only:
3802
Eli Friedman290eeb02009-06-08 23:27:34 +00003803 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003804 }
3805}
3806
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003807bool Parser::isConstructorDeclarator() {
3808 TentativeParsingAction TPA(*this);
3809
3810 // Parse the C++ scope specifier.
3811 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00003812 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003813 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003814 TPA.Revert();
3815 return false;
3816 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003817
3818 // Parse the constructor name.
3819 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3820 // We already know that we have a constructor name; just consume
3821 // the token.
3822 ConsumeToken();
3823 } else {
3824 TPA.Revert();
3825 return false;
3826 }
3827
Richard Smith22592862012-03-27 23:05:05 +00003828 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003829 if (Tok.isNot(tok::l_paren)) {
3830 TPA.Revert();
3831 return false;
3832 }
3833 ConsumeParen();
3834
Richard Smith22592862012-03-27 23:05:05 +00003835 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3836 // that we have a constructor.
3837 if (Tok.is(tok::r_paren) ||
3838 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003839 TPA.Revert();
3840 return true;
3841 }
3842
3843 // If we need to, enter the specified scope.
3844 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003845 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003846 DeclScopeObj.EnterDeclaratorScope();
3847
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003848 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003849 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003850 MaybeParseMicrosoftAttributes(Attrs);
3851
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003852 // Check whether the next token(s) are part of a declaration
3853 // specifier, in which case we have the start of a parameter and,
3854 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003855 bool IsConstructor = false;
3856 if (isDeclarationSpecifier())
3857 IsConstructor = true;
3858 else if (Tok.is(tok::identifier) ||
3859 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3860 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3861 // This might be a parenthesized member name, but is more likely to
3862 // be a constructor declaration with an invalid argument type. Keep
3863 // looking.
3864 if (Tok.is(tok::annot_cxxscope))
3865 ConsumeToken();
3866 ConsumeToken();
3867
3868 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003869 // which must have one of the following syntactic forms (see the
3870 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003871 switch (Tok.getKind()) {
3872 case tok::l_paren:
3873 // C(X ( int));
3874 case tok::l_square:
3875 // C(X [ 5]);
3876 // C(X [ [attribute]]);
3877 case tok::coloncolon:
3878 // C(X :: Y);
3879 // C(X :: *p);
3880 case tok::r_paren:
3881 // C(X )
3882 // Assume this isn't a constructor, rather than assuming it's a
3883 // constructor with an unnamed parameter of an ill-formed type.
3884 break;
3885
3886 default:
3887 IsConstructor = true;
3888 break;
3889 }
3890 }
3891
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003892 TPA.Revert();
3893 return IsConstructor;
3894}
Reid Spencer5f016e22007-07-11 17:01:13 +00003895
3896/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003897/// type-qualifier-list: [C99 6.7.5]
3898/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00003899/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00003900/// [ only if VendorAttributesAllowed=true ]
3901/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00003902/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00003903/// [ only if VendorAttributesAllowed=true ]
3904/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3905/// [ only if CXX0XAttributesAllowed=true ]
3906/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003907///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003908void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3909 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003910 bool CXX11AttributesAllowed) {
3911 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003912 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003913 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003914 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003915 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003916 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003917
3918 SourceLocation EndLoc;
3919
Reid Spencer5f016e22007-07-11 17:01:13 +00003920 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003921 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003922 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003923 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003924 SourceLocation Loc = Tok.getLocation();
3925
3926 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003927 case tok::code_completion:
3928 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003929 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00003930
Reid Spencer5f016e22007-07-11 17:01:13 +00003931 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003932 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith42926a02012-07-24 20:24:58 +00003933 getLangOpts(), /*IsTypeSpec*/false);
Reid Spencer5f016e22007-07-11 17:01:13 +00003934 break;
3935 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003936 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith42926a02012-07-24 20:24:58 +00003937 getLangOpts(), /*IsTypeSpec*/false);
Reid Spencer5f016e22007-07-11 17:01:13 +00003938 break;
3939 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003940 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith42926a02012-07-24 20:24:58 +00003941 getLangOpts(), /*IsTypeSpec*/false);
Reid Spencer5f016e22007-07-11 17:01:13 +00003942 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003943
3944 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00003945 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003946 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003947 goto DoneWithTypeQuals;
3948 case tok::kw___private:
3949 case tok::kw___global:
3950 case tok::kw___local:
3951 case tok::kw___constant:
3952 case tok::kw___read_only:
3953 case tok::kw___write_only:
3954 case tok::kw___read_write:
3955 ParseOpenCLQualifiers(DS);
3956 break;
3957
Eli Friedman290eeb02009-06-08 23:27:34 +00003958 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003959 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003960 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003961 case tok::kw___cdecl:
3962 case tok::kw___stdcall:
3963 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003964 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003965 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003966 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003967 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003968 continue;
3969 }
3970 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003971 case tok::kw___pascal:
3972 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003973 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003974 continue;
3975 }
3976 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003977 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003978 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003979 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003980 continue; // do *not* consume the next token!
3981 }
3982 // otherwise, FALL THROUGH!
3983 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003984 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003985 // If this is not a type-qualifier token, we're done reading type
3986 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003987 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003988 if (EndLoc.isValid())
3989 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003990 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003991 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003992
Reid Spencer5f016e22007-07-11 17:01:13 +00003993 // If the specifier combination wasn't legal, issue a diagnostic.
3994 if (isInvalid) {
3995 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003996 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003997 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003998 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003999 }
4000}
4001
4002
4003/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4004///
4005void Parser::ParseDeclarator(Declarator &D) {
4006 /// This implements the 'declarator' production in the C grammar, then checks
4007 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004008 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004009}
4010
Richard Smith9988f282012-03-29 01:16:42 +00004011static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4012 if (Kind == tok::star || Kind == tok::caret)
4013 return true;
4014
4015 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4016 if (!Lang.CPlusPlus)
4017 return false;
4018
4019 return Kind == tok::amp || Kind == tok::ampamp;
4020}
4021
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004022/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4023/// is parsed by the function passed to it. Pass null, and the direct-declarator
4024/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004025/// ptr-operator production.
4026///
Richard Smith0706df42011-10-19 21:33:05 +00004027/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004028/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4029/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004030///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004031/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4032/// [C] pointer[opt] direct-declarator
4033/// [C++] direct-declarator
4034/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004035///
4036/// pointer: [C99 6.7.5]
4037/// '*' type-qualifier-list[opt]
4038/// '*' type-qualifier-list[opt] pointer
4039///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004040/// ptr-operator:
4041/// '*' cv-qualifier-seq[opt]
4042/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004043/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004044/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004045/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004046/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004047void Parser::ParseDeclaratorInternal(Declarator &D,
4048 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004049 if (Diags.hasAllExtensionsSilenced())
4050 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004051
Sebastian Redlf30208a2009-01-24 21:16:55 +00004052 // C++ member pointers start with a '::' or a nested-name.
4053 // Member pointers get special handling, since there's no place for the
4054 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004055 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004056 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4057 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004058 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4059 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004060 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004061 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004062
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004063 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004064 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004065 // The scope spec really belongs to the direct-declarator.
4066 D.getCXXScopeSpec() = SS;
4067 if (DirectDeclParser)
4068 (this->*DirectDeclParser)(D);
4069 return;
4070 }
4071
4072 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004073 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004074 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004075 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004076 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004077
4078 // Recurse to parse whatever is left.
4079 ParseDeclaratorInternal(D, DirectDeclParser);
4080
4081 // Sema will have to catch (syntactically invalid) pointers into global
4082 // scope. It has to catch pointers into namespace scope anyway.
4083 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004084 Loc),
4085 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004086 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004087 return;
4088 }
4089 }
4090
4091 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004092 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004093 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004094 if (DirectDeclParser)
4095 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004096 return;
4097 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004098
Sebastian Redl05532f22009-03-15 22:02:01 +00004099 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4100 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004101 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004102 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004103
Chris Lattner9af55002009-03-27 04:18:06 +00004104 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004105 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004106 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004107
Richard Smith6ee326a2012-04-10 01:32:12 +00004108 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004109 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004110 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004111
Reid Spencer5f016e22007-07-11 17:01:13 +00004112 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004113 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004114 if (Kind == tok::star)
4115 // Remember that we parsed a pointer type, and remember the type-quals.
4116 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004117 DS.getConstSpecLoc(),
4118 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004119 DS.getRestrictSpecLoc()),
4120 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004121 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004122 else
4123 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004124 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004125 Loc),
4126 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004127 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004128 } else {
4129 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004130 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004131
Sebastian Redl743de1f2009-03-23 00:00:23 +00004132 // Complain about rvalue references in C++03, but then go on and build
4133 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004134 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00004135 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004136 diag::warn_cxx98_compat_rvalue_reference :
4137 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004138
Richard Smith6ee326a2012-04-10 01:32:12 +00004139 // GNU-style and C++11 attributes are allowed here, as is restrict.
4140 ParseTypeQualifierListOpt(DS);
4141 D.ExtendWithDeclSpec(DS);
4142
Reid Spencer5f016e22007-07-11 17:01:13 +00004143 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4144 // cv-qualifiers are introduced through the use of a typedef or of a
4145 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004146 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4147 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4148 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004149 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004150 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4151 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004152 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00004153 }
4154
4155 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004156 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004157
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004158 if (D.getNumTypeObjects() > 0) {
4159 // C++ [dcl.ref]p4: There shall be no references to references.
4160 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4161 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004162 if (const IdentifierInfo *II = D.getIdentifier())
4163 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4164 << II;
4165 else
4166 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4167 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004168
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004169 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004170 // can go ahead and build the (technically ill-formed)
4171 // declarator: reference collapsing will take care of it.
4172 }
4173 }
4174
Reid Spencer5f016e22007-07-11 17:01:13 +00004175 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004176 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004177 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004178 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004179 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004180 }
4181}
4182
Richard Smith9988f282012-03-29 01:16:42 +00004183static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4184 SourceLocation EllipsisLoc) {
4185 if (EllipsisLoc.isValid()) {
4186 FixItHint Insertion;
4187 if (!D.getEllipsisLoc().isValid()) {
4188 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4189 D.setEllipsisLoc(EllipsisLoc);
4190 }
4191 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4192 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4193 }
4194}
4195
Reid Spencer5f016e22007-07-11 17:01:13 +00004196/// ParseDirectDeclarator
4197/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004198/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004199/// '(' declarator ')'
4200/// [GNU] '(' attributes declarator ')'
4201/// [C90] direct-declarator '[' constant-expression[opt] ']'
4202/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4203/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4204/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4205/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004206/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4207/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004208/// direct-declarator '(' parameter-type-list ')'
4209/// direct-declarator '(' identifier-list[opt] ')'
4210/// [GNU] direct-declarator '(' parameter-forward-declarations
4211/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004212/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4213/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004214/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4215/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4216/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004217/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004218/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004219///
4220/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004221/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004222/// '::'[opt] nested-name-specifier[opt] type-name
4223///
4224/// id-expression: [C++ 5.1]
4225/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004226/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004227///
4228/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004229/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004230/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004231/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004232/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004233/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004234///
Richard Smith5d8388c2012-03-27 01:42:32 +00004235/// Note, any additional constructs added here may need corresponding changes
4236/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004237void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004238 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004239
David Blaikie4e4d0842012-03-11 07:00:24 +00004240 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004241 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004242 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004243 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4244 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004245 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004246 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004247 }
4248
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004249 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004250 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004251 // Change the declaration context for name lookup, until this function
4252 // is exited (and the declarator has been parsed).
4253 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004254 }
4255
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004256 // C++0x [dcl.fct]p14:
4257 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004258 // of a parameter-declaration-clause without a preceding comma. In
4259 // this case, the ellipsis is parsed as part of the
4260 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004261 // parameter pack that has not been expanded; otherwise, it is parsed
4262 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004263 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004264 !((D.getContext() == Declarator::PrototypeContext ||
4265 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004266 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00004267 !Actions.containsUnexpandedParameterPacks(D))) {
4268 SourceLocation EllipsisLoc = ConsumeToken();
4269 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4270 // The ellipsis was put in the wrong place. Recover, and explain to
4271 // the user what they should have done.
4272 ParseDeclarator(D);
4273 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4274 return;
4275 } else
4276 D.setEllipsisLoc(EllipsisLoc);
4277
4278 // The ellipsis can't be followed by a parenthesized declarator. We
4279 // check for that in ParseParenDeclarator, after we have disambiguated
4280 // the l_paren token.
4281 }
4282
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004283 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4284 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4285 // We found something that indicates the start of an unqualified-id.
4286 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004287 bool AllowConstructorName;
4288 if (D.getDeclSpec().hasTypeSpecifier())
4289 AllowConstructorName = false;
4290 else if (D.getCXXScopeSpec().isSet())
4291 AllowConstructorName =
4292 (D.getContext() == Declarator::FileContext ||
4293 (D.getContext() == Declarator::MemberContext &&
4294 D.getDeclSpec().isFriendSpecified()));
4295 else
4296 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4297
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004298 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004299 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4300 /*EnteringContext=*/true,
4301 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004302 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004303 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004304 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004305 D.getName()) ||
4306 // Once we're past the identifier, if the scope was bad, mark the
4307 // whole declarator bad.
4308 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004309 D.SetIdentifier(0, Tok.getLocation());
4310 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004311 } else {
4312 // Parsed the unqualified-id; update range information and move along.
4313 if (D.getSourceRange().getBegin().isInvalid())
4314 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4315 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004316 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004317 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004318 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004319 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004320 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004321 "There's a C++-specific check for tok::identifier above");
4322 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4323 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4324 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004325 goto PastIdentifier;
4326 }
Richard Smith9988f282012-03-29 01:16:42 +00004327
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004328 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004329 // direct-declarator: '(' declarator ')'
4330 // direct-declarator: '(' attributes declarator ')'
4331 // Example: 'char (*X)' or 'int (*XX)(void)'
4332 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004333
4334 // If the declarator was parenthesized, we entered the declarator
4335 // scope when parsing the parenthesized declarator, then exited
4336 // the scope already. Re-enter the scope, if we need to.
4337 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004338 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004339 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004340 if (!D.isInvalidType() &&
4341 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004342 // Change the declaration context for name lookup, until this function
4343 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004344 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004345 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004346 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004347 // This could be something simple like "int" (in which case the declarator
4348 // portion is empty), if an abstract-declarator is allowed.
4349 D.SetIdentifier(0, Tok.getLocation());
4350 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004351 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004352 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004353 if (D.getContext() == Declarator::MemberContext)
4354 Diag(Tok, diag::err_expected_member_name_or_semi)
4355 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004356 else if (getLangOpts().CPlusPlus)
4357 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004358 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004359 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004360 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004361 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004362 }
Mike Stump1eb44332009-09-09 15:08:12 +00004363
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004364 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004365 assert(D.isPastIdentifier() &&
4366 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004367
Richard Smith6ee326a2012-04-10 01:32:12 +00004368 // Don't parse attributes unless we have parsed an unparenthesized name.
4369 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004370 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004371
Reid Spencer5f016e22007-07-11 17:01:13 +00004372 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004373 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004374 // Enter function-declaration scope, limiting any declarators to the
4375 // function prototype scope, including parameter declarators.
4376 ParseScope PrototypeScope(this,
4377 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004378 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4379 // In such a case, check if we actually have a function declarator; if it
4380 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004381 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004382 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4383 // The name of the declarator, if any, is tentatively declared within
4384 // a possible direct initializer.
4385 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4386 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4387 TentativelyDeclaredIdentifiers.pop_back();
4388 if (!IsFunctionDecl)
4389 break;
4390 }
John McCall0b7e6782011-03-24 11:26:52 +00004391 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004392 BalancedDelimiterTracker T(*this, tok::l_paren);
4393 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004394 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004395 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004396 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004397 ParseBracketDeclarator(D);
4398 } else {
4399 break;
4400 }
4401 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004402}
Reid Spencer5f016e22007-07-11 17:01:13 +00004403
Chris Lattneref4715c2008-04-06 05:45:57 +00004404/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4405/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004406/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004407/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4408///
4409/// direct-declarator:
4410/// '(' declarator ')'
4411/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004412/// direct-declarator '(' parameter-type-list ')'
4413/// direct-declarator '(' identifier-list[opt] ')'
4414/// [GNU] direct-declarator '(' parameter-forward-declarations
4415/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004416///
4417void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004418 BalancedDelimiterTracker T(*this, tok::l_paren);
4419 T.consumeOpen();
4420
Chris Lattneref4715c2008-04-06 05:45:57 +00004421 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004422
Chris Lattner7399ee02008-10-20 02:05:46 +00004423 // Eat any attributes before we look at whether this is a grouping or function
4424 // declarator paren. If this is a grouping paren, the attribute applies to
4425 // the type being built up, for example:
4426 // int (__attribute__(()) *x)(long y)
4427 // If this ends up not being a grouping paren, the attribute applies to the
4428 // first argument, for example:
4429 // int (__attribute__(()) int x)
4430 // In either case, we need to eat any attributes to be able to determine what
4431 // sort of paren this is.
4432 //
John McCall0b7e6782011-03-24 11:26:52 +00004433 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004434 bool RequiresArg = false;
4435 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004436 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004437
Chris Lattner7399ee02008-10-20 02:05:46 +00004438 // We require that the argument list (if this is a non-grouping paren) be
4439 // present even if the attribute list was empty.
4440 RequiresArg = true;
4441 }
Steve Naroff239f0732008-12-25 14:16:32 +00004442 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004443 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004444 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004445 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004446 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004447 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004448 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004449 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004450 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004451 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004452
Chris Lattneref4715c2008-04-06 05:45:57 +00004453 // If we haven't past the identifier yet (or where the identifier would be
4454 // stored, if this is an abstract declarator), then this is probably just
4455 // grouping parens. However, if this could be an abstract-declarator, then
4456 // this could also be the start of function arguments (consider 'void()').
4457 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004458
Chris Lattneref4715c2008-04-06 05:45:57 +00004459 if (!D.mayOmitIdentifier()) {
4460 // If this can't be an abstract-declarator, this *must* be a grouping
4461 // paren, because we haven't seen the identifier yet.
4462 isGrouping = true;
4463 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004464 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4465 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004466 isDeclarationSpecifier() || // 'int(int)' is a function.
4467 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004468 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4469 // considered to be a type, not a K&R identifier-list.
4470 isGrouping = false;
4471 } else {
4472 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4473 isGrouping = true;
4474 }
Mike Stump1eb44332009-09-09 15:08:12 +00004475
Chris Lattneref4715c2008-04-06 05:45:57 +00004476 // If this is a grouping paren, handle:
4477 // direct-declarator: '(' declarator ')'
4478 // direct-declarator: '(' attributes declarator ')'
4479 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004480 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4481 D.setEllipsisLoc(SourceLocation());
4482
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004483 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004484 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004485 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004486 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004487 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004488 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004489 T.getCloseLocation()),
4490 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004491
4492 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004493
4494 // An ellipsis cannot be placed outside parentheses.
4495 if (EllipsisLoc.isValid())
4496 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4497
Chris Lattneref4715c2008-04-06 05:45:57 +00004498 return;
4499 }
Mike Stump1eb44332009-09-09 15:08:12 +00004500
Chris Lattneref4715c2008-04-06 05:45:57 +00004501 // Okay, if this wasn't a grouping paren, it must be the start of a function
4502 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004503 // identifier (and remember where it would have been), then call into
4504 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004505 D.SetIdentifier(0, Tok.getLocation());
4506
David Blaikie42d6d0c2011-12-04 05:04:18 +00004507 // Enter function-declaration scope, limiting any declarators to the
4508 // function prototype scope, including parameter declarators.
4509 ParseScope PrototypeScope(this,
4510 Scope::FunctionPrototypeScope|Scope::DeclScope);
Richard Smithb9c62612012-07-30 21:30:52 +00004511 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004512 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004513}
4514
4515/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4516/// declarator D up to a paren, which indicates that we are parsing function
4517/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004518///
Richard Smith6ee326a2012-04-10 01:32:12 +00004519/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4520/// immediately after the open paren - they should be considered to be the
4521/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004522///
Richard Smith6ee326a2012-04-10 01:32:12 +00004523/// If RequiresArg is true, then the first argument of the function is required
4524/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004525///
Richard Smith6ee326a2012-04-10 01:32:12 +00004526/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4527/// (C++11) ref-qualifier[opt], exception-specification[opt],
4528/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4529///
4530/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004531/// dynamic-exception-specification
4532/// noexcept-specification
4533///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004534void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004535 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004536 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004537 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004538 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004539 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004540 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004541 // lparen is already consumed!
4542 assert(D.isPastIdentifier() && "Should not call before identifier!");
4543
4544 // This should be true when the function has typed arguments.
4545 // Otherwise, it is treated as a K&R-style function.
4546 bool HasProto = false;
4547 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004548 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004549 // Remember where we see an ellipsis, if any.
4550 SourceLocation EllipsisLoc;
4551
4552 DeclSpec DS(AttrFactory);
4553 bool RefQualifierIsLValueRef = true;
4554 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004555 SourceLocation ConstQualifierLoc;
4556 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004557 ExceptionSpecificationType ESpecType = EST_None;
4558 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004559 SmallVector<ParsedType, 2> DynamicExceptions;
4560 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004561 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004562 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004563 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004564
James Molloy16f1f712012-02-29 10:24:19 +00004565 Actions.ActOnStartFunctionDeclarator();
4566
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004567 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004568 if (isFunctionDeclaratorIdentifierList()) {
4569 if (RequiresArg)
4570 Diag(Tok, diag::err_argument_required_after_attribute);
4571
4572 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4573
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004574 Tracker.consumeClose();
4575 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004576 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004577 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004578 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004579 else if (RequiresArg)
4580 Diag(Tok, diag::err_argument_required_after_attribute);
4581
David Blaikie4e4d0842012-03-11 07:00:24 +00004582 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004583
4584 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004585 Tracker.consumeClose();
4586 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004587
David Blaikie4e4d0842012-03-11 07:00:24 +00004588 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004589 // FIXME: Accept these components in any order, and produce fixits to
4590 // correct the order if the user gets it wrong. Ideally we should deal
4591 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004592
4593 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004594 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4595 if (!DS.getSourceRange().getEnd().isInvalid()) {
4596 EndLoc = DS.getSourceRange().getEnd();
4597 ConstQualifierLoc = DS.getConstSpecLoc();
4598 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4599 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004600
4601 // Parse ref-qualifier[opt].
4602 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004603 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004604 diag::warn_cxx98_compat_ref_qualifier :
4605 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004606
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004607 RefQualifierIsLValueRef = Tok.is(tok::amp);
4608 RefQualifierLoc = ConsumeToken();
4609 EndLoc = RefQualifierLoc;
4610 }
4611
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004612 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00004613 // If a declaration declares a member function or member function
4614 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004615 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00004616 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004617 // declarator.
Chad Rosier8decdee2012-06-26 22:30:43 +00004618 bool IsCXX11MemberFunction =
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004619 getLangOpts().CPlusPlus0x &&
4620 (D.getContext() == Declarator::MemberContext ||
4621 (D.getContext() == Declarator::FileContext &&
Chad Rosier8decdee2012-06-26 22:30:43 +00004622 D.getCXXScopeSpec().isValid() &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004623 Actions.CurContext->isRecord()));
4624 Sema::CXXThisScopeRAII ThisScope(Actions,
4625 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4626 DS.getTypeQualifiers(),
4627 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004628
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004629 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004630 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004631 DynamicExceptions,
4632 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004633 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004634 if (ESpecType != EST_None)
4635 EndLoc = ESpecRange.getEnd();
4636
Richard Smith6ee326a2012-04-10 01:32:12 +00004637 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4638 // after the exception-specification.
4639 MaybeParseCXX0XAttributes(FnAttrs);
4640
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004641 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004642 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004643 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004644 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004645 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004646 if (Range.getEnd().isValid())
4647 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004648 }
4649 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004650 }
4651
4652 // Remember that we parsed a function type, and remember the attributes.
4653 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4654 /*isVariadic=*/EllipsisLoc.isValid(),
Richard Smithb9c62612012-07-30 21:30:52 +00004655 IsAmbiguous, EllipsisLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004656 ParamInfo.data(), ParamInfo.size(),
4657 DS.getTypeQualifiers(),
4658 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004659 RefQualifierLoc, ConstQualifierLoc,
4660 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004661 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004662 ESpecType, ESpecRange.getBegin(),
4663 DynamicExceptions.data(),
4664 DynamicExceptionRanges.data(),
4665 DynamicExceptions.size(),
4666 NoexceptExpr.isUsable() ?
4667 NoexceptExpr.get() : 0,
Chad Rosier8decdee2012-06-26 22:30:43 +00004668 Tracker.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004669 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004670 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004671 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004672
4673 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004674}
4675
4676/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4677/// identifier list form for a K&R-style function: void foo(a,b,c)
4678///
4679/// Note that identifier-lists are only allowed for normal declarators, not for
4680/// abstract-declarators.
4681bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004682 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004683 && Tok.is(tok::identifier)
4684 && !TryAltiVecVectorToken()
4685 // K&R identifier lists can't have typedefs as identifiers, per C99
4686 // 6.7.5.3p11.
4687 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4688 // Identifier lists follow a really simple grammar: the identifiers can
4689 // be followed *only* by a ", identifier" or ")". However, K&R
4690 // identifier lists are really rare in the brave new modern world, and
4691 // it is very common for someone to typo a type in a non-K&R style
4692 // list. If we are presented with something like: "void foo(intptr x,
4693 // float y)", we don't want to start parsing the function declarator as
4694 // though it is a K&R style declarator just because intptr is an
4695 // invalid type.
4696 //
4697 // To handle this, we check to see if the token after the first
4698 // identifier is a "," or ")". Only then do we parse it as an
4699 // identifier list.
4700 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4701}
4702
4703/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4704/// we found a K&R-style identifier list instead of a typed parameter list.
4705///
4706/// After returning, ParamInfo will hold the parsed parameters.
4707///
4708/// identifier-list: [C99 6.7.5]
4709/// identifier
4710/// identifier-list ',' identifier
4711///
4712void Parser::ParseFunctionDeclaratorIdentifierList(
4713 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004714 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004715 // If there was no identifier specified for the declarator, either we are in
4716 // an abstract-declarator, or we are in a parameter declarator which was found
4717 // to be abstract. In abstract-declarators, identifier lists are not valid:
4718 // diagnose this.
4719 if (!D.getIdentifier())
4720 Diag(Tok, diag::ext_ident_list_in_param);
4721
4722 // Maintain an efficient lookup of params we have seen so far.
4723 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4724
4725 while (1) {
4726 // If this isn't an identifier, report the error and skip until ')'.
4727 if (Tok.isNot(tok::identifier)) {
4728 Diag(Tok, diag::err_expected_ident);
4729 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4730 // Forget we parsed anything.
4731 ParamInfo.clear();
4732 return;
4733 }
4734
4735 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4736
4737 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4738 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4739 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4740
4741 // Verify that the argument identifier has not already been mentioned.
4742 if (!ParamsSoFar.insert(ParmII)) {
4743 Diag(Tok, diag::err_param_redefinition) << ParmII;
4744 } else {
4745 // Remember this identifier in ParamInfo.
4746 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4747 Tok.getLocation(),
4748 0));
4749 }
4750
4751 // Eat the identifier.
4752 ConsumeToken();
4753
4754 // The list continues if we see a comma.
4755 if (Tok.isNot(tok::comma))
4756 break;
4757 ConsumeToken();
4758 }
4759}
4760
4761/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4762/// after the opening parenthesis. This function will not parse a K&R-style
4763/// identifier list.
4764///
Richard Smith6ce48a72012-04-11 04:01:28 +00004765/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4766/// caller parsed those arguments immediately after the open paren - they should
4767/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004768///
4769/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4770/// be the location of the ellipsis, if any was parsed.
4771///
Reid Spencer5f016e22007-07-11 17:01:13 +00004772/// parameter-type-list: [C99 6.7.5]
4773/// parameter-list
4774/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004775/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004776///
4777/// parameter-list: [C99 6.7.5]
4778/// parameter-declaration
4779/// parameter-list ',' parameter-declaration
4780///
4781/// parameter-declaration: [C99 6.7.5]
4782/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004783/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004784/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004785/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004786/// declaration-specifiers abstract-declarator[opt]
4787/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004788/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004789/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004790/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004791///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004792void Parser::ParseParameterDeclarationClause(
4793 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004794 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004795 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004796 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004797
Chris Lattnerf97409f2008-04-06 06:57:35 +00004798 while (1) {
4799 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004800 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4801 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004802 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004803 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004804 }
Mike Stump1eb44332009-09-09 15:08:12 +00004805
Chris Lattnerf97409f2008-04-06 06:57:35 +00004806 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004807 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004808 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004809
Richard Smith6ce48a72012-04-11 04:01:28 +00004810 // Parse any C++11 attributes.
4811 MaybeParseCXX0XAttributes(DS.getAttributes());
4812
John McCall7f040a92010-12-24 02:08:15 +00004813 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004814 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004815 ParseMicrosoftAttributes(DS.getAttributes());
4816
4817 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004818
4819 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004820 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004821 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004822 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4823 // too much hassle.
4824 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004825
Chris Lattnere64c5492009-02-27 18:38:20 +00004826 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004827
Chris Lattnerf97409f2008-04-06 06:57:35 +00004828 // Parse the declarator. This is "PrototypeContext", because we must
4829 // accept either 'declarator' or 'abstract-declarator' here.
4830 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4831 ParseDeclarator(ParmDecl);
4832
4833 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004834 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004835
Chris Lattnerf97409f2008-04-06 06:57:35 +00004836 // Remember this parsed parameter in ParamInfo.
4837 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004838
Douglas Gregor72b505b2008-12-16 21:30:33 +00004839 // DefArgToks is used when the parsing of default arguments needs
4840 // to be delayed.
4841 CachedTokens *DefArgToks = 0;
4842
Chris Lattnerf97409f2008-04-06 06:57:35 +00004843 // If no parameter was specified, verify that *something* was specified,
4844 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004845 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4846 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004847 // Completely missing, emit error.
4848 Diag(DSStart, diag::err_missing_param);
4849 } else {
4850 // Otherwise, we have something. Add it and let semantic analysis try
4851 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004852
Chris Lattnerf97409f2008-04-06 06:57:35 +00004853 // Inform the actions module about the parameter declarator, so it gets
4854 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004855 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004856
4857 // Parse the default argument, if any. We parse the default
4858 // arguments in all dialects; the semantic analysis in
4859 // ActOnParamDefaultArgument will reject the default argument in
4860 // C.
4861 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004862 SourceLocation EqualLoc = Tok.getLocation();
4863
Chris Lattner04421082008-04-08 04:40:51 +00004864 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004865 if (D.getContext() == Declarator::MemberContext) {
4866 // If we're inside a class definition, cache the tokens
4867 // corresponding to the default argument. We'll actually parse
4868 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004869 // FIXME: Can we use a smart pointer for Toks?
4870 DefArgToks = new CachedTokens;
4871
Mike Stump1eb44332009-09-09 15:08:12 +00004872 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004873 /*StopAtSemi=*/true,
4874 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004875 delete DefArgToks;
4876 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004877 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004878 } else {
4879 // Mark the end of the default argument so that we know when to
4880 // stop when we parse it later on.
4881 Token DefArgEnd;
4882 DefArgEnd.startToken();
4883 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4884 DefArgEnd.setLocation(Tok.getLocation());
4885 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004886 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004887 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004888 }
Chris Lattner04421082008-04-08 04:40:51 +00004889 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004890 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004891 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004892
Chad Rosier8decdee2012-06-26 22:30:43 +00004893 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004894 // used.
4895 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004896 Sema::PotentiallyEvaluatedIfUsed,
4897 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004898
Sebastian Redl84407ba2012-03-14 15:54:00 +00004899 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004900 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4901 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004902 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004903 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004904 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004905 if (DefArgResult.isInvalid()) {
4906 Actions.ActOnParamDefaultArgumentError(Param);
4907 SkipUntil(tok::comma, tok::r_paren, true, true);
4908 } else {
4909 // Inform the actions module about the default argument
4910 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004911 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004912 }
Chris Lattner04421082008-04-08 04:40:51 +00004913 }
4914 }
Mike Stump1eb44332009-09-09 15:08:12 +00004915
4916 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4917 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004918 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004919 }
4920
4921 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004922 if (Tok.isNot(tok::comma)) {
4923 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004924 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00004925
David Blaikie4e4d0842012-03-11 07:00:24 +00004926 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004927 // We have ellipsis without a preceding ',', which is ill-formed
4928 // in C. Complain and provide the fix.
4929 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004930 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004931 }
4932 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004933
Douglas Gregored5d6512009-09-22 21:41:40 +00004934 break;
4935 }
Mike Stump1eb44332009-09-09 15:08:12 +00004936
Chris Lattnerf97409f2008-04-06 06:57:35 +00004937 // Consume the comma.
4938 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004939 }
Mike Stump1eb44332009-09-09 15:08:12 +00004940
Chris Lattner66d28652008-04-06 06:34:08 +00004941}
Chris Lattneref4715c2008-04-06 05:45:57 +00004942
Reid Spencer5f016e22007-07-11 17:01:13 +00004943/// [C90] direct-declarator '[' constant-expression[opt] ']'
4944/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4945/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4946/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4947/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004948/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4949/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004950void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004951 if (CheckProhibitedCXX11Attribute())
4952 return;
4953
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004954 BalancedDelimiterTracker T(*this, tok::l_square);
4955 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004956
Chris Lattner378c7e42008-12-18 07:27:21 +00004957 // C array syntax has many features, but by-far the most common is [] and [4].
4958 // This code does a fast path to handle some of the most obvious cases.
4959 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004960 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004961 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004962 MaybeParseCXX0XAttributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00004963
Chris Lattner378c7e42008-12-18 07:27:21 +00004964 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004965 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004966 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004967 T.getOpenLocation(),
4968 T.getCloseLocation()),
4969 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004970 return;
4971 } else if (Tok.getKind() == tok::numeric_constant &&
4972 GetLookAheadToken(1).is(tok::r_square)) {
4973 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004974 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004975 ConsumeToken();
4976
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004977 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004978 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004979 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004980
Chris Lattner378c7e42008-12-18 07:27:21 +00004981 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004982 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004983 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004984 T.getOpenLocation(),
4985 T.getCloseLocation()),
4986 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004987 return;
4988 }
Mike Stump1eb44332009-09-09 15:08:12 +00004989
Reid Spencer5f016e22007-07-11 17:01:13 +00004990 // If valid, this location is the position where we read the 'static' keyword.
4991 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004992 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004993 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004994
Reid Spencer5f016e22007-07-11 17:01:13 +00004995 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004996 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004997 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004998 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004999
Reid Spencer5f016e22007-07-11 17:01:13 +00005000 // If we haven't already read 'static', check to see if there is one after the
5001 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005002 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005003 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005004
Reid Spencer5f016e22007-07-11 17:01:13 +00005005 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5006 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005007 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005008
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005009 // Handle the case where we have '[*]' as the array size. However, a leading
5010 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005011 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005012 // infrequent, use of lookahead is not costly here.
5013 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005014 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005015
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005016 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005017 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005018 StaticLoc = SourceLocation(); // Drop the static.
5019 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005020 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005021 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005022 // Note, in C89, this production uses the constant-expr production instead
5023 // of assignment-expr. The only difference is that assignment-expr allows
5024 // things like '=' and '*='. Sema rejects these in C89 mode because they
5025 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005026
Douglas Gregore0762c92009-06-19 23:52:42 +00005027 // Parse the constant-expression or assignment-expression now (depending
5028 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005029 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005030 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005031 } else {
5032 EnterExpressionEvaluationContext Unevaluated(Actions,
5033 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005034 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005035 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005036 }
Mike Stump1eb44332009-09-09 15:08:12 +00005037
Reid Spencer5f016e22007-07-11 17:01:13 +00005038 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005039 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005040 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005041 // If the expression was invalid, skip it.
5042 SkipUntil(tok::r_square);
5043 return;
5044 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005045
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005046 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005047
John McCall0b7e6782011-03-24 11:26:52 +00005048 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00005049 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005050
Chris Lattner378c7e42008-12-18 07:27:21 +00005051 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005052 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005053 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005054 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005055 T.getOpenLocation(),
5056 T.getCloseLocation()),
5057 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005058}
5059
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005060/// [GNU] typeof-specifier:
5061/// typeof ( expressions )
5062/// typeof ( type-name )
5063/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005064///
5065void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005066 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005067 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005068 SourceLocation StartLoc = ConsumeToken();
5069
John McCallcfb708c2010-01-13 20:03:27 +00005070 const bool hasParens = Tok.is(tok::l_paren);
5071
Eli Friedman71b8fb52012-01-21 01:01:51 +00005072 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
5073
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005074 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005075 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005076 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005077 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5078 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005079 if (hasParens)
5080 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005081
5082 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005083 // FIXME: Not accurate, the range gets one token more than it should.
5084 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005085 else
5086 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005087
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005088 if (isCastExpr) {
5089 if (!CastTy) {
5090 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005091 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005092 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005093
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005094 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005095 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005096 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5097 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005098 DiagID, CastTy))
5099 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005100 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005101 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005102
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005103 // If we get here, the operand to the typeof was an expresion.
5104 if (Operand.isInvalid()) {
5105 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005106 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005107 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005108
Eli Friedman71b8fb52012-01-21 01:01:51 +00005109 // We might need to transform the operand if it is potentially evaluated.
5110 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5111 if (Operand.isInvalid()) {
5112 DS.SetTypeSpecError();
5113 return;
5114 }
5115
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005116 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005117 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005118 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5119 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005120 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005121 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005122}
Chris Lattner1b492422010-02-28 18:33:55 +00005123
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005124/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005125/// _Atomic ( type-name )
5126///
5127void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5128 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5129
5130 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005131 BalancedDelimiterTracker T(*this, tok::l_paren);
5132 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00005133 SkipUntil(tok::r_paren);
5134 return;
5135 }
5136
5137 TypeResult Result = ParseTypeName();
5138 if (Result.isInvalid()) {
5139 SkipUntil(tok::r_paren);
5140 return;
5141 }
5142
5143 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005144 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005145
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005146 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005147 return;
5148
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005149 DS.setTypeofParensRange(T.getRange());
5150 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005151
5152 const char *PrevSpec = 0;
5153 unsigned DiagID;
5154 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5155 DiagID, Result.release()))
5156 Diag(StartLoc, DiagID) << PrevSpec;
5157}
5158
Chris Lattner1b492422010-02-28 18:33:55 +00005159
5160/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5161/// from TryAltiVecVectorToken.
5162bool Parser::TryAltiVecVectorTokenOutOfLine() {
5163 Token Next = NextToken();
5164 switch (Next.getKind()) {
5165 default: return false;
5166 case tok::kw_short:
5167 case tok::kw_long:
5168 case tok::kw_signed:
5169 case tok::kw_unsigned:
5170 case tok::kw_void:
5171 case tok::kw_char:
5172 case tok::kw_int:
5173 case tok::kw_float:
5174 case tok::kw_double:
5175 case tok::kw_bool:
5176 case tok::kw___pixel:
5177 Tok.setKind(tok::kw___vector);
5178 return true;
5179 case tok::identifier:
5180 if (Next.getIdentifierInfo() == Ident_pixel) {
5181 Tok.setKind(tok::kw___vector);
5182 return true;
5183 }
5184 return false;
5185 }
5186}
5187
5188bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5189 const char *&PrevSpec, unsigned &DiagID,
5190 bool &isInvalid) {
5191 if (Tok.getIdentifierInfo() == Ident_vector) {
5192 Token Next = NextToken();
5193 switch (Next.getKind()) {
5194 case tok::kw_short:
5195 case tok::kw_long:
5196 case tok::kw_signed:
5197 case tok::kw_unsigned:
5198 case tok::kw_void:
5199 case tok::kw_char:
5200 case tok::kw_int:
5201 case tok::kw_float:
5202 case tok::kw_double:
5203 case tok::kw_bool:
5204 case tok::kw___pixel:
5205 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5206 return true;
5207 case tok::identifier:
5208 if (Next.getIdentifierInfo() == Ident_pixel) {
5209 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5210 return true;
5211 }
5212 break;
5213 default:
5214 break;
5215 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005216 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005217 DS.isTypeAltiVecVector()) {
5218 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5219 return true;
5220 }
5221 return false;
5222}