blob: e7d5ae28bf9a8d2973d731fe2f7307ab4ca84b19 [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Benjamin Kramer9852f582012-12-01 16:35:25 +000016#include "clang/Basic/AddressSpaces.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000017#include "clang/Basic/OpenCL.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000019#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000020#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000025#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// C99 6.7: Declarations.
30//===----------------------------------------------------------------------===//
31
32/// ParseTypeName
33/// type-name: [C99 6.7.6]
34/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000035///
36/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000037TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000038 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000039 AccessSpecifier AS,
40 Decl **OwnedType) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000041 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000042 if (DSC == DSC_normal)
43 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000044
Reid Spencer5f016e22007-07-11 17:01:13 +000045 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000046 DeclSpec DS(AttrFactory);
Richard Smith7796eb52012-03-12 08:56:40 +000047 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000048 if (OwnedType)
49 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000050
Reid Spencer5f016e22007-07-11 17:01:13 +000051 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000052 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000053 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000054 if (Range)
55 *Range = DeclaratorInfo.getSourceRange();
56
Chris Lattnereaaebc72009-04-25 08:06:05 +000057 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000058 return true;
59
Douglas Gregor23c94db2010-07-02 17:43:08 +000060 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000061}
62
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000063
64/// isAttributeLateParsed - Return true if the attribute has arguments that
65/// require late parsing.
66static bool isAttributeLateParsed(const IdentifierInfo &II) {
67 return llvm::StringSwitch<bool>(II.getName())
68#include "clang/Parse/AttrLateParsed.inc"
69 .Default(false);
70}
71
Sean Huntbbd37c62009-11-21 08:43:09 +000072/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000073///
74/// [GNU] attributes:
75/// attribute
76/// attributes attribute
77///
78/// [GNU] attribute:
79/// '__attribute__' '(' '(' attribute-list ')' ')'
80///
81/// [GNU] attribute-list:
82/// attrib
83/// attribute_list ',' attrib
84///
85/// [GNU] attrib:
86/// empty
87/// attrib-name
88/// attrib-name '(' identifier ')'
89/// attrib-name '(' identifier ',' nonempty-expr-list ')'
90/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
91///
92/// [GNU] attrib-name:
93/// identifier
94/// typespec
95/// typequal
96/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000097///
Reid Spencer5f016e22007-07-11 17:01:13 +000098/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000099/// token lookahead. Comment from gcc: "If they start with an identifier
100/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +0000101/// start with that identifier; otherwise they are an expression list."
102///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000103/// GCC does not require the ',' between attribs in an attribute-list.
104///
Reid Spencer5f016e22007-07-11 17:01:13 +0000105/// At the moment, I am not doing 2 token lookahead. I am also unaware of
106/// any attributes that don't work (based on my limited testing). Most
107/// attributes are very simple in practice. Until we find a bug, I don't see
108/// a pressing need to implement the 2 token lookahead.
109
John McCall7f040a92010-12-24 02:08:15 +0000110void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000111 SourceLocation *endLoc,
112 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000113 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattner04d66662007-10-09 17:33:22 +0000115 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 ConsumeToken();
117 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
118 "attribute")) {
119 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000120 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 }
122 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
123 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000124 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 }
126 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000127 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
128 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000129 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
131 ConsumeToken();
132 continue;
133 }
134 // we have an identifier or declaration specifier (const, int, etc.)
135 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
136 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000138 if (Tok.is(tok::l_paren)) {
139 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000140 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000141 LateParsedAttribute *LA =
142 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
143 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000144
Bill Wendlingad017fa2012-12-20 19:22:21 +0000145 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000146 // with other late-parsed declarations.
DeLesley Hutchins161db022012-11-02 21:44:32 +0000147 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000148 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000150 // consume everything up to and including the matching right parens
151 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000153 Token Eof;
154 Eof.startToken();
155 Eof.setLocation(Tok.getLocation());
156 LA->Toks.push_back(Eof);
157 } else {
Michael Han6880f492012-10-03 01:56:22 +0000158 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000159 0, SourceLocation(), AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 }
161 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000162 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000163 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 }
165 }
166 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000168 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000169 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
170 SkipUntil(tok::r_paren, false);
171 }
John McCall7f040a92010-12-24 02:08:15 +0000172 if (endLoc)
173 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000175}
176
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000177
Michael Han6880f492012-10-03 01:56:22 +0000178/// Parse the arguments to a parameterized GNU attribute or
179/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000180void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
181 SourceLocation AttrNameLoc,
182 ParsedAttributes &Attrs,
Michael Han6880f492012-10-03 01:56:22 +0000183 SourceLocation *EndLoc,
184 IdentifierInfo *ScopeName,
185 SourceLocation ScopeLoc,
186 AttributeList::Syntax Syntax) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000187
188 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
189
190 // Availability attributes have their own grammar.
191 if (AttrName->isStr("availability")) {
192 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
193 return;
194 }
195 // Thread safety attributes fit into the FIXME case above, so we
196 // just parse the arguments as a list of expressions
197 if (IsThreadSafetyAttribute(AttrName->getName())) {
198 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
199 return;
200 }
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000201 // Type safety attributes have their own grammar.
202 if (AttrName->isStr("type_tag_for_datatype")) {
203 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
204 return;
205 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000206
207 ConsumeParen(); // ignore the left paren loc for now
208
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000209 IdentifierInfo *ParmName = 0;
210 SourceLocation ParmLoc;
211 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000212
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000213 switch (Tok.getKind()) {
214 case tok::kw_char:
215 case tok::kw_wchar_t:
216 case tok::kw_char16_t:
217 case tok::kw_char32_t:
218 case tok::kw_bool:
219 case tok::kw_short:
220 case tok::kw_int:
221 case tok::kw_long:
222 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000223 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000224 case tok::kw_signed:
225 case tok::kw_unsigned:
226 case tok::kw_float:
227 case tok::kw_double:
228 case tok::kw_void:
229 case tok::kw_typeof:
230 // __attribute__(( vec_type_hint(char) ))
231 // FIXME: Don't just discard the builtin type token.
232 ConsumeToken();
233 BuiltinType = true;
234 break;
235
236 case tok::identifier:
237 ParmName = Tok.getIdentifierInfo();
238 ParmLoc = ConsumeToken();
239 break;
240
241 default:
242 break;
243 }
244
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +0000245 ExprVector ArgExprs;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000246
247 if (!BuiltinType &&
248 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
249 // Eat the comma.
250 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000251 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000252
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000253 // Parse the non-empty comma-separated list of expressions.
254 while (1) {
255 ExprResult ArgExpr(ParseAssignmentExpression());
256 if (ArgExpr.isInvalid()) {
257 SkipUntil(tok::r_paren);
258 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000259 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000260 ArgExprs.push_back(ArgExpr.release());
261 if (Tok.isNot(tok::comma))
262 break;
263 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000264 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000265 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000266 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
267 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
268 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000269 while (Tok.is(tok::identifier)) {
270 ConsumeToken();
271 if (Tok.is(tok::greater))
272 break;
273 if (Tok.is(tok::comma)) {
274 ConsumeToken();
275 continue;
276 }
277 }
278 if (Tok.isNot(tok::greater))
279 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000280 SkipUntil(tok::r_paren, false, true); // skip until ')'
281 }
282 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000283
284 SourceLocation RParen = Tok.getLocation();
285 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han45bed132012-10-04 16:42:52 +0000286 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000287 AttributeList *attr =
Michael Han45bed132012-10-04 16:42:52 +0000288 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen),
Michael Han6880f492012-10-03 01:56:22 +0000289 ScopeName, ScopeLoc, ParmName, ParmLoc,
290 ArgExprs.data(), ArgExprs.size(), Syntax);
Sean Hunt8e083e72012-06-19 23:57:03 +0000291 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000292 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000293 }
294}
295
Chad Rosier8decdee2012-06-26 22:30:43 +0000296/// \brief Parses a single argument for a declspec, including the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000297/// surrounding parens.
Chad Rosier8decdee2012-06-26 22:30:43 +0000298void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000299 SourceLocation AttrNameLoc,
300 ParsedAttributes &Attrs)
301{
302 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000303 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000304 AttrName->getNameStart(), tok::r_paren))
305 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000306
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000307 ExprResult ArgExpr(ParseConstantExpression());
308 if (ArgExpr.isInvalid()) {
309 T.skipToEnd();
310 return;
311 }
312 Expr *ExprList = ArgExpr.take();
Chad Rosier8decdee2012-06-26 22:30:43 +0000313 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000314 &ExprList, 1, AttributeList::AS_Declspec);
315
316 T.consumeClose();
317}
318
Chad Rosier8decdee2012-06-26 22:30:43 +0000319/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000320/// arguments.
321bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
322 return llvm::StringSwitch<bool>(Ident->getName())
323 .Case("dllimport", true)
324 .Case("dllexport", true)
325 .Case("noreturn", true)
326 .Case("nothrow", true)
327 .Case("noinline", true)
328 .Case("naked", true)
329 .Case("appdomain", true)
330 .Case("process", true)
331 .Case("jitintrinsic", true)
332 .Case("noalias", true)
333 .Case("restrict", true)
334 .Case("novtable", true)
335 .Case("selectany", true)
336 .Case("thread", true)
337 .Default(false);
338}
339
Chad Rosier8decdee2012-06-26 22:30:43 +0000340/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000341/// parameters). Will return false if we properly handled the declspec, or
342/// true if it is an unknown declspec.
Chad Rosier8decdee2012-06-26 22:30:43 +0000343void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000344 SourceLocation Loc,
345 ParsedAttributes &Attrs) {
346 // Try to handle the easy case first -- these declspecs all take a single
347 // parameter as their argument.
348 if (llvm::StringSwitch<bool>(Ident->getName())
349 .Case("uuid", true)
350 .Case("align", true)
351 .Case("allocate", true)
352 .Default(false)) {
353 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
354 } else if (Ident->getName() == "deprecated") {
Chad Rosier8decdee2012-06-26 22:30:43 +0000355 // The deprecated declspec has an optional single argument, so we will
356 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000357 // not.
358 if (Tok.getKind() == tok::l_paren)
359 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
360 else
Chad Rosier8decdee2012-06-26 22:30:43 +0000361 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000362 AttributeList::AS_Declspec);
363 } else if (Ident->getName() == "property") {
364 // The property declspec is more complex in that it can take one or two
Chad Rosier8decdee2012-06-26 22:30:43 +0000365 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000366 // must be named get or put.
367 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000368 // For right now, we will just skip to the closing right paren of the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000369 // property expression.
370 //
371 // FIXME: we should deal with __declspec(property) at some point because it
372 // is used in the platform SDK headers for the Parallel Patterns Library
373 // and ATL.
374 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000375 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000376 Ident->getNameStart(), tok::r_paren))
377 return;
378 T.skipToEnd();
379 } else {
380 // We don't recognize this as a valid declspec, but instead of creating the
381 // attribute and allowing sema to warn about it, we will warn here instead.
382 // This is because some attributes have multiple spellings, but we need to
383 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosier8decdee2012-06-26 22:30:43 +0000384 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000385 // both locations.
386 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
387
388 // If there's an open paren, we should eat the open and close parens under
389 // the assumption that this unknown declspec has parameters.
390 BalancedDelimiterTracker T(*this, tok::l_paren);
391 if (!T.consumeOpen())
392 T.skipToEnd();
393 }
394}
395
Eli Friedmana23b4852009-06-08 07:21:15 +0000396/// [MS] decl-specifier:
397/// __declspec ( extended-decl-modifier-seq )
398///
399/// [MS] extended-decl-modifier-seq:
400/// extended-decl-modifier[opt]
401/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000402void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000403 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000404
Steve Narofff59e17e2008-12-24 20:59:21 +0000405 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000406 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000407 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000408 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000409 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000410
Chad Rosier8decdee2012-06-26 22:30:43 +0000411 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000412 // you can specify multiple attributes per declspec.
413 while (Tok.getKind() != tok::r_paren) {
414 // We expect either a well-known identifier or a generic string. Anything
415 // else is a malformed declspec.
416 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosier8decdee2012-06-26 22:30:43 +0000417 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000418 Tok.getKind() != tok::kw_restrict) {
419 Diag(Tok, diag::err_ms_declspec_type);
420 T.skipToEnd();
421 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000422 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000423
424 IdentifierInfo *AttrName;
425 SourceLocation AttrNameLoc;
426 if (IsString) {
427 SmallString<8> StrBuffer;
428 bool Invalid = false;
429 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
430 if (Invalid) {
431 T.skipToEnd();
432 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000433 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000434 AttrName = PP.getIdentifierInfo(Str);
435 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000436 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000437 AttrName = Tok.getIdentifierInfo();
438 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000439 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000440
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000441 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosier8decdee2012-06-26 22:30:43 +0000442 // If we have a generic string, we will allow it because there is no
443 // documented list of allowable string declspecs, but we know they exist
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000444 // (for instance, SAL declspecs in older versions of MSVC).
445 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000446 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000447 // arguments and can be turned into an attribute directly.
Chad Rosier8decdee2012-06-26 22:30:43 +0000448 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000449 0, 0, AttributeList::AS_Declspec);
450 else
451 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000452 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000453 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000454}
455
John McCall7f040a92010-12-24 02:08:15 +0000456void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000457 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000458 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000459 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000460 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Chad Rosierccbb4022012-12-21 21:27:13 +0000461 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000462 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
463 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000464 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000465 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Eli Friedman290eeb02009-06-08 23:27:34 +0000466 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000467}
468
John McCall7f040a92010-12-24 02:08:15 +0000469void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000470 // Treat these like attributes
471 while (Tok.is(tok::kw___pascal)) {
472 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
473 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000474 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000475 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000476 }
John McCall7f040a92010-12-24 02:08:15 +0000477}
478
Peter Collingbournef315fa82011-02-14 01:42:53 +0000479void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
480 // Treat these like attributes
481 while (Tok.is(tok::kw___kernel)) {
482 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000483 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
484 AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000485 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000486 }
487}
488
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000489void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
490 SourceLocation Loc = Tok.getLocation();
491 switch(Tok.getKind()) {
492 // OpenCL qualifiers:
493 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000494 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000495 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000496 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000497 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000498 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000499
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000500 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000501 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000502 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000503 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000504 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000505
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000506 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000507 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000508 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000509 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000510 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000511
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000512 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000513 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000514 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000515 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000516 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000517
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000518 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000519 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000520 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000521 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000522 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000523
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000524 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000525 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000526 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000527 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000528 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000529
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000530 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000531 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000532 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000533 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000534 break;
535 default: break;
536 }
537}
538
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000539/// \brief Parse a version number.
540///
541/// version:
542/// simple-integer
543/// simple-integer ',' simple-integer
544/// simple-integer ',' simple-integer ',' simple-integer
545VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
546 Range = Tok.getLocation();
547
548 if (!Tok.is(tok::numeric_constant)) {
549 Diag(Tok, diag::err_expected_version);
550 SkipUntil(tok::comma, tok::r_paren, true, true, true);
551 return VersionTuple();
552 }
553
554 // Parse the major (and possibly minor and subminor) versions, which
555 // are stored in the numeric constant. We utilize a quirk of the
556 // lexer, which is that it handles something like 1.2.3 as a single
557 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000558 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000559 Buffer.resize(Tok.getLength()+1);
560 const char *ThisTokBegin = &Buffer[0];
561
562 // Get the spelling of the token, which eliminates trigraphs, etc.
563 bool Invalid = false;
564 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
565 if (Invalid)
566 return VersionTuple();
567
568 // Parse the major version.
569 unsigned AfterMajor = 0;
570 unsigned Major = 0;
571 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
572 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
573 ++AfterMajor;
574 }
575
576 if (AfterMajor == 0) {
577 Diag(Tok, diag::err_expected_version);
578 SkipUntil(tok::comma, tok::r_paren, true, true, true);
579 return VersionTuple();
580 }
581
582 if (AfterMajor == ActualLength) {
583 ConsumeToken();
584
585 // We only had a single version component.
586 if (Major == 0) {
587 Diag(Tok, diag::err_zero_version);
588 return VersionTuple();
589 }
590
591 return VersionTuple(Major);
592 }
593
594 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
595 Diag(Tok, diag::err_expected_version);
596 SkipUntil(tok::comma, tok::r_paren, true, true, true);
597 return VersionTuple();
598 }
599
600 // Parse the minor version.
601 unsigned AfterMinor = AfterMajor + 1;
602 unsigned Minor = 0;
603 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
604 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
605 ++AfterMinor;
606 }
607
608 if (AfterMinor == ActualLength) {
609 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000610
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000611 // We had major.minor.
612 if (Major == 0 && Minor == 0) {
613 Diag(Tok, diag::err_zero_version);
614 return VersionTuple();
615 }
616
Chad Rosier8decdee2012-06-26 22:30:43 +0000617 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000618 }
619
620 // If what follows is not a '.', we have a problem.
621 if (ThisTokBegin[AfterMinor] != '.') {
622 Diag(Tok, diag::err_expected_version);
623 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosier8decdee2012-06-26 22:30:43 +0000624 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000625 }
626
627 // Parse the subminor version.
628 unsigned AfterSubminor = AfterMinor + 1;
629 unsigned Subminor = 0;
630 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
631 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
632 ++AfterSubminor;
633 }
634
635 if (AfterSubminor != ActualLength) {
636 Diag(Tok, diag::err_expected_version);
637 SkipUntil(tok::comma, tok::r_paren, true, true, true);
638 return VersionTuple();
639 }
640 ConsumeToken();
641 return VersionTuple(Major, Minor, Subminor);
642}
643
644/// \brief Parse the contents of the "availability" attribute.
645///
646/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000647/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000648///
649/// platform:
650/// identifier
651///
652/// version-arg-list:
653/// version-arg
654/// version-arg ',' version-arg-list
655///
656/// version-arg:
657/// 'introduced' '=' version
658/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000659/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000660/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000661/// opt-message:
662/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000663void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
664 SourceLocation AvailabilityLoc,
665 ParsedAttributes &attrs,
666 SourceLocation *endLoc) {
667 SourceLocation PlatformLoc;
668 IdentifierInfo *Platform = 0;
669
670 enum { Introduced, Deprecated, Obsoleted, Unknown };
671 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000672 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000673
674 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000675 BalancedDelimiterTracker T(*this, tok::l_paren);
676 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000677 Diag(Tok, diag::err_expected_lparen);
678 return;
679 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000680
681 // Parse the platform name,
682 if (Tok.isNot(tok::identifier)) {
683 Diag(Tok, diag::err_availability_expected_platform);
684 SkipUntil(tok::r_paren);
685 return;
686 }
687 Platform = Tok.getIdentifierInfo();
688 PlatformLoc = ConsumeToken();
689
690 // Parse the ',' following the platform name.
691 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
692 return;
693
694 // If we haven't grabbed the pointers for the identifiers
695 // "introduced", "deprecated", and "obsoleted", do so now.
696 if (!Ident_introduced) {
697 Ident_introduced = PP.getIdentifierInfo("introduced");
698 Ident_deprecated = PP.getIdentifierInfo("deprecated");
699 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000700 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000701 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000702 }
703
704 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000705 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000706 do {
707 if (Tok.isNot(tok::identifier)) {
708 Diag(Tok, diag::err_availability_expected_change);
709 SkipUntil(tok::r_paren);
710 return;
711 }
712 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
713 SourceLocation KeywordLoc = ConsumeToken();
714
Douglas Gregorb53e4172011-03-26 03:35:55 +0000715 if (Keyword == Ident_unavailable) {
716 if (UnavailableLoc.isValid()) {
717 Diag(KeywordLoc, diag::err_availability_redundant)
718 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000719 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000720 UnavailableLoc = KeywordLoc;
721
722 if (Tok.isNot(tok::comma))
723 break;
724
725 ConsumeToken();
726 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000727 }
728
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000729 if (Tok.isNot(tok::equal)) {
730 Diag(Tok, diag::err_expected_equal_after)
731 << Keyword;
732 SkipUntil(tok::r_paren);
733 return;
734 }
735 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000736 if (Keyword == Ident_message) {
737 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000738 Diag(Tok, diag::err_expected_string_literal)
739 << /*Source='availability attribute'*/2;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000740 SkipUntil(tok::r_paren);
741 return;
742 }
743 MessageExpr = ParseStringLiteralExpression();
744 break;
745 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000746
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000747 SourceRange VersionRange;
748 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000749
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000750 if (Version.empty()) {
751 SkipUntil(tok::r_paren);
752 return;
753 }
754
755 unsigned Index;
756 if (Keyword == Ident_introduced)
757 Index = Introduced;
758 else if (Keyword == Ident_deprecated)
759 Index = Deprecated;
760 else if (Keyword == Ident_obsoleted)
761 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000762 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000763 Index = Unknown;
764
765 if (Index < Unknown) {
766 if (!Changes[Index].KeywordLoc.isInvalid()) {
767 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000768 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000769 << SourceRange(Changes[Index].KeywordLoc,
770 Changes[Index].VersionRange.getEnd());
771 }
772
773 Changes[Index].KeywordLoc = KeywordLoc;
774 Changes[Index].Version = Version;
775 Changes[Index].VersionRange = VersionRange;
776 } else {
777 Diag(KeywordLoc, diag::err_availability_unknown_change)
778 << Keyword << VersionRange;
779 }
780
781 if (Tok.isNot(tok::comma))
782 break;
783
784 ConsumeToken();
785 } while (true);
786
787 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000788 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000789 return;
790
791 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000792 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000793
Douglas Gregorb53e4172011-03-26 03:35:55 +0000794 // The 'unavailable' availability cannot be combined with any other
795 // availability changes. Make sure that hasn't happened.
796 if (UnavailableLoc.isValid()) {
797 bool Complained = false;
798 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
799 if (Changes[Index].KeywordLoc.isValid()) {
800 if (!Complained) {
801 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
802 << SourceRange(Changes[Index].KeywordLoc,
803 Changes[Index].VersionRange.getEnd());
804 Complained = true;
805 }
806
807 // Clear out the availability.
808 Changes[Index] = AvailabilityChange();
809 }
810 }
811 }
812
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000813 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000814 attrs.addNew(&Availability,
815 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000816 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000817 Platform, PlatformLoc,
818 Changes[Introduced],
819 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000820 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000821 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000822 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000823}
824
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000825
Bill Wendlingad017fa2012-12-20 19:22:21 +0000826// Late Parsed Attributes:
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000827// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
828
829void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
830
831void Parser::LateParsedClass::ParseLexedAttributes() {
832 Self->ParseLexedAttributes(*Class);
833}
834
835void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000836 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000837}
838
839/// Wrapper class which calls ParseLexedAttribute, after setting up the
840/// scope appropriately.
841void Parser::ParseLexedAttributes(ParsingClass &Class) {
842 // Deal with templates
843 // FIXME: Test cases to make sure this does the right thing for templates.
844 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
845 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
846 HasTemplateScope);
847 if (HasTemplateScope)
848 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
849
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000850 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000851 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000852 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000853 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
854 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
855
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000856 // Enter the scope of nested classes
857 if (!AlreadyHasClassScope)
858 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
859 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000860 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000861 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
862 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
863 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000864 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000865
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000866 if (!AlreadyHasClassScope)
867 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
868 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000869}
870
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000871
872/// \brief Parse all attributes in LAs, and attach them to Decl D.
873void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
874 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +0000875 assert(LAs.parseSoon() &&
876 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000877 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +0000878 if (D)
879 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000880 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000881 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000882 }
883 LAs.clear();
884}
885
886
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000887/// \brief Finish parsing an attribute for which parsing was delayed.
888/// This will be called at the end of parsing a class declaration
889/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +0000890/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000891/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000892void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
893 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000894 // Save the current token position.
895 SourceLocation OrigLoc = Tok.getLocation();
896
897 // Append the current token at the end of the new token stream so that it
898 // doesn't get lost.
899 LA.Toks.push_back(Tok);
900 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
901 // Consume the previously pushed token.
902 ConsumeAnyToken();
903
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000904 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
905 Diag(Tok, diag::warn_attribute_on_function_definition)
906 << LA.AttrName.getName();
907 }
908
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000909 ParsedAttributes Attrs(AttrFactory);
910 SourceLocation endLoc;
911
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000912 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000913 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000914 NamedDecl *ND = dyn_cast<NamedDecl>(D);
915 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000916
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000917 // Allow 'this' within late-parsed attributes.
918 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
919 /*TypeQuals=*/0,
920 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000921
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000922 if (LA.Decls.size() == 1) {
923 // If the Decl is templatized, add template parameters to scope.
924 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
925 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
926 if (HasTemplateScope)
927 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000928
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000929 // If the Decl is on a function, add function parameters to the scope.
930 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
931 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
932 if (HasFunScope)
933 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000934
Michael Han6880f492012-10-03 01:56:22 +0000935 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000936 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000937
938 if (HasFunScope) {
939 Actions.ActOnExitFunctionContext();
940 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
941 }
942 if (HasTemplateScope) {
943 TempScope.Exit();
944 }
945 } else {
946 // If there are multiple decls, then the decl cannot be within the
947 // function scope.
Michael Han6880f492012-10-03 01:56:22 +0000948 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000949 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000950 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000951 } else {
952 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000953 }
954
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000955 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
956 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
957 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000958
959 if (Tok.getLocation() != OrigLoc) {
960 // Due to a parsing error, we either went over the cached tokens or
961 // there are still cached tokens left, so we skip the leftover tokens.
962 // Since this is an uncommon situation that should be avoided, use the
963 // expensive isBeforeInTranslationUnit call.
964 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
965 OrigLoc))
966 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000967 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000968 }
969}
970
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000971/// \brief Wrapper around a case statement checking if AttrName is
972/// one of the thread safety attributes
973bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
974 return llvm::StringSwitch<bool>(AttrName)
975 .Case("guarded_by", true)
976 .Case("guarded_var", true)
977 .Case("pt_guarded_by", true)
978 .Case("pt_guarded_var", true)
979 .Case("lockable", true)
980 .Case("scoped_lockable", true)
981 .Case("no_thread_safety_analysis", true)
982 .Case("acquired_after", true)
983 .Case("acquired_before", true)
984 .Case("exclusive_lock_function", true)
985 .Case("shared_lock_function", true)
986 .Case("exclusive_trylock_function", true)
987 .Case("shared_trylock_function", true)
988 .Case("unlock_function", true)
989 .Case("lock_returned", true)
990 .Case("locks_excluded", true)
991 .Case("exclusive_locks_required", true)
992 .Case("shared_locks_required", true)
993 .Default(false);
994}
995
996/// \brief Parse the contents of thread safety attributes. These
997/// should always be parsed as an expression list.
998///
999/// We need to special case the parsing due to the fact that if the first token
1000/// of the first argument is an identifier, the main parse loop will store
1001/// that token as a "parameter" and the rest of
1002/// the arguments will be added to a list of "arguments". However,
1003/// subsequent tokens in the first argument are lost. We instead parse each
1004/// argument as an expression and add all arguments to the list of "arguments".
1005/// In future, we will take advantage of this special case to also
1006/// deal with some argument scoping issues here (for example, referring to a
1007/// function parameter in the attribute on that function).
1008void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1009 SourceLocation AttrNameLoc,
1010 ParsedAttributes &Attrs,
1011 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001012 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001013
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001014 BalancedDelimiterTracker T(*this, tok::l_paren);
1015 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001016
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001017 ExprVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001018 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001019
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001020 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001021 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001022 ExprResult ArgExpr(ParseAssignmentExpression());
1023 if (ArgExpr.isInvalid()) {
1024 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001025 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001026 break;
1027 } else {
1028 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001029 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001030 if (Tok.isNot(tok::comma))
1031 break;
1032 ConsumeToken(); // Eat the comma, move to the next argument
1033 }
1034 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001035 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001036 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001037 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001038 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001039 if (EndLoc)
1040 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001041}
1042
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001043void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1044 SourceLocation AttrNameLoc,
1045 ParsedAttributes &Attrs,
1046 SourceLocation *EndLoc) {
1047 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1048
1049 BalancedDelimiterTracker T(*this, tok::l_paren);
1050 T.consumeOpen();
1051
1052 if (Tok.isNot(tok::identifier)) {
1053 Diag(Tok, diag::err_expected_ident);
1054 T.skipToEnd();
1055 return;
1056 }
1057 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1058 SourceLocation ArgumentKindLoc = ConsumeToken();
1059
1060 if (Tok.isNot(tok::comma)) {
1061 Diag(Tok, diag::err_expected_comma);
1062 T.skipToEnd();
1063 return;
1064 }
1065 ConsumeToken();
1066
1067 SourceRange MatchingCTypeRange;
1068 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1069 if (MatchingCType.isInvalid()) {
1070 T.skipToEnd();
1071 return;
1072 }
1073
1074 bool LayoutCompatible = false;
1075 bool MustBeNull = false;
1076 while (Tok.is(tok::comma)) {
1077 ConsumeToken();
1078 if (Tok.isNot(tok::identifier)) {
1079 Diag(Tok, diag::err_expected_ident);
1080 T.skipToEnd();
1081 return;
1082 }
1083 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1084 if (Flag->isStr("layout_compatible"))
1085 LayoutCompatible = true;
1086 else if (Flag->isStr("must_be_null"))
1087 MustBeNull = true;
1088 else {
1089 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1090 T.skipToEnd();
1091 return;
1092 }
1093 ConsumeToken(); // consume flag
1094 }
1095
1096 if (!T.consumeClose()) {
1097 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1098 ArgumentKind, ArgumentKindLoc,
1099 MatchingCType.release(), LayoutCompatible,
1100 MustBeNull, AttributeList::AS_GNU);
1101 }
1102
1103 if (EndLoc)
1104 *EndLoc = T.getCloseLocation();
1105}
1106
Richard Smith6ee326a2012-04-10 01:32:12 +00001107/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1108/// of a C++11 attribute-specifier in a location where an attribute is not
1109/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1110/// situation.
1111///
1112/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1113/// this doesn't appear to actually be an attribute-specifier, and the caller
1114/// should try to parse it.
1115bool Parser::DiagnoseProhibitedCXX11Attribute() {
1116 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1117
1118 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1119 case CAK_NotAttributeSpecifier:
1120 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1121 return false;
1122
1123 case CAK_InvalidAttributeSpecifier:
1124 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1125 return false;
1126
1127 case CAK_AttributeSpecifier:
1128 // Parse and discard the attributes.
1129 SourceLocation BeginLoc = ConsumeBracket();
1130 ConsumeBracket();
1131 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1132 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1133 SourceLocation EndLoc = ConsumeBracket();
1134 Diag(BeginLoc, diag::err_attributes_not_allowed)
1135 << SourceRange(BeginLoc, EndLoc);
1136 return true;
1137 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001138 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001139}
1140
John McCall7f040a92010-12-24 02:08:15 +00001141void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1142 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1143 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001144}
1145
Michael Hanf64231e2012-11-06 19:34:54 +00001146void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1147 AttributeList *AttrList = attrs.getList();
1148 while (AttrList) {
1149 if (AttrList->isCXX0XAttribute()) {
1150 Diag(AttrList->getLoc(), diag::warn_attribute_no_decl)
1151 << AttrList->getName();
1152 AttrList->setInvalid();
1153 }
1154 AttrList = AttrList->getNext();
1155 }
1156}
1157
Reid Spencer5f016e22007-07-11 17:01:13 +00001158/// ParseDeclaration - Parse a full 'declaration', which consists of
1159/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001160/// 'Context' should be a Declarator::TheContext value. This returns the
1161/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001162///
1163/// declaration: [C99 6.7]
1164/// block-declaration ->
1165/// simple-declaration
1166/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001167/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001168/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001169/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001170/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001171/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001172/// others... [FIXME]
1173///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001174Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1175 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001176 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001177 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001178 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001179 // Must temporarily exit the objective-c container scope for
1180 // parsing c none objective-c decls.
1181 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001182
John McCalld226f652010-08-21 09:40:31 +00001183 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001184 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001185 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001186 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001187 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001188 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001189 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001190 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001191 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001192 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001193 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001194 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001195 SourceLocation InlineLoc = ConsumeToken();
1196 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1197 break;
1198 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001199 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001200 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001201 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001202 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001203 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001204 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001205 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001206 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001207 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001208 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001209 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001210 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001211 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001212 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001213 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001214 default:
John McCall7f040a92010-12-24 02:08:15 +00001215 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001216 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001217
Chris Lattner682bf922009-03-29 16:50:03 +00001218 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001219 // single decl, convert it now. Alias declarations can also declare a type;
1220 // include that too if it is present.
1221 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001222}
1223
1224/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1225/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001226/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1227/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001228///[C90/C++]init-declarator-list ';' [TODO]
1229/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001230///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001231/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001232/// attribute-specifier-seq[opt] type-specifier-seq declarator
1233///
Chris Lattnercd147752009-03-29 17:27:48 +00001234/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001235/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001236///
1237/// If FRI is non-null, we might be parsing a for-range-declaration instead
1238/// of a simple-declaration. If we find that we are, we also parse the
1239/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001240Parser::DeclGroupPtrTy
1241Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1242 SourceLocation &DeclEnd,
1243 ParsedAttributesWithRange &attrs,
1244 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001246 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001247 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001248
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001249 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001250 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001251
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1253 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001254 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001255 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001256 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001257 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001258 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001259 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001260 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001262
1263 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001264}
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Richard Smith0706df42011-10-19 21:33:05 +00001266/// Returns true if this might be the start of a declarator, or a common typo
1267/// for a declarator.
1268bool Parser::MightBeDeclarator(unsigned Context) {
1269 switch (Tok.getKind()) {
1270 case tok::annot_cxxscope:
1271 case tok::annot_template_id:
1272 case tok::caret:
1273 case tok::code_completion:
1274 case tok::coloncolon:
1275 case tok::ellipsis:
1276 case tok::kw___attribute:
1277 case tok::kw_operator:
1278 case tok::l_paren:
1279 case tok::star:
1280 return true;
1281
1282 case tok::amp:
1283 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001284 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001285
Richard Smith1c94c162012-01-09 22:31:44 +00001286 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001287 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001288 NextToken().is(tok::l_square);
1289
1290 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001291 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001292
Richard Smith0706df42011-10-19 21:33:05 +00001293 case tok::identifier:
1294 switch (NextToken().getKind()) {
1295 case tok::code_completion:
1296 case tok::coloncolon:
1297 case tok::comma:
1298 case tok::equal:
1299 case tok::equalequal: // Might be a typo for '='.
1300 case tok::kw_alignas:
1301 case tok::kw_asm:
1302 case tok::kw___attribute:
1303 case tok::l_brace:
1304 case tok::l_paren:
1305 case tok::l_square:
1306 case tok::less:
1307 case tok::r_brace:
1308 case tok::r_paren:
1309 case tok::r_square:
1310 case tok::semi:
1311 return true;
1312
1313 case tok::colon:
1314 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001315 // and in block scope it's probably a label. Inside a class definition,
1316 // this is a bit-field.
1317 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001318 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001319
1320 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001321 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001322
1323 default:
1324 return false;
1325 }
1326
1327 default:
1328 return false;
1329 }
1330}
1331
Richard Smith994d73f2012-04-11 20:59:20 +00001332/// Skip until we reach something which seems like a sensible place to pick
1333/// up parsing after a malformed declaration. This will sometimes stop sooner
1334/// than SkipUntil(tok::r_brace) would, but will never stop later.
1335void Parser::SkipMalformedDecl() {
1336 while (true) {
1337 switch (Tok.getKind()) {
1338 case tok::l_brace:
1339 // Skip until matching }, then stop. We've probably skipped over
1340 // a malformed class or function definition or similar.
1341 ConsumeBrace();
1342 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1343 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1344 // This declaration isn't over yet. Keep skipping.
1345 continue;
1346 }
1347 if (Tok.is(tok::semi))
1348 ConsumeToken();
1349 return;
1350
1351 case tok::l_square:
1352 ConsumeBracket();
1353 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1354 continue;
1355
1356 case tok::l_paren:
1357 ConsumeParen();
1358 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1359 continue;
1360
1361 case tok::r_brace:
1362 return;
1363
1364 case tok::semi:
1365 ConsumeToken();
1366 return;
1367
1368 case tok::kw_inline:
1369 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001370 // a good place to pick back up parsing, except in an Objective-C
1371 // @interface context.
1372 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1373 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001374 return;
1375 break;
1376
1377 case tok::kw_namespace:
1378 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001379 // place to pick back up parsing, except in an Objective-C
1380 // @interface context.
1381 if (Tok.isAtStartOfLine() &&
1382 (!ParsingInObjCContainer || CurParsedObjCImpl))
1383 return;
1384 break;
1385
1386 case tok::at:
1387 // @end is very much like } in Objective-C contexts.
1388 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1389 ParsingInObjCContainer)
1390 return;
1391 break;
1392
1393 case tok::minus:
1394 case tok::plus:
1395 // - and + probably start new method declarations in Objective-C contexts.
1396 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001397 return;
1398 break;
1399
1400 case tok::eof:
1401 return;
1402
1403 default:
1404 break;
1405 }
1406
1407 ConsumeAnyToken();
1408 }
1409}
1410
John McCalld8ac0572009-11-03 19:26:08 +00001411/// ParseDeclGroup - Having concluded that this is either a function
1412/// definition or a group of object declarations, actually parse the
1413/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001414Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1415 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001416 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001417 SourceLocation *DeclEnd,
1418 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001419 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001420 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001421 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001422
John McCalld8ac0572009-11-03 19:26:08 +00001423 // Bail out if the first declarator didn't seem well-formed.
1424 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001425 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001426 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001427 }
Mike Stump1eb44332009-09-09 15:08:12 +00001428
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001429 // Save late-parsed attributes for now; they need to be parsed in the
1430 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001431 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1432 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001433 if (D.isFunctionDeclarator())
1434 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1435
Chris Lattnerc82daef2010-07-11 22:24:20 +00001436 // Check to see if we have a function *definition* which must have a body.
1437 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1438 // Look at the next token to make sure that this isn't a function
1439 // declaration. We have to check this because __attribute__ might be the
1440 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001441 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001442
Chris Lattner004659a2010-07-11 22:42:07 +00001443 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001444 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1445 Diag(Tok, diag::err_function_declared_typedef);
1446
1447 // Recover by treating the 'typedef' as spurious.
1448 DS.ClearStorageClassSpecs();
1449 }
1450
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001451 Decl *TheDecl =
1452 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001453 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001454 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001455
Chris Lattner004659a2010-07-11 22:42:07 +00001456 if (isDeclarationSpecifier()) {
1457 // If there is an invalid declaration specifier right after the function
1458 // prototype, then we must be in a missing semicolon case where this isn't
1459 // actually a body. Just fall through into the code that handles it as a
1460 // prototype, and let the top-level code handle the erroneous declspec
1461 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001462 } else {
1463 Diag(Tok, diag::err_expected_fn_body);
1464 SkipUntil(tok::semi);
1465 return DeclGroupPtrTy();
1466 }
1467 }
1468
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001469 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001470 return DeclGroupPtrTy();
1471
1472 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1473 // must parse and analyze the for-range-initializer before the declaration is
1474 // analyzed.
1475 if (FRI && Tok.is(tok::colon)) {
1476 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001477 if (Tok.is(tok::l_brace))
1478 FRI->RangeExpr = ParseBraceInitializer();
1479 else
1480 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001481 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1482 Actions.ActOnCXXForRangeDecl(ThisDecl);
1483 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001484 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001485 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1486 }
1487
Chris Lattner5f9e2722011-07-23 10:55:15 +00001488 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001489 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001490 if (LateParsedAttrs.size() > 0)
1491 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001492 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001493 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001494 DeclsInGroup.push_back(FirstDecl);
1495
Richard Smith0706df42011-10-19 21:33:05 +00001496 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001497
John McCalld8ac0572009-11-03 19:26:08 +00001498 // If we don't have a comma, it is either the end of the list (a ';') or an
1499 // error, bail out.
1500 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001501 SourceLocation CommaLoc = ConsumeToken();
1502
1503 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1504 // This comma was followed by a line-break and something which can't be
1505 // the start of a declarator. The comma was probably a typo for a
1506 // semicolon.
1507 Diag(CommaLoc, diag::err_expected_semi_declaration)
1508 << FixItHint::CreateReplacement(CommaLoc, ";");
1509 ExpectSemi = false;
1510 break;
1511 }
John McCalld8ac0572009-11-03 19:26:08 +00001512
1513 // Parse the next declarator.
1514 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001515 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001516
1517 // Accept attributes in an init-declarator. In the first declarator in a
1518 // declaration, these would be part of the declspec. In subsequent
1519 // declarators, they become part of the declarator itself, so that they
1520 // don't apply to declarators after *this* one. Examples:
1521 // short __attribute__((common)) var; -> declspec
1522 // short var __attribute__((common)); -> declarator
1523 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001524 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001525
1526 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001527 if (!D.isInvalidType()) {
1528 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1529 D.complete(ThisDecl);
1530 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001531 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001532 }
John McCalld8ac0572009-11-03 19:26:08 +00001533 }
1534
1535 if (DeclEnd)
1536 *DeclEnd = Tok.getLocation();
1537
Richard Smith0706df42011-10-19 21:33:05 +00001538 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001539 ExpectAndConsumeSemi(Context == Declarator::FileContext
1540 ? diag::err_invalid_token_after_toplevel_declarator
1541 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001542 // Okay, there was no semicolon and one was expected. If we see a
1543 // declaration specifier, just assume it was missing and continue parsing.
1544 // Otherwise things are very confused and we skip to recover.
1545 if (!isDeclarationSpecifier()) {
1546 SkipUntil(tok::r_brace, true, true);
1547 if (Tok.is(tok::semi))
1548 ConsumeToken();
1549 }
John McCalld8ac0572009-11-03 19:26:08 +00001550 }
1551
Douglas Gregor23c94db2010-07-02 17:43:08 +00001552 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001553 DeclsInGroup.data(),
1554 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001555}
1556
Richard Smithad762fc2011-04-14 22:09:26 +00001557/// Parse an optional simple-asm-expr and attributes, and attach them to a
1558/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001559bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001560 // If a simple-asm-expr is present, parse it.
1561 if (Tok.is(tok::kw_asm)) {
1562 SourceLocation Loc;
1563 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1564 if (AsmLabel.isInvalid()) {
1565 SkipUntil(tok::semi, true, true);
1566 return true;
1567 }
1568
1569 D.setAsmLabel(AsmLabel.release());
1570 D.SetRangeEnd(Loc);
1571 }
1572
1573 MaybeParseGNUAttributes(D);
1574 return false;
1575}
1576
Douglas Gregor1426e532009-05-12 21:31:51 +00001577/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1578/// declarator'. This method parses the remainder of the declaration
1579/// (including any attributes or initializer, among other things) and
1580/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001581///
Reid Spencer5f016e22007-07-11 17:01:13 +00001582/// init-declarator: [C99 6.7]
1583/// declarator
1584/// declarator '=' initializer
1585/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1586/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001587/// [C++] declarator initializer[opt]
1588///
1589/// [C++] initializer:
1590/// [C++] '=' initializer-clause
1591/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001592/// [C++0x] '=' 'default' [TODO]
1593/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001594/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001595///
1596/// According to the standard grammar, =default and =delete are function
1597/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001598///
John McCalld226f652010-08-21 09:40:31 +00001599Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001600 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001601 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001602 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Richard Smithad762fc2011-04-14 22:09:26 +00001604 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1605}
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Richard Smithad762fc2011-04-14 22:09:26 +00001607Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1608 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001609 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001610 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001611 switch (TemplateInfo.Kind) {
1612 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001613 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001614 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001615
Douglas Gregord5a423b2009-09-25 18:43:00 +00001616 case ParsedTemplateInfo::Template:
1617 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001618 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001619 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001620 D);
1621 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001622
Douglas Gregord5a423b2009-09-25 18:43:00 +00001623 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001624 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001625 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001626 TemplateInfo.ExternLoc,
1627 TemplateInfo.TemplateLoc,
1628 D);
1629 if (ThisRes.isInvalid()) {
1630 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001631 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001632 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001633
Douglas Gregord5a423b2009-09-25 18:43:00 +00001634 ThisDecl = ThisRes.get();
1635 break;
1636 }
1637 }
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Richard Smith34b41d92011-02-20 03:19:35 +00001639 bool TypeContainsAuto =
1640 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1641
Douglas Gregor1426e532009-05-12 21:31:51 +00001642 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001643 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001644 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001645 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001646 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001647 if (D.isFunctionDeclarator())
1648 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1649 << 1 /* delete */;
1650 else
1651 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001652 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001653 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001654 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1655 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001656 else
1657 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001658 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001659 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001660 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001661 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001662 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001663
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001664 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001665 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001666 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001667 cutOffParsing();
1668 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001669 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001670
John McCall60d7b3a2010-08-24 06:29:42 +00001671 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001672
David Blaikie4e4d0842012-03-11 07:00:24 +00001673 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001674 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001675 ExitScope();
1676 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001677
Douglas Gregor1426e532009-05-12 21:31:51 +00001678 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001679 SkipUntil(tok::comma, true, true);
1680 Actions.ActOnInitializerError(ThisDecl);
1681 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001682 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1683 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001684 }
1685 } else if (Tok.is(tok::l_paren)) {
1686 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001687 BalancedDelimiterTracker T(*this, tok::l_paren);
1688 T.consumeOpen();
1689
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001690 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001691 CommaLocsTy CommaLocs;
1692
David Blaikie4e4d0842012-03-11 07:00:24 +00001693 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001694 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001695 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001696 }
1697
Douglas Gregor1426e532009-05-12 21:31:51 +00001698 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001699 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001700 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001701
David Blaikie4e4d0842012-03-11 07:00:24 +00001702 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001703 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001704 ExitScope();
1705 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001706 } else {
1707 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001708 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001709
1710 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1711 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001712
David Blaikie4e4d0842012-03-11 07:00:24 +00001713 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001714 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001715 ExitScope();
1716 }
1717
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001718 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1719 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001720 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001721 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1722 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001723 }
Fariborz Jahanian3b5f9dc2012-07-03 22:54:28 +00001724 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001725 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001726 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001727 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1728
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001729 if (D.getCXXScopeSpec().isSet()) {
1730 EnterScope(0);
1731 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1732 }
1733
1734 ExprResult Init(ParseBraceInitializer());
1735
1736 if (D.getCXXScopeSpec().isSet()) {
1737 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1738 ExitScope();
1739 }
1740
1741 if (Init.isInvalid()) {
1742 Actions.ActOnInitializerError(ThisDecl);
1743 } else
1744 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1745 /*DirectInit=*/true, TypeContainsAuto);
1746
Douglas Gregor1426e532009-05-12 21:31:51 +00001747 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001748 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001749 }
1750
Richard Smith483b9f32011-02-21 20:05:19 +00001751 Actions.FinalizeDeclaration(ThisDecl);
1752
Douglas Gregor1426e532009-05-12 21:31:51 +00001753 return ThisDecl;
1754}
1755
Reid Spencer5f016e22007-07-11 17:01:13 +00001756/// ParseSpecifierQualifierList
1757/// specifier-qualifier-list:
1758/// type-specifier specifier-qualifier-list[opt]
1759/// type-qualifier specifier-qualifier-list[opt]
1760/// [GNU] attributes specifier-qualifier-list[opt]
1761///
Richard Smith69730c12012-03-12 07:56:15 +00001762void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1763 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001764 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1765 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001766 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001767 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Reid Spencer5f016e22007-07-11 17:01:13 +00001769 // Validate declspec for type-name.
1770 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001771 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1772 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001773 Diag(Tok, diag::err_expected_type);
1774 DS.SetTypeSpecError();
1775 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1776 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001778 if (!DS.hasTypeSpecifier())
1779 DS.SetTypeSpecError();
1780 }
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Reid Spencer5f016e22007-07-11 17:01:13 +00001782 // Issue diagnostic and remove storage class if present.
1783 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1784 if (DS.getStorageClassSpecLoc().isValid())
1785 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1786 else
1787 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1788 DS.ClearStorageClassSpecs();
1789 }
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Reid Spencer5f016e22007-07-11 17:01:13 +00001791 // Issue diagnostic and remove function specfier if present.
1792 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001793 if (DS.isInlineSpecified())
1794 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1795 if (DS.isVirtualSpecified())
1796 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1797 if (DS.isExplicitSpecified())
1798 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001799 DS.ClearFunctionSpecs();
1800 }
Richard Smith69730c12012-03-12 07:56:15 +00001801
1802 // Issue diagnostic and remove constexpr specfier if present.
1803 if (DS.isConstexprSpecified()) {
1804 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1805 DS.ClearConstexprSpec();
1806 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001807}
1808
Chris Lattnerc199ab32009-04-12 20:42:31 +00001809/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1810/// specified token is valid after the identifier in a declarator which
1811/// immediately follows the declspec. For example, these things are valid:
1812///
1813/// int x [ 4]; // direct-declarator
1814/// int x ( int y); // direct-declarator
1815/// int(int x ) // direct-declarator
1816/// int x ; // simple-declaration
1817/// int x = 17; // init-declarator-list
1818/// int x , y; // init-declarator-list
1819/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001820/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001821/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001822///
1823/// This is not, because 'x' does not immediately follow the declspec (though
1824/// ')' happens to be valid anyway).
1825/// int (x)
1826///
1827static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1828 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1829 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001830 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001831}
1832
Chris Lattnere40c2952009-04-14 21:34:55 +00001833
1834/// ParseImplicitInt - This method is called when we have an non-typename
1835/// identifier in a declspec (which normally terminates the decl spec) when
1836/// the declspec has no type specifier. In this case, the declspec is either
1837/// malformed or is "implicit int" (in K&R and C89).
1838///
1839/// This method handles diagnosing this prettily and returns false if the
1840/// declspec is done being processed. If it recovers and thinks there may be
1841/// other pieces of declspec after it, it returns true.
1842///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001843bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001844 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00001845 AccessSpecifier AS, DeclSpecContext DSC,
1846 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001847 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001848
Chris Lattnere40c2952009-04-14 21:34:55 +00001849 SourceLocation Loc = Tok.getLocation();
1850 // If we see an identifier that is not a type name, we normally would
1851 // parse it as the identifer being declared. However, when a typename
1852 // is typo'd or the definition is not included, this will incorrectly
1853 // parse the typename as the identifier name and fall over misparsing
1854 // later parts of the diagnostic.
1855 //
1856 // As such, we try to do some look-ahead in cases where this would
1857 // otherwise be an "implicit-int" case to see if this is invalid. For
1858 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1859 // an identifier with implicit int, we'd get a parse error because the
1860 // next token is obviously invalid for a type. Parse these as a case
1861 // with an invalid type specifier.
1862 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001863
Chris Lattnere40c2952009-04-14 21:34:55 +00001864 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001865 // error, do lookahead to try to do better recovery. This never applies
1866 // within a type specifier. Outside of C++, we allow this even if the
1867 // language doesn't "officially" support implicit int -- we support
1868 // implicit int as an extension in C99 and C11. Allegedly, MS also
1869 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001870 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001871 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001872 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001873 // If this token is valid for implicit int, e.g. "static x = 4", then
1874 // we just avoid eating the identifier, so it will be parsed as the
1875 // identifier in the declarator.
1876 return false;
1877 }
Mike Stump1eb44332009-09-09 15:08:12 +00001878
Richard Smith827adaf2012-05-15 21:01:51 +00001879 if (getLangOpts().CPlusPlus &&
1880 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1881 // Don't require a type specifier if we have the 'auto' storage class
1882 // specifier in C++98 -- we'll promote it to a type specifier.
1883 return false;
1884 }
1885
Chris Lattnere40c2952009-04-14 21:34:55 +00001886 // Otherwise, if we don't consume this token, we are going to emit an
1887 // error anyway. Try to recover from various common problems. Check
1888 // to see if this was a reference to a tag name without a tag specified.
1889 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001890 //
1891 // C++ doesn't need this, and isTagName doesn't take SS.
1892 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001893 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001894 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Douglas Gregor23c94db2010-07-02 17:43:08 +00001896 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001897 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001898 case DeclSpec::TST_enum:
1899 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1900 case DeclSpec::TST_union:
1901 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1902 case DeclSpec::TST_struct:
1903 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00001904 case DeclSpec::TST_interface:
1905 TagName="__interface"; FixitTagName = "__interface ";
1906 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001907 case DeclSpec::TST_class:
1908 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001909 }
Mike Stump1eb44332009-09-09 15:08:12 +00001910
Chris Lattnerf4382f52009-04-14 22:17:06 +00001911 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001912 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1913 LookupResult R(Actions, TokenName, SourceLocation(),
1914 Sema::LookupOrdinaryName);
1915
Chris Lattnerf4382f52009-04-14 22:17:06 +00001916 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001917 << TokenName << TagName << getLangOpts().CPlusPlus
1918 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1919
1920 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1921 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1922 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001923 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001924 << TokenName << TagName;
1925 }
Mike Stump1eb44332009-09-09 15:08:12 +00001926
Chris Lattnerf4382f52009-04-14 22:17:06 +00001927 // Parse this as a tag as if the missing tag were present.
1928 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001929 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001930 else
Richard Smith69730c12012-03-12 07:56:15 +00001931 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00001932 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001933 return true;
1934 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001935 }
Mike Stump1eb44332009-09-09 15:08:12 +00001936
Richard Smith8f0a7e72012-05-15 21:29:55 +00001937 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001938 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001939 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1940 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001941 // Look ahead to the next token to try to figure out what this declaration
1942 // was supposed to be.
1943 switch (NextToken().getKind()) {
1944 case tok::comma:
1945 case tok::equal:
1946 case tok::kw_asm:
1947 case tok::l_brace:
1948 case tok::l_square:
1949 case tok::semi:
1950 // This looks like a variable declaration. The type is probably missing.
1951 // We're done parsing decl-specifiers.
1952 return false;
1953
1954 case tok::l_paren: {
1955 // static x(4); // 'x' is not a type
1956 // x(int n); // 'x' is not a type
1957 // x (*p)[]; // 'x' is a type
1958 //
1959 // Since we're in an error case (or the rare 'implicit int in C++' MS
1960 // extension), we can afford to perform a tentative parse to determine
1961 // which case we're in.
1962 TentativeParsingAction PA(*this);
1963 ConsumeToken();
1964 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1965 PA.Revert();
1966 if (TPR == TPResult::False())
1967 return false;
1968 // The identifier is followed by a parenthesized declarator.
1969 // It's supposed to be a type.
1970 break;
1971 }
1972
1973 default:
1974 // This is probably supposed to be a type. This includes cases like:
1975 // int f(itn);
1976 // struct S { unsinged : 4; };
1977 break;
1978 }
1979 }
1980
Chad Rosier8decdee2012-06-26 22:30:43 +00001981 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00001982 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001983 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001984 IdentifierInfo *II = Tok.getIdentifierInfo();
1985 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001986 // The action emitted a diagnostic, so we don't have to.
1987 if (T) {
1988 // The action has suggested that the type T could be used. Set that as
1989 // the type in the declaration specifiers, consume the would-be type
1990 // name token, and we're done.
1991 const char *PrevSpec;
1992 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001993 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001994 DS.SetRangeEnd(Tok.getLocation());
1995 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001996 // There may be other declaration specifiers after this.
1997 return true;
1998 } else if (II != Tok.getIdentifierInfo()) {
1999 // If no type was suggested, the correction is to a keyword
2000 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002001 // There may be other declaration specifiers after this.
2002 return true;
2003 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002004
Douglas Gregora786fdb2009-10-13 23:27:22 +00002005 // Fall through; the action had no suggestion for us.
2006 } else {
2007 // The action did not emit a diagnostic, so emit one now.
2008 SourceRange R;
2009 if (SS) R = SS->getRange();
2010 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2011 }
Mike Stump1eb44332009-09-09 15:08:12 +00002012
Douglas Gregora786fdb2009-10-13 23:27:22 +00002013 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002014 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002015 DS.SetRangeEnd(Tok.getLocation());
2016 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Chris Lattnere40c2952009-04-14 21:34:55 +00002018 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2019 // avoid rippling error messages on subsequent uses of the same type,
2020 // could be useful if #include was forgotten.
2021 return false;
2022}
2023
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002024/// \brief Determine the declaration specifier context from the declarator
2025/// context.
2026///
2027/// \param Context the declarator context, which is one of the
2028/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002029Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002030Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2031 if (Context == Declarator::MemberContext)
2032 return DSC_class;
2033 if (Context == Declarator::FileContext)
2034 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002035 if (Context == Declarator::TrailingReturnContext)
2036 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002037 return DSC_normal;
2038}
2039
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002040/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2041///
2042/// FIXME: Simply returns an alignof() expression if the argument is a
2043/// type. Ideally, the type should be propagated directly into Sema.
2044///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002045/// [C11] type-id
2046/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002047/// [C++0x] type-id ...[opt]
2048/// [C++0x] assignment-expression ...[opt]
2049ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2050 SourceLocation &EllipsisLoc) {
2051 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002052 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002053 SourceLocation TypeLoc = Tok.getLocation();
2054 ParsedType Ty = ParseTypeName().get();
2055 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002056 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2057 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002058 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002059 ER = ParseConstantExpression();
2060
David Blaikie4e4d0842012-03-11 07:00:24 +00002061 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002062 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002063
2064 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002065}
2066
2067/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2068/// attribute to Attrs.
2069///
2070/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002071/// [C11] '_Alignas' '(' type-id ')'
2072/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002073/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
2074/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002075void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2076 SourceLocation *endLoc) {
2077 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2078 "Not an alignment-specifier!");
2079
2080 SourceLocation KWLoc = Tok.getLocation();
2081 ConsumeToken();
2082
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002083 BalancedDelimiterTracker T(*this, tok::l_paren);
2084 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002085 return;
2086
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002087 SourceLocation EllipsisLoc;
2088 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002089 if (ArgExpr.isInvalid()) {
2090 SkipUntil(tok::r_paren);
2091 return;
2092 }
2093
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002094 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002095 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002096 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002097
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002098 // FIXME: Handle pack-expansions here.
2099 if (EllipsisLoc.isValid()) {
2100 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2101 return;
2102 }
2103
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002104 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002105 ArgExprs.push_back(ArgExpr.release());
Sean Hunt8e083e72012-06-19 23:57:03 +00002106 // FIXME: This should not be GNU, but we since the attribute used is
2107 // based on the spelling, and there is no true spelling for
2108 // C++11 attributes, this isn't accepted.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002109 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002110 0, T.getOpenLocation(), ArgExprs.data(), 1,
Sean Hunt8e083e72012-06-19 23:57:03 +00002111 AttributeList::AS_GNU);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002112}
2113
Reid Spencer5f016e22007-07-11 17:01:13 +00002114/// ParseDeclarationSpecifiers
2115/// declaration-specifiers: [C99 6.7]
2116/// storage-class-specifier declaration-specifiers[opt]
2117/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002118/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002119/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002120/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002121/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002122///
2123/// storage-class-specifier: [C99 6.7.1]
2124/// 'typedef'
2125/// 'extern'
2126/// 'static'
2127/// 'auto'
2128/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002129/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002130/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002131/// function-specifier: [C99 6.7.4]
2132/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002133/// [C++] 'virtual'
2134/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002135/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002136/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002137/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002138
Reid Spencer5f016e22007-07-11 17:01:13 +00002139///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002140void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002141 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002142 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002143 DeclSpecContext DSContext,
2144 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002145 if (DS.getSourceRange().isInvalid()) {
2146 DS.SetRangeStart(Tok.getLocation());
2147 DS.SetRangeEnd(Tok.getLocation());
2148 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002149
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002150 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002151 bool AttrsLastTime = false;
2152 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002153 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002154 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002155 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002156 unsigned DiagID = 0;
2157
Reid Spencer5f016e22007-07-11 17:01:13 +00002158 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002159
Reid Spencer5f016e22007-07-11 17:01:13 +00002160 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002161 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002162 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002163 if (!AttrsLastTime)
2164 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002165 else {
2166 // Reject C++11 attributes that appertain to decl specifiers as
2167 // we don't support any C++11 attributes that appertain to decl
2168 // specifiers. This also conforms to what g++ 4.8 is doing.
2169 ProhibitCXX11Attributes(attrs);
2170
Sean Hunt2edf0a22012-06-23 05:07:58 +00002171 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002172 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002173
Reid Spencer5f016e22007-07-11 17:01:13 +00002174 // If this is not a declaration specifier token, we're done reading decl
2175 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002176 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002177 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002178
Sean Hunt2edf0a22012-06-23 05:07:58 +00002179 case tok::l_square:
2180 case tok::kw_alignas:
2181 if (!isCXX11AttributeSpecifier())
2182 goto DoneWithDeclSpec;
2183
2184 ProhibitAttributes(attrs);
2185 // FIXME: It would be good to recover by accepting the attributes,
2186 // but attempting to do that now would cause serious
2187 // madness in terms of diagnostics.
2188 attrs.clear();
2189 attrs.Range = SourceRange();
2190
2191 ParseCXX11Attributes(attrs);
2192 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002193 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002194
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002195 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002196 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002197 if (DS.hasTypeSpecifier()) {
2198 bool AllowNonIdentifiers
2199 = (getCurScope()->getFlags() & (Scope::ControlScope |
2200 Scope::BlockScope |
2201 Scope::TemplateParamScope |
2202 Scope::FunctionPrototypeScope |
2203 Scope::AtCatchScope)) == 0;
2204 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002205 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002206 (DSContext == DSC_class && DS.isFriendSpecified());
2207
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002208 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002209 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002210 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002211 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002212 }
2213
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002214 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2215 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2216 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002217 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002218 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002219 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002220 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002221 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002222 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002223
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002224 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002225 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002226 }
2227
Chris Lattner5e02c472009-01-05 00:07:25 +00002228 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002229 // C++ scope specifier. Annotate and loop, or bail out on error.
2230 if (TryAnnotateCXXScopeToken(true)) {
2231 if (!DS.hasTypeSpecifier())
2232 DS.SetTypeSpecError();
2233 goto DoneWithDeclSpec;
2234 }
John McCall2e0a7152010-03-01 18:20:46 +00002235 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2236 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002237 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002238
2239 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002240 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002241 goto DoneWithDeclSpec;
2242
John McCallaa87d332009-12-12 11:40:51 +00002243 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002244 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2245 Tok.getAnnotationRange(),
2246 SS);
John McCallaa87d332009-12-12 11:40:51 +00002247
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002248 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002249 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002250 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002251 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002252 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002253 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002254
2255 // C++ [class.qual]p2:
2256 // In a lookup in which the constructor is an acceptable lookup
2257 // result and the nested-name-specifier nominates a class C:
2258 //
2259 // - if the name specified after the
2260 // nested-name-specifier, when looked up in C, is the
2261 // injected-class-name of C (Clause 9), or
2262 //
2263 // - if the name specified after the nested-name-specifier
2264 // is the same as the identifier or the
2265 // simple-template-id's template-name in the last
2266 // component of the nested-name-specifier,
2267 //
2268 // the name is instead considered to name the constructor of
2269 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002270 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002271 // Thus, if the template-name is actually the constructor
2272 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002273 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002274 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002275 if ((DSContext == DSC_top_level ||
2276 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2277 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002278 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002279 if (isConstructorDeclarator()) {
2280 // The user meant this to be an out-of-line constructor
2281 // definition, but template arguments are not allowed
2282 // there. Just allow this as a constructor; we'll
2283 // complain about it later.
2284 goto DoneWithDeclSpec;
2285 }
2286
2287 // The user meant this to name a type, but it actually names
2288 // a constructor with some extraneous template
2289 // arguments. Complain, then parse it as a type as the user
2290 // intended.
2291 Diag(TemplateId->TemplateNameLoc,
2292 diag::err_out_of_line_template_id_names_constructor)
2293 << TemplateId->Name;
2294 }
2295
John McCallaa87d332009-12-12 11:40:51 +00002296 DS.getTypeSpecScope() = SS;
2297 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002298 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002299 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002300 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002301 continue;
2302 }
2303
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002304 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002305 DS.getTypeSpecScope() = SS;
2306 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002307 if (Tok.getAnnotationValue()) {
2308 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002309 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002310 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002311 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002312 if (isInvalid)
2313 break;
John McCallb3d87482010-08-24 05:47:05 +00002314 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002315 else
2316 DS.SetTypeSpecError();
2317 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2318 ConsumeToken(); // The typename
2319 }
2320
Douglas Gregor9135c722009-03-25 15:40:00 +00002321 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002322 goto DoneWithDeclSpec;
2323
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002324 // If we're in a context where the identifier could be a class name,
2325 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002326 if ((DSContext == DSC_top_level ||
2327 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002328 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002329 &SS)) {
2330 if (isConstructorDeclarator())
2331 goto DoneWithDeclSpec;
2332
2333 // As noted in C++ [class.qual]p2 (cited above), when the name
2334 // of the class is qualified in a context where it could name
2335 // a constructor, its a constructor name. However, we've
2336 // looked at the declarator, and the user probably meant this
2337 // to be a type. Complain that it isn't supposed to be treated
2338 // as a type, then proceed to parse it as a type.
2339 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2340 << Next.getIdentifierInfo();
2341 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002342
John McCallb3d87482010-08-24 05:47:05 +00002343 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2344 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002345 getCurScope(), &SS,
2346 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002347 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002348 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002349
Chris Lattnerf4382f52009-04-14 22:17:06 +00002350 // If the referenced identifier is not a type, then this declspec is
2351 // erroneous: We already checked about that it has no type specifier, and
2352 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002353 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002354 if (TypeRep == 0) {
2355 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002356 ParsedAttributesWithRange Attrs(AttrFactory);
2357 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2358 if (!Attrs.empty()) {
2359 AttrsLastTime = true;
2360 attrs.takeAllFrom(Attrs);
2361 }
2362 continue;
2363 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002364 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002365 }
Mike Stump1eb44332009-09-09 15:08:12 +00002366
John McCallaa87d332009-12-12 11:40:51 +00002367 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002368 ConsumeToken(); // The C++ scope.
2369
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002370 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002371 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002372 if (isInvalid)
2373 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002374
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002375 DS.SetRangeEnd(Tok.getLocation());
2376 ConsumeToken(); // The typename.
2377
2378 continue;
2379 }
Mike Stump1eb44332009-09-09 15:08:12 +00002380
Chris Lattner80d0c892009-01-21 19:48:37 +00002381 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002382 if (Tok.getAnnotationValue()) {
2383 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002384 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002385 DiagID, T);
2386 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002387 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002388
Chris Lattner5c5db552010-04-05 18:18:31 +00002389 if (isInvalid)
2390 break;
2391
Chris Lattner80d0c892009-01-21 19:48:37 +00002392 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2393 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002394
Chris Lattner80d0c892009-01-21 19:48:37 +00002395 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2396 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002397 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002398 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002399 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002400
Chris Lattner80d0c892009-01-21 19:48:37 +00002401 continue;
2402 }
Mike Stump1eb44332009-09-09 15:08:12 +00002403
Douglas Gregorbfad9152011-04-28 15:48:45 +00002404 case tok::kw___is_signed:
2405 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2406 // typically treats it as a trait. If we see __is_signed as it appears
2407 // in libstdc++, e.g.,
2408 //
2409 // static const bool __is_signed;
2410 //
2411 // then treat __is_signed as an identifier rather than as a keyword.
2412 if (DS.getTypeSpecType() == TST_bool &&
2413 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2414 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2415 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2416 Tok.setKind(tok::identifier);
2417 }
2418
2419 // We're done with the declaration-specifiers.
2420 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002421
Chris Lattner3bd934a2008-07-26 01:18:38 +00002422 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002423 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002424 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002425 // In C++, check to see if this is a scope specifier like foo::bar::, if
2426 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002427 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002428 if (TryAnnotateCXXScopeToken(true)) {
2429 if (!DS.hasTypeSpecifier())
2430 DS.SetTypeSpecError();
2431 goto DoneWithDeclSpec;
2432 }
2433 if (!Tok.is(tok::identifier))
2434 continue;
2435 }
Mike Stump1eb44332009-09-09 15:08:12 +00002436
Chris Lattner3bd934a2008-07-26 01:18:38 +00002437 // This identifier can only be a typedef name if we haven't already seen
2438 // a type-specifier. Without this check we misparse:
2439 // typedef int X; struct Y { short X; }; as 'short int'.
2440 if (DS.hasTypeSpecifier())
2441 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002442
John Thompson82287d12010-02-05 00:12:22 +00002443 // Check for need to substitute AltiVec keyword tokens.
2444 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2445 break;
2446
Richard Smithf63eee72012-05-09 18:56:43 +00002447 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2448 // allow the use of a typedef name as a type specifier.
2449 if (DS.isTypeAltiVecVector())
2450 goto DoneWithDeclSpec;
2451
John McCallb3d87482010-08-24 05:47:05 +00002452 ParsedType TypeRep =
2453 Actions.getTypeName(*Tok.getIdentifierInfo(),
2454 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002455
Chris Lattnerc199ab32009-04-12 20:42:31 +00002456 // If this is not a typedef name, don't parse it as part of the declspec,
2457 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002458 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002459 ParsedAttributesWithRange Attrs(AttrFactory);
2460 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2461 if (!Attrs.empty()) {
2462 AttrsLastTime = true;
2463 attrs.takeAllFrom(Attrs);
2464 }
2465 continue;
2466 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002467 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002468 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002469
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002470 // If we're in a context where the identifier could be a class name,
2471 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002472 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002473 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002474 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002475 goto DoneWithDeclSpec;
2476
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002477 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002478 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002479 if (isInvalid)
2480 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002481
Chris Lattner3bd934a2008-07-26 01:18:38 +00002482 DS.SetRangeEnd(Tok.getLocation());
2483 ConsumeToken(); // The identifier
2484
2485 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2486 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002487 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002488 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002489 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002490
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002491 // Need to support trailing type qualifiers (e.g. "id<p> const").
2492 // If a type specifier follows, it will be diagnosed elsewhere.
2493 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002494 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002495
2496 // type-name
2497 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002498 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002499 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002500 // This template-id does not refer to a type name, so we're
2501 // done with the type-specifiers.
2502 goto DoneWithDeclSpec;
2503 }
2504
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002505 // If we're in a context where the template-id could be a
2506 // constructor name or specialization, check whether this is a
2507 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002508 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002509 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002510 isConstructorDeclarator())
2511 goto DoneWithDeclSpec;
2512
Douglas Gregor39a8de12009-02-25 19:37:18 +00002513 // Turn the template-id annotation token into a type annotation
2514 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002515 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002516 continue;
2517 }
2518
Reid Spencer5f016e22007-07-11 17:01:13 +00002519 // GNU attributes support.
2520 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002521 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002522 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002523
2524 // Microsoft declspec support.
2525 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002526 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002527 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002528
Steve Naroff239f0732008-12-25 14:16:32 +00002529 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002530 case tok::kw___forceinline: {
2531 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2532 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002533 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002534 // FIXME: This does not work correctly if it is set to be a declspec
2535 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002536 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002537 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002538 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002539 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002540
2541 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002542 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002543 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002544 case tok::kw___cdecl:
2545 case tok::kw___stdcall:
2546 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002547 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002548 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002549 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002550 continue;
2551
Dawn Perchik52fc3142010-09-03 01:29:35 +00002552 // Borland single token adornments.
2553 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002554 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002555 continue;
2556
Peter Collingbournef315fa82011-02-14 01:42:53 +00002557 // OpenCL single token adornments.
2558 case tok::kw___kernel:
2559 ParseOpenCLAttributes(DS.getAttributes());
2560 continue;
2561
Reid Spencer5f016e22007-07-11 17:01:13 +00002562 // storage-class-specifier
2563 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002564 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2565 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002566 break;
2567 case tok::kw_extern:
2568 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002569 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002570 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2571 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002572 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002573 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002574 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2575 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002576 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002577 case tok::kw_static:
2578 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002579 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002580 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2581 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002582 break;
2583 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002584 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002585 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002586 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2587 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002588 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002589 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002590 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002591 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002592 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2593 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002594 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002595 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2596 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002597 break;
2598 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002599 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2600 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002601 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002602 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002603 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2604 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002605 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002606 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002607 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002608 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002609
Reid Spencer5f016e22007-07-11 17:01:13 +00002610 // function-specifier
2611 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002612 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002613 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002614 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002615 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002616 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002617 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002618 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002619 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002620
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002621 // alignment-specifier
2622 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002623 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002624 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002625 ParseAlignmentSpecifier(DS.getAttributes());
2626 continue;
2627
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002628 // friend
2629 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002630 if (DSContext == DSC_class)
2631 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2632 else {
2633 PrevSpec = ""; // not actually used by the diagnostic
2634 DiagID = diag::err_friend_invalid_in_context;
2635 isInvalid = true;
2636 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002637 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002638
Douglas Gregor8d267c52011-09-09 02:06:17 +00002639 // Modules
2640 case tok::kw___module_private__:
2641 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2642 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002643
Sebastian Redl2ac67232009-11-05 15:47:02 +00002644 // constexpr
2645 case tok::kw_constexpr:
2646 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2647 break;
2648
Chris Lattner80d0c892009-01-21 19:48:37 +00002649 // type-specifier
2650 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002651 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2652 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002653 break;
2654 case tok::kw_long:
2655 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002656 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2657 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002658 else
John McCallfec54012009-08-03 20:12:06 +00002659 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2660 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002661 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002662 case tok::kw___int64:
2663 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2664 DiagID);
2665 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002666 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002667 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2668 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002669 break;
2670 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002671 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2672 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002673 break;
2674 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002675 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2676 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002677 break;
2678 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002679 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2680 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002681 break;
2682 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002683 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2684 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002685 break;
2686 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002687 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2688 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002689 break;
2690 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002691 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2692 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002693 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002694 case tok::kw___int128:
2695 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2696 DiagID);
2697 break;
2698 case tok::kw_half:
2699 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2700 DiagID);
2701 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002702 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002703 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2704 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002705 break;
2706 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002707 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2708 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002709 break;
2710 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002711 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2712 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002713 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002714 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002715 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2716 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002717 break;
2718 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002719 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2720 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002721 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002722 case tok::kw_bool:
2723 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002724 if (Tok.is(tok::kw_bool) &&
2725 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2726 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2727 PrevSpec = ""; // Not used by the diagnostic.
2728 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002729 // For better error recovery.
2730 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002731 isInvalid = true;
2732 } else {
2733 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2734 DiagID);
2735 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002736 break;
2737 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002738 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2739 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002740 break;
2741 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002742 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2743 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002744 break;
2745 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002746 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2747 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002748 break;
John Thompson82287d12010-02-05 00:12:22 +00002749 case tok::kw___vector:
2750 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2751 break;
2752 case tok::kw___pixel:
2753 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2754 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00002755 case tok::kw_image1d_t:
2756 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2757 PrevSpec, DiagID);
2758 break;
2759 case tok::kw_image1d_array_t:
2760 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2761 PrevSpec, DiagID);
2762 break;
2763 case tok::kw_image1d_buffer_t:
2764 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2765 PrevSpec, DiagID);
2766 break;
2767 case tok::kw_image2d_t:
2768 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2769 PrevSpec, DiagID);
2770 break;
2771 case tok::kw_image2d_array_t:
2772 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2773 PrevSpec, DiagID);
2774 break;
2775 case tok::kw_image3d_t:
2776 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2777 PrevSpec, DiagID);
2778 break;
John McCalla5fc4722011-04-09 22:50:59 +00002779 case tok::kw___unknown_anytype:
2780 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2781 PrevSpec, DiagID);
2782 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002783
2784 // class-specifier:
2785 case tok::kw_class:
2786 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00002787 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00002788 case tok::kw_union: {
2789 tok::TokenKind Kind = Tok.getKind();
2790 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00002791
2792 // These are attributes following class specifiers.
2793 // To produce better diagnostic, we parse them when
2794 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002795 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00002796 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002797 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002798
2799 // If there are attributes following class specifier,
2800 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002801 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00002802 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00002803 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002804 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002805 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002806 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002807
2808 // enum-specifier:
2809 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002810 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002811 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002812 continue;
2813
2814 // cv-qualifier:
2815 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002816 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002817 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002818 break;
2819 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002820 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002821 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002822 break;
2823 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002824 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002825 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002826 break;
2827
Douglas Gregord57959a2009-03-27 23:10:48 +00002828 // C++ typename-specifier:
2829 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002830 if (TryAnnotateTypeOrScopeToken()) {
2831 DS.SetTypeSpecError();
2832 goto DoneWithDeclSpec;
2833 }
2834 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002835 continue;
2836 break;
2837
Chris Lattner80d0c892009-01-21 19:48:37 +00002838 // GNU typeof support.
2839 case tok::kw_typeof:
2840 ParseTypeofSpecifier(DS);
2841 continue;
2842
David Blaikie42d6d0c2011-12-04 05:04:18 +00002843 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002844 ParseDecltypeSpecifier(DS);
2845 continue;
2846
Sean Huntdb5d44b2011-05-19 05:37:45 +00002847 case tok::kw___underlying_type:
2848 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002849 continue;
2850
2851 case tok::kw__Atomic:
2852 ParseAtomicSpecifier(DS);
2853 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002854
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002855 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00002856 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002857 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002858 goto DoneWithDeclSpec;
2859 case tok::kw___private:
2860 case tok::kw___global:
2861 case tok::kw___local:
2862 case tok::kw___constant:
2863 case tok::kw___read_only:
2864 case tok::kw___write_only:
2865 case tok::kw___read_write:
2866 ParseOpenCLQualifiers(DS);
2867 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002868
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002869 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002870 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002871 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2872 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002873 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002874 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002875
Douglas Gregor46f936e2010-11-19 17:10:50 +00002876 if (!ParseObjCProtocolQualifiers(DS))
2877 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2878 << FixItHint::CreateInsertion(Loc, "id")
2879 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00002880
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002881 // Need to support trailing type qualifiers (e.g. "id<p> const").
2882 // If a type specifier follows, it will be diagnosed elsewhere.
2883 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002884 }
John McCallfec54012009-08-03 20:12:06 +00002885 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002886 if (isInvalid) {
2887 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002888 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00002889
Douglas Gregorae2fb142010-08-23 14:34:43 +00002890 if (DiagID == diag::ext_duplicate_declspec)
2891 Diag(Tok, DiagID)
2892 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2893 else
2894 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002895 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002896
Chris Lattner81c018d2008-03-13 06:29:04 +00002897 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002898 if (DiagID != diag::err_bool_redeclaration)
2899 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002900
2901 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002902 }
2903}
Douglas Gregoradcac882008-12-01 23:54:00 +00002904
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002905/// ParseStructDeclaration - Parse a struct declaration without the terminating
2906/// semicolon.
2907///
Reid Spencer5f016e22007-07-11 17:01:13 +00002908/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002909/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002910/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002911/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002912/// struct-declarator-list:
2913/// struct-declarator
2914/// struct-declarator-list ',' struct-declarator
2915/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2916/// struct-declarator:
2917/// declarator
2918/// [GNU] declarator attributes[opt]
2919/// declarator[opt] ':' constant-expression
2920/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2921///
Chris Lattnere1359422008-04-10 06:46:29 +00002922void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002923ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00002924
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002925 if (Tok.is(tok::kw___extension__)) {
2926 // __extension__ silences extension warnings in the subexpression.
2927 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002928 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002929 return ParseStructDeclaration(DS, Fields);
2930 }
Mike Stump1eb44332009-09-09 15:08:12 +00002931
Steve Naroff28a7ca82007-08-20 22:28:22 +00002932 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002933 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002934
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002935 // If there are no declarators, this is a free-standing declaration
2936 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002937 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002938 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2939 DS);
2940 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002941 return;
2942 }
2943
2944 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002945 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002946 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002947 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002948 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00002949 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002950
Bill Wendlingad017fa2012-12-20 19:22:21 +00002951 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002952 if (!FirstDeclarator)
2953 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002954
Steve Naroff28a7ca82007-08-20 22:28:22 +00002955 /// struct-declarator: declarator
2956 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002957 if (Tok.isNot(tok::colon)) {
2958 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2959 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002960 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002961 }
Mike Stump1eb44332009-09-09 15:08:12 +00002962
Chris Lattner04d66662007-10-09 17:33:22 +00002963 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002964 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002965 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002966 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002967 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002968 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002969 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002970 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002971
Steve Naroff28a7ca82007-08-20 22:28:22 +00002972 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002973 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002974
John McCallbdd563e2009-11-03 02:38:08 +00002975 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00002976 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00002977
Steve Naroff28a7ca82007-08-20 22:28:22 +00002978 // If we don't have a comma, it is either the end of the list (a ';')
2979 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002980 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002981 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002982
Steve Naroff28a7ca82007-08-20 22:28:22 +00002983 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002984 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002985
John McCallbdd563e2009-11-03 02:38:08 +00002986 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002987 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002988}
2989
2990/// ParseStructUnionBody
2991/// struct-contents:
2992/// struct-declaration-list
2993/// [EXT] empty
2994/// [GNU] "struct-declaration-list" without terminatoring ';'
2995/// struct-declaration-list:
2996/// struct-declaration
2997/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002998/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002999///
Reid Spencer5f016e22007-07-11 17:01:13 +00003000void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003001 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003002 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3003 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00003004
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003005 BalancedDelimiterTracker T(*this, tok::l_brace);
3006 if (T.consumeOpen())
3007 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003008
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003009 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003010 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003011
Reid Spencer5f016e22007-07-11 17:01:13 +00003012 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
3013 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00003014 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00003015 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3016 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3017 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003018
Chris Lattner5f9e2722011-07-23 10:55:15 +00003019 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003020
Reid Spencer5f016e22007-07-11 17:01:13 +00003021 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003022 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003023 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003024
Reid Spencer5f016e22007-07-11 17:01:13 +00003025 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003026 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003027 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003028 continue;
3029 }
Chris Lattnere1359422008-04-10 06:46:29 +00003030
John McCallbdd563e2009-11-03 02:38:08 +00003031 if (!Tok.is(tok::at)) {
3032 struct CFieldCallback : FieldCallback {
3033 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003034 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003035 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003036
John McCalld226f652010-08-21 09:40:31 +00003037 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003038 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003039 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3040
Eli Friedmandcdff462012-08-08 23:53:27 +00003041 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003042 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003043 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003044 FD.D.getDeclSpec().getSourceRange().getBegin(),
3045 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003046 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003047 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003048 }
John McCallbdd563e2009-11-03 02:38:08 +00003049 } Callback(*this, TagDecl, FieldDecls);
3050
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003051 // Parse all the comma separated declarators.
3052 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003053 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003054 } else { // Handle @defs
3055 ConsumeToken();
3056 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3057 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003058 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003059 continue;
3060 }
3061 ConsumeToken();
3062 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3063 if (!Tok.is(tok::identifier)) {
3064 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003065 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003066 continue;
3067 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003068 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003069 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003070 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003071 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3072 ConsumeToken();
3073 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003074 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003075
Chris Lattner04d66662007-10-09 17:33:22 +00003076 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003077 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003078 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003079 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003080 break;
3081 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003082 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3083 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003084 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003085 // If we stopped at a ';', eat it.
3086 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003087 }
3088 }
Mike Stump1eb44332009-09-09 15:08:12 +00003089
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003090 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003091
John McCall0b7e6782011-03-24 11:26:52 +00003092 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003093 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003094 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003095
Douglas Gregor23c94db2010-07-02 17:43:08 +00003096 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003097 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003098 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003099 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003100 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003101 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3102 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003103}
3104
Reid Spencer5f016e22007-07-11 17:01:13 +00003105/// ParseEnumSpecifier
3106/// enum-specifier: [C99 6.7.2.2]
3107/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003108///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003109/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3110/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003111/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3112/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003113/// 'enum' identifier
3114/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003115///
Richard Smith1af83c42012-03-23 03:33:32 +00003116/// [C++11] enum-head '{' enumerator-list[opt] '}'
3117/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003118///
Richard Smith1af83c42012-03-23 03:33:32 +00003119/// enum-head: [C++11]
3120/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3121/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3122/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003123///
Richard Smith1af83c42012-03-23 03:33:32 +00003124/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003125/// 'enum'
3126/// 'enum' 'class'
3127/// 'enum' 'struct'
3128///
Richard Smith1af83c42012-03-23 03:33:32 +00003129/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003130/// ':' type-specifier-seq
3131///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003132/// [C++] elaborated-type-specifier:
3133/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3134///
Chris Lattner4c97d762009-04-12 21:49:30 +00003135void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003136 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003137 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003138 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003139 if (Tok.is(tok::code_completion)) {
3140 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003141 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003142 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003143 }
John McCall57c13002011-07-06 05:58:41 +00003144
Sean Hunt2edf0a22012-06-23 05:07:58 +00003145 // If attributes exist after tag, parse them.
3146 ParsedAttributesWithRange attrs(AttrFactory);
3147 MaybeParseGNUAttributes(attrs);
3148 MaybeParseCXX0XAttributes(attrs);
3149
3150 // If declspecs exist after tag, parse them.
3151 while (Tok.is(tok::kw___declspec))
3152 ParseMicrosoftDeclSpec(attrs);
3153
Richard Smithbdad7a22012-01-10 01:33:14 +00003154 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003155 bool IsScopedUsingClassTag = false;
3156
John McCall1e12b3d2012-06-23 22:30:04 +00003157 // In C++11, recognize 'enum class' and 'enum struct'.
David Blaikie4e4d0842012-03-11 07:00:24 +00003158 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00003159 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00003160 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003161 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003162 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003163
Bill Wendlingad017fa2012-12-20 19:22:21 +00003164 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003165 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003166 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003167
3168 // They are allowed afterwards, though.
3169 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003170 MaybeParseCXX0XAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003171 while (Tok.is(tok::kw___declspec))
3172 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003173 }
Richard Smith1af83c42012-03-23 03:33:32 +00003174
John McCall13489672012-05-07 06:16:58 +00003175 // C++11 [temp.explicit]p12:
3176 // The usual access controls do not apply to names used to specify
3177 // explicit instantiations.
3178 // We extend this to also cover explicit specializations. Note that
3179 // we don't suppress if this turns out to be an elaborated type
3180 // specifier.
3181 bool shouldDelayDiagsInTag =
3182 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3183 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3184 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003185
Richard Smith7796eb52012-03-12 08:56:40 +00003186 // Enum definitions should not be parsed in a trailing-return-type.
3187 bool AllowDeclaration = DSC != DSC_trailing;
3188
3189 bool AllowFixedUnderlyingType = AllowDeclaration &&
3190 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
3191 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003192
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003193 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003194 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003195 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3196 // if a fixed underlying type is allowed.
3197 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003198
3199 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003200 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00003201 return;
3202
3203 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003204 Diag(Tok, diag::err_expected_ident);
3205 if (Tok.isNot(tok::l_brace)) {
3206 // Has no name and is not a definition.
3207 // Skip the rest of this declarator, up until the comma or semicolon.
3208 SkipUntil(tok::comma, true);
3209 return;
3210 }
3211 }
3212 }
Mike Stump1eb44332009-09-09 15:08:12 +00003213
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003214 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003215 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003216 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003217 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003218
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003219 // Skip the rest of this declarator, up until the comma or semicolon.
3220 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003221 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003222 }
Mike Stump1eb44332009-09-09 15:08:12 +00003223
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003224 // If an identifier is present, consume and remember it.
3225 IdentifierInfo *Name = 0;
3226 SourceLocation NameLoc;
3227 if (Tok.is(tok::identifier)) {
3228 Name = Tok.getIdentifierInfo();
3229 NameLoc = ConsumeToken();
3230 }
Mike Stump1eb44332009-09-09 15:08:12 +00003231
Richard Smithbdad7a22012-01-10 01:33:14 +00003232 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003233 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3234 // declaration of a scoped enumeration.
3235 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003236 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003237 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003238 }
3239
John McCall13489672012-05-07 06:16:58 +00003240 // Okay, end the suppression area. We'll decide whether to emit the
3241 // diagnostics in a second.
3242 if (shouldDelayDiagsInTag)
3243 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003244
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003245 TypeResult BaseType;
3246
Douglas Gregora61b3e72010-12-01 17:42:47 +00003247 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003248 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003249 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003250 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003251 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003252 // If we're in class scope, this can either be an enum declaration with
3253 // an underlying type, or a declaration of a bitfield member. We try to
3254 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003255 // (integer literal, sizeof); if it's still ambiguous, we then consider
3256 // anything that's a simple-type-specifier followed by '(' as an
3257 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003258 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003259 EnterExpressionEvaluationContext Unevaluated(Actions,
3260 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003261 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003262 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003263 // bit-field. This is the common case.
3264 if (TPR == TPResult::True())
3265 PossibleBitfield = true;
3266 // If the next token starts a type-specifier-seq, it may be either a
3267 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003268 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003269 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003270 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003271 GetLookAheadToken(2).getKind() == tok::semi) {
3272 // Consume the ':'.
3273 ConsumeToken();
3274 } else {
3275 // We have the start of a type-specifier-seq, so we have to perform
3276 // tentative parsing to determine whether we have an expression or a
3277 // type.
3278 TentativeParsingAction TPA(*this);
3279
3280 // Consume the ':'.
3281 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003282
3283 // If we see a type specifier followed by an open-brace, we have an
3284 // ambiguity between an underlying type and a C++11 braced
3285 // function-style cast. Resolve this by always treating it as an
3286 // underlying type.
3287 // FIXME: The standard is not entirely clear on how to disambiguate in
3288 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003289 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003290 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003291 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003292 // We'll parse this as a bitfield later.
3293 PossibleBitfield = true;
3294 TPA.Revert();
3295 } else {
3296 // We have a type-specifier-seq.
3297 TPA.Commit();
3298 }
3299 }
3300 } else {
3301 // Consume the ':'.
3302 ConsumeToken();
3303 }
3304
3305 if (!PossibleBitfield) {
3306 SourceRange Range;
3307 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003308
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003309 if (getLangOpts().CPlusPlus0x) {
Richard Smith7fe62082011-10-15 05:09:34 +00003310 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003311 } else if (!getLangOpts().ObjC2) {
3312 if (getLangOpts().CPlusPlus)
3313 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3314 else
3315 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3316 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003317 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003318 }
3319
Richard Smithbdad7a22012-01-10 01:33:14 +00003320 // There are four options here. If we have 'friend enum foo;' then this is a
3321 // friend declaration, and cannot have an accompanying definition. If we have
3322 // 'enum foo;', then this is a forward declaration. If we have
3323 // 'enum foo {...' then this is a definition. Otherwise we have something
3324 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003325 //
3326 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3327 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3328 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3329 //
John McCallf312b1e2010-08-26 23:41:50 +00003330 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003331 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003332 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003333 } else if (Tok.is(tok::l_brace)) {
3334 if (DS.isFriendSpecified()) {
3335 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3336 << SourceRange(DS.getFriendSpecLoc());
3337 ConsumeBrace();
3338 SkipUntil(tok::r_brace);
3339 TUK = Sema::TUK_Friend;
3340 } else {
3341 TUK = Sema::TUK_Definition;
3342 }
Richard Smithc9f35172012-06-25 21:37:02 +00003343 } else if (DSC != DSC_type_specifier &&
3344 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003345 (Tok.isAtStartOfLine() &&
3346 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003347 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3348 if (Tok.isNot(tok::semi)) {
3349 // A semicolon was missing after this declaration. Diagnose and recover.
3350 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3351 "enum");
3352 PP.EnterToken(Tok);
3353 Tok.setKind(tok::semi);
3354 }
John McCall13489672012-05-07 06:16:58 +00003355 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003356 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003357 }
3358
3359 // If this is an elaborated type specifier, and we delayed
3360 // diagnostics before, just merge them into the current pool.
3361 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3362 diagsFromTag.redelay();
3363 }
Richard Smith1af83c42012-03-23 03:33:32 +00003364
3365 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003366 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003367 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003368 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3369 // Skip the rest of this declarator, up until the comma or semicolon.
3370 Diag(Tok, diag::err_enum_template);
3371 SkipUntil(tok::comma, true);
3372 return;
3373 }
3374
3375 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3376 // Enumerations can't be explicitly instantiated.
3377 DS.SetTypeSpecError();
3378 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3379 return;
3380 }
3381
3382 assert(TemplateInfo.TemplateParams && "no template parameters");
3383 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3384 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003385 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003386
Sean Hunt2edf0a22012-06-23 05:07:58 +00003387 if (TUK == Sema::TUK_Reference)
3388 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003389
Douglas Gregorb9075602011-02-22 02:55:24 +00003390 if (!Name && TUK != Sema::TUK_Definition) {
3391 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003392
Douglas Gregorb9075602011-02-22 02:55:24 +00003393 // Skip the rest of this declarator, up until the comma or semicolon.
3394 SkipUntil(tok::comma, true);
3395 return;
3396 }
Richard Smith1af83c42012-03-23 03:33:32 +00003397
Douglas Gregor402abb52009-05-28 23:31:59 +00003398 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003399 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003400 const char *PrevSpec = 0;
3401 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003402 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003403 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003404 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003405 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003406 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003407
Douglas Gregor48c89f42010-04-24 16:38:41 +00003408 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003409 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003410 // dependent tag.
3411 if (!Name) {
3412 DS.SetTypeSpecError();
3413 Diag(Tok, diag::err_expected_type_name_after_typename);
3414 return;
3415 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003416
Douglas Gregor23c94db2010-07-02 17:43:08 +00003417 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003418 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003419 NameLoc);
3420 if (Type.isInvalid()) {
3421 DS.SetTypeSpecError();
3422 return;
3423 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003424
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003425 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3426 NameLoc.isValid() ? NameLoc : StartLoc,
3427 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003428 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003429
Douglas Gregor48c89f42010-04-24 16:38:41 +00003430 return;
3431 }
Mike Stump1eb44332009-09-09 15:08:12 +00003432
John McCalld226f652010-08-21 09:40:31 +00003433 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003434 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003435 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003436 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003437 ConsumeBrace();
3438 SkipUntil(tok::r_brace);
3439 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003440
Douglas Gregor48c89f42010-04-24 16:38:41 +00003441 DS.SetTypeSpecError();
3442 return;
3443 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003444
Richard Smithc9f35172012-06-25 21:37:02 +00003445 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003446 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003447
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003448 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3449 NameLoc.isValid() ? NameLoc : StartLoc,
3450 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003451 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003452}
3453
3454/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3455/// enumerator-list:
3456/// enumerator
3457/// enumerator-list ',' enumerator
3458/// enumerator:
3459/// enumeration-constant
3460/// enumeration-constant '=' constant-expression
3461/// enumeration-constant:
3462/// identifier
3463///
John McCalld226f652010-08-21 09:40:31 +00003464void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003465 // Enter the scope of the enum body and start the definition.
3466 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003467 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003468
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003469 BalancedDelimiterTracker T(*this, tok::l_brace);
3470 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003471
Chris Lattner7946dd32007-08-27 17:24:30 +00003472 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003473 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003474 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003475
Chris Lattner5f9e2722011-07-23 10:55:15 +00003476 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003477
John McCalld226f652010-08-21 09:40:31 +00003478 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003479
Reid Spencer5f016e22007-07-11 17:01:13 +00003480 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003481 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003482 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3483 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003484
John McCall5b629aa2010-10-22 23:36:17 +00003485 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003486 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003487 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003488 MaybeParseCXX0XAttributes(attrs);
3489 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003490
Reid Spencer5f016e22007-07-11 17:01:13 +00003491 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003492 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003493 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003494
Chris Lattner04d66662007-10-09 17:33:22 +00003495 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003496 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003497 AssignedVal = ParseConstantExpression();
3498 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003499 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003500 }
Mike Stump1eb44332009-09-09 15:08:12 +00003501
Reid Spencer5f016e22007-07-11 17:01:13 +00003502 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003503 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3504 LastEnumConstDecl,
3505 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003506 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003507 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003508 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003509
Reid Spencer5f016e22007-07-11 17:01:13 +00003510 EnumConstantDecls.push_back(EnumConstDecl);
3511 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003512
Douglas Gregor751f6922010-09-07 14:51:08 +00003513 if (Tok.is(tok::identifier)) {
3514 // We're missing a comma between enumerators.
3515 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003516 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003517 << FixItHint::CreateInsertion(Loc, ", ");
3518 continue;
3519 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003520
Chris Lattner04d66662007-10-09 17:33:22 +00003521 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003522 break;
3523 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003524
Richard Smith7fe62082011-10-15 05:09:34 +00003525 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003526 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003527 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3528 diag::ext_enumerator_list_comma_cxx :
3529 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003530 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003531 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003532 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3533 << FixItHint::CreateRemoval(CommaLoc);
3534 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003535 }
Mike Stump1eb44332009-09-09 15:08:12 +00003536
Reid Spencer5f016e22007-07-11 17:01:13 +00003537 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003538 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003539
Reid Spencer5f016e22007-07-11 17:01:13 +00003540 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003541 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003542 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003543
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003544 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3545 EnumDecl, EnumConstantDecls.data(),
3546 EnumConstantDecls.size(), getCurScope(),
3547 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003548
Douglas Gregor72de6672009-01-08 20:45:30 +00003549 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003550 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3551 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003552
3553 // The next token must be valid after an enum definition. If not, a ';'
3554 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003555 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3556 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003557 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3558 // Push this token back into the preprocessor and change our current token
3559 // to ';' so that the rest of the code recovers as though there were an
3560 // ';' after the definition.
3561 PP.EnterToken(Tok);
3562 Tok.setKind(tok::semi);
3563 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003564}
3565
3566/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003567/// start of a type-qualifier-list.
3568bool Parser::isTypeQualifier() const {
3569 switch (Tok.getKind()) {
3570 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003571
3572 // type-qualifier only in OpenCL
3573 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003574 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003575
Steve Naroff5f8aa692008-02-11 23:15:56 +00003576 // type-qualifier
3577 case tok::kw_const:
3578 case tok::kw_volatile:
3579 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003580 case tok::kw___private:
3581 case tok::kw___local:
3582 case tok::kw___global:
3583 case tok::kw___constant:
3584 case tok::kw___read_only:
3585 case tok::kw___read_write:
3586 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003587 return true;
3588 }
3589}
3590
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003591/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3592/// is definitely a type-specifier. Return false if it isn't part of a type
3593/// specifier or if we're not sure.
3594bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3595 switch (Tok.getKind()) {
3596 default: return false;
3597 // type-specifiers
3598 case tok::kw_short:
3599 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003600 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003601 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003602 case tok::kw_signed:
3603 case tok::kw_unsigned:
3604 case tok::kw__Complex:
3605 case tok::kw__Imaginary:
3606 case tok::kw_void:
3607 case tok::kw_char:
3608 case tok::kw_wchar_t:
3609 case tok::kw_char16_t:
3610 case tok::kw_char32_t:
3611 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003612 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003613 case tok::kw_float:
3614 case tok::kw_double:
3615 case tok::kw_bool:
3616 case tok::kw__Bool:
3617 case tok::kw__Decimal32:
3618 case tok::kw__Decimal64:
3619 case tok::kw__Decimal128:
3620 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003621
Guy Benyeib13621d2012-12-18 14:38:23 +00003622 // OpenCL specific types:
3623 case tok::kw_image1d_t:
3624 case tok::kw_image1d_array_t:
3625 case tok::kw_image1d_buffer_t:
3626 case tok::kw_image2d_t:
3627 case tok::kw_image2d_array_t:
3628 case tok::kw_image3d_t:
3629
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003630 // struct-or-union-specifier (C99) or class-specifier (C++)
3631 case tok::kw_class:
3632 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003633 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003634 case tok::kw_union:
3635 // enum-specifier
3636 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003637
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003638 // typedef-name
3639 case tok::annot_typename:
3640 return true;
3641 }
3642}
3643
Steve Naroff5f8aa692008-02-11 23:15:56 +00003644/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003645/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003646bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003647 switch (Tok.getKind()) {
3648 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003649
Chris Lattner166a8fc2009-01-04 23:41:41 +00003650 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003651 if (TryAltiVecVectorToken())
3652 return true;
3653 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003654 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003655 // Annotate typenames and C++ scope specifiers. If we get one, just
3656 // recurse to handle whatever we get.
3657 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003658 return true;
3659 if (Tok.is(tok::identifier))
3660 return false;
3661 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003662
Chris Lattner166a8fc2009-01-04 23:41:41 +00003663 case tok::coloncolon: // ::foo::bar
3664 if (NextToken().is(tok::kw_new) || // ::new
3665 NextToken().is(tok::kw_delete)) // ::delete
3666 return false;
3667
Chris Lattner166a8fc2009-01-04 23:41:41 +00003668 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003669 return true;
3670 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003671
Reid Spencer5f016e22007-07-11 17:01:13 +00003672 // GNU attributes support.
3673 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003674 // GNU typeof support.
3675 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003676
Reid Spencer5f016e22007-07-11 17:01:13 +00003677 // type-specifiers
3678 case tok::kw_short:
3679 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003680 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003681 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003682 case tok::kw_signed:
3683 case tok::kw_unsigned:
3684 case tok::kw__Complex:
3685 case tok::kw__Imaginary:
3686 case tok::kw_void:
3687 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003688 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003689 case tok::kw_char16_t:
3690 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003691 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003692 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003693 case tok::kw_float:
3694 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003695 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003696 case tok::kw__Bool:
3697 case tok::kw__Decimal32:
3698 case tok::kw__Decimal64:
3699 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003700 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003701
Guy Benyeib13621d2012-12-18 14:38:23 +00003702 // OpenCL specific types:
3703 case tok::kw_image1d_t:
3704 case tok::kw_image1d_array_t:
3705 case tok::kw_image1d_buffer_t:
3706 case tok::kw_image2d_t:
3707 case tok::kw_image2d_array_t:
3708 case tok::kw_image3d_t:
3709
Chris Lattner99dc9142008-04-13 18:59:07 +00003710 // struct-or-union-specifier (C99) or class-specifier (C++)
3711 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003712 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003713 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003714 case tok::kw_union:
3715 // enum-specifier
3716 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003717
Reid Spencer5f016e22007-07-11 17:01:13 +00003718 // type-qualifier
3719 case tok::kw_const:
3720 case tok::kw_volatile:
3721 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003722
John McCallb8a8de32012-11-14 00:49:39 +00003723 // Debugger support.
3724 case tok::kw___unknown_anytype:
3725
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003726 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003727 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003728 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003729
Chris Lattner7c186be2008-10-20 00:25:30 +00003730 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3731 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003732 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003733
Steve Naroff239f0732008-12-25 14:16:32 +00003734 case tok::kw___cdecl:
3735 case tok::kw___stdcall:
3736 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003737 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003738 case tok::kw___w64:
3739 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003740 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003741 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003742 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003743
3744 case tok::kw___private:
3745 case tok::kw___local:
3746 case tok::kw___global:
3747 case tok::kw___constant:
3748 case tok::kw___read_only:
3749 case tok::kw___read_write:
3750 case tok::kw___write_only:
3751
Eli Friedman290eeb02009-06-08 23:27:34 +00003752 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003753
3754 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003755 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003756
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003757 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003758 case tok::kw__Atomic:
3759 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003760 }
3761}
3762
3763/// isDeclarationSpecifier() - Return true if the current token is part of a
3764/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003765///
3766/// \param DisambiguatingWithExpression True to indicate that the purpose of
3767/// this check is to disambiguate between an expression and a declaration.
3768bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003769 switch (Tok.getKind()) {
3770 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003771
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003772 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003773 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003774
Chris Lattner166a8fc2009-01-04 23:41:41 +00003775 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003776 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003777 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003778 return false;
John Thompson82287d12010-02-05 00:12:22 +00003779 if (TryAltiVecVectorToken())
3780 return true;
3781 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003782 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003783 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003784 // Annotate typenames and C++ scope specifiers. If we get one, just
3785 // recurse to handle whatever we get.
3786 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003787 return true;
3788 if (Tok.is(tok::identifier))
3789 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003790
Douglas Gregor9497a732010-09-16 01:51:54 +00003791 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00003792 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00003793 // expression is permitted, then this is probably a class message send
3794 // missing the initial '['. In this case, we won't consider this to be
3795 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00003796 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00003797 isStartOfObjCClassMessageMissingOpenBracket())
3798 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003799
John McCall9ba61662010-02-26 08:45:28 +00003800 return isDeclarationSpecifier();
3801
Chris Lattner166a8fc2009-01-04 23:41:41 +00003802 case tok::coloncolon: // ::foo::bar
3803 if (NextToken().is(tok::kw_new) || // ::new
3804 NextToken().is(tok::kw_delete)) // ::delete
3805 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003806
Chris Lattner166a8fc2009-01-04 23:41:41 +00003807 // Annotate typenames and C++ scope specifiers. If we get one, just
3808 // recurse to handle whatever we get.
3809 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003810 return true;
3811 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003812
Reid Spencer5f016e22007-07-11 17:01:13 +00003813 // storage-class-specifier
3814 case tok::kw_typedef:
3815 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003816 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003817 case tok::kw_static:
3818 case tok::kw_auto:
3819 case tok::kw_register:
3820 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003821
Douglas Gregor8d267c52011-09-09 02:06:17 +00003822 // Modules
3823 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00003824
John McCallb8a8de32012-11-14 00:49:39 +00003825 // Debugger support
3826 case tok::kw___unknown_anytype:
3827
Reid Spencer5f016e22007-07-11 17:01:13 +00003828 // type-specifiers
3829 case tok::kw_short:
3830 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003831 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003832 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003833 case tok::kw_signed:
3834 case tok::kw_unsigned:
3835 case tok::kw__Complex:
3836 case tok::kw__Imaginary:
3837 case tok::kw_void:
3838 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003839 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003840 case tok::kw_char16_t:
3841 case tok::kw_char32_t:
3842
Reid Spencer5f016e22007-07-11 17:01:13 +00003843 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003844 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003845 case tok::kw_float:
3846 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003847 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003848 case tok::kw__Bool:
3849 case tok::kw__Decimal32:
3850 case tok::kw__Decimal64:
3851 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003852 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003853
Guy Benyeib13621d2012-12-18 14:38:23 +00003854 // OpenCL specific types:
3855 case tok::kw_image1d_t:
3856 case tok::kw_image1d_array_t:
3857 case tok::kw_image1d_buffer_t:
3858 case tok::kw_image2d_t:
3859 case tok::kw_image2d_array_t:
3860 case tok::kw_image3d_t:
3861
Chris Lattner99dc9142008-04-13 18:59:07 +00003862 // struct-or-union-specifier (C99) or class-specifier (C++)
3863 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003864 case tok::kw_struct:
3865 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00003866 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003867 // enum-specifier
3868 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003869
Reid Spencer5f016e22007-07-11 17:01:13 +00003870 // type-qualifier
3871 case tok::kw_const:
3872 case tok::kw_volatile:
3873 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003874
Reid Spencer5f016e22007-07-11 17:01:13 +00003875 // function-specifier
3876 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003877 case tok::kw_virtual:
3878 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003879
Richard Smith53aec2a2012-10-25 00:00:53 +00003880 // friend keyword.
3881 case tok::kw_friend:
3882
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003883 // static_assert-declaration
3884 case tok::kw__Static_assert:
3885
Chris Lattner1ef08762007-08-09 17:01:07 +00003886 // GNU typeof support.
3887 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003888
Chris Lattner1ef08762007-08-09 17:01:07 +00003889 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003890 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00003891
Richard Smith53aec2a2012-10-25 00:00:53 +00003892 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003893 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00003894 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00003895
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003896 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003897 case tok::kw__Atomic:
3898 return true;
3899
Chris Lattnerf3948c42008-07-26 03:38:44 +00003900 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3901 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003902 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003903
Douglas Gregord9d75e52011-04-27 05:41:15 +00003904 // typedef-name
3905 case tok::annot_typename:
3906 return !DisambiguatingWithExpression ||
3907 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00003908
Steve Naroff47f52092009-01-06 19:34:12 +00003909 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003910 case tok::kw___cdecl:
3911 case tok::kw___stdcall:
3912 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003913 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003914 case tok::kw___w64:
3915 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003916 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003917 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003918 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003919 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003920
3921 case tok::kw___private:
3922 case tok::kw___local:
3923 case tok::kw___global:
3924 case tok::kw___constant:
3925 case tok::kw___read_only:
3926 case tok::kw___read_write:
3927 case tok::kw___write_only:
3928
Eli Friedman290eeb02009-06-08 23:27:34 +00003929 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003930 }
3931}
3932
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003933bool Parser::isConstructorDeclarator() {
3934 TentativeParsingAction TPA(*this);
3935
3936 // Parse the C++ scope specifier.
3937 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00003938 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003939 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003940 TPA.Revert();
3941 return false;
3942 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003943
3944 // Parse the constructor name.
3945 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3946 // We already know that we have a constructor name; just consume
3947 // the token.
3948 ConsumeToken();
3949 } else {
3950 TPA.Revert();
3951 return false;
3952 }
3953
Richard Smith22592862012-03-27 23:05:05 +00003954 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003955 if (Tok.isNot(tok::l_paren)) {
3956 TPA.Revert();
3957 return false;
3958 }
3959 ConsumeParen();
3960
Richard Smith22592862012-03-27 23:05:05 +00003961 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3962 // that we have a constructor.
3963 if (Tok.is(tok::r_paren) ||
3964 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003965 TPA.Revert();
3966 return true;
3967 }
3968
3969 // If we need to, enter the specified scope.
3970 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003971 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003972 DeclScopeObj.EnterDeclaratorScope();
3973
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003974 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003975 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003976 MaybeParseMicrosoftAttributes(Attrs);
3977
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003978 // Check whether the next token(s) are part of a declaration
3979 // specifier, in which case we have the start of a parameter and,
3980 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003981 bool IsConstructor = false;
3982 if (isDeclarationSpecifier())
3983 IsConstructor = true;
3984 else if (Tok.is(tok::identifier) ||
3985 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3986 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3987 // This might be a parenthesized member name, but is more likely to
3988 // be a constructor declaration with an invalid argument type. Keep
3989 // looking.
3990 if (Tok.is(tok::annot_cxxscope))
3991 ConsumeToken();
3992 ConsumeToken();
3993
3994 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003995 // which must have one of the following syntactic forms (see the
3996 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003997 switch (Tok.getKind()) {
3998 case tok::l_paren:
3999 // C(X ( int));
4000 case tok::l_square:
4001 // C(X [ 5]);
4002 // C(X [ [attribute]]);
4003 case tok::coloncolon:
4004 // C(X :: Y);
4005 // C(X :: *p);
4006 case tok::r_paren:
4007 // C(X )
4008 // Assume this isn't a constructor, rather than assuming it's a
4009 // constructor with an unnamed parameter of an ill-formed type.
4010 break;
4011
4012 default:
4013 IsConstructor = true;
4014 break;
4015 }
4016 }
4017
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004018 TPA.Revert();
4019 return IsConstructor;
4020}
Reid Spencer5f016e22007-07-11 17:01:13 +00004021
4022/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004023/// type-qualifier-list: [C99 6.7.5]
4024/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004025/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004026/// [ only if VendorAttributesAllowed=true ]
4027/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004028/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004029/// [ only if VendorAttributesAllowed=true ]
4030/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
4031/// [ only if CXX0XAttributesAllowed=true ]
4032/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004033///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004034void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4035 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00004036 bool CXX11AttributesAllowed) {
4037 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004038 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004039 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004040 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004041 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004042 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004043
4044 SourceLocation EndLoc;
4045
Reid Spencer5f016e22007-07-11 17:01:13 +00004046 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004047 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004048 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004049 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004050 SourceLocation Loc = Tok.getLocation();
4051
4052 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004053 case tok::code_completion:
4054 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004055 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004056
Reid Spencer5f016e22007-07-11 17:01:13 +00004057 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004058 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004059 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004060 break;
4061 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004062 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004063 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004064 break;
4065 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004066 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004067 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004068 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004069
4070 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004071 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004072 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004073 goto DoneWithTypeQuals;
4074 case tok::kw___private:
4075 case tok::kw___global:
4076 case tok::kw___local:
4077 case tok::kw___constant:
4078 case tok::kw___read_only:
4079 case tok::kw___write_only:
4080 case tok::kw___read_write:
4081 ParseOpenCLQualifiers(DS);
4082 break;
4083
Eli Friedman290eeb02009-06-08 23:27:34 +00004084 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004085 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004086 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004087 case tok::kw___cdecl:
4088 case tok::kw___stdcall:
4089 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004090 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004091 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004092 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004093 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004094 continue;
4095 }
4096 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004097 case tok::kw___pascal:
4098 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004099 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004100 continue;
4101 }
4102 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004103 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004104 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004105 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004106 continue; // do *not* consume the next token!
4107 }
4108 // otherwise, FALL THROUGH!
4109 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004110 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004111 // If this is not a type-qualifier token, we're done reading type
4112 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004113 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004114 if (EndLoc.isValid())
4115 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004116 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004117 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004118
Reid Spencer5f016e22007-07-11 17:01:13 +00004119 // If the specifier combination wasn't legal, issue a diagnostic.
4120 if (isInvalid) {
4121 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004122 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004123 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004124 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004125 }
4126}
4127
4128
4129/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4130///
4131void Parser::ParseDeclarator(Declarator &D) {
4132 /// This implements the 'declarator' production in the C grammar, then checks
4133 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004134 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004135}
4136
Richard Smith9988f282012-03-29 01:16:42 +00004137static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4138 if (Kind == tok::star || Kind == tok::caret)
4139 return true;
4140
4141 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4142 if (!Lang.CPlusPlus)
4143 return false;
4144
4145 return Kind == tok::amp || Kind == tok::ampamp;
4146}
4147
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004148/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4149/// is parsed by the function passed to it. Pass null, and the direct-declarator
4150/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004151/// ptr-operator production.
4152///
Richard Smith0706df42011-10-19 21:33:05 +00004153/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004154/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4155/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004156///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004157/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4158/// [C] pointer[opt] direct-declarator
4159/// [C++] direct-declarator
4160/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004161///
4162/// pointer: [C99 6.7.5]
4163/// '*' type-qualifier-list[opt]
4164/// '*' type-qualifier-list[opt] pointer
4165///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004166/// ptr-operator:
4167/// '*' cv-qualifier-seq[opt]
4168/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004169/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004170/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004171/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004172/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004173void Parser::ParseDeclaratorInternal(Declarator &D,
4174 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004175 if (Diags.hasAllExtensionsSilenced())
4176 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004177
Sebastian Redlf30208a2009-01-24 21:16:55 +00004178 // C++ member pointers start with a '::' or a nested-name.
4179 // Member pointers get special handling, since there's no place for the
4180 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004181 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004182 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4183 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004184 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4185 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004186 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004187 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004188
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004189 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004190 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004191 // The scope spec really belongs to the direct-declarator.
4192 D.getCXXScopeSpec() = SS;
4193 if (DirectDeclParser)
4194 (this->*DirectDeclParser)(D);
4195 return;
4196 }
4197
4198 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004199 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004200 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004201 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004202 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004203
4204 // Recurse to parse whatever is left.
4205 ParseDeclaratorInternal(D, DirectDeclParser);
4206
4207 // Sema will have to catch (syntactically invalid) pointers into global
4208 // scope. It has to catch pointers into namespace scope anyway.
4209 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004210 Loc),
4211 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004212 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004213 return;
4214 }
4215 }
4216
4217 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004218 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004219 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004220 if (DirectDeclParser)
4221 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004222 return;
4223 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004224
Sebastian Redl05532f22009-03-15 22:02:01 +00004225 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4226 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004227 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004228 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004229
Chris Lattner9af55002009-03-27 04:18:06 +00004230 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004231 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004232 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004233
Richard Smith6ee326a2012-04-10 01:32:12 +00004234 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004235 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004236 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004237
Reid Spencer5f016e22007-07-11 17:01:13 +00004238 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004239 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004240 if (Kind == tok::star)
4241 // Remember that we parsed a pointer type, and remember the type-quals.
4242 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004243 DS.getConstSpecLoc(),
4244 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004245 DS.getRestrictSpecLoc()),
4246 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004247 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004248 else
4249 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004250 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004251 Loc),
4252 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004253 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004254 } else {
4255 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004256 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004257
Sebastian Redl743de1f2009-03-23 00:00:23 +00004258 // Complain about rvalue references in C++03, but then go on and build
4259 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004260 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00004261 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004262 diag::warn_cxx98_compat_rvalue_reference :
4263 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004264
Richard Smith6ee326a2012-04-10 01:32:12 +00004265 // GNU-style and C++11 attributes are allowed here, as is restrict.
4266 ParseTypeQualifierListOpt(DS);
4267 D.ExtendWithDeclSpec(DS);
4268
Reid Spencer5f016e22007-07-11 17:01:13 +00004269 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4270 // cv-qualifiers are introduced through the use of a typedef or of a
4271 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004272 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4273 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4274 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004275 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004276 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4277 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004278 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00004279 }
4280
4281 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004282 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004283
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004284 if (D.getNumTypeObjects() > 0) {
4285 // C++ [dcl.ref]p4: There shall be no references to references.
4286 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4287 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004288 if (const IdentifierInfo *II = D.getIdentifier())
4289 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4290 << II;
4291 else
4292 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4293 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004294
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004295 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004296 // can go ahead and build the (technically ill-formed)
4297 // declarator: reference collapsing will take care of it.
4298 }
4299 }
4300
Reid Spencer5f016e22007-07-11 17:01:13 +00004301 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004302 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004303 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004304 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004305 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004306 }
4307}
4308
Richard Smith9988f282012-03-29 01:16:42 +00004309static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4310 SourceLocation EllipsisLoc) {
4311 if (EllipsisLoc.isValid()) {
4312 FixItHint Insertion;
4313 if (!D.getEllipsisLoc().isValid()) {
4314 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4315 D.setEllipsisLoc(EllipsisLoc);
4316 }
4317 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4318 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4319 }
4320}
4321
Reid Spencer5f016e22007-07-11 17:01:13 +00004322/// ParseDirectDeclarator
4323/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004324/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004325/// '(' declarator ')'
4326/// [GNU] '(' attributes declarator ')'
4327/// [C90] direct-declarator '[' constant-expression[opt] ']'
4328/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4329/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4330/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4331/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004332/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4333/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004334/// direct-declarator '(' parameter-type-list ')'
4335/// direct-declarator '(' identifier-list[opt] ')'
4336/// [GNU] direct-declarator '(' parameter-forward-declarations
4337/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004338/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4339/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004340/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4341/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4342/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004343/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004344/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004345///
4346/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004347/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004348/// '::'[opt] nested-name-specifier[opt] type-name
4349///
4350/// id-expression: [C++ 5.1]
4351/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004352/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004353///
4354/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004355/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004356/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004357/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004358/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004359/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004360///
Richard Smith5d8388c2012-03-27 01:42:32 +00004361/// Note, any additional constructs added here may need corresponding changes
4362/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004363void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004364 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004365
David Blaikie4e4d0842012-03-11 07:00:24 +00004366 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004367 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004368 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004369 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4370 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004371 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004372 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004373 }
4374
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004375 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004376 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004377 // Change the declaration context for name lookup, until this function
4378 // is exited (and the declarator has been parsed).
4379 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004380 }
4381
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004382 // C++0x [dcl.fct]p14:
4383 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004384 // of a parameter-declaration-clause without a preceding comma. In
4385 // this case, the ellipsis is parsed as part of the
4386 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004387 // parameter pack that has not been expanded; otherwise, it is parsed
4388 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004389 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004390 !((D.getContext() == Declarator::PrototypeContext ||
4391 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004392 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00004393 !Actions.containsUnexpandedParameterPacks(D))) {
4394 SourceLocation EllipsisLoc = ConsumeToken();
4395 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4396 // The ellipsis was put in the wrong place. Recover, and explain to
4397 // the user what they should have done.
4398 ParseDeclarator(D);
4399 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4400 return;
4401 } else
4402 D.setEllipsisLoc(EllipsisLoc);
4403
4404 // The ellipsis can't be followed by a parenthesized declarator. We
4405 // check for that in ParseParenDeclarator, after we have disambiguated
4406 // the l_paren token.
4407 }
4408
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004409 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4410 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4411 // We found something that indicates the start of an unqualified-id.
4412 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004413 bool AllowConstructorName;
4414 if (D.getDeclSpec().hasTypeSpecifier())
4415 AllowConstructorName = false;
4416 else if (D.getCXXScopeSpec().isSet())
4417 AllowConstructorName =
4418 (D.getContext() == Declarator::FileContext ||
4419 (D.getContext() == Declarator::MemberContext &&
4420 D.getDeclSpec().isFriendSpecified()));
4421 else
4422 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4423
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004424 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004425 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4426 /*EnteringContext=*/true,
4427 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004428 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004429 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004430 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004431 D.getName()) ||
4432 // Once we're past the identifier, if the scope was bad, mark the
4433 // whole declarator bad.
4434 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004435 D.SetIdentifier(0, Tok.getLocation());
4436 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004437 } else {
4438 // Parsed the unqualified-id; update range information and move along.
4439 if (D.getSourceRange().getBegin().isInvalid())
4440 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4441 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004442 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004443 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004444 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004445 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004446 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004447 "There's a C++-specific check for tok::identifier above");
4448 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4449 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4450 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004451 goto PastIdentifier;
4452 }
Richard Smith9988f282012-03-29 01:16:42 +00004453
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004454 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004455 // direct-declarator: '(' declarator ')'
4456 // direct-declarator: '(' attributes declarator ')'
4457 // Example: 'char (*X)' or 'int (*XX)(void)'
4458 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004459
4460 // If the declarator was parenthesized, we entered the declarator
4461 // scope when parsing the parenthesized declarator, then exited
4462 // the scope already. Re-enter the scope, if we need to.
4463 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004464 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004465 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004466 if (!D.isInvalidType() &&
4467 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004468 // Change the declaration context for name lookup, until this function
4469 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004470 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004471 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004472 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004473 // This could be something simple like "int" (in which case the declarator
4474 // portion is empty), if an abstract-declarator is allowed.
4475 D.SetIdentifier(0, Tok.getLocation());
4476 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004477 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004478 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004479 if (D.getContext() == Declarator::MemberContext)
4480 Diag(Tok, diag::err_expected_member_name_or_semi)
4481 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004482 else if (getLangOpts().CPlusPlus)
4483 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004484 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004485 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004486 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004487 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004488 }
Mike Stump1eb44332009-09-09 15:08:12 +00004489
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004490 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004491 assert(D.isPastIdentifier() &&
4492 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004493
Richard Smith6ee326a2012-04-10 01:32:12 +00004494 // Don't parse attributes unless we have parsed an unparenthesized name.
4495 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004496 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004497
Reid Spencer5f016e22007-07-11 17:01:13 +00004498 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004499 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004500 // Enter function-declaration scope, limiting any declarators to the
4501 // function prototype scope, including parameter declarators.
4502 ParseScope PrototypeScope(this,
4503 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004504 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4505 // In such a case, check if we actually have a function declarator; if it
4506 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004507 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004508 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4509 // The name of the declarator, if any, is tentatively declared within
4510 // a possible direct initializer.
4511 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4512 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4513 TentativelyDeclaredIdentifiers.pop_back();
4514 if (!IsFunctionDecl)
4515 break;
4516 }
John McCall0b7e6782011-03-24 11:26:52 +00004517 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004518 BalancedDelimiterTracker T(*this, tok::l_paren);
4519 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004520 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004521 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004522 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004523 ParseBracketDeclarator(D);
4524 } else {
4525 break;
4526 }
4527 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004528}
Reid Spencer5f016e22007-07-11 17:01:13 +00004529
Chris Lattneref4715c2008-04-06 05:45:57 +00004530/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4531/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004532/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004533/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4534///
4535/// direct-declarator:
4536/// '(' declarator ')'
4537/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004538/// direct-declarator '(' parameter-type-list ')'
4539/// direct-declarator '(' identifier-list[opt] ')'
4540/// [GNU] direct-declarator '(' parameter-forward-declarations
4541/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004542///
4543void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004544 BalancedDelimiterTracker T(*this, tok::l_paren);
4545 T.consumeOpen();
4546
Chris Lattneref4715c2008-04-06 05:45:57 +00004547 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004548
Chris Lattner7399ee02008-10-20 02:05:46 +00004549 // Eat any attributes before we look at whether this is a grouping or function
4550 // declarator paren. If this is a grouping paren, the attribute applies to
4551 // the type being built up, for example:
4552 // int (__attribute__(()) *x)(long y)
4553 // If this ends up not being a grouping paren, the attribute applies to the
4554 // first argument, for example:
4555 // int (__attribute__(()) int x)
4556 // In either case, we need to eat any attributes to be able to determine what
4557 // sort of paren this is.
4558 //
John McCall0b7e6782011-03-24 11:26:52 +00004559 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004560 bool RequiresArg = false;
4561 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004562 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004563
Chris Lattner7399ee02008-10-20 02:05:46 +00004564 // We require that the argument list (if this is a non-grouping paren) be
4565 // present even if the attribute list was empty.
4566 RequiresArg = true;
4567 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004568
Steve Naroff239f0732008-12-25 14:16:32 +00004569 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004570 ParseMicrosoftTypeAttributes(attrs);
4571
Dawn Perchik52fc3142010-09-03 01:29:35 +00004572 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004573 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004574 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004575
Chris Lattneref4715c2008-04-06 05:45:57 +00004576 // If we haven't past the identifier yet (or where the identifier would be
4577 // stored, if this is an abstract declarator), then this is probably just
4578 // grouping parens. However, if this could be an abstract-declarator, then
4579 // this could also be the start of function arguments (consider 'void()').
4580 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004581
Chris Lattneref4715c2008-04-06 05:45:57 +00004582 if (!D.mayOmitIdentifier()) {
4583 // If this can't be an abstract-declarator, this *must* be a grouping
4584 // paren, because we haven't seen the identifier yet.
4585 isGrouping = true;
4586 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004587 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4588 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004589 isDeclarationSpecifier() || // 'int(int)' is a function.
4590 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004591 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4592 // considered to be a type, not a K&R identifier-list.
4593 isGrouping = false;
4594 } else {
4595 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4596 isGrouping = true;
4597 }
Mike Stump1eb44332009-09-09 15:08:12 +00004598
Chris Lattneref4715c2008-04-06 05:45:57 +00004599 // If this is a grouping paren, handle:
4600 // direct-declarator: '(' declarator ')'
4601 // direct-declarator: '(' attributes declarator ')'
4602 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004603 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4604 D.setEllipsisLoc(SourceLocation());
4605
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004606 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004607 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004608 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004609 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004610 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004611 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004612 T.getCloseLocation()),
4613 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004614
4615 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004616
4617 // An ellipsis cannot be placed outside parentheses.
4618 if (EllipsisLoc.isValid())
4619 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4620
Chris Lattneref4715c2008-04-06 05:45:57 +00004621 return;
4622 }
Mike Stump1eb44332009-09-09 15:08:12 +00004623
Chris Lattneref4715c2008-04-06 05:45:57 +00004624 // Okay, if this wasn't a grouping paren, it must be the start of a function
4625 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004626 // identifier (and remember where it would have been), then call into
4627 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004628 D.SetIdentifier(0, Tok.getLocation());
4629
David Blaikie42d6d0c2011-12-04 05:04:18 +00004630 // Enter function-declaration scope, limiting any declarators to the
4631 // function prototype scope, including parameter declarators.
4632 ParseScope PrototypeScope(this,
4633 Scope::FunctionPrototypeScope|Scope::DeclScope);
Richard Smithb9c62612012-07-30 21:30:52 +00004634 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004635 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004636}
4637
4638/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4639/// declarator D up to a paren, which indicates that we are parsing function
4640/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004641///
Richard Smith6ee326a2012-04-10 01:32:12 +00004642/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4643/// immediately after the open paren - they should be considered to be the
4644/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004645///
Richard Smith6ee326a2012-04-10 01:32:12 +00004646/// If RequiresArg is true, then the first argument of the function is required
4647/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004648///
Richard Smith6ee326a2012-04-10 01:32:12 +00004649/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4650/// (C++11) ref-qualifier[opt], exception-specification[opt],
4651/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4652///
4653/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004654/// dynamic-exception-specification
4655/// noexcept-specification
4656///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004657void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004658 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004659 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004660 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004661 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004662 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004663 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004664 // lparen is already consumed!
4665 assert(D.isPastIdentifier() && "Should not call before identifier!");
4666
4667 // This should be true when the function has typed arguments.
4668 // Otherwise, it is treated as a K&R-style function.
4669 bool HasProto = false;
4670 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004671 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004672 // Remember where we see an ellipsis, if any.
4673 SourceLocation EllipsisLoc;
4674
4675 DeclSpec DS(AttrFactory);
4676 bool RefQualifierIsLValueRef = true;
4677 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004678 SourceLocation ConstQualifierLoc;
4679 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004680 ExceptionSpecificationType ESpecType = EST_None;
4681 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004682 SmallVector<ParsedType, 2> DynamicExceptions;
4683 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004684 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004685 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004686 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004687
James Molloy16f1f712012-02-29 10:24:19 +00004688 Actions.ActOnStartFunctionDeclarator();
4689
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004690 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4691 EndLoc is the end location for the function declarator.
4692 They differ for trailing return types. */
4693 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004694 SourceLocation LParenLoc, RParenLoc;
4695 LParenLoc = Tracker.getOpenLocation();
4696 StartLoc = LParenLoc;
4697
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004698 if (isFunctionDeclaratorIdentifierList()) {
4699 if (RequiresArg)
4700 Diag(Tok, diag::err_argument_required_after_attribute);
4701
4702 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4703
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004704 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004705 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004706 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004707 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004708 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004709 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004710 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004711 else if (RequiresArg)
4712 Diag(Tok, diag::err_argument_required_after_attribute);
4713
David Blaikie4e4d0842012-03-11 07:00:24 +00004714 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004715
4716 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004717 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004718 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004719 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004720 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004721
David Blaikie4e4d0842012-03-11 07:00:24 +00004722 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004723 // FIXME: Accept these components in any order, and produce fixits to
4724 // correct the order if the user gets it wrong. Ideally we should deal
4725 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004726
4727 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004728 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4729 if (!DS.getSourceRange().getEnd().isInvalid()) {
4730 EndLoc = DS.getSourceRange().getEnd();
4731 ConstQualifierLoc = DS.getConstSpecLoc();
4732 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4733 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004734
4735 // Parse ref-qualifier[opt].
4736 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004737 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004738 diag::warn_cxx98_compat_ref_qualifier :
4739 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004740
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004741 RefQualifierIsLValueRef = Tok.is(tok::amp);
4742 RefQualifierLoc = ConsumeToken();
4743 EndLoc = RefQualifierLoc;
4744 }
4745
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004746 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00004747 // If a declaration declares a member function or member function
4748 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004749 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00004750 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004751 // declarator.
Chad Rosier8decdee2012-06-26 22:30:43 +00004752 bool IsCXX11MemberFunction =
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004753 getLangOpts().CPlusPlus0x &&
4754 (D.getContext() == Declarator::MemberContext ||
4755 (D.getContext() == Declarator::FileContext &&
Chad Rosier8decdee2012-06-26 22:30:43 +00004756 D.getCXXScopeSpec().isValid() &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004757 Actions.CurContext->isRecord()));
4758 Sema::CXXThisScopeRAII ThisScope(Actions,
4759 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4760 DS.getTypeQualifiers(),
4761 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004762
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004763 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004764 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004765 DynamicExceptions,
4766 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004767 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004768 if (ESpecType != EST_None)
4769 EndLoc = ESpecRange.getEnd();
4770
Richard Smith6ee326a2012-04-10 01:32:12 +00004771 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4772 // after the exception-specification.
4773 MaybeParseCXX0XAttributes(FnAttrs);
4774
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004775 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004776 LocalEndLoc = EndLoc;
David Blaikie4e4d0842012-03-11 07:00:24 +00004777 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004778 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004779 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4780 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004781 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00004782 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004783 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004784 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004785 }
4786 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004787 }
4788
4789 // Remember that we parsed a function type, and remember the attributes.
4790 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004791 IsAmbiguous,
4792 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004793 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004794 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004795 DS.getTypeQualifiers(),
4796 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004797 RefQualifierLoc, ConstQualifierLoc,
4798 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004799 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004800 ESpecType, ESpecRange.getBegin(),
4801 DynamicExceptions.data(),
4802 DynamicExceptionRanges.data(),
4803 DynamicExceptions.size(),
4804 NoexceptExpr.isUsable() ?
4805 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004806 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004807 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004808 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004809
4810 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004811}
4812
4813/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4814/// identifier list form for a K&R-style function: void foo(a,b,c)
4815///
4816/// Note that identifier-lists are only allowed for normal declarators, not for
4817/// abstract-declarators.
4818bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004819 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004820 && Tok.is(tok::identifier)
4821 && !TryAltiVecVectorToken()
4822 // K&R identifier lists can't have typedefs as identifiers, per C99
4823 // 6.7.5.3p11.
4824 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4825 // Identifier lists follow a really simple grammar: the identifiers can
4826 // be followed *only* by a ", identifier" or ")". However, K&R
4827 // identifier lists are really rare in the brave new modern world, and
4828 // it is very common for someone to typo a type in a non-K&R style
4829 // list. If we are presented with something like: "void foo(intptr x,
4830 // float y)", we don't want to start parsing the function declarator as
4831 // though it is a K&R style declarator just because intptr is an
4832 // invalid type.
4833 //
4834 // To handle this, we check to see if the token after the first
4835 // identifier is a "," or ")". Only then do we parse it as an
4836 // identifier list.
4837 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4838}
4839
4840/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4841/// we found a K&R-style identifier list instead of a typed parameter list.
4842///
4843/// After returning, ParamInfo will hold the parsed parameters.
4844///
4845/// identifier-list: [C99 6.7.5]
4846/// identifier
4847/// identifier-list ',' identifier
4848///
4849void Parser::ParseFunctionDeclaratorIdentifierList(
4850 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004851 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004852 // If there was no identifier specified for the declarator, either we are in
4853 // an abstract-declarator, or we are in a parameter declarator which was found
4854 // to be abstract. In abstract-declarators, identifier lists are not valid:
4855 // diagnose this.
4856 if (!D.getIdentifier())
4857 Diag(Tok, diag::ext_ident_list_in_param);
4858
4859 // Maintain an efficient lookup of params we have seen so far.
4860 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4861
4862 while (1) {
4863 // If this isn't an identifier, report the error and skip until ')'.
4864 if (Tok.isNot(tok::identifier)) {
4865 Diag(Tok, diag::err_expected_ident);
4866 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4867 // Forget we parsed anything.
4868 ParamInfo.clear();
4869 return;
4870 }
4871
4872 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4873
4874 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4875 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4876 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4877
4878 // Verify that the argument identifier has not already been mentioned.
4879 if (!ParamsSoFar.insert(ParmII)) {
4880 Diag(Tok, diag::err_param_redefinition) << ParmII;
4881 } else {
4882 // Remember this identifier in ParamInfo.
4883 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4884 Tok.getLocation(),
4885 0));
4886 }
4887
4888 // Eat the identifier.
4889 ConsumeToken();
4890
4891 // The list continues if we see a comma.
4892 if (Tok.isNot(tok::comma))
4893 break;
4894 ConsumeToken();
4895 }
4896}
4897
4898/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4899/// after the opening parenthesis. This function will not parse a K&R-style
4900/// identifier list.
4901///
Richard Smith6ce48a72012-04-11 04:01:28 +00004902/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4903/// caller parsed those arguments immediately after the open paren - they should
4904/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004905///
4906/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4907/// be the location of the ellipsis, if any was parsed.
4908///
Reid Spencer5f016e22007-07-11 17:01:13 +00004909/// parameter-type-list: [C99 6.7.5]
4910/// parameter-list
4911/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004912/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004913///
4914/// parameter-list: [C99 6.7.5]
4915/// parameter-declaration
4916/// parameter-list ',' parameter-declaration
4917///
4918/// parameter-declaration: [C99 6.7.5]
4919/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004920/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004921/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004922/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004923/// declaration-specifiers abstract-declarator[opt]
4924/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004925/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004926/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004927/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004928///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004929void Parser::ParseParameterDeclarationClause(
4930 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004931 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004932 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004933 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004934
Chris Lattnerf97409f2008-04-06 06:57:35 +00004935 while (1) {
4936 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004937 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4938 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004939 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004940 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004941 }
Mike Stump1eb44332009-09-09 15:08:12 +00004942
Chris Lattnerf97409f2008-04-06 06:57:35 +00004943 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004944 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004945 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004946
Richard Smith6ce48a72012-04-11 04:01:28 +00004947 // Parse any C++11 attributes.
4948 MaybeParseCXX0XAttributes(DS.getAttributes());
4949
John McCall7f040a92010-12-24 02:08:15 +00004950 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00004951 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00004952
4953 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004954
4955 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004956 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004957 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004958 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4959 // too much hassle.
4960 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004961
Chris Lattnere64c5492009-02-27 18:38:20 +00004962 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004963
Chris Lattnerf97409f2008-04-06 06:57:35 +00004964 // Parse the declarator. This is "PrototypeContext", because we must
4965 // accept either 'declarator' or 'abstract-declarator' here.
4966 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4967 ParseDeclarator(ParmDecl);
4968
4969 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004970 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004971
Chris Lattnerf97409f2008-04-06 06:57:35 +00004972 // Remember this parsed parameter in ParamInfo.
4973 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004974
Douglas Gregor72b505b2008-12-16 21:30:33 +00004975 // DefArgToks is used when the parsing of default arguments needs
4976 // to be delayed.
4977 CachedTokens *DefArgToks = 0;
4978
Chris Lattnerf97409f2008-04-06 06:57:35 +00004979 // If no parameter was specified, verify that *something* was specified,
4980 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004981 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4982 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004983 // Completely missing, emit error.
4984 Diag(DSStart, diag::err_missing_param);
4985 } else {
4986 // Otherwise, we have something. Add it and let semantic analysis try
4987 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004988
Chris Lattnerf97409f2008-04-06 06:57:35 +00004989 // Inform the actions module about the parameter declarator, so it gets
4990 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004991 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004992
4993 // Parse the default argument, if any. We parse the default
4994 // arguments in all dialects; the semantic analysis in
4995 // ActOnParamDefaultArgument will reject the default argument in
4996 // C.
4997 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004998 SourceLocation EqualLoc = Tok.getLocation();
4999
Chris Lattner04421082008-04-08 04:40:51 +00005000 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005001 if (D.getContext() == Declarator::MemberContext) {
5002 // If we're inside a class definition, cache the tokens
5003 // corresponding to the default argument. We'll actually parse
5004 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005005 // FIXME: Can we use a smart pointer for Toks?
5006 DefArgToks = new CachedTokens;
5007
Mike Stump1eb44332009-09-09 15:08:12 +00005008 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00005009 /*StopAtSemi=*/true,
5010 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005011 delete DefArgToks;
5012 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005013 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005014 } else {
5015 // Mark the end of the default argument so that we know when to
5016 // stop when we parse it later on.
5017 Token DefArgEnd;
5018 DefArgEnd.startToken();
5019 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5020 DefArgEnd.setLocation(Tok.getLocation());
5021 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005022 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005023 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005024 }
Chris Lattner04421082008-04-08 04:40:51 +00005025 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005026 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005027 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005028
Chad Rosier8decdee2012-06-26 22:30:43 +00005029 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005030 // used.
5031 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005032 Sema::PotentiallyEvaluatedIfUsed,
5033 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005034
Sebastian Redl84407ba2012-03-14 15:54:00 +00005035 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00005036 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
5037 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005038 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005039 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005040 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005041 if (DefArgResult.isInvalid()) {
5042 Actions.ActOnParamDefaultArgumentError(Param);
5043 SkipUntil(tok::comma, tok::r_paren, true, true);
5044 } else {
5045 // Inform the actions module about the default argument
5046 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005047 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005048 }
Chris Lattner04421082008-04-08 04:40:51 +00005049 }
5050 }
Mike Stump1eb44332009-09-09 15:08:12 +00005051
5052 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5053 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00005054 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005055 }
5056
5057 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005058 if (Tok.isNot(tok::comma)) {
5059 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005060 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005061
David Blaikie4e4d0842012-03-11 07:00:24 +00005062 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005063 // We have ellipsis without a preceding ',', which is ill-formed
5064 // in C. Complain and provide the fix.
5065 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005066 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005067 }
5068 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005069
Douglas Gregored5d6512009-09-22 21:41:40 +00005070 break;
5071 }
Mike Stump1eb44332009-09-09 15:08:12 +00005072
Chris Lattnerf97409f2008-04-06 06:57:35 +00005073 // Consume the comma.
5074 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005075 }
Mike Stump1eb44332009-09-09 15:08:12 +00005076
Chris Lattner66d28652008-04-06 06:34:08 +00005077}
Chris Lattneref4715c2008-04-06 05:45:57 +00005078
Reid Spencer5f016e22007-07-11 17:01:13 +00005079/// [C90] direct-declarator '[' constant-expression[opt] ']'
5080/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5081/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5082/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5083/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005084/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5085/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005086void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005087 if (CheckProhibitedCXX11Attribute())
5088 return;
5089
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005090 BalancedDelimiterTracker T(*this, tok::l_square);
5091 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005092
Chris Lattner378c7e42008-12-18 07:27:21 +00005093 // C array syntax has many features, but by-far the most common is [] and [4].
5094 // This code does a fast path to handle some of the most obvious cases.
5095 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005096 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005097 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00005098 MaybeParseCXX0XAttributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005099
Chris Lattner378c7e42008-12-18 07:27:21 +00005100 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005101 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005102 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005103 T.getOpenLocation(),
5104 T.getCloseLocation()),
5105 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005106 return;
5107 } else if (Tok.getKind() == tok::numeric_constant &&
5108 GetLookAheadToken(1).is(tok::r_square)) {
5109 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005110 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005111 ConsumeToken();
5112
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005113 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005114 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00005115 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005116
Chris Lattner378c7e42008-12-18 07:27:21 +00005117 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005118 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00005119 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005120 T.getOpenLocation(),
5121 T.getCloseLocation()),
5122 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005123 return;
5124 }
Mike Stump1eb44332009-09-09 15:08:12 +00005125
Reid Spencer5f016e22007-07-11 17:01:13 +00005126 // If valid, this location is the position where we read the 'static' keyword.
5127 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005128 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005129 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005130
Reid Spencer5f016e22007-07-11 17:01:13 +00005131 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005132 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005133 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005134 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005135
Reid Spencer5f016e22007-07-11 17:01:13 +00005136 // If we haven't already read 'static', check to see if there is one after the
5137 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005138 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005139 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005140
Reid Spencer5f016e22007-07-11 17:01:13 +00005141 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5142 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005143 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005144
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005145 // Handle the case where we have '[*]' as the array size. However, a leading
5146 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005147 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005148 // infrequent, use of lookahead is not costly here.
5149 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005150 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005151
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005152 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005153 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005154 StaticLoc = SourceLocation(); // Drop the static.
5155 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005156 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005157 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005158 // Note, in C89, this production uses the constant-expr production instead
5159 // of assignment-expr. The only difference is that assignment-expr allows
5160 // things like '=' and '*='. Sema rejects these in C89 mode because they
5161 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005162
Douglas Gregore0762c92009-06-19 23:52:42 +00005163 // Parse the constant-expression or assignment-expression now (depending
5164 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005165 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005166 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005167 } else {
5168 EnterExpressionEvaluationContext Unevaluated(Actions,
5169 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005170 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005171 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005172 }
Mike Stump1eb44332009-09-09 15:08:12 +00005173
Reid Spencer5f016e22007-07-11 17:01:13 +00005174 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005175 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005176 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005177 // If the expression was invalid, skip it.
5178 SkipUntil(tok::r_square);
5179 return;
5180 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005181
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005182 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005183
John McCall0b7e6782011-03-24 11:26:52 +00005184 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00005185 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005186
Chris Lattner378c7e42008-12-18 07:27:21 +00005187 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005188 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005189 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005190 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005191 T.getOpenLocation(),
5192 T.getCloseLocation()),
5193 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005194}
5195
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005196/// [GNU] typeof-specifier:
5197/// typeof ( expressions )
5198/// typeof ( type-name )
5199/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005200///
5201void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005202 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005203 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005204 SourceLocation StartLoc = ConsumeToken();
5205
John McCallcfb708c2010-01-13 20:03:27 +00005206 const bool hasParens = Tok.is(tok::l_paren);
5207
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005208 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5209 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005210
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005211 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005212 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005213 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005214 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5215 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005216 if (hasParens)
5217 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005218
5219 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005220 // FIXME: Not accurate, the range gets one token more than it should.
5221 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005222 else
5223 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005224
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005225 if (isCastExpr) {
5226 if (!CastTy) {
5227 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005228 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005229 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005230
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005231 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005232 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005233 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5234 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005235 DiagID, CastTy))
5236 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005237 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005238 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005239
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005240 // If we get here, the operand to the typeof was an expresion.
5241 if (Operand.isInvalid()) {
5242 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005243 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005244 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005245
Eli Friedman71b8fb52012-01-21 01:01:51 +00005246 // We might need to transform the operand if it is potentially evaluated.
5247 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5248 if (Operand.isInvalid()) {
5249 DS.SetTypeSpecError();
5250 return;
5251 }
5252
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005253 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005254 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005255 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5256 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005257 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005258 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005259}
Chris Lattner1b492422010-02-28 18:33:55 +00005260
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005261/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005262/// _Atomic ( type-name )
5263///
5264void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5265 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5266
5267 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005268 BalancedDelimiterTracker T(*this, tok::l_paren);
5269 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00005270 SkipUntil(tok::r_paren);
5271 return;
5272 }
5273
5274 TypeResult Result = ParseTypeName();
5275 if (Result.isInvalid()) {
5276 SkipUntil(tok::r_paren);
5277 return;
5278 }
5279
5280 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005281 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005282
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005283 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005284 return;
5285
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005286 DS.setTypeofParensRange(T.getRange());
5287 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005288
5289 const char *PrevSpec = 0;
5290 unsigned DiagID;
5291 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5292 DiagID, Result.release()))
5293 Diag(StartLoc, DiagID) << PrevSpec;
5294}
5295
Chris Lattner1b492422010-02-28 18:33:55 +00005296
5297/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5298/// from TryAltiVecVectorToken.
5299bool Parser::TryAltiVecVectorTokenOutOfLine() {
5300 Token Next = NextToken();
5301 switch (Next.getKind()) {
5302 default: return false;
5303 case tok::kw_short:
5304 case tok::kw_long:
5305 case tok::kw_signed:
5306 case tok::kw_unsigned:
5307 case tok::kw_void:
5308 case tok::kw_char:
5309 case tok::kw_int:
5310 case tok::kw_float:
5311 case tok::kw_double:
5312 case tok::kw_bool:
5313 case tok::kw___pixel:
5314 Tok.setKind(tok::kw___vector);
5315 return true;
5316 case tok::identifier:
5317 if (Next.getIdentifierInfo() == Ident_pixel) {
5318 Tok.setKind(tok::kw___vector);
5319 return true;
5320 }
5321 return false;
5322 }
5323}
5324
5325bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5326 const char *&PrevSpec, unsigned &DiagID,
5327 bool &isInvalid) {
5328 if (Tok.getIdentifierInfo() == Ident_vector) {
5329 Token Next = NextToken();
5330 switch (Next.getKind()) {
5331 case tok::kw_short:
5332 case tok::kw_long:
5333 case tok::kw_signed:
5334 case tok::kw_unsigned:
5335 case tok::kw_void:
5336 case tok::kw_char:
5337 case tok::kw_int:
5338 case tok::kw_float:
5339 case tok::kw_double:
5340 case tok::kw_bool:
5341 case tok::kw___pixel:
5342 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5343 return true;
5344 case tok::identifier:
5345 if (Next.getIdentifierInfo() == Ident_pixel) {
5346 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5347 return true;
5348 }
5349 break;
5350 default:
5351 break;
5352 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005353 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005354 DS.isTypeAltiVecVector()) {
5355 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5356 return true;
5357 }
5358 return false;
5359}