blob: 9a8f8005cfaaab0fbd06cd8a76be4c8058b7b04b [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
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"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000018#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
20#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000022#include "RAIIObjectsForParser.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
145 // Attributes in a class are parsed at the end of the class, along
146 // 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) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000461 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000462 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000463 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
464 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000465 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000466 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Eli Friedman290eeb02009-06-08 23:27:34 +0000467 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000468}
469
John McCall7f040a92010-12-24 02:08:15 +0000470void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000471 // Treat these like attributes
472 while (Tok.is(tok::kw___pascal)) {
473 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
474 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000475 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000476 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000477 }
John McCall7f040a92010-12-24 02:08:15 +0000478}
479
Peter Collingbournef315fa82011-02-14 01:42:53 +0000480void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
481 // Treat these like attributes
482 while (Tok.is(tok::kw___kernel)) {
483 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000484 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
485 AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000486 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000487 }
488}
489
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000490void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
491 SourceLocation Loc = Tok.getLocation();
492 switch(Tok.getKind()) {
493 // OpenCL qualifiers:
494 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000495 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000496 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000497 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000498 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000499 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000500
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000501 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000502 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000503 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000504 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000505 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000506
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000507 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000508 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000509 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000510 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000511 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000512
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000513 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000514 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000515 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000516 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000517 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000518
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000519 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000520 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000521 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000522 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000523 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000524
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000525 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000526 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000527 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000528 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000529 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000530
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000531 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000532 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000533 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000534 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000535 break;
536 default: break;
537 }
538}
539
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000540/// \brief Parse a version number.
541///
542/// version:
543/// simple-integer
544/// simple-integer ',' simple-integer
545/// simple-integer ',' simple-integer ',' simple-integer
546VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
547 Range = Tok.getLocation();
548
549 if (!Tok.is(tok::numeric_constant)) {
550 Diag(Tok, diag::err_expected_version);
551 SkipUntil(tok::comma, tok::r_paren, true, true, true);
552 return VersionTuple();
553 }
554
555 // Parse the major (and possibly minor and subminor) versions, which
556 // are stored in the numeric constant. We utilize a quirk of the
557 // lexer, which is that it handles something like 1.2.3 as a single
558 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000559 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000560 Buffer.resize(Tok.getLength()+1);
561 const char *ThisTokBegin = &Buffer[0];
562
563 // Get the spelling of the token, which eliminates trigraphs, etc.
564 bool Invalid = false;
565 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
566 if (Invalid)
567 return VersionTuple();
568
569 // Parse the major version.
570 unsigned AfterMajor = 0;
571 unsigned Major = 0;
572 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
573 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
574 ++AfterMajor;
575 }
576
577 if (AfterMajor == 0) {
578 Diag(Tok, diag::err_expected_version);
579 SkipUntil(tok::comma, tok::r_paren, true, true, true);
580 return VersionTuple();
581 }
582
583 if (AfterMajor == ActualLength) {
584 ConsumeToken();
585
586 // We only had a single version component.
587 if (Major == 0) {
588 Diag(Tok, diag::err_zero_version);
589 return VersionTuple();
590 }
591
592 return VersionTuple(Major);
593 }
594
595 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
596 Diag(Tok, diag::err_expected_version);
597 SkipUntil(tok::comma, tok::r_paren, true, true, true);
598 return VersionTuple();
599 }
600
601 // Parse the minor version.
602 unsigned AfterMinor = AfterMajor + 1;
603 unsigned Minor = 0;
604 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
605 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
606 ++AfterMinor;
607 }
608
609 if (AfterMinor == ActualLength) {
610 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000611
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000612 // We had major.minor.
613 if (Major == 0 && Minor == 0) {
614 Diag(Tok, diag::err_zero_version);
615 return VersionTuple();
616 }
617
Chad Rosier8decdee2012-06-26 22:30:43 +0000618 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000619 }
620
621 // If what follows is not a '.', we have a problem.
622 if (ThisTokBegin[AfterMinor] != '.') {
623 Diag(Tok, diag::err_expected_version);
624 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosier8decdee2012-06-26 22:30:43 +0000625 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000626 }
627
628 // Parse the subminor version.
629 unsigned AfterSubminor = AfterMinor + 1;
630 unsigned Subminor = 0;
631 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
632 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
633 ++AfterSubminor;
634 }
635
636 if (AfterSubminor != ActualLength) {
637 Diag(Tok, diag::err_expected_version);
638 SkipUntil(tok::comma, tok::r_paren, true, true, true);
639 return VersionTuple();
640 }
641 ConsumeToken();
642 return VersionTuple(Major, Minor, Subminor);
643}
644
645/// \brief Parse the contents of the "availability" attribute.
646///
647/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000648/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000649///
650/// platform:
651/// identifier
652///
653/// version-arg-list:
654/// version-arg
655/// version-arg ',' version-arg-list
656///
657/// version-arg:
658/// 'introduced' '=' version
659/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000660/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000661/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000662/// opt-message:
663/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000664void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
665 SourceLocation AvailabilityLoc,
666 ParsedAttributes &attrs,
667 SourceLocation *endLoc) {
668 SourceLocation PlatformLoc;
669 IdentifierInfo *Platform = 0;
670
671 enum { Introduced, Deprecated, Obsoleted, Unknown };
672 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000673 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000674
675 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000676 BalancedDelimiterTracker T(*this, tok::l_paren);
677 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000678 Diag(Tok, diag::err_expected_lparen);
679 return;
680 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000681
682 // Parse the platform name,
683 if (Tok.isNot(tok::identifier)) {
684 Diag(Tok, diag::err_availability_expected_platform);
685 SkipUntil(tok::r_paren);
686 return;
687 }
688 Platform = Tok.getIdentifierInfo();
689 PlatformLoc = ConsumeToken();
690
691 // Parse the ',' following the platform name.
692 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
693 return;
694
695 // If we haven't grabbed the pointers for the identifiers
696 // "introduced", "deprecated", and "obsoleted", do so now.
697 if (!Ident_introduced) {
698 Ident_introduced = PP.getIdentifierInfo("introduced");
699 Ident_deprecated = PP.getIdentifierInfo("deprecated");
700 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000701 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000702 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000703 }
704
705 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000706 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000707 do {
708 if (Tok.isNot(tok::identifier)) {
709 Diag(Tok, diag::err_availability_expected_change);
710 SkipUntil(tok::r_paren);
711 return;
712 }
713 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
714 SourceLocation KeywordLoc = ConsumeToken();
715
Douglas Gregorb53e4172011-03-26 03:35:55 +0000716 if (Keyword == Ident_unavailable) {
717 if (UnavailableLoc.isValid()) {
718 Diag(KeywordLoc, diag::err_availability_redundant)
719 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000720 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000721 UnavailableLoc = KeywordLoc;
722
723 if (Tok.isNot(tok::comma))
724 break;
725
726 ConsumeToken();
727 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000728 }
729
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000730 if (Tok.isNot(tok::equal)) {
731 Diag(Tok, diag::err_expected_equal_after)
732 << Keyword;
733 SkipUntil(tok::r_paren);
734 return;
735 }
736 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000737 if (Keyword == Ident_message) {
738 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000739 Diag(Tok, diag::err_expected_string_literal)
740 << /*Source='availability attribute'*/2;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000741 SkipUntil(tok::r_paren);
742 return;
743 }
744 MessageExpr = ParseStringLiteralExpression();
745 break;
746 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000747
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000748 SourceRange VersionRange;
749 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000750
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000751 if (Version.empty()) {
752 SkipUntil(tok::r_paren);
753 return;
754 }
755
756 unsigned Index;
757 if (Keyword == Ident_introduced)
758 Index = Introduced;
759 else if (Keyword == Ident_deprecated)
760 Index = Deprecated;
761 else if (Keyword == Ident_obsoleted)
762 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000763 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000764 Index = Unknown;
765
766 if (Index < Unknown) {
767 if (!Changes[Index].KeywordLoc.isInvalid()) {
768 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000769 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000770 << SourceRange(Changes[Index].KeywordLoc,
771 Changes[Index].VersionRange.getEnd());
772 }
773
774 Changes[Index].KeywordLoc = KeywordLoc;
775 Changes[Index].Version = Version;
776 Changes[Index].VersionRange = VersionRange;
777 } else {
778 Diag(KeywordLoc, diag::err_availability_unknown_change)
779 << Keyword << VersionRange;
780 }
781
782 if (Tok.isNot(tok::comma))
783 break;
784
785 ConsumeToken();
786 } while (true);
787
788 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000789 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000790 return;
791
792 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000793 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000794
Douglas Gregorb53e4172011-03-26 03:35:55 +0000795 // The 'unavailable' availability cannot be combined with any other
796 // availability changes. Make sure that hasn't happened.
797 if (UnavailableLoc.isValid()) {
798 bool Complained = false;
799 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
800 if (Changes[Index].KeywordLoc.isValid()) {
801 if (!Complained) {
802 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
803 << SourceRange(Changes[Index].KeywordLoc,
804 Changes[Index].VersionRange.getEnd());
805 Complained = true;
806 }
807
808 // Clear out the availability.
809 Changes[Index] = AvailabilityChange();
810 }
811 }
812 }
813
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000814 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000815 attrs.addNew(&Availability,
816 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000817 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000818 Platform, PlatformLoc,
819 Changes[Introduced],
820 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000821 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000822 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000823 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000824}
825
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000826
827// Late Parsed Attributes:
828// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
829
830void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
831
832void Parser::LateParsedClass::ParseLexedAttributes() {
833 Self->ParseLexedAttributes(*Class);
834}
835
836void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000837 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000838}
839
840/// Wrapper class which calls ParseLexedAttribute, after setting up the
841/// scope appropriately.
842void Parser::ParseLexedAttributes(ParsingClass &Class) {
843 // Deal with templates
844 // FIXME: Test cases to make sure this does the right thing for templates.
845 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
846 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
847 HasTemplateScope);
848 if (HasTemplateScope)
849 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
850
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000851 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000852 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000853 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000854 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
855 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
856
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000857 // Enter the scope of nested classes
858 if (!AlreadyHasClassScope)
859 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
860 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000861 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000862 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
863 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
864 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000865 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000866
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000867 if (!AlreadyHasClassScope)
868 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
869 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000870}
871
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000872
873/// \brief Parse all attributes in LAs, and attach them to Decl D.
874void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
875 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +0000876 assert(LAs.parseSoon() &&
877 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000878 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +0000879 if (D)
880 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000881 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000882 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000883 }
884 LAs.clear();
885}
886
887
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000888/// \brief Finish parsing an attribute for which parsing was delayed.
889/// This will be called at the end of parsing a class declaration
890/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +0000891/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000892/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000893void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
894 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000895 // Save the current token position.
896 SourceLocation OrigLoc = Tok.getLocation();
897
898 // Append the current token at the end of the new token stream so that it
899 // doesn't get lost.
900 LA.Toks.push_back(Tok);
901 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
902 // Consume the previously pushed token.
903 ConsumeAnyToken();
904
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000905 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
906 Diag(Tok, diag::warn_attribute_on_function_definition)
907 << LA.AttrName.getName();
908 }
909
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000910 ParsedAttributes Attrs(AttrFactory);
911 SourceLocation endLoc;
912
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000913 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000914 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000915 NamedDecl *ND = dyn_cast<NamedDecl>(D);
916 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000917
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000918 // Allow 'this' within late-parsed attributes.
919 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
920 /*TypeQuals=*/0,
921 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000922
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000923 if (LA.Decls.size() == 1) {
924 // If the Decl is templatized, add template parameters to scope.
925 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
926 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
927 if (HasTemplateScope)
928 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000929
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000930 // If the Decl is on a function, add function parameters to the scope.
931 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
932 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
933 if (HasFunScope)
934 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000935
Michael Han6880f492012-10-03 01:56:22 +0000936 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000937 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000938
939 if (HasFunScope) {
940 Actions.ActOnExitFunctionContext();
941 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
942 }
943 if (HasTemplateScope) {
944 TempScope.Exit();
945 }
946 } else {
947 // If there are multiple decls, then the decl cannot be within the
948 // function scope.
Michael Han6880f492012-10-03 01:56:22 +0000949 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000950 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000951 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000952 } else {
953 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000954 }
955
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000956 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
957 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
958 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000959
960 if (Tok.getLocation() != OrigLoc) {
961 // Due to a parsing error, we either went over the cached tokens or
962 // there are still cached tokens left, so we skip the leftover tokens.
963 // Since this is an uncommon situation that should be avoided, use the
964 // expensive isBeforeInTranslationUnit call.
965 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
966 OrigLoc))
967 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000968 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000969 }
970}
971
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000972/// \brief Wrapper around a case statement checking if AttrName is
973/// one of the thread safety attributes
974bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
975 return llvm::StringSwitch<bool>(AttrName)
976 .Case("guarded_by", true)
977 .Case("guarded_var", true)
978 .Case("pt_guarded_by", true)
979 .Case("pt_guarded_var", true)
980 .Case("lockable", true)
981 .Case("scoped_lockable", true)
982 .Case("no_thread_safety_analysis", true)
983 .Case("acquired_after", true)
984 .Case("acquired_before", true)
985 .Case("exclusive_lock_function", true)
986 .Case("shared_lock_function", true)
987 .Case("exclusive_trylock_function", true)
988 .Case("shared_trylock_function", true)
989 .Case("unlock_function", true)
990 .Case("lock_returned", true)
991 .Case("locks_excluded", true)
992 .Case("exclusive_locks_required", true)
993 .Case("shared_locks_required", true)
994 .Default(false);
995}
996
997/// \brief Parse the contents of thread safety attributes. These
998/// should always be parsed as an expression list.
999///
1000/// We need to special case the parsing due to the fact that if the first token
1001/// of the first argument is an identifier, the main parse loop will store
1002/// that token as a "parameter" and the rest of
1003/// the arguments will be added to a list of "arguments". However,
1004/// subsequent tokens in the first argument are lost. We instead parse each
1005/// argument as an expression and add all arguments to the list of "arguments".
1006/// In future, we will take advantage of this special case to also
1007/// deal with some argument scoping issues here (for example, referring to a
1008/// function parameter in the attribute on that function).
1009void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1010 SourceLocation AttrNameLoc,
1011 ParsedAttributes &Attrs,
1012 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001013 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001014
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001015 BalancedDelimiterTracker T(*this, tok::l_paren);
1016 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001017
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001018 ExprVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001019 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001020
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001021 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001022 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001023 ExprResult ArgExpr(ParseAssignmentExpression());
1024 if (ArgExpr.isInvalid()) {
1025 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001026 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001027 break;
1028 } else {
1029 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001030 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001031 if (Tok.isNot(tok::comma))
1032 break;
1033 ConsumeToken(); // Eat the comma, move to the next argument
1034 }
1035 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001036 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001037 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001038 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001039 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001040 if (EndLoc)
1041 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001042}
1043
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001044void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1045 SourceLocation AttrNameLoc,
1046 ParsedAttributes &Attrs,
1047 SourceLocation *EndLoc) {
1048 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1049
1050 BalancedDelimiterTracker T(*this, tok::l_paren);
1051 T.consumeOpen();
1052
1053 if (Tok.isNot(tok::identifier)) {
1054 Diag(Tok, diag::err_expected_ident);
1055 T.skipToEnd();
1056 return;
1057 }
1058 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1059 SourceLocation ArgumentKindLoc = ConsumeToken();
1060
1061 if (Tok.isNot(tok::comma)) {
1062 Diag(Tok, diag::err_expected_comma);
1063 T.skipToEnd();
1064 return;
1065 }
1066 ConsumeToken();
1067
1068 SourceRange MatchingCTypeRange;
1069 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1070 if (MatchingCType.isInvalid()) {
1071 T.skipToEnd();
1072 return;
1073 }
1074
1075 bool LayoutCompatible = false;
1076 bool MustBeNull = false;
1077 while (Tok.is(tok::comma)) {
1078 ConsumeToken();
1079 if (Tok.isNot(tok::identifier)) {
1080 Diag(Tok, diag::err_expected_ident);
1081 T.skipToEnd();
1082 return;
1083 }
1084 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1085 if (Flag->isStr("layout_compatible"))
1086 LayoutCompatible = true;
1087 else if (Flag->isStr("must_be_null"))
1088 MustBeNull = true;
1089 else {
1090 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1091 T.skipToEnd();
1092 return;
1093 }
1094 ConsumeToken(); // consume flag
1095 }
1096
1097 if (!T.consumeClose()) {
1098 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1099 ArgumentKind, ArgumentKindLoc,
1100 MatchingCType.release(), LayoutCompatible,
1101 MustBeNull, AttributeList::AS_GNU);
1102 }
1103
1104 if (EndLoc)
1105 *EndLoc = T.getCloseLocation();
1106}
1107
Richard Smith6ee326a2012-04-10 01:32:12 +00001108/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1109/// of a C++11 attribute-specifier in a location where an attribute is not
1110/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1111/// situation.
1112///
1113/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1114/// this doesn't appear to actually be an attribute-specifier, and the caller
1115/// should try to parse it.
1116bool Parser::DiagnoseProhibitedCXX11Attribute() {
1117 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1118
1119 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1120 case CAK_NotAttributeSpecifier:
1121 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1122 return false;
1123
1124 case CAK_InvalidAttributeSpecifier:
1125 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1126 return false;
1127
1128 case CAK_AttributeSpecifier:
1129 // Parse and discard the attributes.
1130 SourceLocation BeginLoc = ConsumeBracket();
1131 ConsumeBracket();
1132 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1133 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1134 SourceLocation EndLoc = ConsumeBracket();
1135 Diag(BeginLoc, diag::err_attributes_not_allowed)
1136 << SourceRange(BeginLoc, EndLoc);
1137 return true;
1138 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001139 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001140}
1141
John McCall7f040a92010-12-24 02:08:15 +00001142void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1143 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1144 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001145}
1146
Michael Hanf64231e2012-11-06 19:34:54 +00001147void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1148 AttributeList *AttrList = attrs.getList();
1149 while (AttrList) {
1150 if (AttrList->isCXX0XAttribute()) {
1151 Diag(AttrList->getLoc(), diag::warn_attribute_no_decl)
1152 << AttrList->getName();
1153 AttrList->setInvalid();
1154 }
1155 AttrList = AttrList->getNext();
1156 }
1157}
1158
Reid Spencer5f016e22007-07-11 17:01:13 +00001159/// ParseDeclaration - Parse a full 'declaration', which consists of
1160/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001161/// 'Context' should be a Declarator::TheContext value. This returns the
1162/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001163///
1164/// declaration: [C99 6.7]
1165/// block-declaration ->
1166/// simple-declaration
1167/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001168/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001169/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001170/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001171/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001172/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001173/// others... [FIXME]
1174///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001175Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1176 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001177 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001178 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001179 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001180 // Must temporarily exit the objective-c container scope for
1181 // parsing c none objective-c decls.
1182 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001183
John McCalld226f652010-08-21 09:40:31 +00001184 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001185 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001186 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001187 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001188 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001189 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001190 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001191 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001192 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001193 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001194 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001195 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001196 SourceLocation InlineLoc = ConsumeToken();
1197 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1198 break;
1199 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001200 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001201 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001202 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001203 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001204 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001205 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001206 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001207 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001208 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001209 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001210 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001211 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001212 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001213 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001214 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001215 default:
John McCall7f040a92010-12-24 02:08:15 +00001216 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001217 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001218
Chris Lattner682bf922009-03-29 16:50:03 +00001219 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001220 // single decl, convert it now. Alias declarations can also declare a type;
1221 // include that too if it is present.
1222 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001223}
1224
1225/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1226/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001227/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1228/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001229///[C90/C++]init-declarator-list ';' [TODO]
1230/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001231///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001232/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001233/// attribute-specifier-seq[opt] type-specifier-seq declarator
1234///
Chris Lattnercd147752009-03-29 17:27:48 +00001235/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001236/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001237///
1238/// If FRI is non-null, we might be parsing a for-range-declaration instead
1239/// of a simple-declaration. If we find that we are, we also parse the
1240/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001241Parser::DeclGroupPtrTy
1242Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1243 SourceLocation &DeclEnd,
1244 ParsedAttributesWithRange &attrs,
1245 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001247 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001248 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001249
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001250 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001251 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001252
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1254 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001255 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001256 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001257 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001258 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001259 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001260 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001261 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001262 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001263
1264 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001265}
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Richard Smith0706df42011-10-19 21:33:05 +00001267/// Returns true if this might be the start of a declarator, or a common typo
1268/// for a declarator.
1269bool Parser::MightBeDeclarator(unsigned Context) {
1270 switch (Tok.getKind()) {
1271 case tok::annot_cxxscope:
1272 case tok::annot_template_id:
1273 case tok::caret:
1274 case tok::code_completion:
1275 case tok::coloncolon:
1276 case tok::ellipsis:
1277 case tok::kw___attribute:
1278 case tok::kw_operator:
1279 case tok::l_paren:
1280 case tok::star:
1281 return true;
1282
1283 case tok::amp:
1284 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001285 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001286
Richard Smith1c94c162012-01-09 22:31:44 +00001287 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001288 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001289 NextToken().is(tok::l_square);
1290
1291 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001292 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001293
Richard Smith0706df42011-10-19 21:33:05 +00001294 case tok::identifier:
1295 switch (NextToken().getKind()) {
1296 case tok::code_completion:
1297 case tok::coloncolon:
1298 case tok::comma:
1299 case tok::equal:
1300 case tok::equalequal: // Might be a typo for '='.
1301 case tok::kw_alignas:
1302 case tok::kw_asm:
1303 case tok::kw___attribute:
1304 case tok::l_brace:
1305 case tok::l_paren:
1306 case tok::l_square:
1307 case tok::less:
1308 case tok::r_brace:
1309 case tok::r_paren:
1310 case tok::r_square:
1311 case tok::semi:
1312 return true;
1313
1314 case tok::colon:
1315 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001316 // and in block scope it's probably a label. Inside a class definition,
1317 // this is a bit-field.
1318 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001319 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001320
1321 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001322 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001323
1324 default:
1325 return false;
1326 }
1327
1328 default:
1329 return false;
1330 }
1331}
1332
Richard Smith994d73f2012-04-11 20:59:20 +00001333/// Skip until we reach something which seems like a sensible place to pick
1334/// up parsing after a malformed declaration. This will sometimes stop sooner
1335/// than SkipUntil(tok::r_brace) would, but will never stop later.
1336void Parser::SkipMalformedDecl() {
1337 while (true) {
1338 switch (Tok.getKind()) {
1339 case tok::l_brace:
1340 // Skip until matching }, then stop. We've probably skipped over
1341 // a malformed class or function definition or similar.
1342 ConsumeBrace();
1343 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1344 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1345 // This declaration isn't over yet. Keep skipping.
1346 continue;
1347 }
1348 if (Tok.is(tok::semi))
1349 ConsumeToken();
1350 return;
1351
1352 case tok::l_square:
1353 ConsumeBracket();
1354 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1355 continue;
1356
1357 case tok::l_paren:
1358 ConsumeParen();
1359 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1360 continue;
1361
1362 case tok::r_brace:
1363 return;
1364
1365 case tok::semi:
1366 ConsumeToken();
1367 return;
1368
1369 case tok::kw_inline:
1370 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001371 // a good place to pick back up parsing, except in an Objective-C
1372 // @interface context.
1373 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1374 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001375 return;
1376 break;
1377
1378 case tok::kw_namespace:
1379 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001380 // place to pick back up parsing, except in an Objective-C
1381 // @interface context.
1382 if (Tok.isAtStartOfLine() &&
1383 (!ParsingInObjCContainer || CurParsedObjCImpl))
1384 return;
1385 break;
1386
1387 case tok::at:
1388 // @end is very much like } in Objective-C contexts.
1389 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1390 ParsingInObjCContainer)
1391 return;
1392 break;
1393
1394 case tok::minus:
1395 case tok::plus:
1396 // - and + probably start new method declarations in Objective-C contexts.
1397 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001398 return;
1399 break;
1400
1401 case tok::eof:
1402 return;
1403
1404 default:
1405 break;
1406 }
1407
1408 ConsumeAnyToken();
1409 }
1410}
1411
John McCalld8ac0572009-11-03 19:26:08 +00001412/// ParseDeclGroup - Having concluded that this is either a function
1413/// definition or a group of object declarations, actually parse the
1414/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001415Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1416 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001417 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001418 SourceLocation *DeclEnd,
1419 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001420 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001421 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001422 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001423
John McCalld8ac0572009-11-03 19:26:08 +00001424 // Bail out if the first declarator didn't seem well-formed.
1425 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001426 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001427 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001428 }
Mike Stump1eb44332009-09-09 15:08:12 +00001429
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001430 // Save late-parsed attributes for now; they need to be parsed in the
1431 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001432 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1433 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001434 if (D.isFunctionDeclarator())
1435 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1436
Chris Lattnerc82daef2010-07-11 22:24:20 +00001437 // Check to see if we have a function *definition* which must have a body.
1438 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1439 // Look at the next token to make sure that this isn't a function
1440 // declaration. We have to check this because __attribute__ might be the
1441 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001442 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001443
Chris Lattner004659a2010-07-11 22:42:07 +00001444 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001445 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1446 Diag(Tok, diag::err_function_declared_typedef);
1447
1448 // Recover by treating the 'typedef' as spurious.
1449 DS.ClearStorageClassSpecs();
1450 }
1451
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001452 Decl *TheDecl =
1453 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001454 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001455 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001456
Chris Lattner004659a2010-07-11 22:42:07 +00001457 if (isDeclarationSpecifier()) {
1458 // If there is an invalid declaration specifier right after the function
1459 // prototype, then we must be in a missing semicolon case where this isn't
1460 // actually a body. Just fall through into the code that handles it as a
1461 // prototype, and let the top-level code handle the erroneous declspec
1462 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001463 } else {
1464 Diag(Tok, diag::err_expected_fn_body);
1465 SkipUntil(tok::semi);
1466 return DeclGroupPtrTy();
1467 }
1468 }
1469
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001470 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001471 return DeclGroupPtrTy();
1472
1473 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1474 // must parse and analyze the for-range-initializer before the declaration is
1475 // analyzed.
1476 if (FRI && Tok.is(tok::colon)) {
1477 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001478 if (Tok.is(tok::l_brace))
1479 FRI->RangeExpr = ParseBraceInitializer();
1480 else
1481 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001482 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1483 Actions.ActOnCXXForRangeDecl(ThisDecl);
1484 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001485 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001486 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1487 }
1488
Chris Lattner5f9e2722011-07-23 10:55:15 +00001489 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001490 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001491 if (LateParsedAttrs.size() > 0)
1492 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001493 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001494 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001495 DeclsInGroup.push_back(FirstDecl);
1496
Richard Smith0706df42011-10-19 21:33:05 +00001497 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001498
John McCalld8ac0572009-11-03 19:26:08 +00001499 // If we don't have a comma, it is either the end of the list (a ';') or an
1500 // error, bail out.
1501 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001502 SourceLocation CommaLoc = ConsumeToken();
1503
1504 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1505 // This comma was followed by a line-break and something which can't be
1506 // the start of a declarator. The comma was probably a typo for a
1507 // semicolon.
1508 Diag(CommaLoc, diag::err_expected_semi_declaration)
1509 << FixItHint::CreateReplacement(CommaLoc, ";");
1510 ExpectSemi = false;
1511 break;
1512 }
John McCalld8ac0572009-11-03 19:26:08 +00001513
1514 // Parse the next declarator.
1515 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001516 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001517
1518 // Accept attributes in an init-declarator. In the first declarator in a
1519 // declaration, these would be part of the declspec. In subsequent
1520 // declarators, they become part of the declarator itself, so that they
1521 // don't apply to declarators after *this* one. Examples:
1522 // short __attribute__((common)) var; -> declspec
1523 // short var __attribute__((common)); -> declarator
1524 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001525 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001526
1527 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001528 if (!D.isInvalidType()) {
1529 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1530 D.complete(ThisDecl);
1531 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001532 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001533 }
John McCalld8ac0572009-11-03 19:26:08 +00001534 }
1535
1536 if (DeclEnd)
1537 *DeclEnd = Tok.getLocation();
1538
Richard Smith0706df42011-10-19 21:33:05 +00001539 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001540 ExpectAndConsumeSemi(Context == Declarator::FileContext
1541 ? diag::err_invalid_token_after_toplevel_declarator
1542 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001543 // Okay, there was no semicolon and one was expected. If we see a
1544 // declaration specifier, just assume it was missing and continue parsing.
1545 // Otherwise things are very confused and we skip to recover.
1546 if (!isDeclarationSpecifier()) {
1547 SkipUntil(tok::r_brace, true, true);
1548 if (Tok.is(tok::semi))
1549 ConsumeToken();
1550 }
John McCalld8ac0572009-11-03 19:26:08 +00001551 }
1552
Douglas Gregor23c94db2010-07-02 17:43:08 +00001553 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001554 DeclsInGroup.data(),
1555 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001556}
1557
Richard Smithad762fc2011-04-14 22:09:26 +00001558/// Parse an optional simple-asm-expr and attributes, and attach them to a
1559/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001560bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001561 // If a simple-asm-expr is present, parse it.
1562 if (Tok.is(tok::kw_asm)) {
1563 SourceLocation Loc;
1564 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1565 if (AsmLabel.isInvalid()) {
1566 SkipUntil(tok::semi, true, true);
1567 return true;
1568 }
1569
1570 D.setAsmLabel(AsmLabel.release());
1571 D.SetRangeEnd(Loc);
1572 }
1573
1574 MaybeParseGNUAttributes(D);
1575 return false;
1576}
1577
Douglas Gregor1426e532009-05-12 21:31:51 +00001578/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1579/// declarator'. This method parses the remainder of the declaration
1580/// (including any attributes or initializer, among other things) and
1581/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001582///
Reid Spencer5f016e22007-07-11 17:01:13 +00001583/// init-declarator: [C99 6.7]
1584/// declarator
1585/// declarator '=' initializer
1586/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1587/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001588/// [C++] declarator initializer[opt]
1589///
1590/// [C++] initializer:
1591/// [C++] '=' initializer-clause
1592/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001593/// [C++0x] '=' 'default' [TODO]
1594/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001595/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001596///
1597/// According to the standard grammar, =default and =delete are function
1598/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001599///
John McCalld226f652010-08-21 09:40:31 +00001600Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001601 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001602 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001603 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Richard Smithad762fc2011-04-14 22:09:26 +00001605 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1606}
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Richard Smithad762fc2011-04-14 22:09:26 +00001608Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1609 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001610 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001611 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001612 switch (TemplateInfo.Kind) {
1613 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001614 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001615 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001616
Douglas Gregord5a423b2009-09-25 18:43:00 +00001617 case ParsedTemplateInfo::Template:
1618 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001619 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001620 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001621 D);
1622 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001623
Douglas Gregord5a423b2009-09-25 18:43:00 +00001624 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001625 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001626 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001627 TemplateInfo.ExternLoc,
1628 TemplateInfo.TemplateLoc,
1629 D);
1630 if (ThisRes.isInvalid()) {
1631 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001632 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001633 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001634
Douglas Gregord5a423b2009-09-25 18:43:00 +00001635 ThisDecl = ThisRes.get();
1636 break;
1637 }
1638 }
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Richard Smith34b41d92011-02-20 03:19:35 +00001640 bool TypeContainsAuto =
1641 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1642
Douglas Gregor1426e532009-05-12 21:31:51 +00001643 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001644 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001645 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001646 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001647 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001648 if (D.isFunctionDeclarator())
1649 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1650 << 1 /* delete */;
1651 else
1652 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001653 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001654 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001655 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1656 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001657 else
1658 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001659 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001660 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001661 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001662 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001663 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001664
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001665 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001666 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001667 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001668 cutOffParsing();
1669 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001670 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001671
John McCall60d7b3a2010-08-24 06:29:42 +00001672 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001673
David Blaikie4e4d0842012-03-11 07:00:24 +00001674 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001675 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001676 ExitScope();
1677 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001678
Douglas Gregor1426e532009-05-12 21:31:51 +00001679 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001680 SkipUntil(tok::comma, true, true);
1681 Actions.ActOnInitializerError(ThisDecl);
1682 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001683 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1684 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001685 }
1686 } else if (Tok.is(tok::l_paren)) {
1687 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001688 BalancedDelimiterTracker T(*this, tok::l_paren);
1689 T.consumeOpen();
1690
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001691 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001692 CommaLocsTy CommaLocs;
1693
David Blaikie4e4d0842012-03-11 07:00:24 +00001694 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001695 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001696 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001697 }
1698
Douglas Gregor1426e532009-05-12 21:31:51 +00001699 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001700 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001701 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001702
David Blaikie4e4d0842012-03-11 07:00:24 +00001703 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001704 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001705 ExitScope();
1706 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001707 } else {
1708 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001709 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001710
1711 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1712 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001713
David Blaikie4e4d0842012-03-11 07:00:24 +00001714 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001715 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001716 ExitScope();
1717 }
1718
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001719 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1720 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001721 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001722 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1723 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001724 }
Fariborz Jahanian3b5f9dc2012-07-03 22:54:28 +00001725 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001726 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001727 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001728 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1729
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001730 if (D.getCXXScopeSpec().isSet()) {
1731 EnterScope(0);
1732 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1733 }
1734
1735 ExprResult Init(ParseBraceInitializer());
1736
1737 if (D.getCXXScopeSpec().isSet()) {
1738 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1739 ExitScope();
1740 }
1741
1742 if (Init.isInvalid()) {
1743 Actions.ActOnInitializerError(ThisDecl);
1744 } else
1745 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1746 /*DirectInit=*/true, TypeContainsAuto);
1747
Douglas Gregor1426e532009-05-12 21:31:51 +00001748 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001749 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001750 }
1751
Richard Smith483b9f32011-02-21 20:05:19 +00001752 Actions.FinalizeDeclaration(ThisDecl);
1753
Douglas Gregor1426e532009-05-12 21:31:51 +00001754 return ThisDecl;
1755}
1756
Reid Spencer5f016e22007-07-11 17:01:13 +00001757/// ParseSpecifierQualifierList
1758/// specifier-qualifier-list:
1759/// type-specifier specifier-qualifier-list[opt]
1760/// type-qualifier specifier-qualifier-list[opt]
1761/// [GNU] attributes specifier-qualifier-list[opt]
1762///
Richard Smith69730c12012-03-12 07:56:15 +00001763void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1764 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001765 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1766 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001767 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001768 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001769
Reid Spencer5f016e22007-07-11 17:01:13 +00001770 // Validate declspec for type-name.
1771 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001772 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1773 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001774 Diag(Tok, diag::err_expected_type);
1775 DS.SetTypeSpecError();
1776 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1777 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001778 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001779 if (!DS.hasTypeSpecifier())
1780 DS.SetTypeSpecError();
1781 }
Mike Stump1eb44332009-09-09 15:08:12 +00001782
Reid Spencer5f016e22007-07-11 17:01:13 +00001783 // Issue diagnostic and remove storage class if present.
1784 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1785 if (DS.getStorageClassSpecLoc().isValid())
1786 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1787 else
1788 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1789 DS.ClearStorageClassSpecs();
1790 }
Mike Stump1eb44332009-09-09 15:08:12 +00001791
Reid Spencer5f016e22007-07-11 17:01:13 +00001792 // Issue diagnostic and remove function specfier if present.
1793 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001794 if (DS.isInlineSpecified())
1795 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1796 if (DS.isVirtualSpecified())
1797 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1798 if (DS.isExplicitSpecified())
1799 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001800 DS.ClearFunctionSpecs();
1801 }
Richard Smith69730c12012-03-12 07:56:15 +00001802
1803 // Issue diagnostic and remove constexpr specfier if present.
1804 if (DS.isConstexprSpecified()) {
1805 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1806 DS.ClearConstexprSpec();
1807 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001808}
1809
Chris Lattnerc199ab32009-04-12 20:42:31 +00001810/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1811/// specified token is valid after the identifier in a declarator which
1812/// immediately follows the declspec. For example, these things are valid:
1813///
1814/// int x [ 4]; // direct-declarator
1815/// int x ( int y); // direct-declarator
1816/// int(int x ) // direct-declarator
1817/// int x ; // simple-declaration
1818/// int x = 17; // init-declarator-list
1819/// int x , y; // init-declarator-list
1820/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001821/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001822/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001823///
1824/// This is not, because 'x' does not immediately follow the declspec (though
1825/// ')' happens to be valid anyway).
1826/// int (x)
1827///
1828static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1829 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1830 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001831 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001832}
1833
Chris Lattnere40c2952009-04-14 21:34:55 +00001834
1835/// ParseImplicitInt - This method is called when we have an non-typename
1836/// identifier in a declspec (which normally terminates the decl spec) when
1837/// the declspec has no type specifier. In this case, the declspec is either
1838/// malformed or is "implicit int" (in K&R and C89).
1839///
1840/// This method handles diagnosing this prettily and returns false if the
1841/// declspec is done being processed. If it recovers and thinks there may be
1842/// other pieces of declspec after it, it returns true.
1843///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001844bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001845 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00001846 AccessSpecifier AS, DeclSpecContext DSC,
1847 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001848 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Chris Lattnere40c2952009-04-14 21:34:55 +00001850 SourceLocation Loc = Tok.getLocation();
1851 // If we see an identifier that is not a type name, we normally would
1852 // parse it as the identifer being declared. However, when a typename
1853 // is typo'd or the definition is not included, this will incorrectly
1854 // parse the typename as the identifier name and fall over misparsing
1855 // later parts of the diagnostic.
1856 //
1857 // As such, we try to do some look-ahead in cases where this would
1858 // otherwise be an "implicit-int" case to see if this is invalid. For
1859 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1860 // an identifier with implicit int, we'd get a parse error because the
1861 // next token is obviously invalid for a type. Parse these as a case
1862 // with an invalid type specifier.
1863 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001864
Chris Lattnere40c2952009-04-14 21:34:55 +00001865 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001866 // error, do lookahead to try to do better recovery. This never applies
1867 // within a type specifier. Outside of C++, we allow this even if the
1868 // language doesn't "officially" support implicit int -- we support
1869 // implicit int as an extension in C99 and C11. Allegedly, MS also
1870 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001871 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001872 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001873 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001874 // If this token is valid for implicit int, e.g. "static x = 4", then
1875 // we just avoid eating the identifier, so it will be parsed as the
1876 // identifier in the declarator.
1877 return false;
1878 }
Mike Stump1eb44332009-09-09 15:08:12 +00001879
Richard Smith827adaf2012-05-15 21:01:51 +00001880 if (getLangOpts().CPlusPlus &&
1881 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1882 // Don't require a type specifier if we have the 'auto' storage class
1883 // specifier in C++98 -- we'll promote it to a type specifier.
1884 return false;
1885 }
1886
Chris Lattnere40c2952009-04-14 21:34:55 +00001887 // Otherwise, if we don't consume this token, we are going to emit an
1888 // error anyway. Try to recover from various common problems. Check
1889 // to see if this was a reference to a tag name without a tag specified.
1890 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001891 //
1892 // C++ doesn't need this, and isTagName doesn't take SS.
1893 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001894 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001895 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001896
Douglas Gregor23c94db2010-07-02 17:43:08 +00001897 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001898 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001899 case DeclSpec::TST_enum:
1900 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1901 case DeclSpec::TST_union:
1902 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1903 case DeclSpec::TST_struct:
1904 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00001905 case DeclSpec::TST_interface:
1906 TagName="__interface"; FixitTagName = "__interface ";
1907 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001908 case DeclSpec::TST_class:
1909 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001910 }
Mike Stump1eb44332009-09-09 15:08:12 +00001911
Chris Lattnerf4382f52009-04-14 22:17:06 +00001912 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001913 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1914 LookupResult R(Actions, TokenName, SourceLocation(),
1915 Sema::LookupOrdinaryName);
1916
Chris Lattnerf4382f52009-04-14 22:17:06 +00001917 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001918 << TokenName << TagName << getLangOpts().CPlusPlus
1919 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1920
1921 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1922 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1923 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001924 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001925 << TokenName << TagName;
1926 }
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Chris Lattnerf4382f52009-04-14 22:17:06 +00001928 // Parse this as a tag as if the missing tag were present.
1929 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001930 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001931 else
Richard Smith69730c12012-03-12 07:56:15 +00001932 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00001933 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001934 return true;
1935 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001936 }
Mike Stump1eb44332009-09-09 15:08:12 +00001937
Richard Smith8f0a7e72012-05-15 21:29:55 +00001938 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001939 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001940 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1941 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001942 // Look ahead to the next token to try to figure out what this declaration
1943 // was supposed to be.
1944 switch (NextToken().getKind()) {
1945 case tok::comma:
1946 case tok::equal:
1947 case tok::kw_asm:
1948 case tok::l_brace:
1949 case tok::l_square:
1950 case tok::semi:
1951 // This looks like a variable declaration. The type is probably missing.
1952 // We're done parsing decl-specifiers.
1953 return false;
1954
1955 case tok::l_paren: {
1956 // static x(4); // 'x' is not a type
1957 // x(int n); // 'x' is not a type
1958 // x (*p)[]; // 'x' is a type
1959 //
1960 // Since we're in an error case (or the rare 'implicit int in C++' MS
1961 // extension), we can afford to perform a tentative parse to determine
1962 // which case we're in.
1963 TentativeParsingAction PA(*this);
1964 ConsumeToken();
1965 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1966 PA.Revert();
1967 if (TPR == TPResult::False())
1968 return false;
1969 // The identifier is followed by a parenthesized declarator.
1970 // It's supposed to be a type.
1971 break;
1972 }
1973
1974 default:
1975 // This is probably supposed to be a type. This includes cases like:
1976 // int f(itn);
1977 // struct S { unsinged : 4; };
1978 break;
1979 }
1980 }
1981
Chad Rosier8decdee2012-06-26 22:30:43 +00001982 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00001983 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001984 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001985 IdentifierInfo *II = Tok.getIdentifierInfo();
1986 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001987 // The action emitted a diagnostic, so we don't have to.
1988 if (T) {
1989 // The action has suggested that the type T could be used. Set that as
1990 // the type in the declaration specifiers, consume the would-be type
1991 // name token, and we're done.
1992 const char *PrevSpec;
1993 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001994 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001995 DS.SetRangeEnd(Tok.getLocation());
1996 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001997 // There may be other declaration specifiers after this.
1998 return true;
1999 } else if (II != Tok.getIdentifierInfo()) {
2000 // If no type was suggested, the correction is to a keyword
2001 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002002 // There may be other declaration specifiers after this.
2003 return true;
2004 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002005
Douglas Gregora786fdb2009-10-13 23:27:22 +00002006 // Fall through; the action had no suggestion for us.
2007 } else {
2008 // The action did not emit a diagnostic, so emit one now.
2009 SourceRange R;
2010 if (SS) R = SS->getRange();
2011 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2012 }
Mike Stump1eb44332009-09-09 15:08:12 +00002013
Douglas Gregora786fdb2009-10-13 23:27:22 +00002014 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002015 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002016 DS.SetRangeEnd(Tok.getLocation());
2017 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Chris Lattnere40c2952009-04-14 21:34:55 +00002019 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2020 // avoid rippling error messages on subsequent uses of the same type,
2021 // could be useful if #include was forgotten.
2022 return false;
2023}
2024
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002025/// \brief Determine the declaration specifier context from the declarator
2026/// context.
2027///
2028/// \param Context the declarator context, which is one of the
2029/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002030Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002031Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2032 if (Context == Declarator::MemberContext)
2033 return DSC_class;
2034 if (Context == Declarator::FileContext)
2035 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002036 if (Context == Declarator::TrailingReturnContext)
2037 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002038 return DSC_normal;
2039}
2040
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002041/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2042///
2043/// FIXME: Simply returns an alignof() expression if the argument is a
2044/// type. Ideally, the type should be propagated directly into Sema.
2045///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002046/// [C11] type-id
2047/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002048/// [C++0x] type-id ...[opt]
2049/// [C++0x] assignment-expression ...[opt]
2050ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2051 SourceLocation &EllipsisLoc) {
2052 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002053 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002054 SourceLocation TypeLoc = Tok.getLocation();
2055 ParsedType Ty = ParseTypeName().get();
2056 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002057 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2058 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002059 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002060 ER = ParseConstantExpression();
2061
David Blaikie4e4d0842012-03-11 07:00:24 +00002062 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002063 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002064
2065 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002066}
2067
2068/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2069/// attribute to Attrs.
2070///
2071/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002072/// [C11] '_Alignas' '(' type-id ')'
2073/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002074/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
2075/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002076void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2077 SourceLocation *endLoc) {
2078 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2079 "Not an alignment-specifier!");
2080
2081 SourceLocation KWLoc = Tok.getLocation();
2082 ConsumeToken();
2083
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002084 BalancedDelimiterTracker T(*this, tok::l_paren);
2085 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002086 return;
2087
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002088 SourceLocation EllipsisLoc;
2089 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002090 if (ArgExpr.isInvalid()) {
2091 SkipUntil(tok::r_paren);
2092 return;
2093 }
2094
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002095 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002096 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002097 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002098
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002099 // FIXME: Handle pack-expansions here.
2100 if (EllipsisLoc.isValid()) {
2101 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2102 return;
2103 }
2104
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002105 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002106 ArgExprs.push_back(ArgExpr.release());
Sean Hunt8e083e72012-06-19 23:57:03 +00002107 // FIXME: This should not be GNU, but we since the attribute used is
2108 // based on the spelling, and there is no true spelling for
2109 // C++11 attributes, this isn't accepted.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002110 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002111 0, T.getOpenLocation(), ArgExprs.data(), 1,
Sean Hunt8e083e72012-06-19 23:57:03 +00002112 AttributeList::AS_GNU);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002113}
2114
Reid Spencer5f016e22007-07-11 17:01:13 +00002115/// ParseDeclarationSpecifiers
2116/// declaration-specifiers: [C99 6.7]
2117/// storage-class-specifier declaration-specifiers[opt]
2118/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002119/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002120/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002121/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002122/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002123///
2124/// storage-class-specifier: [C99 6.7.1]
2125/// 'typedef'
2126/// 'extern'
2127/// 'static'
2128/// 'auto'
2129/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002130/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002131/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002132/// function-specifier: [C99 6.7.4]
2133/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002134/// [C++] 'virtual'
2135/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002136/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002137/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002138/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002139
Reid Spencer5f016e22007-07-11 17:01:13 +00002140///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002141void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002142 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002143 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002144 DeclSpecContext DSContext,
2145 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002146 if (DS.getSourceRange().isInvalid()) {
2147 DS.SetRangeStart(Tok.getLocation());
2148 DS.SetRangeEnd(Tok.getLocation());
2149 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002150
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002151 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002152 bool AttrsLastTime = false;
2153 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002154 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002155 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002156 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002157 unsigned DiagID = 0;
2158
Reid Spencer5f016e22007-07-11 17:01:13 +00002159 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002160
Reid Spencer5f016e22007-07-11 17:01:13 +00002161 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002162 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002163 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002164 if (!AttrsLastTime)
2165 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002166 else {
2167 // Reject C++11 attributes that appertain to decl specifiers as
2168 // we don't support any C++11 attributes that appertain to decl
2169 // specifiers. This also conforms to what g++ 4.8 is doing.
2170 ProhibitCXX11Attributes(attrs);
2171
Sean Hunt2edf0a22012-06-23 05:07:58 +00002172 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002173 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002174
Reid Spencer5f016e22007-07-11 17:01:13 +00002175 // If this is not a declaration specifier token, we're done reading decl
2176 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002177 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002178 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002179
Sean Hunt2edf0a22012-06-23 05:07:58 +00002180 case tok::l_square:
2181 case tok::kw_alignas:
2182 if (!isCXX11AttributeSpecifier())
2183 goto DoneWithDeclSpec;
2184
2185 ProhibitAttributes(attrs);
2186 // FIXME: It would be good to recover by accepting the attributes,
2187 // but attempting to do that now would cause serious
2188 // madness in terms of diagnostics.
2189 attrs.clear();
2190 attrs.Range = SourceRange();
2191
2192 ParseCXX11Attributes(attrs);
2193 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002194 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002195
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002196 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002197 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002198 if (DS.hasTypeSpecifier()) {
2199 bool AllowNonIdentifiers
2200 = (getCurScope()->getFlags() & (Scope::ControlScope |
2201 Scope::BlockScope |
2202 Scope::TemplateParamScope |
2203 Scope::FunctionPrototypeScope |
2204 Scope::AtCatchScope)) == 0;
2205 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002206 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002207 (DSContext == DSC_class && DS.isFriendSpecified());
2208
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002209 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002210 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002211 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002212 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002213 }
2214
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002215 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2216 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2217 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002218 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002219 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002220 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002221 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002222 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002223 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002224
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002225 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002226 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002227 }
2228
Chris Lattner5e02c472009-01-05 00:07:25 +00002229 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002230 // C++ scope specifier. Annotate and loop, or bail out on error.
2231 if (TryAnnotateCXXScopeToken(true)) {
2232 if (!DS.hasTypeSpecifier())
2233 DS.SetTypeSpecError();
2234 goto DoneWithDeclSpec;
2235 }
John McCall2e0a7152010-03-01 18:20:46 +00002236 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2237 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002238 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002239
2240 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002241 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002242 goto DoneWithDeclSpec;
2243
John McCallaa87d332009-12-12 11:40:51 +00002244 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002245 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2246 Tok.getAnnotationRange(),
2247 SS);
John McCallaa87d332009-12-12 11:40:51 +00002248
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002249 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002250 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002251 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002252 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002253 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002254 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002255
2256 // C++ [class.qual]p2:
2257 // In a lookup in which the constructor is an acceptable lookup
2258 // result and the nested-name-specifier nominates a class C:
2259 //
2260 // - if the name specified after the
2261 // nested-name-specifier, when looked up in C, is the
2262 // injected-class-name of C (Clause 9), or
2263 //
2264 // - if the name specified after the nested-name-specifier
2265 // is the same as the identifier or the
2266 // simple-template-id's template-name in the last
2267 // component of the nested-name-specifier,
2268 //
2269 // the name is instead considered to name the constructor of
2270 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002271 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002272 // Thus, if the template-name is actually the constructor
2273 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002274 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002275 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002276 if ((DSContext == DSC_top_level ||
2277 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2278 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002279 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002280 if (isConstructorDeclarator()) {
2281 // The user meant this to be an out-of-line constructor
2282 // definition, but template arguments are not allowed
2283 // there. Just allow this as a constructor; we'll
2284 // complain about it later.
2285 goto DoneWithDeclSpec;
2286 }
2287
2288 // The user meant this to name a type, but it actually names
2289 // a constructor with some extraneous template
2290 // arguments. Complain, then parse it as a type as the user
2291 // intended.
2292 Diag(TemplateId->TemplateNameLoc,
2293 diag::err_out_of_line_template_id_names_constructor)
2294 << TemplateId->Name;
2295 }
2296
John McCallaa87d332009-12-12 11:40:51 +00002297 DS.getTypeSpecScope() = SS;
2298 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002299 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002300 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002301 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002302 continue;
2303 }
2304
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002305 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002306 DS.getTypeSpecScope() = SS;
2307 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002308 if (Tok.getAnnotationValue()) {
2309 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002310 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002311 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002312 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002313 if (isInvalid)
2314 break;
John McCallb3d87482010-08-24 05:47:05 +00002315 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002316 else
2317 DS.SetTypeSpecError();
2318 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2319 ConsumeToken(); // The typename
2320 }
2321
Douglas Gregor9135c722009-03-25 15:40:00 +00002322 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002323 goto DoneWithDeclSpec;
2324
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002325 // If we're in a context where the identifier could be a class name,
2326 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002327 if ((DSContext == DSC_top_level ||
2328 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002329 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002330 &SS)) {
2331 if (isConstructorDeclarator())
2332 goto DoneWithDeclSpec;
2333
2334 // As noted in C++ [class.qual]p2 (cited above), when the name
2335 // of the class is qualified in a context where it could name
2336 // a constructor, its a constructor name. However, we've
2337 // looked at the declarator, and the user probably meant this
2338 // to be a type. Complain that it isn't supposed to be treated
2339 // as a type, then proceed to parse it as a type.
2340 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2341 << Next.getIdentifierInfo();
2342 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002343
John McCallb3d87482010-08-24 05:47:05 +00002344 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2345 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002346 getCurScope(), &SS,
2347 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002348 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002349 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002350
Chris Lattnerf4382f52009-04-14 22:17:06 +00002351 // If the referenced identifier is not a type, then this declspec is
2352 // erroneous: We already checked about that it has no type specifier, and
2353 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002354 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002355 if (TypeRep == 0) {
2356 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002357 ParsedAttributesWithRange Attrs(AttrFactory);
2358 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2359 if (!Attrs.empty()) {
2360 AttrsLastTime = true;
2361 attrs.takeAllFrom(Attrs);
2362 }
2363 continue;
2364 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002365 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002366 }
Mike Stump1eb44332009-09-09 15:08:12 +00002367
John McCallaa87d332009-12-12 11:40:51 +00002368 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002369 ConsumeToken(); // The C++ scope.
2370
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002371 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002372 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002373 if (isInvalid)
2374 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002375
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002376 DS.SetRangeEnd(Tok.getLocation());
2377 ConsumeToken(); // The typename.
2378
2379 continue;
2380 }
Mike Stump1eb44332009-09-09 15:08:12 +00002381
Chris Lattner80d0c892009-01-21 19:48:37 +00002382 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002383 if (Tok.getAnnotationValue()) {
2384 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002385 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002386 DiagID, T);
2387 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002388 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002389
Chris Lattner5c5db552010-04-05 18:18:31 +00002390 if (isInvalid)
2391 break;
2392
Chris Lattner80d0c892009-01-21 19:48:37 +00002393 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2394 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002395
Chris Lattner80d0c892009-01-21 19:48:37 +00002396 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2397 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002398 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002399 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002400 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002401
Chris Lattner80d0c892009-01-21 19:48:37 +00002402 continue;
2403 }
Mike Stump1eb44332009-09-09 15:08:12 +00002404
Douglas Gregorbfad9152011-04-28 15:48:45 +00002405 case tok::kw___is_signed:
2406 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2407 // typically treats it as a trait. If we see __is_signed as it appears
2408 // in libstdc++, e.g.,
2409 //
2410 // static const bool __is_signed;
2411 //
2412 // then treat __is_signed as an identifier rather than as a keyword.
2413 if (DS.getTypeSpecType() == TST_bool &&
2414 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2415 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2416 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2417 Tok.setKind(tok::identifier);
2418 }
2419
2420 // We're done with the declaration-specifiers.
2421 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002422
Chris Lattner3bd934a2008-07-26 01:18:38 +00002423 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002424 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002425 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002426 // In C++, check to see if this is a scope specifier like foo::bar::, if
2427 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002428 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002429 if (TryAnnotateCXXScopeToken(true)) {
2430 if (!DS.hasTypeSpecifier())
2431 DS.SetTypeSpecError();
2432 goto DoneWithDeclSpec;
2433 }
2434 if (!Tok.is(tok::identifier))
2435 continue;
2436 }
Mike Stump1eb44332009-09-09 15:08:12 +00002437
Chris Lattner3bd934a2008-07-26 01:18:38 +00002438 // This identifier can only be a typedef name if we haven't already seen
2439 // a type-specifier. Without this check we misparse:
2440 // typedef int X; struct Y { short X; }; as 'short int'.
2441 if (DS.hasTypeSpecifier())
2442 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002443
John Thompson82287d12010-02-05 00:12:22 +00002444 // Check for need to substitute AltiVec keyword tokens.
2445 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2446 break;
2447
Richard Smithf63eee72012-05-09 18:56:43 +00002448 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2449 // allow the use of a typedef name as a type specifier.
2450 if (DS.isTypeAltiVecVector())
2451 goto DoneWithDeclSpec;
2452
John McCallb3d87482010-08-24 05:47:05 +00002453 ParsedType TypeRep =
2454 Actions.getTypeName(*Tok.getIdentifierInfo(),
2455 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002456
Chris Lattnerc199ab32009-04-12 20:42:31 +00002457 // If this is not a typedef name, don't parse it as part of the declspec,
2458 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002459 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002460 ParsedAttributesWithRange Attrs(AttrFactory);
2461 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2462 if (!Attrs.empty()) {
2463 AttrsLastTime = true;
2464 attrs.takeAllFrom(Attrs);
2465 }
2466 continue;
2467 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002468 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002469 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002470
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002471 // If we're in a context where the identifier could be a class name,
2472 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002473 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002474 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002475 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002476 goto DoneWithDeclSpec;
2477
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002478 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002479 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002480 if (isInvalid)
2481 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002482
Chris Lattner3bd934a2008-07-26 01:18:38 +00002483 DS.SetRangeEnd(Tok.getLocation());
2484 ConsumeToken(); // The identifier
2485
2486 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2487 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002488 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002489 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002490 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002491
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002492 // Need to support trailing type qualifiers (e.g. "id<p> const").
2493 // If a type specifier follows, it will be diagnosed elsewhere.
2494 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002495 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002496
2497 // type-name
2498 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002499 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002500 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002501 // This template-id does not refer to a type name, so we're
2502 // done with the type-specifiers.
2503 goto DoneWithDeclSpec;
2504 }
2505
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002506 // If we're in a context where the template-id could be a
2507 // constructor name or specialization, check whether this is a
2508 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002509 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002510 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002511 isConstructorDeclarator())
2512 goto DoneWithDeclSpec;
2513
Douglas Gregor39a8de12009-02-25 19:37:18 +00002514 // Turn the template-id annotation token into a type annotation
2515 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002516 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002517 continue;
2518 }
2519
Reid Spencer5f016e22007-07-11 17:01:13 +00002520 // GNU attributes support.
2521 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002522 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002523 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002524
2525 // Microsoft declspec support.
2526 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002527 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002528 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002529
Steve Naroff239f0732008-12-25 14:16:32 +00002530 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002531 case tok::kw___forceinline: {
2532 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2533 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002534 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002535 // FIXME: This does not work correctly if it is set to be a declspec
2536 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002537 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002538 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002539 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002540 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002541
2542 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002543 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002544 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002545 case tok::kw___cdecl:
2546 case tok::kw___stdcall:
2547 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002548 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002549 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002550 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002551 continue;
2552
Dawn Perchik52fc3142010-09-03 01:29:35 +00002553 // Borland single token adornments.
2554 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002555 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002556 continue;
2557
Peter Collingbournef315fa82011-02-14 01:42:53 +00002558 // OpenCL single token adornments.
2559 case tok::kw___kernel:
2560 ParseOpenCLAttributes(DS.getAttributes());
2561 continue;
2562
Reid Spencer5f016e22007-07-11 17:01:13 +00002563 // storage-class-specifier
2564 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002565 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2566 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002567 break;
2568 case tok::kw_extern:
2569 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002570 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002571 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2572 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002573 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002574 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002575 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2576 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002577 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002578 case tok::kw_static:
2579 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002580 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002581 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2582 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002583 break;
2584 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002585 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002586 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002587 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2588 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002589 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002590 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002591 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002592 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002593 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2594 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002595 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002596 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2597 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002598 break;
2599 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002600 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2601 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002602 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002603 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002604 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2605 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002606 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002607 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002608 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002609 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002610
Reid Spencer5f016e22007-07-11 17:01:13 +00002611 // function-specifier
2612 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002613 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002614 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002615 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002616 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002617 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002618 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002619 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002620 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002621
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002622 // alignment-specifier
2623 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002624 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002625 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002626 ParseAlignmentSpecifier(DS.getAttributes());
2627 continue;
2628
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002629 // friend
2630 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002631 if (DSContext == DSC_class)
2632 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2633 else {
2634 PrevSpec = ""; // not actually used by the diagnostic
2635 DiagID = diag::err_friend_invalid_in_context;
2636 isInvalid = true;
2637 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002638 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002639
Douglas Gregor8d267c52011-09-09 02:06:17 +00002640 // Modules
2641 case tok::kw___module_private__:
2642 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2643 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002644
Sebastian Redl2ac67232009-11-05 15:47:02 +00002645 // constexpr
2646 case tok::kw_constexpr:
2647 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2648 break;
2649
Chris Lattner80d0c892009-01-21 19:48:37 +00002650 // type-specifier
2651 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002652 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2653 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002654 break;
2655 case tok::kw_long:
2656 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002657 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2658 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002659 else
John McCallfec54012009-08-03 20:12:06 +00002660 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2661 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002662 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002663 case tok::kw___int64:
2664 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2665 DiagID);
2666 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002667 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002668 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2669 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002670 break;
2671 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002672 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2673 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002674 break;
2675 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002676 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2677 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002678 break;
2679 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002680 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2681 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002682 break;
2683 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002684 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2685 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002686 break;
2687 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002688 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2689 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002690 break;
2691 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002692 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2693 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002694 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002695 case tok::kw___int128:
2696 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2697 DiagID);
2698 break;
2699 case tok::kw_half:
2700 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2701 DiagID);
2702 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002703 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002704 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2705 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002706 break;
2707 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002708 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2709 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002710 break;
2711 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2713 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002714 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002715 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002716 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2717 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002718 break;
2719 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002720 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2721 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002722 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002723 case tok::kw_bool:
2724 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002725 if (Tok.is(tok::kw_bool) &&
2726 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2727 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2728 PrevSpec = ""; // Not used by the diagnostic.
2729 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002730 // For better error recovery.
2731 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002732 isInvalid = true;
2733 } else {
2734 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2735 DiagID);
2736 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002737 break;
2738 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002739 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2740 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002741 break;
2742 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002743 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2744 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002745 break;
2746 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002747 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2748 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002749 break;
John Thompson82287d12010-02-05 00:12:22 +00002750 case tok::kw___vector:
2751 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2752 break;
2753 case tok::kw___pixel:
2754 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2755 break;
John McCalla5fc4722011-04-09 22:50:59 +00002756 case tok::kw___unknown_anytype:
2757 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2758 PrevSpec, DiagID);
2759 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002760
2761 // class-specifier:
2762 case tok::kw_class:
2763 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00002764 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00002765 case tok::kw_union: {
2766 tok::TokenKind Kind = Tok.getKind();
2767 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00002768
2769 // These are attributes following class specifiers.
2770 // To produce better diagnostic, we parse them when
2771 // parsing class specifier.
2772 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00002773 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00002774 EnteringContext, DSContext, Attributes);
2775
2776 // If there are attributes following class specifier,
2777 // take them over and handle them here.
2778 if (!Attributes.empty()) {
2779 AttrsLastTime = true;
2780 attrs.takeAllFrom(Attributes);
2781 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002782 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002783 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002784
2785 // enum-specifier:
2786 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002787 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002788 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002789 continue;
2790
2791 // cv-qualifier:
2792 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002793 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002794 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002795 break;
2796 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002797 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002798 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002799 break;
2800 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002801 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002802 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002803 break;
2804
Douglas Gregord57959a2009-03-27 23:10:48 +00002805 // C++ typename-specifier:
2806 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002807 if (TryAnnotateTypeOrScopeToken()) {
2808 DS.SetTypeSpecError();
2809 goto DoneWithDeclSpec;
2810 }
2811 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002812 continue;
2813 break;
2814
Chris Lattner80d0c892009-01-21 19:48:37 +00002815 // GNU typeof support.
2816 case tok::kw_typeof:
2817 ParseTypeofSpecifier(DS);
2818 continue;
2819
David Blaikie42d6d0c2011-12-04 05:04:18 +00002820 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002821 ParseDecltypeSpecifier(DS);
2822 continue;
2823
Sean Huntdb5d44b2011-05-19 05:37:45 +00002824 case tok::kw___underlying_type:
2825 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002826 continue;
2827
2828 case tok::kw__Atomic:
2829 ParseAtomicSpecifier(DS);
2830 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002831
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002832 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00002833 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002834 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002835 goto DoneWithDeclSpec;
2836 case tok::kw___private:
2837 case tok::kw___global:
2838 case tok::kw___local:
2839 case tok::kw___constant:
2840 case tok::kw___read_only:
2841 case tok::kw___write_only:
2842 case tok::kw___read_write:
2843 ParseOpenCLQualifiers(DS);
2844 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002845
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002846 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002847 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002848 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2849 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002850 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002851 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002852
Douglas Gregor46f936e2010-11-19 17:10:50 +00002853 if (!ParseObjCProtocolQualifiers(DS))
2854 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2855 << FixItHint::CreateInsertion(Loc, "id")
2856 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00002857
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002858 // Need to support trailing type qualifiers (e.g. "id<p> const").
2859 // If a type specifier follows, it will be diagnosed elsewhere.
2860 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002861 }
John McCallfec54012009-08-03 20:12:06 +00002862 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002863 if (isInvalid) {
2864 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002865 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00002866
Douglas Gregorae2fb142010-08-23 14:34:43 +00002867 if (DiagID == diag::ext_duplicate_declspec)
2868 Diag(Tok, DiagID)
2869 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2870 else
2871 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002872 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002873
Chris Lattner81c018d2008-03-13 06:29:04 +00002874 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002875 if (DiagID != diag::err_bool_redeclaration)
2876 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002877
2878 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002879 }
2880}
Douglas Gregoradcac882008-12-01 23:54:00 +00002881
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002882/// ParseStructDeclaration - Parse a struct declaration without the terminating
2883/// semicolon.
2884///
Reid Spencer5f016e22007-07-11 17:01:13 +00002885/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002886/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002887/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002888/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002889/// struct-declarator-list:
2890/// struct-declarator
2891/// struct-declarator-list ',' struct-declarator
2892/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2893/// struct-declarator:
2894/// declarator
2895/// [GNU] declarator attributes[opt]
2896/// declarator[opt] ':' constant-expression
2897/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2898///
Chris Lattnere1359422008-04-10 06:46:29 +00002899void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002900ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00002901
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002902 if (Tok.is(tok::kw___extension__)) {
2903 // __extension__ silences extension warnings in the subexpression.
2904 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002905 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002906 return ParseStructDeclaration(DS, Fields);
2907 }
Mike Stump1eb44332009-09-09 15:08:12 +00002908
Steve Naroff28a7ca82007-08-20 22:28:22 +00002909 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002910 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002911
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002912 // If there are no declarators, this is a free-standing declaration
2913 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002914 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002915 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2916 DS);
2917 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002918 return;
2919 }
2920
2921 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002922 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002923 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002924 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002925 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00002926 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002927
2928 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002929 if (!FirstDeclarator)
2930 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002931
Steve Naroff28a7ca82007-08-20 22:28:22 +00002932 /// struct-declarator: declarator
2933 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002934 if (Tok.isNot(tok::colon)) {
2935 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2936 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002937 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002938 }
Mike Stump1eb44332009-09-09 15:08:12 +00002939
Chris Lattner04d66662007-10-09 17:33:22 +00002940 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002941 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002942 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002943 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002944 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002945 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002946 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002947 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002948
Steve Naroff28a7ca82007-08-20 22:28:22 +00002949 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002950 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002951
John McCallbdd563e2009-11-03 02:38:08 +00002952 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00002953 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00002954
Steve Naroff28a7ca82007-08-20 22:28:22 +00002955 // If we don't have a comma, it is either the end of the list (a ';')
2956 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002957 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002958 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002959
Steve Naroff28a7ca82007-08-20 22:28:22 +00002960 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002961 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002962
John McCallbdd563e2009-11-03 02:38:08 +00002963 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002964 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002965}
2966
2967/// ParseStructUnionBody
2968/// struct-contents:
2969/// struct-declaration-list
2970/// [EXT] empty
2971/// [GNU] "struct-declaration-list" without terminatoring ';'
2972/// struct-declaration-list:
2973/// struct-declaration
2974/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002975/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002976///
Reid Spencer5f016e22007-07-11 17:01:13 +00002977void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002978 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002979 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2980 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002981
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002982 BalancedDelimiterTracker T(*this, tok::l_brace);
2983 if (T.consumeOpen())
2984 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002985
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002986 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002987 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002988
Reid Spencer5f016e22007-07-11 17:01:13 +00002989 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2990 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002991 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002992 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2993 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2994 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002995
Chris Lattner5f9e2722011-07-23 10:55:15 +00002996 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002997
Reid Spencer5f016e22007-07-11 17:01:13 +00002998 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002999 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003000 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003001
Reid Spencer5f016e22007-07-11 17:01:13 +00003002 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003003 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003004 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003005 continue;
3006 }
Chris Lattnere1359422008-04-10 06:46:29 +00003007
John McCallbdd563e2009-11-03 02:38:08 +00003008 if (!Tok.is(tok::at)) {
3009 struct CFieldCallback : FieldCallback {
3010 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003011 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003012 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003013
John McCalld226f652010-08-21 09:40:31 +00003014 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003015 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003016 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3017
Eli Friedmandcdff462012-08-08 23:53:27 +00003018 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003019 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003020 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003021 FD.D.getDeclSpec().getSourceRange().getBegin(),
3022 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003023 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003024 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003025 }
John McCallbdd563e2009-11-03 02:38:08 +00003026 } Callback(*this, TagDecl, FieldDecls);
3027
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003028 // Parse all the comma separated declarators.
3029 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003030 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003031 } else { // Handle @defs
3032 ConsumeToken();
3033 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3034 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003035 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003036 continue;
3037 }
3038 ConsumeToken();
3039 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3040 if (!Tok.is(tok::identifier)) {
3041 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003042 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003043 continue;
3044 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003045 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003046 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003047 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003048 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3049 ConsumeToken();
3050 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003051 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003052
Chris Lattner04d66662007-10-09 17:33:22 +00003053 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003054 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003055 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003056 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003057 break;
3058 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003059 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3060 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003061 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003062 // If we stopped at a ';', eat it.
3063 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003064 }
3065 }
Mike Stump1eb44332009-09-09 15:08:12 +00003066
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003067 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003068
John McCall0b7e6782011-03-24 11:26:52 +00003069 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003070 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003071 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003072
Douglas Gregor23c94db2010-07-02 17:43:08 +00003073 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003074 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003075 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003076 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003077 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003078 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3079 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003080}
3081
Reid Spencer5f016e22007-07-11 17:01:13 +00003082/// ParseEnumSpecifier
3083/// enum-specifier: [C99 6.7.2.2]
3084/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003085///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003086/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3087/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003088/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3089/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003090/// 'enum' identifier
3091/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003092///
Richard Smith1af83c42012-03-23 03:33:32 +00003093/// [C++11] enum-head '{' enumerator-list[opt] '}'
3094/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003095///
Richard Smith1af83c42012-03-23 03:33:32 +00003096/// enum-head: [C++11]
3097/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3098/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3099/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003100///
Richard Smith1af83c42012-03-23 03:33:32 +00003101/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003102/// 'enum'
3103/// 'enum' 'class'
3104/// 'enum' 'struct'
3105///
Richard Smith1af83c42012-03-23 03:33:32 +00003106/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003107/// ':' type-specifier-seq
3108///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003109/// [C++] elaborated-type-specifier:
3110/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3111///
Chris Lattner4c97d762009-04-12 21:49:30 +00003112void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003113 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003114 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003115 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003116 if (Tok.is(tok::code_completion)) {
3117 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003118 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003119 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003120 }
John McCall57c13002011-07-06 05:58:41 +00003121
Sean Hunt2edf0a22012-06-23 05:07:58 +00003122 // If attributes exist after tag, parse them.
3123 ParsedAttributesWithRange attrs(AttrFactory);
3124 MaybeParseGNUAttributes(attrs);
3125 MaybeParseCXX0XAttributes(attrs);
3126
3127 // If declspecs exist after tag, parse them.
3128 while (Tok.is(tok::kw___declspec))
3129 ParseMicrosoftDeclSpec(attrs);
3130
Richard Smithbdad7a22012-01-10 01:33:14 +00003131 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003132 bool IsScopedUsingClassTag = false;
3133
John McCall1e12b3d2012-06-23 22:30:04 +00003134 // In C++11, recognize 'enum class' and 'enum struct'.
David Blaikie4e4d0842012-03-11 07:00:24 +00003135 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00003136 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00003137 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003138 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003139 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003140
John McCall1e12b3d2012-06-23 22:30:04 +00003141 // Attributes are not allowed between these keywords. Diagnose,
3142 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003143 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003144
3145 // They are allowed afterwards, though.
3146 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003147 MaybeParseCXX0XAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003148 while (Tok.is(tok::kw___declspec))
3149 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003150 }
Richard Smith1af83c42012-03-23 03:33:32 +00003151
John McCall13489672012-05-07 06:16:58 +00003152 // C++11 [temp.explicit]p12:
3153 // The usual access controls do not apply to names used to specify
3154 // explicit instantiations.
3155 // We extend this to also cover explicit specializations. Note that
3156 // we don't suppress if this turns out to be an elaborated type
3157 // specifier.
3158 bool shouldDelayDiagsInTag =
3159 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3160 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3161 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003162
Richard Smith7796eb52012-03-12 08:56:40 +00003163 // Enum definitions should not be parsed in a trailing-return-type.
3164 bool AllowDeclaration = DSC != DSC_trailing;
3165
3166 bool AllowFixedUnderlyingType = AllowDeclaration &&
3167 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
3168 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003169
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003170 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003171 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003172 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3173 // if a fixed underlying type is allowed.
3174 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003175
3176 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003177 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00003178 return;
3179
3180 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003181 Diag(Tok, diag::err_expected_ident);
3182 if (Tok.isNot(tok::l_brace)) {
3183 // Has no name and is not a definition.
3184 // Skip the rest of this declarator, up until the comma or semicolon.
3185 SkipUntil(tok::comma, true);
3186 return;
3187 }
3188 }
3189 }
Mike Stump1eb44332009-09-09 15:08:12 +00003190
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003191 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003192 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003193 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003194 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003195
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003196 // Skip the rest of this declarator, up until the comma or semicolon.
3197 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003198 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003199 }
Mike Stump1eb44332009-09-09 15:08:12 +00003200
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003201 // If an identifier is present, consume and remember it.
3202 IdentifierInfo *Name = 0;
3203 SourceLocation NameLoc;
3204 if (Tok.is(tok::identifier)) {
3205 Name = Tok.getIdentifierInfo();
3206 NameLoc = ConsumeToken();
3207 }
Mike Stump1eb44332009-09-09 15:08:12 +00003208
Richard Smithbdad7a22012-01-10 01:33:14 +00003209 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003210 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3211 // declaration of a scoped enumeration.
3212 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003213 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003214 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003215 }
3216
John McCall13489672012-05-07 06:16:58 +00003217 // Okay, end the suppression area. We'll decide whether to emit the
3218 // diagnostics in a second.
3219 if (shouldDelayDiagsInTag)
3220 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003221
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003222 TypeResult BaseType;
3223
Douglas Gregora61b3e72010-12-01 17:42:47 +00003224 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003225 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003226 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003227 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003228 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003229 // If we're in class scope, this can either be an enum declaration with
3230 // an underlying type, or a declaration of a bitfield member. We try to
3231 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003232 // (integer literal, sizeof); if it's still ambiguous, we then consider
3233 // anything that's a simple-type-specifier followed by '(' as an
3234 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003235 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003236 EnterExpressionEvaluationContext Unevaluated(Actions,
3237 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003238 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003239 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003240 // bit-field. This is the common case.
3241 if (TPR == TPResult::True())
3242 PossibleBitfield = true;
3243 // If the next token starts a type-specifier-seq, it may be either a
3244 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003245 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003246 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003247 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003248 GetLookAheadToken(2).getKind() == tok::semi) {
3249 // Consume the ':'.
3250 ConsumeToken();
3251 } else {
3252 // We have the start of a type-specifier-seq, so we have to perform
3253 // tentative parsing to determine whether we have an expression or a
3254 // type.
3255 TentativeParsingAction TPA(*this);
3256
3257 // Consume the ':'.
3258 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003259
3260 // If we see a type specifier followed by an open-brace, we have an
3261 // ambiguity between an underlying type and a C++11 braced
3262 // function-style cast. Resolve this by always treating it as an
3263 // underlying type.
3264 // FIXME: The standard is not entirely clear on how to disambiguate in
3265 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003266 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003267 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003268 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003269 // We'll parse this as a bitfield later.
3270 PossibleBitfield = true;
3271 TPA.Revert();
3272 } else {
3273 // We have a type-specifier-seq.
3274 TPA.Commit();
3275 }
3276 }
3277 } else {
3278 // Consume the ':'.
3279 ConsumeToken();
3280 }
3281
3282 if (!PossibleBitfield) {
3283 SourceRange Range;
3284 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003285
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003286 if (getLangOpts().CPlusPlus0x) {
Richard Smith7fe62082011-10-15 05:09:34 +00003287 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003288 } else if (!getLangOpts().ObjC2) {
3289 if (getLangOpts().CPlusPlus)
3290 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3291 else
3292 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3293 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003294 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003295 }
3296
Richard Smithbdad7a22012-01-10 01:33:14 +00003297 // There are four options here. If we have 'friend enum foo;' then this is a
3298 // friend declaration, and cannot have an accompanying definition. If we have
3299 // 'enum foo;', then this is a forward declaration. If we have
3300 // 'enum foo {...' then this is a definition. Otherwise we have something
3301 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003302 //
3303 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3304 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3305 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3306 //
John McCallf312b1e2010-08-26 23:41:50 +00003307 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003308 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003309 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003310 } else if (Tok.is(tok::l_brace)) {
3311 if (DS.isFriendSpecified()) {
3312 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3313 << SourceRange(DS.getFriendSpecLoc());
3314 ConsumeBrace();
3315 SkipUntil(tok::r_brace);
3316 TUK = Sema::TUK_Friend;
3317 } else {
3318 TUK = Sema::TUK_Definition;
3319 }
Richard Smithc9f35172012-06-25 21:37:02 +00003320 } else if (DSC != DSC_type_specifier &&
3321 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003322 (Tok.isAtStartOfLine() &&
3323 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003324 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3325 if (Tok.isNot(tok::semi)) {
3326 // A semicolon was missing after this declaration. Diagnose and recover.
3327 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3328 "enum");
3329 PP.EnterToken(Tok);
3330 Tok.setKind(tok::semi);
3331 }
John McCall13489672012-05-07 06:16:58 +00003332 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003333 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003334 }
3335
3336 // If this is an elaborated type specifier, and we delayed
3337 // diagnostics before, just merge them into the current pool.
3338 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3339 diagsFromTag.redelay();
3340 }
Richard Smith1af83c42012-03-23 03:33:32 +00003341
3342 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003343 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003344 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003345 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3346 // Skip the rest of this declarator, up until the comma or semicolon.
3347 Diag(Tok, diag::err_enum_template);
3348 SkipUntil(tok::comma, true);
3349 return;
3350 }
3351
3352 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3353 // Enumerations can't be explicitly instantiated.
3354 DS.SetTypeSpecError();
3355 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3356 return;
3357 }
3358
3359 assert(TemplateInfo.TemplateParams && "no template parameters");
3360 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3361 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003362 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003363
Sean Hunt2edf0a22012-06-23 05:07:58 +00003364 if (TUK == Sema::TUK_Reference)
3365 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003366
Douglas Gregorb9075602011-02-22 02:55:24 +00003367 if (!Name && TUK != Sema::TUK_Definition) {
3368 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003369
Douglas Gregorb9075602011-02-22 02:55:24 +00003370 // Skip the rest of this declarator, up until the comma or semicolon.
3371 SkipUntil(tok::comma, true);
3372 return;
3373 }
Richard Smith1af83c42012-03-23 03:33:32 +00003374
Douglas Gregor402abb52009-05-28 23:31:59 +00003375 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003376 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003377 const char *PrevSpec = 0;
3378 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003379 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003380 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003381 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003382 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003383 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003384
Douglas Gregor48c89f42010-04-24 16:38:41 +00003385 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003386 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003387 // dependent tag.
3388 if (!Name) {
3389 DS.SetTypeSpecError();
3390 Diag(Tok, diag::err_expected_type_name_after_typename);
3391 return;
3392 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003393
Douglas Gregor23c94db2010-07-02 17:43:08 +00003394 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003395 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003396 NameLoc);
3397 if (Type.isInvalid()) {
3398 DS.SetTypeSpecError();
3399 return;
3400 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003401
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003402 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3403 NameLoc.isValid() ? NameLoc : StartLoc,
3404 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003405 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003406
Douglas Gregor48c89f42010-04-24 16:38:41 +00003407 return;
3408 }
Mike Stump1eb44332009-09-09 15:08:12 +00003409
John McCalld226f652010-08-21 09:40:31 +00003410 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003411 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003412 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003413 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003414 ConsumeBrace();
3415 SkipUntil(tok::r_brace);
3416 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003417
Douglas Gregor48c89f42010-04-24 16:38:41 +00003418 DS.SetTypeSpecError();
3419 return;
3420 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003421
Richard Smithc9f35172012-06-25 21:37:02 +00003422 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003423 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003424
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003425 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3426 NameLoc.isValid() ? NameLoc : StartLoc,
3427 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003428 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003429}
3430
3431/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3432/// enumerator-list:
3433/// enumerator
3434/// enumerator-list ',' enumerator
3435/// enumerator:
3436/// enumeration-constant
3437/// enumeration-constant '=' constant-expression
3438/// enumeration-constant:
3439/// identifier
3440///
John McCalld226f652010-08-21 09:40:31 +00003441void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003442 // Enter the scope of the enum body and start the definition.
3443 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003444 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003445
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003446 BalancedDelimiterTracker T(*this, tok::l_brace);
3447 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003448
Chris Lattner7946dd32007-08-27 17:24:30 +00003449 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003450 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003451 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003452
Chris Lattner5f9e2722011-07-23 10:55:15 +00003453 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003454
John McCalld226f652010-08-21 09:40:31 +00003455 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003456
Reid Spencer5f016e22007-07-11 17:01:13 +00003457 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003458 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003459 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3460 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003461
John McCall5b629aa2010-10-22 23:36:17 +00003462 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003463 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003464 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003465 MaybeParseCXX0XAttributes(attrs);
3466 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003467
Reid Spencer5f016e22007-07-11 17:01:13 +00003468 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003469 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003470 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003471
Chris Lattner04d66662007-10-09 17:33:22 +00003472 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003473 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003474 AssignedVal = ParseConstantExpression();
3475 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003476 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003477 }
Mike Stump1eb44332009-09-09 15:08:12 +00003478
Reid Spencer5f016e22007-07-11 17:01:13 +00003479 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003480 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3481 LastEnumConstDecl,
3482 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003483 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003484 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003485 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003486
Reid Spencer5f016e22007-07-11 17:01:13 +00003487 EnumConstantDecls.push_back(EnumConstDecl);
3488 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003489
Douglas Gregor751f6922010-09-07 14:51:08 +00003490 if (Tok.is(tok::identifier)) {
3491 // We're missing a comma between enumerators.
3492 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003493 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003494 << FixItHint::CreateInsertion(Loc, ", ");
3495 continue;
3496 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003497
Chris Lattner04d66662007-10-09 17:33:22 +00003498 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003499 break;
3500 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003501
Richard Smith7fe62082011-10-15 05:09:34 +00003502 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003503 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003504 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3505 diag::ext_enumerator_list_comma_cxx :
3506 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003507 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003508 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003509 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3510 << FixItHint::CreateRemoval(CommaLoc);
3511 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003512 }
Mike Stump1eb44332009-09-09 15:08:12 +00003513
Reid Spencer5f016e22007-07-11 17:01:13 +00003514 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003515 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003516
Reid Spencer5f016e22007-07-11 17:01:13 +00003517 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003518 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003519 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003520
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003521 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3522 EnumDecl, EnumConstantDecls.data(),
3523 EnumConstantDecls.size(), getCurScope(),
3524 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003525
Douglas Gregor72de6672009-01-08 20:45:30 +00003526 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003527 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3528 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003529
3530 // The next token must be valid after an enum definition. If not, a ';'
3531 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003532 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3533 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003534 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3535 // Push this token back into the preprocessor and change our current token
3536 // to ';' so that the rest of the code recovers as though there were an
3537 // ';' after the definition.
3538 PP.EnterToken(Tok);
3539 Tok.setKind(tok::semi);
3540 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003541}
3542
3543/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003544/// start of a type-qualifier-list.
3545bool Parser::isTypeQualifier() const {
3546 switch (Tok.getKind()) {
3547 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003548
3549 // type-qualifier only in OpenCL
3550 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003551 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003552
Steve Naroff5f8aa692008-02-11 23:15:56 +00003553 // type-qualifier
3554 case tok::kw_const:
3555 case tok::kw_volatile:
3556 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003557 case tok::kw___private:
3558 case tok::kw___local:
3559 case tok::kw___global:
3560 case tok::kw___constant:
3561 case tok::kw___read_only:
3562 case tok::kw___read_write:
3563 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003564 return true;
3565 }
3566}
3567
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003568/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3569/// is definitely a type-specifier. Return false if it isn't part of a type
3570/// specifier or if we're not sure.
3571bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3572 switch (Tok.getKind()) {
3573 default: return false;
3574 // type-specifiers
3575 case tok::kw_short:
3576 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003577 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003578 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003579 case tok::kw_signed:
3580 case tok::kw_unsigned:
3581 case tok::kw__Complex:
3582 case tok::kw__Imaginary:
3583 case tok::kw_void:
3584 case tok::kw_char:
3585 case tok::kw_wchar_t:
3586 case tok::kw_char16_t:
3587 case tok::kw_char32_t:
3588 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003589 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003590 case tok::kw_float:
3591 case tok::kw_double:
3592 case tok::kw_bool:
3593 case tok::kw__Bool:
3594 case tok::kw__Decimal32:
3595 case tok::kw__Decimal64:
3596 case tok::kw__Decimal128:
3597 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003598
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003599 // struct-or-union-specifier (C99) or class-specifier (C++)
3600 case tok::kw_class:
3601 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003602 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003603 case tok::kw_union:
3604 // enum-specifier
3605 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003606
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003607 // typedef-name
3608 case tok::annot_typename:
3609 return true;
3610 }
3611}
3612
Steve Naroff5f8aa692008-02-11 23:15:56 +00003613/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003614/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003615bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003616 switch (Tok.getKind()) {
3617 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003618
Chris Lattner166a8fc2009-01-04 23:41:41 +00003619 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003620 if (TryAltiVecVectorToken())
3621 return true;
3622 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003623 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003624 // Annotate typenames and C++ scope specifiers. If we get one, just
3625 // recurse to handle whatever we get.
3626 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003627 return true;
3628 if (Tok.is(tok::identifier))
3629 return false;
3630 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003631
Chris Lattner166a8fc2009-01-04 23:41:41 +00003632 case tok::coloncolon: // ::foo::bar
3633 if (NextToken().is(tok::kw_new) || // ::new
3634 NextToken().is(tok::kw_delete)) // ::delete
3635 return false;
3636
Chris Lattner166a8fc2009-01-04 23:41:41 +00003637 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003638 return true;
3639 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003640
Reid Spencer5f016e22007-07-11 17:01:13 +00003641 // GNU attributes support.
3642 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003643 // GNU typeof support.
3644 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003645
Reid Spencer5f016e22007-07-11 17:01:13 +00003646 // type-specifiers
3647 case tok::kw_short:
3648 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003649 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003650 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003651 case tok::kw_signed:
3652 case tok::kw_unsigned:
3653 case tok::kw__Complex:
3654 case tok::kw__Imaginary:
3655 case tok::kw_void:
3656 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003657 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003658 case tok::kw_char16_t:
3659 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003660 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003661 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003662 case tok::kw_float:
3663 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003664 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003665 case tok::kw__Bool:
3666 case tok::kw__Decimal32:
3667 case tok::kw__Decimal64:
3668 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003669 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003670
Chris Lattner99dc9142008-04-13 18:59:07 +00003671 // struct-or-union-specifier (C99) or class-specifier (C++)
3672 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003673 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003674 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003675 case tok::kw_union:
3676 // enum-specifier
3677 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003678
Reid Spencer5f016e22007-07-11 17:01:13 +00003679 // type-qualifier
3680 case tok::kw_const:
3681 case tok::kw_volatile:
3682 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003683
John McCallb8a8de32012-11-14 00:49:39 +00003684 // Debugger support.
3685 case tok::kw___unknown_anytype:
3686
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003687 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003688 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003689 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003690
Chris Lattner7c186be2008-10-20 00:25:30 +00003691 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3692 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003693 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003694
Steve Naroff239f0732008-12-25 14:16:32 +00003695 case tok::kw___cdecl:
3696 case tok::kw___stdcall:
3697 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003698 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003699 case tok::kw___w64:
3700 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003701 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003702 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003703 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003704
3705 case tok::kw___private:
3706 case tok::kw___local:
3707 case tok::kw___global:
3708 case tok::kw___constant:
3709 case tok::kw___read_only:
3710 case tok::kw___read_write:
3711 case tok::kw___write_only:
3712
Eli Friedman290eeb02009-06-08 23:27:34 +00003713 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003714
3715 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003716 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003717
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003718 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003719 case tok::kw__Atomic:
3720 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003721 }
3722}
3723
3724/// isDeclarationSpecifier() - Return true if the current token is part of a
3725/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003726///
3727/// \param DisambiguatingWithExpression True to indicate that the purpose of
3728/// this check is to disambiguate between an expression and a declaration.
3729bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003730 switch (Tok.getKind()) {
3731 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003732
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003733 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003734 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003735
Chris Lattner166a8fc2009-01-04 23:41:41 +00003736 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003737 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003738 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003739 return false;
John Thompson82287d12010-02-05 00:12:22 +00003740 if (TryAltiVecVectorToken())
3741 return true;
3742 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003743 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003744 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003745 // Annotate typenames and C++ scope specifiers. If we get one, just
3746 // recurse to handle whatever we get.
3747 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003748 return true;
3749 if (Tok.is(tok::identifier))
3750 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003751
Douglas Gregor9497a732010-09-16 01:51:54 +00003752 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00003753 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00003754 // expression is permitted, then this is probably a class message send
3755 // missing the initial '['. In this case, we won't consider this to be
3756 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00003757 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00003758 isStartOfObjCClassMessageMissingOpenBracket())
3759 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003760
John McCall9ba61662010-02-26 08:45:28 +00003761 return isDeclarationSpecifier();
3762
Chris Lattner166a8fc2009-01-04 23:41:41 +00003763 case tok::coloncolon: // ::foo::bar
3764 if (NextToken().is(tok::kw_new) || // ::new
3765 NextToken().is(tok::kw_delete)) // ::delete
3766 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003767
Chris Lattner166a8fc2009-01-04 23:41:41 +00003768 // Annotate typenames and C++ scope specifiers. If we get one, just
3769 // recurse to handle whatever we get.
3770 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003771 return true;
3772 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003773
Reid Spencer5f016e22007-07-11 17:01:13 +00003774 // storage-class-specifier
3775 case tok::kw_typedef:
3776 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003777 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003778 case tok::kw_static:
3779 case tok::kw_auto:
3780 case tok::kw_register:
3781 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003782
Douglas Gregor8d267c52011-09-09 02:06:17 +00003783 // Modules
3784 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00003785
John McCallb8a8de32012-11-14 00:49:39 +00003786 // Debugger support
3787 case tok::kw___unknown_anytype:
3788
Reid Spencer5f016e22007-07-11 17:01:13 +00003789 // type-specifiers
3790 case tok::kw_short:
3791 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003792 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003793 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003794 case tok::kw_signed:
3795 case tok::kw_unsigned:
3796 case tok::kw__Complex:
3797 case tok::kw__Imaginary:
3798 case tok::kw_void:
3799 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003800 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003801 case tok::kw_char16_t:
3802 case tok::kw_char32_t:
3803
Reid Spencer5f016e22007-07-11 17:01:13 +00003804 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003805 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003806 case tok::kw_float:
3807 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003808 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003809 case tok::kw__Bool:
3810 case tok::kw__Decimal32:
3811 case tok::kw__Decimal64:
3812 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003813 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003814
Chris Lattner99dc9142008-04-13 18:59:07 +00003815 // struct-or-union-specifier (C99) or class-specifier (C++)
3816 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003817 case tok::kw_struct:
3818 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00003819 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003820 // enum-specifier
3821 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003822
Reid Spencer5f016e22007-07-11 17:01:13 +00003823 // type-qualifier
3824 case tok::kw_const:
3825 case tok::kw_volatile:
3826 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003827
Reid Spencer5f016e22007-07-11 17:01:13 +00003828 // function-specifier
3829 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003830 case tok::kw_virtual:
3831 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003832
Richard Smith53aec2a2012-10-25 00:00:53 +00003833 // friend keyword.
3834 case tok::kw_friend:
3835
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003836 // static_assert-declaration
3837 case tok::kw__Static_assert:
3838
Chris Lattner1ef08762007-08-09 17:01:07 +00003839 // GNU typeof support.
3840 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003841
Chris Lattner1ef08762007-08-09 17:01:07 +00003842 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003843 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00003844
Richard Smith53aec2a2012-10-25 00:00:53 +00003845 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003846 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00003847 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00003848
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003849 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003850 case tok::kw__Atomic:
3851 return true;
3852
Chris Lattnerf3948c42008-07-26 03:38:44 +00003853 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3854 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003855 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003856
Douglas Gregord9d75e52011-04-27 05:41:15 +00003857 // typedef-name
3858 case tok::annot_typename:
3859 return !DisambiguatingWithExpression ||
3860 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00003861
Steve Naroff47f52092009-01-06 19:34:12 +00003862 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003863 case tok::kw___cdecl:
3864 case tok::kw___stdcall:
3865 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003866 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003867 case tok::kw___w64:
3868 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003869 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003870 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003871 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003872 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003873
3874 case tok::kw___private:
3875 case tok::kw___local:
3876 case tok::kw___global:
3877 case tok::kw___constant:
3878 case tok::kw___read_only:
3879 case tok::kw___read_write:
3880 case tok::kw___write_only:
3881
Eli Friedman290eeb02009-06-08 23:27:34 +00003882 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003883 }
3884}
3885
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003886bool Parser::isConstructorDeclarator() {
3887 TentativeParsingAction TPA(*this);
3888
3889 // Parse the C++ scope specifier.
3890 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00003891 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003892 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003893 TPA.Revert();
3894 return false;
3895 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003896
3897 // Parse the constructor name.
3898 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3899 // We already know that we have a constructor name; just consume
3900 // the token.
3901 ConsumeToken();
3902 } else {
3903 TPA.Revert();
3904 return false;
3905 }
3906
Richard Smith22592862012-03-27 23:05:05 +00003907 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003908 if (Tok.isNot(tok::l_paren)) {
3909 TPA.Revert();
3910 return false;
3911 }
3912 ConsumeParen();
3913
Richard Smith22592862012-03-27 23:05:05 +00003914 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3915 // that we have a constructor.
3916 if (Tok.is(tok::r_paren) ||
3917 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003918 TPA.Revert();
3919 return true;
3920 }
3921
3922 // If we need to, enter the specified scope.
3923 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003924 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003925 DeclScopeObj.EnterDeclaratorScope();
3926
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003927 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003928 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003929 MaybeParseMicrosoftAttributes(Attrs);
3930
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003931 // Check whether the next token(s) are part of a declaration
3932 // specifier, in which case we have the start of a parameter and,
3933 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003934 bool IsConstructor = false;
3935 if (isDeclarationSpecifier())
3936 IsConstructor = true;
3937 else if (Tok.is(tok::identifier) ||
3938 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3939 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3940 // This might be a parenthesized member name, but is more likely to
3941 // be a constructor declaration with an invalid argument type. Keep
3942 // looking.
3943 if (Tok.is(tok::annot_cxxscope))
3944 ConsumeToken();
3945 ConsumeToken();
3946
3947 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003948 // which must have one of the following syntactic forms (see the
3949 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003950 switch (Tok.getKind()) {
3951 case tok::l_paren:
3952 // C(X ( int));
3953 case tok::l_square:
3954 // C(X [ 5]);
3955 // C(X [ [attribute]]);
3956 case tok::coloncolon:
3957 // C(X :: Y);
3958 // C(X :: *p);
3959 case tok::r_paren:
3960 // C(X )
3961 // Assume this isn't a constructor, rather than assuming it's a
3962 // constructor with an unnamed parameter of an ill-formed type.
3963 break;
3964
3965 default:
3966 IsConstructor = true;
3967 break;
3968 }
3969 }
3970
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003971 TPA.Revert();
3972 return IsConstructor;
3973}
Reid Spencer5f016e22007-07-11 17:01:13 +00003974
3975/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003976/// type-qualifier-list: [C99 6.7.5]
3977/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00003978/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00003979/// [ only if VendorAttributesAllowed=true ]
3980/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00003981/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00003982/// [ only if VendorAttributesAllowed=true ]
3983/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3984/// [ only if CXX0XAttributesAllowed=true ]
3985/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003986///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003987void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3988 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003989 bool CXX11AttributesAllowed) {
3990 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003991 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003992 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003993 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003994 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003995 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003996
3997 SourceLocation EndLoc;
3998
Reid Spencer5f016e22007-07-11 17:01:13 +00003999 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004000 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004001 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004002 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004003 SourceLocation Loc = Tok.getLocation();
4004
4005 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004006 case tok::code_completion:
4007 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004008 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004009
Reid Spencer5f016e22007-07-11 17:01:13 +00004010 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004011 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004012 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004013 break;
4014 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004015 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004016 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004017 break;
4018 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004019 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004020 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004021 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004022
4023 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004024 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004025 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004026 goto DoneWithTypeQuals;
4027 case tok::kw___private:
4028 case tok::kw___global:
4029 case tok::kw___local:
4030 case tok::kw___constant:
4031 case tok::kw___read_only:
4032 case tok::kw___write_only:
4033 case tok::kw___read_write:
4034 ParseOpenCLQualifiers(DS);
4035 break;
4036
Eli Friedman290eeb02009-06-08 23:27:34 +00004037 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004038 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004039 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004040 case tok::kw___cdecl:
4041 case tok::kw___stdcall:
4042 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004043 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004044 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004045 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004046 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004047 continue;
4048 }
4049 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004050 case tok::kw___pascal:
4051 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004052 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004053 continue;
4054 }
4055 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004056 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004057 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004058 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004059 continue; // do *not* consume the next token!
4060 }
4061 // otherwise, FALL THROUGH!
4062 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004063 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004064 // If this is not a type-qualifier token, we're done reading type
4065 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004066 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004067 if (EndLoc.isValid())
4068 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004069 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004070 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004071
Reid Spencer5f016e22007-07-11 17:01:13 +00004072 // If the specifier combination wasn't legal, issue a diagnostic.
4073 if (isInvalid) {
4074 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004075 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004076 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004077 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004078 }
4079}
4080
4081
4082/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4083///
4084void Parser::ParseDeclarator(Declarator &D) {
4085 /// This implements the 'declarator' production in the C grammar, then checks
4086 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004087 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004088}
4089
Richard Smith9988f282012-03-29 01:16:42 +00004090static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4091 if (Kind == tok::star || Kind == tok::caret)
4092 return true;
4093
4094 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4095 if (!Lang.CPlusPlus)
4096 return false;
4097
4098 return Kind == tok::amp || Kind == tok::ampamp;
4099}
4100
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004101/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4102/// is parsed by the function passed to it. Pass null, and the direct-declarator
4103/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004104/// ptr-operator production.
4105///
Richard Smith0706df42011-10-19 21:33:05 +00004106/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004107/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4108/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004109///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004110/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4111/// [C] pointer[opt] direct-declarator
4112/// [C++] direct-declarator
4113/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004114///
4115/// pointer: [C99 6.7.5]
4116/// '*' type-qualifier-list[opt]
4117/// '*' type-qualifier-list[opt] pointer
4118///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004119/// ptr-operator:
4120/// '*' cv-qualifier-seq[opt]
4121/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004122/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004123/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004124/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004125/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004126void Parser::ParseDeclaratorInternal(Declarator &D,
4127 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004128 if (Diags.hasAllExtensionsSilenced())
4129 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004130
Sebastian Redlf30208a2009-01-24 21:16:55 +00004131 // C++ member pointers start with a '::' or a nested-name.
4132 // Member pointers get special handling, since there's no place for the
4133 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004134 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004135 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4136 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004137 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4138 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004139 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004140 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004141
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004142 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004143 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004144 // The scope spec really belongs to the direct-declarator.
4145 D.getCXXScopeSpec() = SS;
4146 if (DirectDeclParser)
4147 (this->*DirectDeclParser)(D);
4148 return;
4149 }
4150
4151 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004152 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004153 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004154 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004155 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004156
4157 // Recurse to parse whatever is left.
4158 ParseDeclaratorInternal(D, DirectDeclParser);
4159
4160 // Sema will have to catch (syntactically invalid) pointers into global
4161 // scope. It has to catch pointers into namespace scope anyway.
4162 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004163 Loc),
4164 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004165 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004166 return;
4167 }
4168 }
4169
4170 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004171 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004172 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004173 if (DirectDeclParser)
4174 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004175 return;
4176 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004177
Sebastian Redl05532f22009-03-15 22:02:01 +00004178 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4179 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004180 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004181 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004182
Chris Lattner9af55002009-03-27 04:18:06 +00004183 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004184 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004185 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004186
Richard Smith6ee326a2012-04-10 01:32:12 +00004187 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004188 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004189 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004190
Reid Spencer5f016e22007-07-11 17:01:13 +00004191 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004192 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004193 if (Kind == tok::star)
4194 // Remember that we parsed a pointer type, and remember the type-quals.
4195 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004196 DS.getConstSpecLoc(),
4197 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004198 DS.getRestrictSpecLoc()),
4199 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004200 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004201 else
4202 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004203 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004204 Loc),
4205 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004206 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004207 } else {
4208 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004209 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004210
Sebastian Redl743de1f2009-03-23 00:00:23 +00004211 // Complain about rvalue references in C++03, but then go on and build
4212 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004213 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00004214 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004215 diag::warn_cxx98_compat_rvalue_reference :
4216 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004217
Richard Smith6ee326a2012-04-10 01:32:12 +00004218 // GNU-style and C++11 attributes are allowed here, as is restrict.
4219 ParseTypeQualifierListOpt(DS);
4220 D.ExtendWithDeclSpec(DS);
4221
Reid Spencer5f016e22007-07-11 17:01:13 +00004222 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4223 // cv-qualifiers are introduced through the use of a typedef or of a
4224 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004225 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4226 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4227 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004228 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004229 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4230 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004231 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00004232 }
4233
4234 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004235 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004236
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004237 if (D.getNumTypeObjects() > 0) {
4238 // C++ [dcl.ref]p4: There shall be no references to references.
4239 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4240 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004241 if (const IdentifierInfo *II = D.getIdentifier())
4242 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4243 << II;
4244 else
4245 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4246 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004247
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004248 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004249 // can go ahead and build the (technically ill-formed)
4250 // declarator: reference collapsing will take care of it.
4251 }
4252 }
4253
Reid Spencer5f016e22007-07-11 17:01:13 +00004254 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004255 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004256 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004257 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004258 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004259 }
4260}
4261
Richard Smith9988f282012-03-29 01:16:42 +00004262static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4263 SourceLocation EllipsisLoc) {
4264 if (EllipsisLoc.isValid()) {
4265 FixItHint Insertion;
4266 if (!D.getEllipsisLoc().isValid()) {
4267 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4268 D.setEllipsisLoc(EllipsisLoc);
4269 }
4270 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4271 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4272 }
4273}
4274
Reid Spencer5f016e22007-07-11 17:01:13 +00004275/// ParseDirectDeclarator
4276/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004277/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004278/// '(' declarator ')'
4279/// [GNU] '(' attributes declarator ')'
4280/// [C90] direct-declarator '[' constant-expression[opt] ']'
4281/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4282/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4283/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4284/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004285/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4286/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004287/// direct-declarator '(' parameter-type-list ')'
4288/// direct-declarator '(' identifier-list[opt] ')'
4289/// [GNU] direct-declarator '(' parameter-forward-declarations
4290/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004291/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4292/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004293/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4294/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4295/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004296/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004297/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004298///
4299/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004300/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004301/// '::'[opt] nested-name-specifier[opt] type-name
4302///
4303/// id-expression: [C++ 5.1]
4304/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004305/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004306///
4307/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004308/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004309/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004310/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004311/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004312/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004313///
Richard Smith5d8388c2012-03-27 01:42:32 +00004314/// Note, any additional constructs added here may need corresponding changes
4315/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004316void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004317 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004318
David Blaikie4e4d0842012-03-11 07:00:24 +00004319 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004320 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004321 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004322 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4323 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004324 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004325 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004326 }
4327
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004328 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004329 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004330 // Change the declaration context for name lookup, until this function
4331 // is exited (and the declarator has been parsed).
4332 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004333 }
4334
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004335 // C++0x [dcl.fct]p14:
4336 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004337 // of a parameter-declaration-clause without a preceding comma. In
4338 // this case, the ellipsis is parsed as part of the
4339 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004340 // parameter pack that has not been expanded; otherwise, it is parsed
4341 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004342 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004343 !((D.getContext() == Declarator::PrototypeContext ||
4344 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004345 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00004346 !Actions.containsUnexpandedParameterPacks(D))) {
4347 SourceLocation EllipsisLoc = ConsumeToken();
4348 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4349 // The ellipsis was put in the wrong place. Recover, and explain to
4350 // the user what they should have done.
4351 ParseDeclarator(D);
4352 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4353 return;
4354 } else
4355 D.setEllipsisLoc(EllipsisLoc);
4356
4357 // The ellipsis can't be followed by a parenthesized declarator. We
4358 // check for that in ParseParenDeclarator, after we have disambiguated
4359 // the l_paren token.
4360 }
4361
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004362 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4363 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4364 // We found something that indicates the start of an unqualified-id.
4365 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004366 bool AllowConstructorName;
4367 if (D.getDeclSpec().hasTypeSpecifier())
4368 AllowConstructorName = false;
4369 else if (D.getCXXScopeSpec().isSet())
4370 AllowConstructorName =
4371 (D.getContext() == Declarator::FileContext ||
4372 (D.getContext() == Declarator::MemberContext &&
4373 D.getDeclSpec().isFriendSpecified()));
4374 else
4375 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4376
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004377 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004378 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4379 /*EnteringContext=*/true,
4380 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004381 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004382 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004383 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004384 D.getName()) ||
4385 // Once we're past the identifier, if the scope was bad, mark the
4386 // whole declarator bad.
4387 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004388 D.SetIdentifier(0, Tok.getLocation());
4389 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004390 } else {
4391 // Parsed the unqualified-id; update range information and move along.
4392 if (D.getSourceRange().getBegin().isInvalid())
4393 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4394 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004395 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004396 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004397 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004398 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004399 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004400 "There's a C++-specific check for tok::identifier above");
4401 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4402 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4403 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004404 goto PastIdentifier;
4405 }
Richard Smith9988f282012-03-29 01:16:42 +00004406
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004407 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004408 // direct-declarator: '(' declarator ')'
4409 // direct-declarator: '(' attributes declarator ')'
4410 // Example: 'char (*X)' or 'int (*XX)(void)'
4411 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004412
4413 // If the declarator was parenthesized, we entered the declarator
4414 // scope when parsing the parenthesized declarator, then exited
4415 // the scope already. Re-enter the scope, if we need to.
4416 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004417 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004418 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004419 if (!D.isInvalidType() &&
4420 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004421 // Change the declaration context for name lookup, until this function
4422 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004423 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004424 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004425 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004426 // This could be something simple like "int" (in which case the declarator
4427 // portion is empty), if an abstract-declarator is allowed.
4428 D.SetIdentifier(0, Tok.getLocation());
4429 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004430 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004431 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004432 if (D.getContext() == Declarator::MemberContext)
4433 Diag(Tok, diag::err_expected_member_name_or_semi)
4434 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004435 else if (getLangOpts().CPlusPlus)
4436 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004437 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004438 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004439 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004440 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004441 }
Mike Stump1eb44332009-09-09 15:08:12 +00004442
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004443 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004444 assert(D.isPastIdentifier() &&
4445 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004446
Richard Smith6ee326a2012-04-10 01:32:12 +00004447 // Don't parse attributes unless we have parsed an unparenthesized name.
4448 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004449 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004450
Reid Spencer5f016e22007-07-11 17:01:13 +00004451 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004452 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004453 // Enter function-declaration scope, limiting any declarators to the
4454 // function prototype scope, including parameter declarators.
4455 ParseScope PrototypeScope(this,
4456 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004457 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4458 // In such a case, check if we actually have a function declarator; if it
4459 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004460 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004461 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4462 // The name of the declarator, if any, is tentatively declared within
4463 // a possible direct initializer.
4464 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4465 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4466 TentativelyDeclaredIdentifiers.pop_back();
4467 if (!IsFunctionDecl)
4468 break;
4469 }
John McCall0b7e6782011-03-24 11:26:52 +00004470 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004471 BalancedDelimiterTracker T(*this, tok::l_paren);
4472 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004473 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004474 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004475 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004476 ParseBracketDeclarator(D);
4477 } else {
4478 break;
4479 }
4480 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004481}
Reid Spencer5f016e22007-07-11 17:01:13 +00004482
Chris Lattneref4715c2008-04-06 05:45:57 +00004483/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4484/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004485/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004486/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4487///
4488/// direct-declarator:
4489/// '(' declarator ')'
4490/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004491/// direct-declarator '(' parameter-type-list ')'
4492/// direct-declarator '(' identifier-list[opt] ')'
4493/// [GNU] direct-declarator '(' parameter-forward-declarations
4494/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004495///
4496void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004497 BalancedDelimiterTracker T(*this, tok::l_paren);
4498 T.consumeOpen();
4499
Chris Lattneref4715c2008-04-06 05:45:57 +00004500 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004501
Chris Lattner7399ee02008-10-20 02:05:46 +00004502 // Eat any attributes before we look at whether this is a grouping or function
4503 // declarator paren. If this is a grouping paren, the attribute applies to
4504 // the type being built up, for example:
4505 // int (__attribute__(()) *x)(long y)
4506 // If this ends up not being a grouping paren, the attribute applies to the
4507 // first argument, for example:
4508 // int (__attribute__(()) int x)
4509 // In either case, we need to eat any attributes to be able to determine what
4510 // sort of paren this is.
4511 //
John McCall0b7e6782011-03-24 11:26:52 +00004512 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004513 bool RequiresArg = false;
4514 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004515 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004516
Chris Lattner7399ee02008-10-20 02:05:46 +00004517 // We require that the argument list (if this is a non-grouping paren) be
4518 // present even if the attribute list was empty.
4519 RequiresArg = true;
4520 }
Steve Naroff239f0732008-12-25 14:16:32 +00004521 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004522 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004523 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004524 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004525 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004526 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004527 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004528 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004529 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004530 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004531
Chris Lattneref4715c2008-04-06 05:45:57 +00004532 // If we haven't past the identifier yet (or where the identifier would be
4533 // stored, if this is an abstract declarator), then this is probably just
4534 // grouping parens. However, if this could be an abstract-declarator, then
4535 // this could also be the start of function arguments (consider 'void()').
4536 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004537
Chris Lattneref4715c2008-04-06 05:45:57 +00004538 if (!D.mayOmitIdentifier()) {
4539 // If this can't be an abstract-declarator, this *must* be a grouping
4540 // paren, because we haven't seen the identifier yet.
4541 isGrouping = true;
4542 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004543 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4544 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004545 isDeclarationSpecifier() || // 'int(int)' is a function.
4546 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004547 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4548 // considered to be a type, not a K&R identifier-list.
4549 isGrouping = false;
4550 } else {
4551 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4552 isGrouping = true;
4553 }
Mike Stump1eb44332009-09-09 15:08:12 +00004554
Chris Lattneref4715c2008-04-06 05:45:57 +00004555 // If this is a grouping paren, handle:
4556 // direct-declarator: '(' declarator ')'
4557 // direct-declarator: '(' attributes declarator ')'
4558 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004559 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4560 D.setEllipsisLoc(SourceLocation());
4561
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004562 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004563 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004564 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004565 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004566 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004567 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004568 T.getCloseLocation()),
4569 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004570
4571 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004572
4573 // An ellipsis cannot be placed outside parentheses.
4574 if (EllipsisLoc.isValid())
4575 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4576
Chris Lattneref4715c2008-04-06 05:45:57 +00004577 return;
4578 }
Mike Stump1eb44332009-09-09 15:08:12 +00004579
Chris Lattneref4715c2008-04-06 05:45:57 +00004580 // Okay, if this wasn't a grouping paren, it must be the start of a function
4581 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004582 // identifier (and remember where it would have been), then call into
4583 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004584 D.SetIdentifier(0, Tok.getLocation());
4585
David Blaikie42d6d0c2011-12-04 05:04:18 +00004586 // Enter function-declaration scope, limiting any declarators to the
4587 // function prototype scope, including parameter declarators.
4588 ParseScope PrototypeScope(this,
4589 Scope::FunctionPrototypeScope|Scope::DeclScope);
Richard Smithb9c62612012-07-30 21:30:52 +00004590 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004591 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004592}
4593
4594/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4595/// declarator D up to a paren, which indicates that we are parsing function
4596/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004597///
Richard Smith6ee326a2012-04-10 01:32:12 +00004598/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4599/// immediately after the open paren - they should be considered to be the
4600/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004601///
Richard Smith6ee326a2012-04-10 01:32:12 +00004602/// If RequiresArg is true, then the first argument of the function is required
4603/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004604///
Richard Smith6ee326a2012-04-10 01:32:12 +00004605/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4606/// (C++11) ref-qualifier[opt], exception-specification[opt],
4607/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4608///
4609/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004610/// dynamic-exception-specification
4611/// noexcept-specification
4612///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004613void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004614 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004615 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004616 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004617 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004618 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004619 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004620 // lparen is already consumed!
4621 assert(D.isPastIdentifier() && "Should not call before identifier!");
4622
4623 // This should be true when the function has typed arguments.
4624 // Otherwise, it is treated as a K&R-style function.
4625 bool HasProto = false;
4626 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004627 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004628 // Remember where we see an ellipsis, if any.
4629 SourceLocation EllipsisLoc;
4630
4631 DeclSpec DS(AttrFactory);
4632 bool RefQualifierIsLValueRef = true;
4633 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004634 SourceLocation ConstQualifierLoc;
4635 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004636 ExceptionSpecificationType ESpecType = EST_None;
4637 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004638 SmallVector<ParsedType, 2> DynamicExceptions;
4639 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004640 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004641 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004642 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004643
James Molloy16f1f712012-02-29 10:24:19 +00004644 Actions.ActOnStartFunctionDeclarator();
4645
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004646 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4647 EndLoc is the end location for the function declarator.
4648 They differ for trailing return types. */
4649 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004650 SourceLocation LParenLoc, RParenLoc;
4651 LParenLoc = Tracker.getOpenLocation();
4652 StartLoc = LParenLoc;
4653
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004654 if (isFunctionDeclaratorIdentifierList()) {
4655 if (RequiresArg)
4656 Diag(Tok, diag::err_argument_required_after_attribute);
4657
4658 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4659
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004660 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004661 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004662 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004663 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004664 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004665 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004666 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004667 else if (RequiresArg)
4668 Diag(Tok, diag::err_argument_required_after_attribute);
4669
David Blaikie4e4d0842012-03-11 07:00:24 +00004670 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004671
4672 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004673 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004674 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004675 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004676 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004677
David Blaikie4e4d0842012-03-11 07:00:24 +00004678 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004679 // FIXME: Accept these components in any order, and produce fixits to
4680 // correct the order if the user gets it wrong. Ideally we should deal
4681 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004682
4683 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004684 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4685 if (!DS.getSourceRange().getEnd().isInvalid()) {
4686 EndLoc = DS.getSourceRange().getEnd();
4687 ConstQualifierLoc = DS.getConstSpecLoc();
4688 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4689 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004690
4691 // Parse ref-qualifier[opt].
4692 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004693 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004694 diag::warn_cxx98_compat_ref_qualifier :
4695 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004696
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004697 RefQualifierIsLValueRef = Tok.is(tok::amp);
4698 RefQualifierLoc = ConsumeToken();
4699 EndLoc = RefQualifierLoc;
4700 }
4701
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004702 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00004703 // If a declaration declares a member function or member function
4704 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004705 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00004706 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004707 // declarator.
Chad Rosier8decdee2012-06-26 22:30:43 +00004708 bool IsCXX11MemberFunction =
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004709 getLangOpts().CPlusPlus0x &&
4710 (D.getContext() == Declarator::MemberContext ||
4711 (D.getContext() == Declarator::FileContext &&
Chad Rosier8decdee2012-06-26 22:30:43 +00004712 D.getCXXScopeSpec().isValid() &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004713 Actions.CurContext->isRecord()));
4714 Sema::CXXThisScopeRAII ThisScope(Actions,
4715 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4716 DS.getTypeQualifiers(),
4717 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004718
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004719 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004720 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004721 DynamicExceptions,
4722 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004723 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004724 if (ESpecType != EST_None)
4725 EndLoc = ESpecRange.getEnd();
4726
Richard Smith6ee326a2012-04-10 01:32:12 +00004727 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4728 // after the exception-specification.
4729 MaybeParseCXX0XAttributes(FnAttrs);
4730
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004731 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004732 LocalEndLoc = EndLoc;
David Blaikie4e4d0842012-03-11 07:00:24 +00004733 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004734 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004735 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4736 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004737 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00004738 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004739 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004740 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004741 }
4742 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004743 }
4744
4745 // Remember that we parsed a function type, and remember the attributes.
4746 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004747 IsAmbiguous,
4748 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004749 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004750 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004751 DS.getTypeQualifiers(),
4752 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004753 RefQualifierLoc, ConstQualifierLoc,
4754 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004755 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004756 ESpecType, ESpecRange.getBegin(),
4757 DynamicExceptions.data(),
4758 DynamicExceptionRanges.data(),
4759 DynamicExceptions.size(),
4760 NoexceptExpr.isUsable() ?
4761 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004762 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004763 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004764 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004765
4766 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004767}
4768
4769/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4770/// identifier list form for a K&R-style function: void foo(a,b,c)
4771///
4772/// Note that identifier-lists are only allowed for normal declarators, not for
4773/// abstract-declarators.
4774bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004775 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004776 && Tok.is(tok::identifier)
4777 && !TryAltiVecVectorToken()
4778 // K&R identifier lists can't have typedefs as identifiers, per C99
4779 // 6.7.5.3p11.
4780 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4781 // Identifier lists follow a really simple grammar: the identifiers can
4782 // be followed *only* by a ", identifier" or ")". However, K&R
4783 // identifier lists are really rare in the brave new modern world, and
4784 // it is very common for someone to typo a type in a non-K&R style
4785 // list. If we are presented with something like: "void foo(intptr x,
4786 // float y)", we don't want to start parsing the function declarator as
4787 // though it is a K&R style declarator just because intptr is an
4788 // invalid type.
4789 //
4790 // To handle this, we check to see if the token after the first
4791 // identifier is a "," or ")". Only then do we parse it as an
4792 // identifier list.
4793 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4794}
4795
4796/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4797/// we found a K&R-style identifier list instead of a typed parameter list.
4798///
4799/// After returning, ParamInfo will hold the parsed parameters.
4800///
4801/// identifier-list: [C99 6.7.5]
4802/// identifier
4803/// identifier-list ',' identifier
4804///
4805void Parser::ParseFunctionDeclaratorIdentifierList(
4806 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004807 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004808 // If there was no identifier specified for the declarator, either we are in
4809 // an abstract-declarator, or we are in a parameter declarator which was found
4810 // to be abstract. In abstract-declarators, identifier lists are not valid:
4811 // diagnose this.
4812 if (!D.getIdentifier())
4813 Diag(Tok, diag::ext_ident_list_in_param);
4814
4815 // Maintain an efficient lookup of params we have seen so far.
4816 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4817
4818 while (1) {
4819 // If this isn't an identifier, report the error and skip until ')'.
4820 if (Tok.isNot(tok::identifier)) {
4821 Diag(Tok, diag::err_expected_ident);
4822 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4823 // Forget we parsed anything.
4824 ParamInfo.clear();
4825 return;
4826 }
4827
4828 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4829
4830 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4831 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4832 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4833
4834 // Verify that the argument identifier has not already been mentioned.
4835 if (!ParamsSoFar.insert(ParmII)) {
4836 Diag(Tok, diag::err_param_redefinition) << ParmII;
4837 } else {
4838 // Remember this identifier in ParamInfo.
4839 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4840 Tok.getLocation(),
4841 0));
4842 }
4843
4844 // Eat the identifier.
4845 ConsumeToken();
4846
4847 // The list continues if we see a comma.
4848 if (Tok.isNot(tok::comma))
4849 break;
4850 ConsumeToken();
4851 }
4852}
4853
4854/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4855/// after the opening parenthesis. This function will not parse a K&R-style
4856/// identifier list.
4857///
Richard Smith6ce48a72012-04-11 04:01:28 +00004858/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4859/// caller parsed those arguments immediately after the open paren - they should
4860/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004861///
4862/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4863/// be the location of the ellipsis, if any was parsed.
4864///
Reid Spencer5f016e22007-07-11 17:01:13 +00004865/// parameter-type-list: [C99 6.7.5]
4866/// parameter-list
4867/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004868/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004869///
4870/// parameter-list: [C99 6.7.5]
4871/// parameter-declaration
4872/// parameter-list ',' parameter-declaration
4873///
4874/// parameter-declaration: [C99 6.7.5]
4875/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004876/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004877/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004878/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004879/// declaration-specifiers abstract-declarator[opt]
4880/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004881/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004882/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004883/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004884///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004885void Parser::ParseParameterDeclarationClause(
4886 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004887 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004888 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004889 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004890
Chris Lattnerf97409f2008-04-06 06:57:35 +00004891 while (1) {
4892 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004893 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4894 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004895 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004896 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004897 }
Mike Stump1eb44332009-09-09 15:08:12 +00004898
Chris Lattnerf97409f2008-04-06 06:57:35 +00004899 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004900 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004901 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004902
Richard Smith6ce48a72012-04-11 04:01:28 +00004903 // Parse any C++11 attributes.
4904 MaybeParseCXX0XAttributes(DS.getAttributes());
4905
John McCall7f040a92010-12-24 02:08:15 +00004906 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004907 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004908 ParseMicrosoftAttributes(DS.getAttributes());
4909
4910 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004911
4912 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004913 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004914 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004915 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4916 // too much hassle.
4917 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004918
Chris Lattnere64c5492009-02-27 18:38:20 +00004919 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004920
Chris Lattnerf97409f2008-04-06 06:57:35 +00004921 // Parse the declarator. This is "PrototypeContext", because we must
4922 // accept either 'declarator' or 'abstract-declarator' here.
4923 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4924 ParseDeclarator(ParmDecl);
4925
4926 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004927 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004928
Chris Lattnerf97409f2008-04-06 06:57:35 +00004929 // Remember this parsed parameter in ParamInfo.
4930 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004931
Douglas Gregor72b505b2008-12-16 21:30:33 +00004932 // DefArgToks is used when the parsing of default arguments needs
4933 // to be delayed.
4934 CachedTokens *DefArgToks = 0;
4935
Chris Lattnerf97409f2008-04-06 06:57:35 +00004936 // If no parameter was specified, verify that *something* was specified,
4937 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004938 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4939 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004940 // Completely missing, emit error.
4941 Diag(DSStart, diag::err_missing_param);
4942 } else {
4943 // Otherwise, we have something. Add it and let semantic analysis try
4944 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004945
Chris Lattnerf97409f2008-04-06 06:57:35 +00004946 // Inform the actions module about the parameter declarator, so it gets
4947 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004948 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004949
4950 // Parse the default argument, if any. We parse the default
4951 // arguments in all dialects; the semantic analysis in
4952 // ActOnParamDefaultArgument will reject the default argument in
4953 // C.
4954 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004955 SourceLocation EqualLoc = Tok.getLocation();
4956
Chris Lattner04421082008-04-08 04:40:51 +00004957 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004958 if (D.getContext() == Declarator::MemberContext) {
4959 // If we're inside a class definition, cache the tokens
4960 // corresponding to the default argument. We'll actually parse
4961 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004962 // FIXME: Can we use a smart pointer for Toks?
4963 DefArgToks = new CachedTokens;
4964
Mike Stump1eb44332009-09-09 15:08:12 +00004965 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004966 /*StopAtSemi=*/true,
4967 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004968 delete DefArgToks;
4969 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004970 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004971 } else {
4972 // Mark the end of the default argument so that we know when to
4973 // stop when we parse it later on.
4974 Token DefArgEnd;
4975 DefArgEnd.startToken();
4976 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4977 DefArgEnd.setLocation(Tok.getLocation());
4978 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004979 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004980 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004981 }
Chris Lattner04421082008-04-08 04:40:51 +00004982 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004983 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004984 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004985
Chad Rosier8decdee2012-06-26 22:30:43 +00004986 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004987 // used.
4988 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004989 Sema::PotentiallyEvaluatedIfUsed,
4990 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004991
Sebastian Redl84407ba2012-03-14 15:54:00 +00004992 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004993 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4994 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004995 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004996 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004997 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004998 if (DefArgResult.isInvalid()) {
4999 Actions.ActOnParamDefaultArgumentError(Param);
5000 SkipUntil(tok::comma, tok::r_paren, true, true);
5001 } else {
5002 // Inform the actions module about the default argument
5003 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005004 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005005 }
Chris Lattner04421082008-04-08 04:40:51 +00005006 }
5007 }
Mike Stump1eb44332009-09-09 15:08:12 +00005008
5009 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5010 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00005011 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005012 }
5013
5014 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005015 if (Tok.isNot(tok::comma)) {
5016 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005017 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005018
David Blaikie4e4d0842012-03-11 07:00:24 +00005019 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005020 // We have ellipsis without a preceding ',', which is ill-formed
5021 // in C. Complain and provide the fix.
5022 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005023 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005024 }
5025 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005026
Douglas Gregored5d6512009-09-22 21:41:40 +00005027 break;
5028 }
Mike Stump1eb44332009-09-09 15:08:12 +00005029
Chris Lattnerf97409f2008-04-06 06:57:35 +00005030 // Consume the comma.
5031 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005032 }
Mike Stump1eb44332009-09-09 15:08:12 +00005033
Chris Lattner66d28652008-04-06 06:34:08 +00005034}
Chris Lattneref4715c2008-04-06 05:45:57 +00005035
Reid Spencer5f016e22007-07-11 17:01:13 +00005036/// [C90] direct-declarator '[' constant-expression[opt] ']'
5037/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5038/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5039/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5040/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005041/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5042/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005043void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005044 if (CheckProhibitedCXX11Attribute())
5045 return;
5046
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005047 BalancedDelimiterTracker T(*this, tok::l_square);
5048 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005049
Chris Lattner378c7e42008-12-18 07:27:21 +00005050 // C array syntax has many features, but by-far the most common is [] and [4].
5051 // This code does a fast path to handle some of the most obvious cases.
5052 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005053 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005054 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00005055 MaybeParseCXX0XAttributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005056
Chris Lattner378c7e42008-12-18 07:27:21 +00005057 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005058 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005059 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005060 T.getOpenLocation(),
5061 T.getCloseLocation()),
5062 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005063 return;
5064 } else if (Tok.getKind() == tok::numeric_constant &&
5065 GetLookAheadToken(1).is(tok::r_square)) {
5066 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005067 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005068 ConsumeToken();
5069
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005070 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005071 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00005072 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005073
Chris Lattner378c7e42008-12-18 07:27:21 +00005074 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005075 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00005076 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005077 T.getOpenLocation(),
5078 T.getCloseLocation()),
5079 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005080 return;
5081 }
Mike Stump1eb44332009-09-09 15:08:12 +00005082
Reid Spencer5f016e22007-07-11 17:01:13 +00005083 // If valid, this location is the position where we read the 'static' keyword.
5084 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005085 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005086 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005087
Reid Spencer5f016e22007-07-11 17:01:13 +00005088 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005089 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005090 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005091 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005092
Reid Spencer5f016e22007-07-11 17:01:13 +00005093 // If we haven't already read 'static', check to see if there is one after the
5094 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005095 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005096 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005097
Reid Spencer5f016e22007-07-11 17:01:13 +00005098 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5099 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005100 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005101
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005102 // Handle the case where we have '[*]' as the array size. However, a leading
5103 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005104 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005105 // infrequent, use of lookahead is not costly here.
5106 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005107 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005108
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005109 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005110 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005111 StaticLoc = SourceLocation(); // Drop the static.
5112 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005113 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005114 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005115 // Note, in C89, this production uses the constant-expr production instead
5116 // of assignment-expr. The only difference is that assignment-expr allows
5117 // things like '=' and '*='. Sema rejects these in C89 mode because they
5118 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005119
Douglas Gregore0762c92009-06-19 23:52:42 +00005120 // Parse the constant-expression or assignment-expression now (depending
5121 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005122 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005123 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005124 } else {
5125 EnterExpressionEvaluationContext Unevaluated(Actions,
5126 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005127 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005128 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005129 }
Mike Stump1eb44332009-09-09 15:08:12 +00005130
Reid Spencer5f016e22007-07-11 17:01:13 +00005131 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005132 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005133 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005134 // If the expression was invalid, skip it.
5135 SkipUntil(tok::r_square);
5136 return;
5137 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005138
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005139 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005140
John McCall0b7e6782011-03-24 11:26:52 +00005141 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00005142 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005143
Chris Lattner378c7e42008-12-18 07:27:21 +00005144 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005145 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005146 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005147 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005148 T.getOpenLocation(),
5149 T.getCloseLocation()),
5150 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005151}
5152
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005153/// [GNU] typeof-specifier:
5154/// typeof ( expressions )
5155/// typeof ( type-name )
5156/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005157///
5158void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005159 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005160 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005161 SourceLocation StartLoc = ConsumeToken();
5162
John McCallcfb708c2010-01-13 20:03:27 +00005163 const bool hasParens = Tok.is(tok::l_paren);
5164
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005165 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5166 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005167
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005168 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005169 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005170 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005171 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5172 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005173 if (hasParens)
5174 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005175
5176 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005177 // FIXME: Not accurate, the range gets one token more than it should.
5178 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005179 else
5180 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005181
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005182 if (isCastExpr) {
5183 if (!CastTy) {
5184 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005185 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005186 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005187
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005188 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005189 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005190 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5191 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005192 DiagID, CastTy))
5193 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005194 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005195 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005196
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005197 // If we get here, the operand to the typeof was an expresion.
5198 if (Operand.isInvalid()) {
5199 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005200 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005201 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005202
Eli Friedman71b8fb52012-01-21 01:01:51 +00005203 // We might need to transform the operand if it is potentially evaluated.
5204 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5205 if (Operand.isInvalid()) {
5206 DS.SetTypeSpecError();
5207 return;
5208 }
5209
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005210 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005211 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005212 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5213 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005214 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005215 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005216}
Chris Lattner1b492422010-02-28 18:33:55 +00005217
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005218/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005219/// _Atomic ( type-name )
5220///
5221void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5222 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5223
5224 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005225 BalancedDelimiterTracker T(*this, tok::l_paren);
5226 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00005227 SkipUntil(tok::r_paren);
5228 return;
5229 }
5230
5231 TypeResult Result = ParseTypeName();
5232 if (Result.isInvalid()) {
5233 SkipUntil(tok::r_paren);
5234 return;
5235 }
5236
5237 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005238 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005239
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005240 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005241 return;
5242
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005243 DS.setTypeofParensRange(T.getRange());
5244 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005245
5246 const char *PrevSpec = 0;
5247 unsigned DiagID;
5248 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5249 DiagID, Result.release()))
5250 Diag(StartLoc, DiagID) << PrevSpec;
5251}
5252
Chris Lattner1b492422010-02-28 18:33:55 +00005253
5254/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5255/// from TryAltiVecVectorToken.
5256bool Parser::TryAltiVecVectorTokenOutOfLine() {
5257 Token Next = NextToken();
5258 switch (Next.getKind()) {
5259 default: return false;
5260 case tok::kw_short:
5261 case tok::kw_long:
5262 case tok::kw_signed:
5263 case tok::kw_unsigned:
5264 case tok::kw_void:
5265 case tok::kw_char:
5266 case tok::kw_int:
5267 case tok::kw_float:
5268 case tok::kw_double:
5269 case tok::kw_bool:
5270 case tok::kw___pixel:
5271 Tok.setKind(tok::kw___vector);
5272 return true;
5273 case tok::identifier:
5274 if (Next.getIdentifierInfo() == Ident_pixel) {
5275 Tok.setKind(tok::kw___vector);
5276 return true;
5277 }
5278 return false;
5279 }
5280}
5281
5282bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5283 const char *&PrevSpec, unsigned &DiagID,
5284 bool &isInvalid) {
5285 if (Tok.getIdentifierInfo() == Ident_vector) {
5286 Token Next = NextToken();
5287 switch (Next.getKind()) {
5288 case tok::kw_short:
5289 case tok::kw_long:
5290 case tok::kw_signed:
5291 case tok::kw_unsigned:
5292 case tok::kw_void:
5293 case tok::kw_char:
5294 case tok::kw_int:
5295 case tok::kw_float:
5296 case tok::kw_double:
5297 case tok::kw_bool:
5298 case tok::kw___pixel:
5299 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5300 return true;
5301 case tok::identifier:
5302 if (Next.getIdentifierInfo() == Ident_pixel) {
5303 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5304 return true;
5305 }
5306 break;
5307 default:
5308 break;
5309 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005310 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005311 DS.isTypeAltiVecVector()) {
5312 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5313 return true;
5314 }
5315 return false;
5316}