blob: 0a8ab1ac1aa4a2132ee73ba7658be94f99420eae [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000017#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000018#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000024#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// C99 6.7: Declarations.
29//===----------------------------------------------------------------------===//
30
31/// ParseTypeName
32/// type-name: [C99 6.7.6]
33/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000034///
35/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000036TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000037 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000038 AccessSpecifier AS,
39 Decl **OwnedType) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000040 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000041 if (DSC == DSC_normal)
42 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000043
Reid Spencer5f016e22007-07-11 17:01:13 +000044 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000045 DeclSpec DS(AttrFactory);
Richard Smith7796eb52012-03-12 08:56:40 +000046 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000047 if (OwnedType)
48 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000049
Reid Spencer5f016e22007-07-11 17:01:13 +000050 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000051 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000052 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000053 if (Range)
54 *Range = DeclaratorInfo.getSourceRange();
55
Chris Lattnereaaebc72009-04-25 08:06:05 +000056 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000057 return true;
58
Douglas Gregor23c94db2010-07-02 17:43:08 +000059 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000060}
61
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000062
63/// isAttributeLateParsed - Return true if the attribute has arguments that
64/// require late parsing.
65static bool isAttributeLateParsed(const IdentifierInfo &II) {
66 return llvm::StringSwitch<bool>(II.getName())
67#include "clang/Parse/AttrLateParsed.inc"
68 .Default(false);
69}
70
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.
147 if (!ClassStack.empty())
148 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 {
158 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 }
160 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000161 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000162 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 }
164 }
165 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000167 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000168 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
169 SkipUntil(tok::r_paren, false);
170 }
John McCall7f040a92010-12-24 02:08:15 +0000171 if (endLoc)
172 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000174}
175
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000176
177/// Parse the arguments to a parameterized GNU attribute
178void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
179 SourceLocation AttrNameLoc,
180 ParsedAttributes &Attrs,
181 SourceLocation *EndLoc) {
182
183 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
184
185 // Availability attributes have their own grammar.
186 if (AttrName->isStr("availability")) {
187 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
188 return;
189 }
190 // Thread safety attributes fit into the FIXME case above, so we
191 // just parse the arguments as a list of expressions
192 if (IsThreadSafetyAttribute(AttrName->getName())) {
193 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
194 return;
195 }
196
197 ConsumeParen(); // ignore the left paren loc for now
198
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000199 IdentifierInfo *ParmName = 0;
200 SourceLocation ParmLoc;
201 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000202
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000203 switch (Tok.getKind()) {
204 case tok::kw_char:
205 case tok::kw_wchar_t:
206 case tok::kw_char16_t:
207 case tok::kw_char32_t:
208 case tok::kw_bool:
209 case tok::kw_short:
210 case tok::kw_int:
211 case tok::kw_long:
212 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000213 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000214 case tok::kw_signed:
215 case tok::kw_unsigned:
216 case tok::kw_float:
217 case tok::kw_double:
218 case tok::kw_void:
219 case tok::kw_typeof:
220 // __attribute__(( vec_type_hint(char) ))
221 // FIXME: Don't just discard the builtin type token.
222 ConsumeToken();
223 BuiltinType = true;
224 break;
225
226 case tok::identifier:
227 ParmName = Tok.getIdentifierInfo();
228 ParmLoc = ConsumeToken();
229 break;
230
231 default:
232 break;
233 }
234
235 ExprVector ArgExprs(Actions);
236
237 if (!BuiltinType &&
238 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
239 // Eat the comma.
240 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000241 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000242
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000243 // Parse the non-empty comma-separated list of expressions.
244 while (1) {
245 ExprResult ArgExpr(ParseAssignmentExpression());
246 if (ArgExpr.isInvalid()) {
247 SkipUntil(tok::r_paren);
248 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000249 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000250 ArgExprs.push_back(ArgExpr.release());
251 if (Tok.isNot(tok::comma))
252 break;
253 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000254 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000255 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000256 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
257 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
258 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000259 while (Tok.is(tok::identifier)) {
260 ConsumeToken();
261 if (Tok.is(tok::greater))
262 break;
263 if (Tok.is(tok::comma)) {
264 ConsumeToken();
265 continue;
266 }
267 }
268 if (Tok.isNot(tok::greater))
269 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000270 SkipUntil(tok::r_paren, false, true); // skip until ')'
271 }
272 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000273
274 SourceLocation RParen = Tok.getLocation();
275 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
276 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000277 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000278 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size(),
279 AttributeList::AS_GNU);
Sean Hunt8e083e72012-06-19 23:57:03 +0000280 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000281 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000282 }
283}
284
Chad Rosier8decdee2012-06-26 22:30:43 +0000285/// \brief Parses a single argument for a declspec, including the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000286/// surrounding parens.
Chad Rosier8decdee2012-06-26 22:30:43 +0000287void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000288 SourceLocation AttrNameLoc,
289 ParsedAttributes &Attrs)
290{
291 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000292 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000293 AttrName->getNameStart(), tok::r_paren))
294 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000295
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000296 ExprResult ArgExpr(ParseConstantExpression());
297 if (ArgExpr.isInvalid()) {
298 T.skipToEnd();
299 return;
300 }
301 Expr *ExprList = ArgExpr.take();
Chad Rosier8decdee2012-06-26 22:30:43 +0000302 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000303 &ExprList, 1, AttributeList::AS_Declspec);
304
305 T.consumeClose();
306}
307
Chad Rosier8decdee2012-06-26 22:30:43 +0000308/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000309/// arguments.
310bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
311 return llvm::StringSwitch<bool>(Ident->getName())
312 .Case("dllimport", true)
313 .Case("dllexport", true)
314 .Case("noreturn", true)
315 .Case("nothrow", true)
316 .Case("noinline", true)
317 .Case("naked", true)
318 .Case("appdomain", true)
319 .Case("process", true)
320 .Case("jitintrinsic", true)
321 .Case("noalias", true)
322 .Case("restrict", true)
323 .Case("novtable", true)
324 .Case("selectany", true)
325 .Case("thread", true)
326 .Default(false);
327}
328
Chad Rosier8decdee2012-06-26 22:30:43 +0000329/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000330/// parameters). Will return false if we properly handled the declspec, or
331/// true if it is an unknown declspec.
Chad Rosier8decdee2012-06-26 22:30:43 +0000332void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000333 SourceLocation Loc,
334 ParsedAttributes &Attrs) {
335 // Try to handle the easy case first -- these declspecs all take a single
336 // parameter as their argument.
337 if (llvm::StringSwitch<bool>(Ident->getName())
338 .Case("uuid", true)
339 .Case("align", true)
340 .Case("allocate", true)
341 .Default(false)) {
342 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
343 } else if (Ident->getName() == "deprecated") {
Chad Rosier8decdee2012-06-26 22:30:43 +0000344 // The deprecated declspec has an optional single argument, so we will
345 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000346 // not.
347 if (Tok.getKind() == tok::l_paren)
348 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
349 else
Chad Rosier8decdee2012-06-26 22:30:43 +0000350 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000351 AttributeList::AS_Declspec);
352 } else if (Ident->getName() == "property") {
353 // The property declspec is more complex in that it can take one or two
Chad Rosier8decdee2012-06-26 22:30:43 +0000354 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000355 // must be named get or put.
356 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000357 // For right now, we will just skip to the closing right paren of the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000358 // property expression.
359 //
360 // FIXME: we should deal with __declspec(property) at some point because it
361 // is used in the platform SDK headers for the Parallel Patterns Library
362 // and ATL.
363 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000364 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000365 Ident->getNameStart(), tok::r_paren))
366 return;
367 T.skipToEnd();
368 } else {
369 // We don't recognize this as a valid declspec, but instead of creating the
370 // attribute and allowing sema to warn about it, we will warn here instead.
371 // This is because some attributes have multiple spellings, but we need to
372 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosier8decdee2012-06-26 22:30:43 +0000373 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000374 // both locations.
375 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
376
377 // If there's an open paren, we should eat the open and close parens under
378 // the assumption that this unknown declspec has parameters.
379 BalancedDelimiterTracker T(*this, tok::l_paren);
380 if (!T.consumeOpen())
381 T.skipToEnd();
382 }
383}
384
Eli Friedmana23b4852009-06-08 07:21:15 +0000385/// [MS] decl-specifier:
386/// __declspec ( extended-decl-modifier-seq )
387///
388/// [MS] extended-decl-modifier-seq:
389/// extended-decl-modifier[opt]
390/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000391void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000392 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000393
Steve Narofff59e17e2008-12-24 20:59:21 +0000394 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000395 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000396 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000397 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000398 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000399
Chad Rosier8decdee2012-06-26 22:30:43 +0000400 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000401 // you can specify multiple attributes per declspec.
402 while (Tok.getKind() != tok::r_paren) {
403 // We expect either a well-known identifier or a generic string. Anything
404 // else is a malformed declspec.
405 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosier8decdee2012-06-26 22:30:43 +0000406 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000407 Tok.getKind() != tok::kw_restrict) {
408 Diag(Tok, diag::err_ms_declspec_type);
409 T.skipToEnd();
410 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000411 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000412
413 IdentifierInfo *AttrName;
414 SourceLocation AttrNameLoc;
415 if (IsString) {
416 SmallString<8> StrBuffer;
417 bool Invalid = false;
418 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
419 if (Invalid) {
420 T.skipToEnd();
421 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000422 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000423 AttrName = PP.getIdentifierInfo(Str);
424 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000425 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000426 AttrName = Tok.getIdentifierInfo();
427 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000428 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000429
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000430 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosier8decdee2012-06-26 22:30:43 +0000431 // If we have a generic string, we will allow it because there is no
432 // documented list of allowable string declspecs, but we know they exist
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000433 // (for instance, SAL declspecs in older versions of MSVC).
434 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000435 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000436 // arguments and can be turned into an attribute directly.
Chad Rosier8decdee2012-06-26 22:30:43 +0000437 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000438 0, 0, AttributeList::AS_Declspec);
439 else
440 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000441 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000442 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000443}
444
John McCall7f040a92010-12-24 02:08:15 +0000445void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000446 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000447 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000448 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000449 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000450 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000451 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000452 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
453 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000454 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000455 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Eli Friedman290eeb02009-06-08 23:27:34 +0000456 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000457}
458
John McCall7f040a92010-12-24 02:08:15 +0000459void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000460 // Treat these like attributes
461 while (Tok.is(tok::kw___pascal)) {
462 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
463 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000464 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000465 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000466 }
John McCall7f040a92010-12-24 02:08:15 +0000467}
468
Peter Collingbournef315fa82011-02-14 01:42:53 +0000469void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
470 // Treat these like attributes
471 while (Tok.is(tok::kw___kernel)) {
472 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000473 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
474 AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000475 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000476 }
477}
478
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000479void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
480 SourceLocation Loc = Tok.getLocation();
481 switch(Tok.getKind()) {
482 // OpenCL qualifiers:
483 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000484 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000485 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000486 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000487 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000488 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000489
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000490 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000491 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000492 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000493 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000494 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000495
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000496 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000497 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000498 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000499 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000500 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000501
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000502 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000503 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000504 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000505 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000506 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000507
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000508 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000509 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000510 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000511 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000512 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000513
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000514 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000515 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000516 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000517 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000518 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000519
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000520 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000521 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000522 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000523 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000524 break;
525 default: break;
526 }
527}
528
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000529/// \brief Parse a version number.
530///
531/// version:
532/// simple-integer
533/// simple-integer ',' simple-integer
534/// simple-integer ',' simple-integer ',' simple-integer
535VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
536 Range = Tok.getLocation();
537
538 if (!Tok.is(tok::numeric_constant)) {
539 Diag(Tok, diag::err_expected_version);
540 SkipUntil(tok::comma, tok::r_paren, true, true, true);
541 return VersionTuple();
542 }
543
544 // Parse the major (and possibly minor and subminor) versions, which
545 // are stored in the numeric constant. We utilize a quirk of the
546 // lexer, which is that it handles something like 1.2.3 as a single
547 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000548 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000549 Buffer.resize(Tok.getLength()+1);
550 const char *ThisTokBegin = &Buffer[0];
551
552 // Get the spelling of the token, which eliminates trigraphs, etc.
553 bool Invalid = false;
554 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
555 if (Invalid)
556 return VersionTuple();
557
558 // Parse the major version.
559 unsigned AfterMajor = 0;
560 unsigned Major = 0;
561 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
562 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
563 ++AfterMajor;
564 }
565
566 if (AfterMajor == 0) {
567 Diag(Tok, diag::err_expected_version);
568 SkipUntil(tok::comma, tok::r_paren, true, true, true);
569 return VersionTuple();
570 }
571
572 if (AfterMajor == ActualLength) {
573 ConsumeToken();
574
575 // We only had a single version component.
576 if (Major == 0) {
577 Diag(Tok, diag::err_zero_version);
578 return VersionTuple();
579 }
580
581 return VersionTuple(Major);
582 }
583
584 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
585 Diag(Tok, diag::err_expected_version);
586 SkipUntil(tok::comma, tok::r_paren, true, true, true);
587 return VersionTuple();
588 }
589
590 // Parse the minor version.
591 unsigned AfterMinor = AfterMajor + 1;
592 unsigned Minor = 0;
593 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
594 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
595 ++AfterMinor;
596 }
597
598 if (AfterMinor == ActualLength) {
599 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000600
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000601 // We had major.minor.
602 if (Major == 0 && Minor == 0) {
603 Diag(Tok, diag::err_zero_version);
604 return VersionTuple();
605 }
606
Chad Rosier8decdee2012-06-26 22:30:43 +0000607 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000608 }
609
610 // If what follows is not a '.', we have a problem.
611 if (ThisTokBegin[AfterMinor] != '.') {
612 Diag(Tok, diag::err_expected_version);
613 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosier8decdee2012-06-26 22:30:43 +0000614 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000615 }
616
617 // Parse the subminor version.
618 unsigned AfterSubminor = AfterMinor + 1;
619 unsigned Subminor = 0;
620 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
621 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
622 ++AfterSubminor;
623 }
624
625 if (AfterSubminor != ActualLength) {
626 Diag(Tok, diag::err_expected_version);
627 SkipUntil(tok::comma, tok::r_paren, true, true, true);
628 return VersionTuple();
629 }
630 ConsumeToken();
631 return VersionTuple(Major, Minor, Subminor);
632}
633
634/// \brief Parse the contents of the "availability" attribute.
635///
636/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000637/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000638///
639/// platform:
640/// identifier
641///
642/// version-arg-list:
643/// version-arg
644/// version-arg ',' version-arg-list
645///
646/// version-arg:
647/// 'introduced' '=' version
648/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000649/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000650/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000651/// opt-message:
652/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000653void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
654 SourceLocation AvailabilityLoc,
655 ParsedAttributes &attrs,
656 SourceLocation *endLoc) {
657 SourceLocation PlatformLoc;
658 IdentifierInfo *Platform = 0;
659
660 enum { Introduced, Deprecated, Obsoleted, Unknown };
661 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000662 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000663
664 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000665 BalancedDelimiterTracker T(*this, tok::l_paren);
666 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000667 Diag(Tok, diag::err_expected_lparen);
668 return;
669 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000670
671 // Parse the platform name,
672 if (Tok.isNot(tok::identifier)) {
673 Diag(Tok, diag::err_availability_expected_platform);
674 SkipUntil(tok::r_paren);
675 return;
676 }
677 Platform = Tok.getIdentifierInfo();
678 PlatformLoc = ConsumeToken();
679
680 // Parse the ',' following the platform name.
681 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
682 return;
683
684 // If we haven't grabbed the pointers for the identifiers
685 // "introduced", "deprecated", and "obsoleted", do so now.
686 if (!Ident_introduced) {
687 Ident_introduced = PP.getIdentifierInfo("introduced");
688 Ident_deprecated = PP.getIdentifierInfo("deprecated");
689 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000690 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000691 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000692 }
693
694 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000695 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000696 do {
697 if (Tok.isNot(tok::identifier)) {
698 Diag(Tok, diag::err_availability_expected_change);
699 SkipUntil(tok::r_paren);
700 return;
701 }
702 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
703 SourceLocation KeywordLoc = ConsumeToken();
704
Douglas Gregorb53e4172011-03-26 03:35:55 +0000705 if (Keyword == Ident_unavailable) {
706 if (UnavailableLoc.isValid()) {
707 Diag(KeywordLoc, diag::err_availability_redundant)
708 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000709 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000710 UnavailableLoc = KeywordLoc;
711
712 if (Tok.isNot(tok::comma))
713 break;
714
715 ConsumeToken();
716 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000717 }
718
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000719 if (Tok.isNot(tok::equal)) {
720 Diag(Tok, diag::err_expected_equal_after)
721 << Keyword;
722 SkipUntil(tok::r_paren);
723 return;
724 }
725 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000726 if (Keyword == Ident_message) {
727 if (!isTokenStringLiteral()) {
728 Diag(Tok, diag::err_expected_string_literal);
729 SkipUntil(tok::r_paren);
730 return;
731 }
732 MessageExpr = ParseStringLiteralExpression();
733 break;
734 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000735
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000736 SourceRange VersionRange;
737 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000738
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000739 if (Version.empty()) {
740 SkipUntil(tok::r_paren);
741 return;
742 }
743
744 unsigned Index;
745 if (Keyword == Ident_introduced)
746 Index = Introduced;
747 else if (Keyword == Ident_deprecated)
748 Index = Deprecated;
749 else if (Keyword == Ident_obsoleted)
750 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000751 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000752 Index = Unknown;
753
754 if (Index < Unknown) {
755 if (!Changes[Index].KeywordLoc.isInvalid()) {
756 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000757 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000758 << SourceRange(Changes[Index].KeywordLoc,
759 Changes[Index].VersionRange.getEnd());
760 }
761
762 Changes[Index].KeywordLoc = KeywordLoc;
763 Changes[Index].Version = Version;
764 Changes[Index].VersionRange = VersionRange;
765 } else {
766 Diag(KeywordLoc, diag::err_availability_unknown_change)
767 << Keyword << VersionRange;
768 }
769
770 if (Tok.isNot(tok::comma))
771 break;
772
773 ConsumeToken();
774 } while (true);
775
776 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000777 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000778 return;
779
780 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000781 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000782
Douglas Gregorb53e4172011-03-26 03:35:55 +0000783 // The 'unavailable' availability cannot be combined with any other
784 // availability changes. Make sure that hasn't happened.
785 if (UnavailableLoc.isValid()) {
786 bool Complained = false;
787 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
788 if (Changes[Index].KeywordLoc.isValid()) {
789 if (!Complained) {
790 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
791 << SourceRange(Changes[Index].KeywordLoc,
792 Changes[Index].VersionRange.getEnd());
793 Complained = true;
794 }
795
796 // Clear out the availability.
797 Changes[Index] = AvailabilityChange();
798 }
799 }
800 }
801
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000802 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000803 attrs.addNew(&Availability,
804 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000805 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000806 Platform, PlatformLoc,
807 Changes[Introduced],
808 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000809 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000810 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000811 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000812}
813
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000814
815// Late Parsed Attributes:
816// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
817
818void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
819
820void Parser::LateParsedClass::ParseLexedAttributes() {
821 Self->ParseLexedAttributes(*Class);
822}
823
824void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000825 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000826}
827
828/// Wrapper class which calls ParseLexedAttribute, after setting up the
829/// scope appropriately.
830void Parser::ParseLexedAttributes(ParsingClass &Class) {
831 // Deal with templates
832 // FIXME: Test cases to make sure this does the right thing for templates.
833 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
834 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
835 HasTemplateScope);
836 if (HasTemplateScope)
837 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
838
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000839 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000840 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000841 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000842 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
843 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
844
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000845 // Enter the scope of nested classes
846 if (!AlreadyHasClassScope)
847 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
848 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000849 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000850 // Allow 'this' within late-parsed attributes.
Chad Rosier8decdee2012-06-26 22:30:43 +0000851 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000852 /*TypeQuals=*/0);
Chad Rosier8decdee2012-06-26 22:30:43 +0000853
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000854 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
855 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
856 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000857 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000858
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000859 if (!AlreadyHasClassScope)
860 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
861 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000862}
863
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000864
865/// \brief Parse all attributes in LAs, and attach them to Decl D.
866void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
867 bool EnterScope, bool OnDefinition) {
868 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000869 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000870 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000871 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000872 }
873 LAs.clear();
874}
875
876
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000877/// \brief Finish parsing an attribute for which parsing was delayed.
878/// This will be called at the end of parsing a class declaration
879/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +0000880/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000881/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000882void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
883 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000884 // Save the current token position.
885 SourceLocation OrigLoc = Tok.getLocation();
886
887 // Append the current token at the end of the new token stream so that it
888 // doesn't get lost.
889 LA.Toks.push_back(Tok);
890 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
891 // Consume the previously pushed token.
892 ConsumeAnyToken();
893
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000894 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
895 Diag(Tok, diag::warn_attribute_on_function_definition)
896 << LA.AttrName.getName();
897 }
898
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000899 ParsedAttributes Attrs(AttrFactory);
900 SourceLocation endLoc;
901
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000902 if (LA.Decls.size() == 1) {
903 Decl *D = LA.Decls[0];
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000904
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000905 // If the Decl is templatized, add template parameters to scope.
906 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
907 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
908 if (HasTemplateScope)
909 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000910
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000911 // If the Decl is on a function, add function parameters to the scope.
912 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
913 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
914 if (HasFunctionScope)
915 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
916
917 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
918
919 if (HasFunctionScope) {
920 Actions.ActOnExitFunctionContext();
921 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
922 }
923 if (HasTemplateScope) {
924 TempScope.Exit();
925 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000926 } else if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000927 // If there are multiple decls, then the decl cannot be within the
928 // function scope.
929 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000930 } else {
931 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000932 }
933
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000934 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
935 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
936 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000937
938 if (Tok.getLocation() != OrigLoc) {
939 // Due to a parsing error, we either went over the cached tokens or
940 // there are still cached tokens left, so we skip the leftover tokens.
941 // Since this is an uncommon situation that should be avoided, use the
942 // expensive isBeforeInTranslationUnit call.
943 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
944 OrigLoc))
945 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000946 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000947 }
948}
949
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000950/// \brief Wrapper around a case statement checking if AttrName is
951/// one of the thread safety attributes
952bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
953 return llvm::StringSwitch<bool>(AttrName)
954 .Case("guarded_by", true)
955 .Case("guarded_var", true)
956 .Case("pt_guarded_by", true)
957 .Case("pt_guarded_var", true)
958 .Case("lockable", true)
959 .Case("scoped_lockable", true)
960 .Case("no_thread_safety_analysis", true)
961 .Case("acquired_after", true)
962 .Case("acquired_before", true)
963 .Case("exclusive_lock_function", true)
964 .Case("shared_lock_function", true)
965 .Case("exclusive_trylock_function", true)
966 .Case("shared_trylock_function", true)
967 .Case("unlock_function", true)
968 .Case("lock_returned", true)
969 .Case("locks_excluded", true)
970 .Case("exclusive_locks_required", true)
971 .Case("shared_locks_required", true)
972 .Default(false);
973}
974
975/// \brief Parse the contents of thread safety attributes. These
976/// should always be parsed as an expression list.
977///
978/// We need to special case the parsing due to the fact that if the first token
979/// of the first argument is an identifier, the main parse loop will store
980/// that token as a "parameter" and the rest of
981/// the arguments will be added to a list of "arguments". However,
982/// subsequent tokens in the first argument are lost. We instead parse each
983/// argument as an expression and add all arguments to the list of "arguments".
984/// In future, we will take advantage of this special case to also
985/// deal with some argument scoping issues here (for example, referring to a
986/// function parameter in the attribute on that function).
987void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
988 SourceLocation AttrNameLoc,
989 ParsedAttributes &Attrs,
990 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000991 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000992
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000993 BalancedDelimiterTracker T(*this, tok::l_paren);
994 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +0000995
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000996 ExprVector ArgExprs(Actions);
997 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +0000998
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000999 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001000 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001001 ExprResult ArgExpr(ParseAssignmentExpression());
1002 if (ArgExpr.isInvalid()) {
1003 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001004 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001005 break;
1006 } else {
1007 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001008 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001009 if (Tok.isNot(tok::comma))
1010 break;
1011 ConsumeToken(); // Eat the comma, move to the next argument
1012 }
1013 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001014 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001015 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Sean Hunt93f95f22012-06-18 16:13:52 +00001016 ArgExprs.take(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001017 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001018 if (EndLoc)
1019 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001020}
1021
Richard Smith6ee326a2012-04-10 01:32:12 +00001022/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1023/// of a C++11 attribute-specifier in a location where an attribute is not
1024/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1025/// situation.
1026///
1027/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1028/// this doesn't appear to actually be an attribute-specifier, and the caller
1029/// should try to parse it.
1030bool Parser::DiagnoseProhibitedCXX11Attribute() {
1031 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1032
1033 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1034 case CAK_NotAttributeSpecifier:
1035 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1036 return false;
1037
1038 case CAK_InvalidAttributeSpecifier:
1039 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1040 return false;
1041
1042 case CAK_AttributeSpecifier:
1043 // Parse and discard the attributes.
1044 SourceLocation BeginLoc = ConsumeBracket();
1045 ConsumeBracket();
1046 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1047 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1048 SourceLocation EndLoc = ConsumeBracket();
1049 Diag(BeginLoc, diag::err_attributes_not_allowed)
1050 << SourceRange(BeginLoc, EndLoc);
1051 return true;
1052 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001053 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001054}
1055
John McCall7f040a92010-12-24 02:08:15 +00001056void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1057 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1058 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001059}
1060
Reid Spencer5f016e22007-07-11 17:01:13 +00001061/// ParseDeclaration - Parse a full 'declaration', which consists of
1062/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001063/// 'Context' should be a Declarator::TheContext value. This returns the
1064/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001065///
1066/// declaration: [C99 6.7]
1067/// block-declaration ->
1068/// simple-declaration
1069/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001070/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001071/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001072/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001073/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001074/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001075/// others... [FIXME]
1076///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001077Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1078 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001079 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001080 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001081 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001082 // Must temporarily exit the objective-c container scope for
1083 // parsing c none objective-c decls.
1084 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001085
John McCalld226f652010-08-21 09:40:31 +00001086 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001087 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001088 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001089 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001090 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001091 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001092 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001093 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001094 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001095 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001096 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001097 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001098 SourceLocation InlineLoc = ConsumeToken();
1099 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1100 break;
1101 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001102 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001103 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001104 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001105 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001106 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001107 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001108 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001109 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001110 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001111 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001112 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001113 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001114 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001115 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001116 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001117 default:
John McCall7f040a92010-12-24 02:08:15 +00001118 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001119 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001120
Chris Lattner682bf922009-03-29 16:50:03 +00001121 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001122 // single decl, convert it now. Alias declarations can also declare a type;
1123 // include that too if it is present.
1124 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001125}
1126
1127/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1128/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001129/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1130/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001131///[C90/C++]init-declarator-list ';' [TODO]
1132/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001133///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001134/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001135/// attribute-specifier-seq[opt] type-specifier-seq declarator
1136///
Chris Lattnercd147752009-03-29 17:27:48 +00001137/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001138/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001139///
1140/// If FRI is non-null, we might be parsing a for-range-declaration instead
1141/// of a simple-declaration. If we find that we are, we also parse the
1142/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001143Parser::DeclGroupPtrTy
1144Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1145 SourceLocation &DeclEnd,
1146 ParsedAttributesWithRange &attrs,
1147 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001149 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001150 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001151
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001152 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001153 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001154
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1156 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001157 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001158 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001159 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001160 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001161 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001162 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001163 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001165
1166 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001167}
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Richard Smith0706df42011-10-19 21:33:05 +00001169/// Returns true if this might be the start of a declarator, or a common typo
1170/// for a declarator.
1171bool Parser::MightBeDeclarator(unsigned Context) {
1172 switch (Tok.getKind()) {
1173 case tok::annot_cxxscope:
1174 case tok::annot_template_id:
1175 case tok::caret:
1176 case tok::code_completion:
1177 case tok::coloncolon:
1178 case tok::ellipsis:
1179 case tok::kw___attribute:
1180 case tok::kw_operator:
1181 case tok::l_paren:
1182 case tok::star:
1183 return true;
1184
1185 case tok::amp:
1186 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001187 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001188
Richard Smith1c94c162012-01-09 22:31:44 +00001189 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001190 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001191 NextToken().is(tok::l_square);
1192
1193 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001194 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001195
Richard Smith0706df42011-10-19 21:33:05 +00001196 case tok::identifier:
1197 switch (NextToken().getKind()) {
1198 case tok::code_completion:
1199 case tok::coloncolon:
1200 case tok::comma:
1201 case tok::equal:
1202 case tok::equalequal: // Might be a typo for '='.
1203 case tok::kw_alignas:
1204 case tok::kw_asm:
1205 case tok::kw___attribute:
1206 case tok::l_brace:
1207 case tok::l_paren:
1208 case tok::l_square:
1209 case tok::less:
1210 case tok::r_brace:
1211 case tok::r_paren:
1212 case tok::r_square:
1213 case tok::semi:
1214 return true;
1215
1216 case tok::colon:
1217 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001218 // and in block scope it's probably a label. Inside a class definition,
1219 // this is a bit-field.
1220 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001221 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001222
1223 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001224 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001225
1226 default:
1227 return false;
1228 }
1229
1230 default:
1231 return false;
1232 }
1233}
1234
Richard Smith994d73f2012-04-11 20:59:20 +00001235/// Skip until we reach something which seems like a sensible place to pick
1236/// up parsing after a malformed declaration. This will sometimes stop sooner
1237/// than SkipUntil(tok::r_brace) would, but will never stop later.
1238void Parser::SkipMalformedDecl() {
1239 while (true) {
1240 switch (Tok.getKind()) {
1241 case tok::l_brace:
1242 // Skip until matching }, then stop. We've probably skipped over
1243 // a malformed class or function definition or similar.
1244 ConsumeBrace();
1245 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1246 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1247 // This declaration isn't over yet. Keep skipping.
1248 continue;
1249 }
1250 if (Tok.is(tok::semi))
1251 ConsumeToken();
1252 return;
1253
1254 case tok::l_square:
1255 ConsumeBracket();
1256 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1257 continue;
1258
1259 case tok::l_paren:
1260 ConsumeParen();
1261 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1262 continue;
1263
1264 case tok::r_brace:
1265 return;
1266
1267 case tok::semi:
1268 ConsumeToken();
1269 return;
1270
1271 case tok::kw_inline:
1272 // 'inline namespace' at the start of a line is almost certainly
1273 // a good place to pick back up parsing.
1274 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace))
1275 return;
1276 break;
1277
1278 case tok::kw_namespace:
1279 // 'namespace' at the start of a line is almost certainly a good
1280 // place to pick back up parsing.
1281 if (Tok.isAtStartOfLine())
1282 return;
1283 break;
1284
1285 case tok::eof:
1286 return;
1287
1288 default:
1289 break;
1290 }
1291
1292 ConsumeAnyToken();
1293 }
1294}
1295
John McCalld8ac0572009-11-03 19:26:08 +00001296/// ParseDeclGroup - Having concluded that this is either a function
1297/// definition or a group of object declarations, actually parse the
1298/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001299Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1300 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001301 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001302 SourceLocation *DeclEnd,
1303 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001304 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001305 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001306 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001307
John McCalld8ac0572009-11-03 19:26:08 +00001308 // Bail out if the first declarator didn't seem well-formed.
1309 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001310 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001311 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001312 }
Mike Stump1eb44332009-09-09 15:08:12 +00001313
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001314 // Save late-parsed attributes for now; they need to be parsed in the
1315 // appropriate function scope after the function Decl has been constructed.
1316 LateParsedAttrList LateParsedAttrs;
1317 if (D.isFunctionDeclarator())
1318 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1319
Chris Lattnerc82daef2010-07-11 22:24:20 +00001320 // Check to see if we have a function *definition* which must have a body.
1321 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1322 // Look at the next token to make sure that this isn't a function
1323 // declaration. We have to check this because __attribute__ might be the
1324 // start of a function definition in GCC-extended K&R C.
1325 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001326
Chris Lattner004659a2010-07-11 22:42:07 +00001327 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001328 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1329 Diag(Tok, diag::err_function_declared_typedef);
1330
1331 // Recover by treating the 'typedef' as spurious.
1332 DS.ClearStorageClassSpecs();
1333 }
1334
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001335 Decl *TheDecl =
1336 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001337 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001338 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001339
Chris Lattner004659a2010-07-11 22:42:07 +00001340 if (isDeclarationSpecifier()) {
1341 // If there is an invalid declaration specifier right after the function
1342 // prototype, then we must be in a missing semicolon case where this isn't
1343 // actually a body. Just fall through into the code that handles it as a
1344 // prototype, and let the top-level code handle the erroneous declspec
1345 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001346 } else {
1347 Diag(Tok, diag::err_expected_fn_body);
1348 SkipUntil(tok::semi);
1349 return DeclGroupPtrTy();
1350 }
1351 }
1352
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001353 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001354 return DeclGroupPtrTy();
1355
1356 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1357 // must parse and analyze the for-range-initializer before the declaration is
1358 // analyzed.
1359 if (FRI && Tok.is(tok::colon)) {
1360 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001361 if (Tok.is(tok::l_brace))
1362 FRI->RangeExpr = ParseBraceInitializer();
1363 else
1364 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001365 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1366 Actions.ActOnCXXForRangeDecl(ThisDecl);
1367 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001368 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001369 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1370 }
1371
Chris Lattner5f9e2722011-07-23 10:55:15 +00001372 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001373 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001374 if (LateParsedAttrs.size() > 0)
1375 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001376 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001377 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001378 DeclsInGroup.push_back(FirstDecl);
1379
Richard Smith0706df42011-10-19 21:33:05 +00001380 bool ExpectSemi = Context != Declarator::ForContext;
1381
Fariborz Jahaniancda10412012-07-03 22:29:23 +00001382 // FIXME. make this work for Obj-C++11 parser.
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001383 if (CurParsedObjCImpl && D.isFunctionDeclarator() &&
Fariborz Jahaniancda10412012-07-03 22:29:23 +00001384 Tok.is(tok::l_brace) &&
1385 !getLangOpts().CPlusPlus0x) {
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001386 // Consume the tokens and store them for later parsing.
1387 StashAwayMethodOrFunctionBodyTokens(FirstDecl);
1388 CurParsedObjCImpl->HasCFunction = true;
1389 ExpectSemi = false;
1390 }
1391
John McCalld8ac0572009-11-03 19:26:08 +00001392 // If we don't have a comma, it is either the end of the list (a ';') or an
1393 // error, bail out.
1394 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001395 SourceLocation CommaLoc = ConsumeToken();
1396
1397 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1398 // This comma was followed by a line-break and something which can't be
1399 // the start of a declarator. The comma was probably a typo for a
1400 // semicolon.
1401 Diag(CommaLoc, diag::err_expected_semi_declaration)
1402 << FixItHint::CreateReplacement(CommaLoc, ";");
1403 ExpectSemi = false;
1404 break;
1405 }
John McCalld8ac0572009-11-03 19:26:08 +00001406
1407 // Parse the next declarator.
1408 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001409 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001410
1411 // Accept attributes in an init-declarator. In the first declarator in a
1412 // declaration, these would be part of the declspec. In subsequent
1413 // declarators, they become part of the declarator itself, so that they
1414 // don't apply to declarators after *this* one. Examples:
1415 // short __attribute__((common)) var; -> declspec
1416 // short var __attribute__((common)); -> declarator
1417 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001418 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001419
1420 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001421 if (!D.isInvalidType()) {
1422 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1423 D.complete(ThisDecl);
1424 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001425 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001426 }
John McCalld8ac0572009-11-03 19:26:08 +00001427 }
1428
1429 if (DeclEnd)
1430 *DeclEnd = Tok.getLocation();
1431
Richard Smith0706df42011-10-19 21:33:05 +00001432 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001433 ExpectAndConsumeSemi(Context == Declarator::FileContext
1434 ? diag::err_invalid_token_after_toplevel_declarator
1435 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001436 // Okay, there was no semicolon and one was expected. If we see a
1437 // declaration specifier, just assume it was missing and continue parsing.
1438 // Otherwise things are very confused and we skip to recover.
1439 if (!isDeclarationSpecifier()) {
1440 SkipUntil(tok::r_brace, true, true);
1441 if (Tok.is(tok::semi))
1442 ConsumeToken();
1443 }
John McCalld8ac0572009-11-03 19:26:08 +00001444 }
1445
Douglas Gregor23c94db2010-07-02 17:43:08 +00001446 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001447 DeclsInGroup.data(),
1448 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001449}
1450
Richard Smithad762fc2011-04-14 22:09:26 +00001451/// Parse an optional simple-asm-expr and attributes, and attach them to a
1452/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001453bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001454 // If a simple-asm-expr is present, parse it.
1455 if (Tok.is(tok::kw_asm)) {
1456 SourceLocation Loc;
1457 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1458 if (AsmLabel.isInvalid()) {
1459 SkipUntil(tok::semi, true, true);
1460 return true;
1461 }
1462
1463 D.setAsmLabel(AsmLabel.release());
1464 D.SetRangeEnd(Loc);
1465 }
1466
1467 MaybeParseGNUAttributes(D);
1468 return false;
1469}
1470
Douglas Gregor1426e532009-05-12 21:31:51 +00001471/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1472/// declarator'. This method parses the remainder of the declaration
1473/// (including any attributes or initializer, among other things) and
1474/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001475///
Reid Spencer5f016e22007-07-11 17:01:13 +00001476/// init-declarator: [C99 6.7]
1477/// declarator
1478/// declarator '=' initializer
1479/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1480/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001481/// [C++] declarator initializer[opt]
1482///
1483/// [C++] initializer:
1484/// [C++] '=' initializer-clause
1485/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001486/// [C++0x] '=' 'default' [TODO]
1487/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001488/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001489///
1490/// According to the standard grammar, =default and =delete are function
1491/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001492///
John McCalld226f652010-08-21 09:40:31 +00001493Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001494 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001495 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001496 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Richard Smithad762fc2011-04-14 22:09:26 +00001498 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1499}
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Richard Smithad762fc2011-04-14 22:09:26 +00001501Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1502 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001503 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001504 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001505 switch (TemplateInfo.Kind) {
1506 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001507 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001508 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001509
Douglas Gregord5a423b2009-09-25 18:43:00 +00001510 case ParsedTemplateInfo::Template:
1511 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001512 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001513 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001514 TemplateInfo.TemplateParams->data(),
1515 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001516 D);
1517 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001518
Douglas Gregord5a423b2009-09-25 18:43:00 +00001519 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001520 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001521 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001522 TemplateInfo.ExternLoc,
1523 TemplateInfo.TemplateLoc,
1524 D);
1525 if (ThisRes.isInvalid()) {
1526 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001527 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001528 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001529
Douglas Gregord5a423b2009-09-25 18:43:00 +00001530 ThisDecl = ThisRes.get();
1531 break;
1532 }
1533 }
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Richard Smith34b41d92011-02-20 03:19:35 +00001535 bool TypeContainsAuto =
1536 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1537
Douglas Gregor1426e532009-05-12 21:31:51 +00001538 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001539 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001540 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001541 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001542 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001543 if (D.isFunctionDeclarator())
1544 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1545 << 1 /* delete */;
1546 else
1547 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001548 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001549 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001550 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1551 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001552 else
1553 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001554 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001555 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001556 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001557 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001558 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001559
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001560 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001561 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001562 cutOffParsing();
1563 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001564 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001565
John McCall60d7b3a2010-08-24 06:29:42 +00001566 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001567
David Blaikie4e4d0842012-03-11 07:00:24 +00001568 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001569 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001570 ExitScope();
1571 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001572
Douglas Gregor1426e532009-05-12 21:31:51 +00001573 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001574 SkipUntil(tok::comma, true, true);
1575 Actions.ActOnInitializerError(ThisDecl);
1576 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001577 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1578 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001579 }
1580 } else if (Tok.is(tok::l_paren)) {
1581 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001582 BalancedDelimiterTracker T(*this, tok::l_paren);
1583 T.consumeOpen();
1584
Douglas Gregor1426e532009-05-12 21:31:51 +00001585 ExprVector Exprs(Actions);
1586 CommaLocsTy CommaLocs;
1587
David Blaikie4e4d0842012-03-11 07:00:24 +00001588 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001589 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001590 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001591 }
1592
Douglas Gregor1426e532009-05-12 21:31:51 +00001593 if (ParseExpressionList(Exprs, CommaLocs)) {
1594 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001595
David Blaikie4e4d0842012-03-11 07:00:24 +00001596 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001597 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001598 ExitScope();
1599 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001600 } else {
1601 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001602 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001603
1604 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1605 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001606
David Blaikie4e4d0842012-03-11 07:00:24 +00001607 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001608 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001609 ExitScope();
1610 }
1611
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001612 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1613 T.getCloseLocation(),
1614 move_arg(Exprs));
1615 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1616 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001617 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001618 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001619 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001620 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1621
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001622 if (D.getCXXScopeSpec().isSet()) {
1623 EnterScope(0);
1624 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1625 }
1626
1627 ExprResult Init(ParseBraceInitializer());
1628
1629 if (D.getCXXScopeSpec().isSet()) {
1630 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1631 ExitScope();
1632 }
1633
1634 if (Init.isInvalid()) {
1635 Actions.ActOnInitializerError(ThisDecl);
1636 } else
1637 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1638 /*DirectInit=*/true, TypeContainsAuto);
1639
Douglas Gregor1426e532009-05-12 21:31:51 +00001640 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001641 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001642 }
1643
Richard Smith483b9f32011-02-21 20:05:19 +00001644 Actions.FinalizeDeclaration(ThisDecl);
1645
Douglas Gregor1426e532009-05-12 21:31:51 +00001646 return ThisDecl;
1647}
1648
Reid Spencer5f016e22007-07-11 17:01:13 +00001649/// ParseSpecifierQualifierList
1650/// specifier-qualifier-list:
1651/// type-specifier specifier-qualifier-list[opt]
1652/// type-qualifier specifier-qualifier-list[opt]
1653/// [GNU] attributes specifier-qualifier-list[opt]
1654///
Richard Smith69730c12012-03-12 07:56:15 +00001655void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1656 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1658 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001659 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001660 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Reid Spencer5f016e22007-07-11 17:01:13 +00001662 // Validate declspec for type-name.
1663 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001664 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1665 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001666 Diag(Tok, diag::err_expected_type);
1667 DS.SetTypeSpecError();
1668 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1669 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001670 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001671 if (!DS.hasTypeSpecifier())
1672 DS.SetTypeSpecError();
1673 }
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Reid Spencer5f016e22007-07-11 17:01:13 +00001675 // Issue diagnostic and remove storage class if present.
1676 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1677 if (DS.getStorageClassSpecLoc().isValid())
1678 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1679 else
1680 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1681 DS.ClearStorageClassSpecs();
1682 }
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Reid Spencer5f016e22007-07-11 17:01:13 +00001684 // Issue diagnostic and remove function specfier if present.
1685 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001686 if (DS.isInlineSpecified())
1687 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1688 if (DS.isVirtualSpecified())
1689 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1690 if (DS.isExplicitSpecified())
1691 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001692 DS.ClearFunctionSpecs();
1693 }
Richard Smith69730c12012-03-12 07:56:15 +00001694
1695 // Issue diagnostic and remove constexpr specfier if present.
1696 if (DS.isConstexprSpecified()) {
1697 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1698 DS.ClearConstexprSpec();
1699 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001700}
1701
Chris Lattnerc199ab32009-04-12 20:42:31 +00001702/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1703/// specified token is valid after the identifier in a declarator which
1704/// immediately follows the declspec. For example, these things are valid:
1705///
1706/// int x [ 4]; // direct-declarator
1707/// int x ( int y); // direct-declarator
1708/// int(int x ) // direct-declarator
1709/// int x ; // simple-declaration
1710/// int x = 17; // init-declarator-list
1711/// int x , y; // init-declarator-list
1712/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001713/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001714/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001715///
1716/// This is not, because 'x' does not immediately follow the declspec (though
1717/// ')' happens to be valid anyway).
1718/// int (x)
1719///
1720static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1721 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1722 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001723 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001724}
1725
Chris Lattnere40c2952009-04-14 21:34:55 +00001726
1727/// ParseImplicitInt - This method is called when we have an non-typename
1728/// identifier in a declspec (which normally terminates the decl spec) when
1729/// the declspec has no type specifier. In this case, the declspec is either
1730/// malformed or is "implicit int" (in K&R and C89).
1731///
1732/// This method handles diagnosing this prettily and returns false if the
1733/// declspec is done being processed. If it recovers and thinks there may be
1734/// other pieces of declspec after it, it returns true.
1735///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001736bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001737 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001738 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001739 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001740
Chris Lattnere40c2952009-04-14 21:34:55 +00001741 SourceLocation Loc = Tok.getLocation();
1742 // If we see an identifier that is not a type name, we normally would
1743 // parse it as the identifer being declared. However, when a typename
1744 // is typo'd or the definition is not included, this will incorrectly
1745 // parse the typename as the identifier name and fall over misparsing
1746 // later parts of the diagnostic.
1747 //
1748 // As such, we try to do some look-ahead in cases where this would
1749 // otherwise be an "implicit-int" case to see if this is invalid. For
1750 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1751 // an identifier with implicit int, we'd get a parse error because the
1752 // next token is obviously invalid for a type. Parse these as a case
1753 // with an invalid type specifier.
1754 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001755
Chris Lattnere40c2952009-04-14 21:34:55 +00001756 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001757 // error, do lookahead to try to do better recovery. This never applies
1758 // within a type specifier. Outside of C++, we allow this even if the
1759 // language doesn't "officially" support implicit int -- we support
1760 // implicit int as an extension in C99 and C11. Allegedly, MS also
1761 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001762 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001763 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001764 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001765 // If this token is valid for implicit int, e.g. "static x = 4", then
1766 // we just avoid eating the identifier, so it will be parsed as the
1767 // identifier in the declarator.
1768 return false;
1769 }
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Richard Smith827adaf2012-05-15 21:01:51 +00001771 if (getLangOpts().CPlusPlus &&
1772 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1773 // Don't require a type specifier if we have the 'auto' storage class
1774 // specifier in C++98 -- we'll promote it to a type specifier.
1775 return false;
1776 }
1777
Chris Lattnere40c2952009-04-14 21:34:55 +00001778 // Otherwise, if we don't consume this token, we are going to emit an
1779 // error anyway. Try to recover from various common problems. Check
1780 // to see if this was a reference to a tag name without a tag specified.
1781 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001782 //
1783 // C++ doesn't need this, and isTagName doesn't take SS.
1784 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001785 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001786 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Douglas Gregor23c94db2010-07-02 17:43:08 +00001788 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001789 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001790 case DeclSpec::TST_enum:
1791 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1792 case DeclSpec::TST_union:
1793 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1794 case DeclSpec::TST_struct:
1795 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1796 case DeclSpec::TST_class:
1797 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001798 }
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Chris Lattnerf4382f52009-04-14 22:17:06 +00001800 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001801 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1802 LookupResult R(Actions, TokenName, SourceLocation(),
1803 Sema::LookupOrdinaryName);
1804
Chris Lattnerf4382f52009-04-14 22:17:06 +00001805 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001806 << TokenName << TagName << getLangOpts().CPlusPlus
1807 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1808
1809 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1810 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1811 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001812 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001813 << TokenName << TagName;
1814 }
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Chris Lattnerf4382f52009-04-14 22:17:06 +00001816 // Parse this as a tag as if the missing tag were present.
1817 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001818 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001819 else
Richard Smith69730c12012-03-12 07:56:15 +00001820 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1821 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001822 return true;
1823 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001824 }
Mike Stump1eb44332009-09-09 15:08:12 +00001825
Richard Smith8f0a7e72012-05-15 21:29:55 +00001826 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001827 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001828 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1829 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001830 // Look ahead to the next token to try to figure out what this declaration
1831 // was supposed to be.
1832 switch (NextToken().getKind()) {
1833 case tok::comma:
1834 case tok::equal:
1835 case tok::kw_asm:
1836 case tok::l_brace:
1837 case tok::l_square:
1838 case tok::semi:
1839 // This looks like a variable declaration. The type is probably missing.
1840 // We're done parsing decl-specifiers.
1841 return false;
1842
1843 case tok::l_paren: {
1844 // static x(4); // 'x' is not a type
1845 // x(int n); // 'x' is not a type
1846 // x (*p)[]; // 'x' is a type
1847 //
1848 // Since we're in an error case (or the rare 'implicit int in C++' MS
1849 // extension), we can afford to perform a tentative parse to determine
1850 // which case we're in.
1851 TentativeParsingAction PA(*this);
1852 ConsumeToken();
1853 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1854 PA.Revert();
1855 if (TPR == TPResult::False())
1856 return false;
1857 // The identifier is followed by a parenthesized declarator.
1858 // It's supposed to be a type.
1859 break;
1860 }
1861
1862 default:
1863 // This is probably supposed to be a type. This includes cases like:
1864 // int f(itn);
1865 // struct S { unsinged : 4; };
1866 break;
1867 }
1868 }
1869
Chad Rosier8decdee2012-06-26 22:30:43 +00001870 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00001871 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001872 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001873 IdentifierInfo *II = Tok.getIdentifierInfo();
1874 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001875 // The action emitted a diagnostic, so we don't have to.
1876 if (T) {
1877 // The action has suggested that the type T could be used. Set that as
1878 // the type in the declaration specifiers, consume the would-be type
1879 // name token, and we're done.
1880 const char *PrevSpec;
1881 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001882 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001883 DS.SetRangeEnd(Tok.getLocation());
1884 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001885 // There may be other declaration specifiers after this.
1886 return true;
1887 } else if (II != Tok.getIdentifierInfo()) {
1888 // If no type was suggested, the correction is to a keyword
1889 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00001890 // There may be other declaration specifiers after this.
1891 return true;
1892 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001893
Douglas Gregora786fdb2009-10-13 23:27:22 +00001894 // Fall through; the action had no suggestion for us.
1895 } else {
1896 // The action did not emit a diagnostic, so emit one now.
1897 SourceRange R;
1898 if (SS) R = SS->getRange();
1899 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1900 }
Mike Stump1eb44332009-09-09 15:08:12 +00001901
Douglas Gregora786fdb2009-10-13 23:27:22 +00001902 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001903 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001904 DS.SetRangeEnd(Tok.getLocation());
1905 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Chris Lattnere40c2952009-04-14 21:34:55 +00001907 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1908 // avoid rippling error messages on subsequent uses of the same type,
1909 // could be useful if #include was forgotten.
1910 return false;
1911}
1912
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001913/// \brief Determine the declaration specifier context from the declarator
1914/// context.
1915///
1916/// \param Context the declarator context, which is one of the
1917/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00001918Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001919Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1920 if (Context == Declarator::MemberContext)
1921 return DSC_class;
1922 if (Context == Declarator::FileContext)
1923 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001924 if (Context == Declarator::TrailingReturnContext)
1925 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001926 return DSC_normal;
1927}
1928
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001929/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1930///
1931/// FIXME: Simply returns an alignof() expression if the argument is a
1932/// type. Ideally, the type should be propagated directly into Sema.
1933///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001934/// [C11] type-id
1935/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001936/// [C++0x] type-id ...[opt]
1937/// [C++0x] assignment-expression ...[opt]
1938ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1939 SourceLocation &EllipsisLoc) {
1940 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001941 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001942 SourceLocation TypeLoc = Tok.getLocation();
1943 ParsedType Ty = ParseTypeName().get();
1944 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001945 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1946 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001947 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001948 ER = ParseConstantExpression();
1949
David Blaikie4e4d0842012-03-11 07:00:24 +00001950 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001951 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001952
1953 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001954}
1955
1956/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1957/// attribute to Attrs.
1958///
1959/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001960/// [C11] '_Alignas' '(' type-id ')'
1961/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001962/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1963/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001964void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1965 SourceLocation *endLoc) {
1966 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1967 "Not an alignment-specifier!");
1968
1969 SourceLocation KWLoc = Tok.getLocation();
1970 ConsumeToken();
1971
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001972 BalancedDelimiterTracker T(*this, tok::l_paren);
1973 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001974 return;
1975
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001976 SourceLocation EllipsisLoc;
1977 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001978 if (ArgExpr.isInvalid()) {
1979 SkipUntil(tok::r_paren);
1980 return;
1981 }
1982
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001983 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001984 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001985 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001986
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001987 // FIXME: Handle pack-expansions here.
1988 if (EllipsisLoc.isValid()) {
1989 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1990 return;
1991 }
1992
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001993 ExprVector ArgExprs(Actions);
1994 ArgExprs.push_back(ArgExpr.release());
Sean Hunt8e083e72012-06-19 23:57:03 +00001995 // FIXME: This should not be GNU, but we since the attribute used is
1996 // based on the spelling, and there is no true spelling for
1997 // C++11 attributes, this isn't accepted.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001998 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +00001999 0, T.getOpenLocation(), ArgExprs.take(), 1,
Sean Hunt8e083e72012-06-19 23:57:03 +00002000 AttributeList::AS_GNU);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002001}
2002
Reid Spencer5f016e22007-07-11 17:01:13 +00002003/// ParseDeclarationSpecifiers
2004/// declaration-specifiers: [C99 6.7]
2005/// storage-class-specifier declaration-specifiers[opt]
2006/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002007/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002008/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002009/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002010/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002011///
2012/// storage-class-specifier: [C99 6.7.1]
2013/// 'typedef'
2014/// 'extern'
2015/// 'static'
2016/// 'auto'
2017/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002018/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002019/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002020/// function-specifier: [C99 6.7.4]
2021/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002022/// [C++] 'virtual'
2023/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002024/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002025/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002026/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002027
Reid Spencer5f016e22007-07-11 17:01:13 +00002028///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002029void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002030 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002031 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002032 DeclSpecContext DSContext,
2033 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002034 if (DS.getSourceRange().isInvalid()) {
2035 DS.SetRangeStart(Tok.getLocation());
2036 DS.SetRangeEnd(Tok.getLocation());
2037 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002038
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002039 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002040 bool AttrsLastTime = false;
2041 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002042 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002043 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002044 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002045 unsigned DiagID = 0;
2046
Reid Spencer5f016e22007-07-11 17:01:13 +00002047 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002048
Reid Spencer5f016e22007-07-11 17:01:13 +00002049 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002050 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002051 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002052 if (!AttrsLastTime)
2053 ProhibitAttributes(attrs);
2054 else
2055 DS.takeAttributesFrom(attrs);
Peter Collingbournef1907682011-09-29 18:03:57 +00002056
Reid Spencer5f016e22007-07-11 17:01:13 +00002057 // If this is not a declaration specifier token, we're done reading decl
2058 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002059 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002060 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002061
Sean Hunt2edf0a22012-06-23 05:07:58 +00002062 case tok::l_square:
2063 case tok::kw_alignas:
2064 if (!isCXX11AttributeSpecifier())
2065 goto DoneWithDeclSpec;
2066
2067 ProhibitAttributes(attrs);
2068 // FIXME: It would be good to recover by accepting the attributes,
2069 // but attempting to do that now would cause serious
2070 // madness in terms of diagnostics.
2071 attrs.clear();
2072 attrs.Range = SourceRange();
2073
2074 ParseCXX11Attributes(attrs);
2075 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002076 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002077
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002078 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002079 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002080 if (DS.hasTypeSpecifier()) {
2081 bool AllowNonIdentifiers
2082 = (getCurScope()->getFlags() & (Scope::ControlScope |
2083 Scope::BlockScope |
2084 Scope::TemplateParamScope |
2085 Scope::FunctionPrototypeScope |
2086 Scope::AtCatchScope)) == 0;
2087 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002088 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002089 (DSContext == DSC_class && DS.isFriendSpecified());
2090
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002091 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002092 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002093 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002094 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002095 }
2096
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002097 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2098 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2099 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002100 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002101 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002102 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002103 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002104 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002105 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002106
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002107 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002108 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002109 }
2110
Chris Lattner5e02c472009-01-05 00:07:25 +00002111 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002112 // C++ scope specifier. Annotate and loop, or bail out on error.
2113 if (TryAnnotateCXXScopeToken(true)) {
2114 if (!DS.hasTypeSpecifier())
2115 DS.SetTypeSpecError();
2116 goto DoneWithDeclSpec;
2117 }
John McCall2e0a7152010-03-01 18:20:46 +00002118 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2119 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002120 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002121
2122 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002123 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002124 goto DoneWithDeclSpec;
2125
John McCallaa87d332009-12-12 11:40:51 +00002126 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002127 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2128 Tok.getAnnotationRange(),
2129 SS);
John McCallaa87d332009-12-12 11:40:51 +00002130
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002131 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002132 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002133 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002134 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002135 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002136 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002137
2138 // C++ [class.qual]p2:
2139 // In a lookup in which the constructor is an acceptable lookup
2140 // result and the nested-name-specifier nominates a class C:
2141 //
2142 // - if the name specified after the
2143 // nested-name-specifier, when looked up in C, is the
2144 // injected-class-name of C (Clause 9), or
2145 //
2146 // - if the name specified after the nested-name-specifier
2147 // is the same as the identifier or the
2148 // simple-template-id's template-name in the last
2149 // component of the nested-name-specifier,
2150 //
2151 // the name is instead considered to name the constructor of
2152 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002153 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002154 // Thus, if the template-name is actually the constructor
2155 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002156 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002157 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002158 if ((DSContext == DSC_top_level ||
2159 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2160 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002161 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002162 if (isConstructorDeclarator()) {
2163 // The user meant this to be an out-of-line constructor
2164 // definition, but template arguments are not allowed
2165 // there. Just allow this as a constructor; we'll
2166 // complain about it later.
2167 goto DoneWithDeclSpec;
2168 }
2169
2170 // The user meant this to name a type, but it actually names
2171 // a constructor with some extraneous template
2172 // arguments. Complain, then parse it as a type as the user
2173 // intended.
2174 Diag(TemplateId->TemplateNameLoc,
2175 diag::err_out_of_line_template_id_names_constructor)
2176 << TemplateId->Name;
2177 }
2178
John McCallaa87d332009-12-12 11:40:51 +00002179 DS.getTypeSpecScope() = SS;
2180 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002181 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002182 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002183 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002184 continue;
2185 }
2186
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002187 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002188 DS.getTypeSpecScope() = SS;
2189 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002190 if (Tok.getAnnotationValue()) {
2191 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002192 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002193 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002194 PrevSpec, DiagID, T);
2195 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002196 else
2197 DS.SetTypeSpecError();
2198 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2199 ConsumeToken(); // The typename
2200 }
2201
Douglas Gregor9135c722009-03-25 15:40:00 +00002202 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002203 goto DoneWithDeclSpec;
2204
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002205 // If we're in a context where the identifier could be a class name,
2206 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002207 if ((DSContext == DSC_top_level ||
2208 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002209 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002210 &SS)) {
2211 if (isConstructorDeclarator())
2212 goto DoneWithDeclSpec;
2213
2214 // As noted in C++ [class.qual]p2 (cited above), when the name
2215 // of the class is qualified in a context where it could name
2216 // a constructor, its a constructor name. However, we've
2217 // looked at the declarator, and the user probably meant this
2218 // to be a type. Complain that it isn't supposed to be treated
2219 // as a type, then proceed to parse it as a type.
2220 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2221 << Next.getIdentifierInfo();
2222 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002223
John McCallb3d87482010-08-24 05:47:05 +00002224 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2225 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002226 getCurScope(), &SS,
2227 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002228 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002229 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002230
Chris Lattnerf4382f52009-04-14 22:17:06 +00002231 // If the referenced identifier is not a type, then this declspec is
2232 // erroneous: We already checked about that it has no type specifier, and
2233 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002234 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002235 if (TypeRep == 0) {
2236 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002237 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002238 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002239 }
Mike Stump1eb44332009-09-09 15:08:12 +00002240
John McCallaa87d332009-12-12 11:40:51 +00002241 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002242 ConsumeToken(); // The C++ scope.
2243
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002244 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002245 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002246 if (isInvalid)
2247 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002248
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002249 DS.SetRangeEnd(Tok.getLocation());
2250 ConsumeToken(); // The typename.
2251
2252 continue;
2253 }
Mike Stump1eb44332009-09-09 15:08:12 +00002254
Chris Lattner80d0c892009-01-21 19:48:37 +00002255 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002256 if (Tok.getAnnotationValue()) {
2257 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002258 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002259 DiagID, T);
2260 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002261 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002262
Chris Lattner5c5db552010-04-05 18:18:31 +00002263 if (isInvalid)
2264 break;
2265
Chris Lattner80d0c892009-01-21 19:48:37 +00002266 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2267 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002268
Chris Lattner80d0c892009-01-21 19:48:37 +00002269 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2270 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002271 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002272 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002273 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002274
Chris Lattner80d0c892009-01-21 19:48:37 +00002275 continue;
2276 }
Mike Stump1eb44332009-09-09 15:08:12 +00002277
Douglas Gregorbfad9152011-04-28 15:48:45 +00002278 case tok::kw___is_signed:
2279 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2280 // typically treats it as a trait. If we see __is_signed as it appears
2281 // in libstdc++, e.g.,
2282 //
2283 // static const bool __is_signed;
2284 //
2285 // then treat __is_signed as an identifier rather than as a keyword.
2286 if (DS.getTypeSpecType() == TST_bool &&
2287 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2288 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2289 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2290 Tok.setKind(tok::identifier);
2291 }
2292
2293 // We're done with the declaration-specifiers.
2294 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002295
Chris Lattner3bd934a2008-07-26 01:18:38 +00002296 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002297 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002298 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002299 // In C++, check to see if this is a scope specifier like foo::bar::, if
2300 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002301 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002302 if (TryAnnotateCXXScopeToken(true)) {
2303 if (!DS.hasTypeSpecifier())
2304 DS.SetTypeSpecError();
2305 goto DoneWithDeclSpec;
2306 }
2307 if (!Tok.is(tok::identifier))
2308 continue;
2309 }
Mike Stump1eb44332009-09-09 15:08:12 +00002310
Chris Lattner3bd934a2008-07-26 01:18:38 +00002311 // This identifier can only be a typedef name if we haven't already seen
2312 // a type-specifier. Without this check we misparse:
2313 // typedef int X; struct Y { short X; }; as 'short int'.
2314 if (DS.hasTypeSpecifier())
2315 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002316
John Thompson82287d12010-02-05 00:12:22 +00002317 // Check for need to substitute AltiVec keyword tokens.
2318 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2319 break;
2320
Richard Smithf63eee72012-05-09 18:56:43 +00002321 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2322 // allow the use of a typedef name as a type specifier.
2323 if (DS.isTypeAltiVecVector())
2324 goto DoneWithDeclSpec;
2325
John McCallb3d87482010-08-24 05:47:05 +00002326 ParsedType TypeRep =
2327 Actions.getTypeName(*Tok.getIdentifierInfo(),
2328 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002329
Chris Lattnerc199ab32009-04-12 20:42:31 +00002330 // If this is not a typedef name, don't parse it as part of the declspec,
2331 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002332 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002333 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002334 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002335 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002336
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002337 // If we're in a context where the identifier could be a class name,
2338 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002339 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002340 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002341 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002342 goto DoneWithDeclSpec;
2343
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002344 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002345 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002346 if (isInvalid)
2347 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002348
Chris Lattner3bd934a2008-07-26 01:18:38 +00002349 DS.SetRangeEnd(Tok.getLocation());
2350 ConsumeToken(); // The identifier
2351
2352 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2353 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002354 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002355 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002356 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002357
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002358 // Need to support trailing type qualifiers (e.g. "id<p> const").
2359 // If a type specifier follows, it will be diagnosed elsewhere.
2360 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002361 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002362
2363 // type-name
2364 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002365 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002366 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002367 // This template-id does not refer to a type name, so we're
2368 // done with the type-specifiers.
2369 goto DoneWithDeclSpec;
2370 }
2371
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002372 // If we're in a context where the template-id could be a
2373 // constructor name or specialization, check whether this is a
2374 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002375 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002376 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002377 isConstructorDeclarator())
2378 goto DoneWithDeclSpec;
2379
Douglas Gregor39a8de12009-02-25 19:37:18 +00002380 // Turn the template-id annotation token into a type annotation
2381 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002382 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002383 continue;
2384 }
2385
Reid Spencer5f016e22007-07-11 17:01:13 +00002386 // GNU attributes support.
2387 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002388 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002389 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002390
2391 // Microsoft declspec support.
2392 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002393 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002394 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002395
Steve Naroff239f0732008-12-25 14:16:32 +00002396 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002397 case tok::kw___forceinline: {
2398 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2399 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
2400 SourceLocation AttrNameLoc = ConsumeToken();
Sean Hunt93f95f22012-06-18 16:13:52 +00002401 // FIXME: This does not work correctly if it is set to be a declspec
2402 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002403 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002404 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002405 continue;
2406 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002407
2408 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002409 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002410 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002411 case tok::kw___cdecl:
2412 case tok::kw___stdcall:
2413 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002414 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002415 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002416 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002417 continue;
2418
Dawn Perchik52fc3142010-09-03 01:29:35 +00002419 // Borland single token adornments.
2420 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002421 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002422 continue;
2423
Peter Collingbournef315fa82011-02-14 01:42:53 +00002424 // OpenCL single token adornments.
2425 case tok::kw___kernel:
2426 ParseOpenCLAttributes(DS.getAttributes());
2427 continue;
2428
Reid Spencer5f016e22007-07-11 17:01:13 +00002429 // storage-class-specifier
2430 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002431 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2432 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002433 break;
2434 case tok::kw_extern:
2435 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002436 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002437 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2438 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002439 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002440 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002441 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2442 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002443 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002444 case tok::kw_static:
2445 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002446 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002447 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2448 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002449 break;
2450 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002451 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002452 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002453 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2454 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002455 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002456 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002457 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002458 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002459 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2460 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002461 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002462 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2463 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002464 break;
2465 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002466 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2467 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002468 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002469 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002470 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2471 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002472 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002473 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002474 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002475 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002476
Reid Spencer5f016e22007-07-11 17:01:13 +00002477 // function-specifier
2478 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002479 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002480 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002481 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002482 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002483 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002484 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002485 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002486 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002487
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002488 // alignment-specifier
2489 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002490 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002491 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002492 ParseAlignmentSpecifier(DS.getAttributes());
2493 continue;
2494
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002495 // friend
2496 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002497 if (DSContext == DSC_class)
2498 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2499 else {
2500 PrevSpec = ""; // not actually used by the diagnostic
2501 DiagID = diag::err_friend_invalid_in_context;
2502 isInvalid = true;
2503 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002504 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002505
Douglas Gregor8d267c52011-09-09 02:06:17 +00002506 // Modules
2507 case tok::kw___module_private__:
2508 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2509 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002510
Sebastian Redl2ac67232009-11-05 15:47:02 +00002511 // constexpr
2512 case tok::kw_constexpr:
2513 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2514 break;
2515
Chris Lattner80d0c892009-01-21 19:48:37 +00002516 // type-specifier
2517 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002518 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2519 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002520 break;
2521 case tok::kw_long:
2522 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002523 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2524 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002525 else
John McCallfec54012009-08-03 20:12:06 +00002526 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2527 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002528 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002529 case tok::kw___int64:
2530 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2531 DiagID);
2532 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002533 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002534 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2535 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002536 break;
2537 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002538 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2539 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002540 break;
2541 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002542 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2543 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002544 break;
2545 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002546 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2547 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002548 break;
2549 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002550 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2551 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002552 break;
2553 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002554 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2555 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002556 break;
2557 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002558 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2559 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002560 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002561 case tok::kw___int128:
2562 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2563 DiagID);
2564 break;
2565 case tok::kw_half:
2566 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2567 DiagID);
2568 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002569 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002570 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2571 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002572 break;
2573 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002574 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2575 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002576 break;
2577 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002578 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2579 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002580 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002581 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002582 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2583 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002584 break;
2585 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002586 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2587 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002588 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002589 case tok::kw_bool:
2590 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002591 if (Tok.is(tok::kw_bool) &&
2592 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2593 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2594 PrevSpec = ""; // Not used by the diagnostic.
2595 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002596 // For better error recovery.
2597 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002598 isInvalid = true;
2599 } else {
2600 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2601 DiagID);
2602 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002603 break;
2604 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002605 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2606 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002607 break;
2608 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002609 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2610 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002611 break;
2612 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002613 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2614 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002615 break;
John Thompson82287d12010-02-05 00:12:22 +00002616 case tok::kw___vector:
2617 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2618 break;
2619 case tok::kw___pixel:
2620 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2621 break;
John McCalla5fc4722011-04-09 22:50:59 +00002622 case tok::kw___unknown_anytype:
2623 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2624 PrevSpec, DiagID);
2625 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002626
2627 // class-specifier:
2628 case tok::kw_class:
2629 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002630 case tok::kw_union: {
2631 tok::TokenKind Kind = Tok.getKind();
2632 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002633 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2634 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002635 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002636 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002637
2638 // enum-specifier:
2639 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002640 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002641 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002642 continue;
2643
2644 // cv-qualifier:
2645 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002646 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002647 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002648 break;
2649 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002650 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002651 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002652 break;
2653 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002654 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002655 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002656 break;
2657
Douglas Gregord57959a2009-03-27 23:10:48 +00002658 // C++ typename-specifier:
2659 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002660 if (TryAnnotateTypeOrScopeToken()) {
2661 DS.SetTypeSpecError();
2662 goto DoneWithDeclSpec;
2663 }
2664 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002665 continue;
2666 break;
2667
Chris Lattner80d0c892009-01-21 19:48:37 +00002668 // GNU typeof support.
2669 case tok::kw_typeof:
2670 ParseTypeofSpecifier(DS);
2671 continue;
2672
David Blaikie42d6d0c2011-12-04 05:04:18 +00002673 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002674 ParseDecltypeSpecifier(DS);
2675 continue;
2676
Sean Huntdb5d44b2011-05-19 05:37:45 +00002677 case tok::kw___underlying_type:
2678 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002679 continue;
2680
2681 case tok::kw__Atomic:
2682 ParseAtomicSpecifier(DS);
2683 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002684
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002685 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00002686 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002687 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002688 goto DoneWithDeclSpec;
2689 case tok::kw___private:
2690 case tok::kw___global:
2691 case tok::kw___local:
2692 case tok::kw___constant:
2693 case tok::kw___read_only:
2694 case tok::kw___write_only:
2695 case tok::kw___read_write:
2696 ParseOpenCLQualifiers(DS);
2697 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002698
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002699 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002700 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002701 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2702 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002703 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002704 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002705
Douglas Gregor46f936e2010-11-19 17:10:50 +00002706 if (!ParseObjCProtocolQualifiers(DS))
2707 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2708 << FixItHint::CreateInsertion(Loc, "id")
2709 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00002710
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002711 // Need to support trailing type qualifiers (e.g. "id<p> const").
2712 // If a type specifier follows, it will be diagnosed elsewhere.
2713 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002714 }
John McCallfec54012009-08-03 20:12:06 +00002715 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002716 if (isInvalid) {
2717 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002718 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00002719
Douglas Gregorae2fb142010-08-23 14:34:43 +00002720 if (DiagID == diag::ext_duplicate_declspec)
2721 Diag(Tok, DiagID)
2722 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2723 else
2724 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002725 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002726
Chris Lattner81c018d2008-03-13 06:29:04 +00002727 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002728 if (DiagID != diag::err_bool_redeclaration)
2729 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002730
2731 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002732 }
2733}
Douglas Gregoradcac882008-12-01 23:54:00 +00002734
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002735/// ParseStructDeclaration - Parse a struct declaration without the terminating
2736/// semicolon.
2737///
Reid Spencer5f016e22007-07-11 17:01:13 +00002738/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002739/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002740/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002741/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002742/// struct-declarator-list:
2743/// struct-declarator
2744/// struct-declarator-list ',' struct-declarator
2745/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2746/// struct-declarator:
2747/// declarator
2748/// [GNU] declarator attributes[opt]
2749/// declarator[opt] ':' constant-expression
2750/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2751///
Chris Lattnere1359422008-04-10 06:46:29 +00002752void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002753ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00002754
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002755 if (Tok.is(tok::kw___extension__)) {
2756 // __extension__ silences extension warnings in the subexpression.
2757 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002758 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002759 return ParseStructDeclaration(DS, Fields);
2760 }
Mike Stump1eb44332009-09-09 15:08:12 +00002761
Steve Naroff28a7ca82007-08-20 22:28:22 +00002762 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002763 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002764
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002765 // If there are no declarators, this is a free-standing declaration
2766 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002767 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002768 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002769 return;
2770 }
2771
2772 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002773 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002774 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002775 while (1) {
John McCall92576642012-05-07 06:16:41 +00002776 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCallbdd563e2009-11-03 02:38:08 +00002777 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002778 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002779
2780 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002781 if (!FirstDeclarator)
2782 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002783
Steve Naroff28a7ca82007-08-20 22:28:22 +00002784 /// struct-declarator: declarator
2785 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002786 if (Tok.isNot(tok::colon)) {
2787 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2788 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002789 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002790 }
Mike Stump1eb44332009-09-09 15:08:12 +00002791
Chris Lattner04d66662007-10-09 17:33:22 +00002792 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002793 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002794 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002795 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002796 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002797 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002798 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002799 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002800
Steve Naroff28a7ca82007-08-20 22:28:22 +00002801 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002802 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002803
John McCallbdd563e2009-11-03 02:38:08 +00002804 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002805 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002806 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002807
Steve Naroff28a7ca82007-08-20 22:28:22 +00002808 // If we don't have a comma, it is either the end of the list (a ';')
2809 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002810 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002811 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002812
Steve Naroff28a7ca82007-08-20 22:28:22 +00002813 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002814 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002815
John McCallbdd563e2009-11-03 02:38:08 +00002816 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002817 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002818}
2819
2820/// ParseStructUnionBody
2821/// struct-contents:
2822/// struct-declaration-list
2823/// [EXT] empty
2824/// [GNU] "struct-declaration-list" without terminatoring ';'
2825/// struct-declaration-list:
2826/// struct-declaration
2827/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002828/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002829///
Reid Spencer5f016e22007-07-11 17:01:13 +00002830void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002831 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002832 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2833 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002834
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002835 BalancedDelimiterTracker T(*this, tok::l_brace);
2836 if (T.consumeOpen())
2837 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002838
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002839 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002840 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002841
Reid Spencer5f016e22007-07-11 17:01:13 +00002842 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2843 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002844 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002845 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2846 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2847 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002848
Chris Lattner5f9e2722011-07-23 10:55:15 +00002849 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002850
Reid Spencer5f016e22007-07-11 17:01:13 +00002851 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002852 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002853 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002854
Reid Spencer5f016e22007-07-11 17:01:13 +00002855 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002856 if (Tok.is(tok::semi)) {
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002857 ConsumeExtraSemi(InsideStruct,
2858 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Reid Spencer5f016e22007-07-11 17:01:13 +00002859 continue;
2860 }
Chris Lattnere1359422008-04-10 06:46:29 +00002861
2862 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002863 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002864
John McCallbdd563e2009-11-03 02:38:08 +00002865 if (!Tok.is(tok::at)) {
2866 struct CFieldCallback : FieldCallback {
2867 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002868 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002869 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002870
John McCalld226f652010-08-21 09:40:31 +00002871 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002872 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002873 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2874
John McCalld226f652010-08-21 09:40:31 +00002875 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002876 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002877 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002878 FD.D.getDeclSpec().getSourceRange().getBegin(),
2879 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002880 FieldDecls.push_back(Field);
2881 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002882 }
John McCallbdd563e2009-11-03 02:38:08 +00002883 } Callback(*this, TagDecl, FieldDecls);
2884
2885 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002886 } else { // Handle @defs
2887 ConsumeToken();
2888 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2889 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002890 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002891 continue;
2892 }
2893 ConsumeToken();
2894 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2895 if (!Tok.is(tok::identifier)) {
2896 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002897 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002898 continue;
2899 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002900 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002901 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002902 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002903 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2904 ConsumeToken();
2905 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002906 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002907
Chris Lattner04d66662007-10-09 17:33:22 +00002908 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002909 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002910 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002911 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002912 break;
2913 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002914 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2915 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002916 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002917 // If we stopped at a ';', eat it.
2918 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002919 }
2920 }
Mike Stump1eb44332009-09-09 15:08:12 +00002921
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002922 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002923
John McCall0b7e6782011-03-24 11:26:52 +00002924 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002925 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002926 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002927
Douglas Gregor23c94db2010-07-02 17:43:08 +00002928 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002929 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002930 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002931 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002932 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002933 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2934 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002935}
2936
Reid Spencer5f016e22007-07-11 17:01:13 +00002937/// ParseEnumSpecifier
2938/// enum-specifier: [C99 6.7.2.2]
2939/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002940///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002941/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2942/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002943/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2944/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002945/// 'enum' identifier
2946/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002947///
Richard Smith1af83c42012-03-23 03:33:32 +00002948/// [C++11] enum-head '{' enumerator-list[opt] '}'
2949/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002950///
Richard Smith1af83c42012-03-23 03:33:32 +00002951/// enum-head: [C++11]
2952/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2953/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2954/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002955///
Richard Smith1af83c42012-03-23 03:33:32 +00002956/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002957/// 'enum'
2958/// 'enum' 'class'
2959/// 'enum' 'struct'
2960///
Richard Smith1af83c42012-03-23 03:33:32 +00002961/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002962/// ':' type-specifier-seq
2963///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002964/// [C++] elaborated-type-specifier:
2965/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2966///
Chris Lattner4c97d762009-04-12 21:49:30 +00002967void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002968 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002969 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002970 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002971 if (Tok.is(tok::code_completion)) {
2972 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002973 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002974 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002975 }
John McCall57c13002011-07-06 05:58:41 +00002976
Sean Hunt2edf0a22012-06-23 05:07:58 +00002977 // If attributes exist after tag, parse them.
2978 ParsedAttributesWithRange attrs(AttrFactory);
2979 MaybeParseGNUAttributes(attrs);
2980 MaybeParseCXX0XAttributes(attrs);
2981
2982 // If declspecs exist after tag, parse them.
2983 while (Tok.is(tok::kw___declspec))
2984 ParseMicrosoftDeclSpec(attrs);
2985
Richard Smithbdad7a22012-01-10 01:33:14 +00002986 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002987 bool IsScopedUsingClassTag = false;
2988
John McCall1e12b3d2012-06-23 22:30:04 +00002989 // In C++11, recognize 'enum class' and 'enum struct'.
David Blaikie4e4d0842012-03-11 07:00:24 +00002990 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002991 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002992 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002993 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002994 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00002995
John McCall1e12b3d2012-06-23 22:30:04 +00002996 // Attributes are not allowed between these keywords. Diagnose,
2997 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00002998 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00002999
3000 // They are allowed afterwards, though.
3001 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003002 MaybeParseCXX0XAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003003 while (Tok.is(tok::kw___declspec))
3004 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003005 }
Richard Smith1af83c42012-03-23 03:33:32 +00003006
John McCall13489672012-05-07 06:16:58 +00003007 // C++11 [temp.explicit]p12:
3008 // The usual access controls do not apply to names used to specify
3009 // explicit instantiations.
3010 // We extend this to also cover explicit specializations. Note that
3011 // we don't suppress if this turns out to be an elaborated type
3012 // specifier.
3013 bool shouldDelayDiagsInTag =
3014 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3015 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3016 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003017
Richard Smith7796eb52012-03-12 08:56:40 +00003018 // Enum definitions should not be parsed in a trailing-return-type.
3019 bool AllowDeclaration = DSC != DSC_trailing;
3020
3021 bool AllowFixedUnderlyingType = AllowDeclaration &&
3022 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
3023 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003024
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003025 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003026 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003027 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3028 // if a fixed underlying type is allowed.
3029 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003030
3031 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003032 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00003033 return;
3034
3035 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003036 Diag(Tok, diag::err_expected_ident);
3037 if (Tok.isNot(tok::l_brace)) {
3038 // Has no name and is not a definition.
3039 // Skip the rest of this declarator, up until the comma or semicolon.
3040 SkipUntil(tok::comma, true);
3041 return;
3042 }
3043 }
3044 }
Mike Stump1eb44332009-09-09 15:08:12 +00003045
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003046 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003047 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003048 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003049 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003050
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003051 // Skip the rest of this declarator, up until the comma or semicolon.
3052 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003053 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003054 }
Mike Stump1eb44332009-09-09 15:08:12 +00003055
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003056 // If an identifier is present, consume and remember it.
3057 IdentifierInfo *Name = 0;
3058 SourceLocation NameLoc;
3059 if (Tok.is(tok::identifier)) {
3060 Name = Tok.getIdentifierInfo();
3061 NameLoc = ConsumeToken();
3062 }
Mike Stump1eb44332009-09-09 15:08:12 +00003063
Richard Smithbdad7a22012-01-10 01:33:14 +00003064 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003065 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3066 // declaration of a scoped enumeration.
3067 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003068 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003069 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003070 }
3071
John McCall13489672012-05-07 06:16:58 +00003072 // Okay, end the suppression area. We'll decide whether to emit the
3073 // diagnostics in a second.
3074 if (shouldDelayDiagsInTag)
3075 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003076
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003077 TypeResult BaseType;
3078
Douglas Gregora61b3e72010-12-01 17:42:47 +00003079 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003080 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003081 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003082 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003083 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003084 // If we're in class scope, this can either be an enum declaration with
3085 // an underlying type, or a declaration of a bitfield member. We try to
3086 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003087 // (integer literal, sizeof); if it's still ambiguous, we then consider
3088 // anything that's a simple-type-specifier followed by '(' as an
3089 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003090 // underlying types anyway.
3091 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003092 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003093 // bit-field. This is the common case.
3094 if (TPR == TPResult::True())
3095 PossibleBitfield = true;
3096 // If the next token starts a type-specifier-seq, it may be either a
3097 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003098 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003099 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003100 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003101 GetLookAheadToken(2).getKind() == tok::semi) {
3102 // Consume the ':'.
3103 ConsumeToken();
3104 } else {
3105 // We have the start of a type-specifier-seq, so we have to perform
3106 // tentative parsing to determine whether we have an expression or a
3107 // type.
3108 TentativeParsingAction TPA(*this);
3109
3110 // Consume the ':'.
3111 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003112
3113 // If we see a type specifier followed by an open-brace, we have an
3114 // ambiguity between an underlying type and a C++11 braced
3115 // function-style cast. Resolve this by always treating it as an
3116 // underlying type.
3117 // FIXME: The standard is not entirely clear on how to disambiguate in
3118 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003119 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003120 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003121 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003122 // We'll parse this as a bitfield later.
3123 PossibleBitfield = true;
3124 TPA.Revert();
3125 } else {
3126 // We have a type-specifier-seq.
3127 TPA.Commit();
3128 }
3129 }
3130 } else {
3131 // Consume the ':'.
3132 ConsumeToken();
3133 }
3134
3135 if (!PossibleBitfield) {
3136 SourceRange Range;
3137 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003138
David Blaikie4e4d0842012-03-11 07:00:24 +00003139 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00003140 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
3141 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00003142 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003143 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003144 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003145 }
3146
Richard Smithbdad7a22012-01-10 01:33:14 +00003147 // There are four options here. If we have 'friend enum foo;' then this is a
3148 // friend declaration, and cannot have an accompanying definition. If we have
3149 // 'enum foo;', then this is a forward declaration. If we have
3150 // 'enum foo {...' then this is a definition. Otherwise we have something
3151 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003152 //
3153 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3154 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3155 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3156 //
John McCallf312b1e2010-08-26 23:41:50 +00003157 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003158 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003159 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003160 } else if (Tok.is(tok::l_brace)) {
3161 if (DS.isFriendSpecified()) {
3162 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3163 << SourceRange(DS.getFriendSpecLoc());
3164 ConsumeBrace();
3165 SkipUntil(tok::r_brace);
3166 TUK = Sema::TUK_Friend;
3167 } else {
3168 TUK = Sema::TUK_Definition;
3169 }
Richard Smithc9f35172012-06-25 21:37:02 +00003170 } else if (DSC != DSC_type_specifier &&
3171 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003172 (Tok.isAtStartOfLine() &&
3173 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003174 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3175 if (Tok.isNot(tok::semi)) {
3176 // A semicolon was missing after this declaration. Diagnose and recover.
3177 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3178 "enum");
3179 PP.EnterToken(Tok);
3180 Tok.setKind(tok::semi);
3181 }
John McCall13489672012-05-07 06:16:58 +00003182 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003183 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003184 }
3185
3186 // If this is an elaborated type specifier, and we delayed
3187 // diagnostics before, just merge them into the current pool.
3188 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3189 diagsFromTag.redelay();
3190 }
Richard Smith1af83c42012-03-23 03:33:32 +00003191
3192 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003193 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003194 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003195 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3196 // Skip the rest of this declarator, up until the comma or semicolon.
3197 Diag(Tok, diag::err_enum_template);
3198 SkipUntil(tok::comma, true);
3199 return;
3200 }
3201
3202 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3203 // Enumerations can't be explicitly instantiated.
3204 DS.SetTypeSpecError();
3205 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3206 return;
3207 }
3208
3209 assert(TemplateInfo.TemplateParams && "no template parameters");
3210 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3211 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003212 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003213
Sean Hunt2edf0a22012-06-23 05:07:58 +00003214 if (TUK == Sema::TUK_Reference)
3215 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003216
Douglas Gregorb9075602011-02-22 02:55:24 +00003217 if (!Name && TUK != Sema::TUK_Definition) {
3218 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003219
Douglas Gregorb9075602011-02-22 02:55:24 +00003220 // Skip the rest of this declarator, up until the comma or semicolon.
3221 SkipUntil(tok::comma, true);
3222 return;
3223 }
Richard Smith1af83c42012-03-23 03:33:32 +00003224
Douglas Gregor402abb52009-05-28 23:31:59 +00003225 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003226 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003227 const char *PrevSpec = 0;
3228 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003229 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003230 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003231 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003232 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003233 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003234
Douglas Gregor48c89f42010-04-24 16:38:41 +00003235 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003236 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003237 // dependent tag.
3238 if (!Name) {
3239 DS.SetTypeSpecError();
3240 Diag(Tok, diag::err_expected_type_name_after_typename);
3241 return;
3242 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003243
Douglas Gregor23c94db2010-07-02 17:43:08 +00003244 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003245 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003246 NameLoc);
3247 if (Type.isInvalid()) {
3248 DS.SetTypeSpecError();
3249 return;
3250 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003251
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003252 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3253 NameLoc.isValid() ? NameLoc : StartLoc,
3254 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003255 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003256
Douglas Gregor48c89f42010-04-24 16:38:41 +00003257 return;
3258 }
Mike Stump1eb44332009-09-09 15:08:12 +00003259
John McCalld226f652010-08-21 09:40:31 +00003260 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003261 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003262 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003263 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003264 ConsumeBrace();
3265 SkipUntil(tok::r_brace);
3266 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003267
Douglas Gregor48c89f42010-04-24 16:38:41 +00003268 DS.SetTypeSpecError();
3269 return;
3270 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003271
Richard Smithc9f35172012-06-25 21:37:02 +00003272 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003273 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003274
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003275 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3276 NameLoc.isValid() ? NameLoc : StartLoc,
3277 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003278 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003279}
3280
3281/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3282/// enumerator-list:
3283/// enumerator
3284/// enumerator-list ',' enumerator
3285/// enumerator:
3286/// enumeration-constant
3287/// enumeration-constant '=' constant-expression
3288/// enumeration-constant:
3289/// identifier
3290///
John McCalld226f652010-08-21 09:40:31 +00003291void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003292 // Enter the scope of the enum body and start the definition.
3293 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003294 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003295
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003296 BalancedDelimiterTracker T(*this, tok::l_brace);
3297 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003298
Chris Lattner7946dd32007-08-27 17:24:30 +00003299 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003300 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003301 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003302
Chris Lattner5f9e2722011-07-23 10:55:15 +00003303 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003304
John McCalld226f652010-08-21 09:40:31 +00003305 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003306
Reid Spencer5f016e22007-07-11 17:01:13 +00003307 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003308 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003309 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3310 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003311
John McCall5b629aa2010-10-22 23:36:17 +00003312 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003313 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003314 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003315 MaybeParseCXX0XAttributes(attrs);
3316 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003317
Reid Spencer5f016e22007-07-11 17:01:13 +00003318 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003319 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003320 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003321
Chris Lattner04d66662007-10-09 17:33:22 +00003322 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003323 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003324 AssignedVal = ParseConstantExpression();
3325 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003326 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003327 }
Mike Stump1eb44332009-09-09 15:08:12 +00003328
Reid Spencer5f016e22007-07-11 17:01:13 +00003329 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003330 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3331 LastEnumConstDecl,
3332 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003333 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003334 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003335 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003336
Reid Spencer5f016e22007-07-11 17:01:13 +00003337 EnumConstantDecls.push_back(EnumConstDecl);
3338 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003339
Douglas Gregor751f6922010-09-07 14:51:08 +00003340 if (Tok.is(tok::identifier)) {
3341 // We're missing a comma between enumerators.
3342 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003343 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003344 << FixItHint::CreateInsertion(Loc, ", ");
3345 continue;
3346 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003347
Chris Lattner04d66662007-10-09 17:33:22 +00003348 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003349 break;
3350 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003351
Richard Smith7fe62082011-10-15 05:09:34 +00003352 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003353 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003354 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003355 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003356 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003357 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003358 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3359 << FixItHint::CreateRemoval(CommaLoc);
3360 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003361 }
Mike Stump1eb44332009-09-09 15:08:12 +00003362
Reid Spencer5f016e22007-07-11 17:01:13 +00003363 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003364 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003365
Reid Spencer5f016e22007-07-11 17:01:13 +00003366 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003367 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003368 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003369
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003370 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3371 EnumDecl, EnumConstantDecls.data(),
3372 EnumConstantDecls.size(), getCurScope(),
3373 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003374
Douglas Gregor72de6672009-01-08 20:45:30 +00003375 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003376 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3377 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003378
3379 // The next token must be valid after an enum definition. If not, a ';'
3380 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003381 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3382 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003383 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3384 // Push this token back into the preprocessor and change our current token
3385 // to ';' so that the rest of the code recovers as though there were an
3386 // ';' after the definition.
3387 PP.EnterToken(Tok);
3388 Tok.setKind(tok::semi);
3389 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003390}
3391
3392/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003393/// start of a type-qualifier-list.
3394bool Parser::isTypeQualifier() const {
3395 switch (Tok.getKind()) {
3396 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003397
3398 // type-qualifier only in OpenCL
3399 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003400 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003401
Steve Naroff5f8aa692008-02-11 23:15:56 +00003402 // type-qualifier
3403 case tok::kw_const:
3404 case tok::kw_volatile:
3405 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003406 case tok::kw___private:
3407 case tok::kw___local:
3408 case tok::kw___global:
3409 case tok::kw___constant:
3410 case tok::kw___read_only:
3411 case tok::kw___read_write:
3412 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003413 return true;
3414 }
3415}
3416
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003417/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3418/// is definitely a type-specifier. Return false if it isn't part of a type
3419/// specifier or if we're not sure.
3420bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3421 switch (Tok.getKind()) {
3422 default: return false;
3423 // type-specifiers
3424 case tok::kw_short:
3425 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003426 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003427 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003428 case tok::kw_signed:
3429 case tok::kw_unsigned:
3430 case tok::kw__Complex:
3431 case tok::kw__Imaginary:
3432 case tok::kw_void:
3433 case tok::kw_char:
3434 case tok::kw_wchar_t:
3435 case tok::kw_char16_t:
3436 case tok::kw_char32_t:
3437 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003438 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003439 case tok::kw_float:
3440 case tok::kw_double:
3441 case tok::kw_bool:
3442 case tok::kw__Bool:
3443 case tok::kw__Decimal32:
3444 case tok::kw__Decimal64:
3445 case tok::kw__Decimal128:
3446 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003447
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003448 // struct-or-union-specifier (C99) or class-specifier (C++)
3449 case tok::kw_class:
3450 case tok::kw_struct:
3451 case tok::kw_union:
3452 // enum-specifier
3453 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003454
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003455 // typedef-name
3456 case tok::annot_typename:
3457 return true;
3458 }
3459}
3460
Steve Naroff5f8aa692008-02-11 23:15:56 +00003461/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003462/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003463bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003464 switch (Tok.getKind()) {
3465 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003466
Chris Lattner166a8fc2009-01-04 23:41:41 +00003467 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003468 if (TryAltiVecVectorToken())
3469 return true;
3470 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003471 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003472 // Annotate typenames and C++ scope specifiers. If we get one, just
3473 // recurse to handle whatever we get.
3474 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003475 return true;
3476 if (Tok.is(tok::identifier))
3477 return false;
3478 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003479
Chris Lattner166a8fc2009-01-04 23:41:41 +00003480 case tok::coloncolon: // ::foo::bar
3481 if (NextToken().is(tok::kw_new) || // ::new
3482 NextToken().is(tok::kw_delete)) // ::delete
3483 return false;
3484
Chris Lattner166a8fc2009-01-04 23:41:41 +00003485 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003486 return true;
3487 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003488
Reid Spencer5f016e22007-07-11 17:01:13 +00003489 // GNU attributes support.
3490 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003491 // GNU typeof support.
3492 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003493
Reid Spencer5f016e22007-07-11 17:01:13 +00003494 // type-specifiers
3495 case tok::kw_short:
3496 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003497 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003498 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003499 case tok::kw_signed:
3500 case tok::kw_unsigned:
3501 case tok::kw__Complex:
3502 case tok::kw__Imaginary:
3503 case tok::kw_void:
3504 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003505 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003506 case tok::kw_char16_t:
3507 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003508 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003509 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003510 case tok::kw_float:
3511 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003512 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003513 case tok::kw__Bool:
3514 case tok::kw__Decimal32:
3515 case tok::kw__Decimal64:
3516 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003517 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003518
Chris Lattner99dc9142008-04-13 18:59:07 +00003519 // struct-or-union-specifier (C99) or class-specifier (C++)
3520 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003521 case tok::kw_struct:
3522 case tok::kw_union:
3523 // enum-specifier
3524 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003525
Reid Spencer5f016e22007-07-11 17:01:13 +00003526 // type-qualifier
3527 case tok::kw_const:
3528 case tok::kw_volatile:
3529 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003530
3531 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003532 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003533 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003534
Chris Lattner7c186be2008-10-20 00:25:30 +00003535 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3536 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003537 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003538
Steve Naroff239f0732008-12-25 14:16:32 +00003539 case tok::kw___cdecl:
3540 case tok::kw___stdcall:
3541 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003542 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003543 case tok::kw___w64:
3544 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003545 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003546 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003547 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003548
3549 case tok::kw___private:
3550 case tok::kw___local:
3551 case tok::kw___global:
3552 case tok::kw___constant:
3553 case tok::kw___read_only:
3554 case tok::kw___read_write:
3555 case tok::kw___write_only:
3556
Eli Friedman290eeb02009-06-08 23:27:34 +00003557 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003558
3559 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003560 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003561
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003562 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003563 case tok::kw__Atomic:
3564 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003565 }
3566}
3567
3568/// isDeclarationSpecifier() - Return true if the current token is part of a
3569/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003570///
3571/// \param DisambiguatingWithExpression True to indicate that the purpose of
3572/// this check is to disambiguate between an expression and a declaration.
3573bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003574 switch (Tok.getKind()) {
3575 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003576
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003577 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003578 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003579
Chris Lattner166a8fc2009-01-04 23:41:41 +00003580 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003581 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003582 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003583 return false;
John Thompson82287d12010-02-05 00:12:22 +00003584 if (TryAltiVecVectorToken())
3585 return true;
3586 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003587 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003588 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003589 // Annotate typenames and C++ scope specifiers. If we get one, just
3590 // recurse to handle whatever we get.
3591 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003592 return true;
3593 if (Tok.is(tok::identifier))
3594 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003595
Douglas Gregor9497a732010-09-16 01:51:54 +00003596 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00003597 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00003598 // expression is permitted, then this is probably a class message send
3599 // missing the initial '['. In this case, we won't consider this to be
3600 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00003601 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00003602 isStartOfObjCClassMessageMissingOpenBracket())
3603 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003604
John McCall9ba61662010-02-26 08:45:28 +00003605 return isDeclarationSpecifier();
3606
Chris Lattner166a8fc2009-01-04 23:41:41 +00003607 case tok::coloncolon: // ::foo::bar
3608 if (NextToken().is(tok::kw_new) || // ::new
3609 NextToken().is(tok::kw_delete)) // ::delete
3610 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003611
Chris Lattner166a8fc2009-01-04 23:41:41 +00003612 // Annotate typenames and C++ scope specifiers. If we get one, just
3613 // recurse to handle whatever we get.
3614 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003615 return true;
3616 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003617
Reid Spencer5f016e22007-07-11 17:01:13 +00003618 // storage-class-specifier
3619 case tok::kw_typedef:
3620 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003621 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003622 case tok::kw_static:
3623 case tok::kw_auto:
3624 case tok::kw_register:
3625 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003626
Douglas Gregor8d267c52011-09-09 02:06:17 +00003627 // Modules
3628 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00003629
Reid Spencer5f016e22007-07-11 17:01:13 +00003630 // type-specifiers
3631 case tok::kw_short:
3632 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003633 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003634 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003635 case tok::kw_signed:
3636 case tok::kw_unsigned:
3637 case tok::kw__Complex:
3638 case tok::kw__Imaginary:
3639 case tok::kw_void:
3640 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003641 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003642 case tok::kw_char16_t:
3643 case tok::kw_char32_t:
3644
Reid Spencer5f016e22007-07-11 17:01:13 +00003645 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003646 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003647 case tok::kw_float:
3648 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003649 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003650 case tok::kw__Bool:
3651 case tok::kw__Decimal32:
3652 case tok::kw__Decimal64:
3653 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003654 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003655
Chris Lattner99dc9142008-04-13 18:59:07 +00003656 // struct-or-union-specifier (C99) or class-specifier (C++)
3657 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003658 case tok::kw_struct:
3659 case tok::kw_union:
3660 // enum-specifier
3661 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003662
Reid Spencer5f016e22007-07-11 17:01:13 +00003663 // type-qualifier
3664 case tok::kw_const:
3665 case tok::kw_volatile:
3666 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003667
Reid Spencer5f016e22007-07-11 17:01:13 +00003668 // function-specifier
3669 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003670 case tok::kw_virtual:
3671 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003672
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003673 // static_assert-declaration
3674 case tok::kw__Static_assert:
3675
Chris Lattner1ef08762007-08-09 17:01:07 +00003676 // GNU typeof support.
3677 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003678
Chris Lattner1ef08762007-08-09 17:01:07 +00003679 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003680 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003681 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003682
Francois Pichete3d49b42011-06-19 08:02:06 +00003683 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003684 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003685 return true;
3686
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003687 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003688 case tok::kw__Atomic:
3689 return true;
3690
Chris Lattnerf3948c42008-07-26 03:38:44 +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
Douglas Gregord9d75e52011-04-27 05:41:15 +00003695 // typedef-name
3696 case tok::annot_typename:
3697 return !DisambiguatingWithExpression ||
3698 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00003699
Steve Naroff47f52092009-01-06 19:34:12 +00003700 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003701 case tok::kw___cdecl:
3702 case tok::kw___stdcall:
3703 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003704 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003705 case tok::kw___w64:
3706 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003707 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003708 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003709 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003710 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003711
3712 case tok::kw___private:
3713 case tok::kw___local:
3714 case tok::kw___global:
3715 case tok::kw___constant:
3716 case tok::kw___read_only:
3717 case tok::kw___read_write:
3718 case tok::kw___write_only:
3719
Eli Friedman290eeb02009-06-08 23:27:34 +00003720 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003721 }
3722}
3723
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003724bool Parser::isConstructorDeclarator() {
3725 TentativeParsingAction TPA(*this);
3726
3727 // Parse the C++ scope specifier.
3728 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00003729 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003730 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003731 TPA.Revert();
3732 return false;
3733 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003734
3735 // Parse the constructor name.
3736 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3737 // We already know that we have a constructor name; just consume
3738 // the token.
3739 ConsumeToken();
3740 } else {
3741 TPA.Revert();
3742 return false;
3743 }
3744
Richard Smith22592862012-03-27 23:05:05 +00003745 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003746 if (Tok.isNot(tok::l_paren)) {
3747 TPA.Revert();
3748 return false;
3749 }
3750 ConsumeParen();
3751
Richard Smith22592862012-03-27 23:05:05 +00003752 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3753 // that we have a constructor.
3754 if (Tok.is(tok::r_paren) ||
3755 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003756 TPA.Revert();
3757 return true;
3758 }
3759
3760 // If we need to, enter the specified scope.
3761 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003762 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003763 DeclScopeObj.EnterDeclaratorScope();
3764
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003765 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003766 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003767 MaybeParseMicrosoftAttributes(Attrs);
3768
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003769 // Check whether the next token(s) are part of a declaration
3770 // specifier, in which case we have the start of a parameter and,
3771 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003772 bool IsConstructor = false;
3773 if (isDeclarationSpecifier())
3774 IsConstructor = true;
3775 else if (Tok.is(tok::identifier) ||
3776 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3777 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3778 // This might be a parenthesized member name, but is more likely to
3779 // be a constructor declaration with an invalid argument type. Keep
3780 // looking.
3781 if (Tok.is(tok::annot_cxxscope))
3782 ConsumeToken();
3783 ConsumeToken();
3784
3785 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003786 // which must have one of the following syntactic forms (see the
3787 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003788 switch (Tok.getKind()) {
3789 case tok::l_paren:
3790 // C(X ( int));
3791 case tok::l_square:
3792 // C(X [ 5]);
3793 // C(X [ [attribute]]);
3794 case tok::coloncolon:
3795 // C(X :: Y);
3796 // C(X :: *p);
3797 case tok::r_paren:
3798 // C(X )
3799 // Assume this isn't a constructor, rather than assuming it's a
3800 // constructor with an unnamed parameter of an ill-formed type.
3801 break;
3802
3803 default:
3804 IsConstructor = true;
3805 break;
3806 }
3807 }
3808
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003809 TPA.Revert();
3810 return IsConstructor;
3811}
Reid Spencer5f016e22007-07-11 17:01:13 +00003812
3813/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003814/// type-qualifier-list: [C99 6.7.5]
3815/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00003816/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00003817/// [ only if VendorAttributesAllowed=true ]
3818/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00003819/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00003820/// [ only if VendorAttributesAllowed=true ]
3821/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3822/// [ only if CXX0XAttributesAllowed=true ]
3823/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003824///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003825void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3826 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003827 bool CXX11AttributesAllowed) {
3828 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003829 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003830 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003831 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003832 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003833 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003834
3835 SourceLocation EndLoc;
3836
Reid Spencer5f016e22007-07-11 17:01:13 +00003837 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003838 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003839 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003840 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003841 SourceLocation Loc = Tok.getLocation();
3842
3843 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003844 case tok::code_completion:
3845 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003846 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00003847
Reid Spencer5f016e22007-07-11 17:01:13 +00003848 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003849 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003850 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003851 break;
3852 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003853 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003854 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003855 break;
3856 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003857 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003858 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003859 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003860
3861 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00003862 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003863 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003864 goto DoneWithTypeQuals;
3865 case tok::kw___private:
3866 case tok::kw___global:
3867 case tok::kw___local:
3868 case tok::kw___constant:
3869 case tok::kw___read_only:
3870 case tok::kw___write_only:
3871 case tok::kw___read_write:
3872 ParseOpenCLQualifiers(DS);
3873 break;
3874
Eli Friedman290eeb02009-06-08 23:27:34 +00003875 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003876 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003877 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003878 case tok::kw___cdecl:
3879 case tok::kw___stdcall:
3880 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003881 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003882 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003883 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003884 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003885 continue;
3886 }
3887 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003888 case tok::kw___pascal:
3889 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003890 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003891 continue;
3892 }
3893 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003894 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003895 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003896 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003897 continue; // do *not* consume the next token!
3898 }
3899 // otherwise, FALL THROUGH!
3900 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003901 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003902 // If this is not a type-qualifier token, we're done reading type
3903 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003904 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003905 if (EndLoc.isValid())
3906 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003907 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003908 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003909
Reid Spencer5f016e22007-07-11 17:01:13 +00003910 // If the specifier combination wasn't legal, issue a diagnostic.
3911 if (isInvalid) {
3912 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003913 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003914 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003915 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003916 }
3917}
3918
3919
3920/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3921///
3922void Parser::ParseDeclarator(Declarator &D) {
3923 /// This implements the 'declarator' production in the C grammar, then checks
3924 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003925 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003926}
3927
Richard Smith9988f282012-03-29 01:16:42 +00003928static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3929 if (Kind == tok::star || Kind == tok::caret)
3930 return true;
3931
3932 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3933 if (!Lang.CPlusPlus)
3934 return false;
3935
3936 return Kind == tok::amp || Kind == tok::ampamp;
3937}
3938
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003939/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3940/// is parsed by the function passed to it. Pass null, and the direct-declarator
3941/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003942/// ptr-operator production.
3943///
Richard Smith0706df42011-10-19 21:33:05 +00003944/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003945/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3946/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003947///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003948/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3949/// [C] pointer[opt] direct-declarator
3950/// [C++] direct-declarator
3951/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003952///
3953/// pointer: [C99 6.7.5]
3954/// '*' type-qualifier-list[opt]
3955/// '*' type-qualifier-list[opt] pointer
3956///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003957/// ptr-operator:
3958/// '*' cv-qualifier-seq[opt]
3959/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003960/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003961/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003962/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003963/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003964void Parser::ParseDeclaratorInternal(Declarator &D,
3965 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003966 if (Diags.hasAllExtensionsSilenced())
3967 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00003968
Sebastian Redlf30208a2009-01-24 21:16:55 +00003969 // C++ member pointers start with a '::' or a nested-name.
3970 // Member pointers get special handling, since there's no place for the
3971 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003972 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003973 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3974 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003975 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3976 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003977 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003978 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003979
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003980 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003981 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003982 // The scope spec really belongs to the direct-declarator.
3983 D.getCXXScopeSpec() = SS;
3984 if (DirectDeclParser)
3985 (this->*DirectDeclParser)(D);
3986 return;
3987 }
3988
3989 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003990 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003991 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003992 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003993 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003994
3995 // Recurse to parse whatever is left.
3996 ParseDeclaratorInternal(D, DirectDeclParser);
3997
3998 // Sema will have to catch (syntactically invalid) pointers into global
3999 // scope. It has to catch pointers into namespace scope anyway.
4000 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004001 Loc),
4002 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004003 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004004 return;
4005 }
4006 }
4007
4008 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004009 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004010 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004011 if (DirectDeclParser)
4012 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004013 return;
4014 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004015
Sebastian Redl05532f22009-03-15 22:02:01 +00004016 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4017 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004018 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004019 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004020
Chris Lattner9af55002009-03-27 04:18:06 +00004021 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004022 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004023 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004024
Richard Smith6ee326a2012-04-10 01:32:12 +00004025 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004026 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004027 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004028
Reid Spencer5f016e22007-07-11 17:01:13 +00004029 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004030 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004031 if (Kind == tok::star)
4032 // Remember that we parsed a pointer type, and remember the type-quals.
4033 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004034 DS.getConstSpecLoc(),
4035 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004036 DS.getRestrictSpecLoc()),
4037 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004038 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004039 else
4040 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004041 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004042 Loc),
4043 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004044 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004045 } else {
4046 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004047 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004048
Sebastian Redl743de1f2009-03-23 00:00:23 +00004049 // Complain about rvalue references in C++03, but then go on and build
4050 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004051 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00004052 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004053 diag::warn_cxx98_compat_rvalue_reference :
4054 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004055
Richard Smith6ee326a2012-04-10 01:32:12 +00004056 // GNU-style and C++11 attributes are allowed here, as is restrict.
4057 ParseTypeQualifierListOpt(DS);
4058 D.ExtendWithDeclSpec(DS);
4059
Reid Spencer5f016e22007-07-11 17:01:13 +00004060 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4061 // cv-qualifiers are introduced through the use of a typedef or of a
4062 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004063 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4064 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4065 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004066 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004067 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4068 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004069 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00004070 }
4071
4072 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004073 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004074
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004075 if (D.getNumTypeObjects() > 0) {
4076 // C++ [dcl.ref]p4: There shall be no references to references.
4077 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4078 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004079 if (const IdentifierInfo *II = D.getIdentifier())
4080 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4081 << II;
4082 else
4083 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4084 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004085
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004086 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004087 // can go ahead and build the (technically ill-formed)
4088 // declarator: reference collapsing will take care of it.
4089 }
4090 }
4091
Reid Spencer5f016e22007-07-11 17:01:13 +00004092 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004093 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004094 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004095 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004096 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004097 }
4098}
4099
Richard Smith9988f282012-03-29 01:16:42 +00004100static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4101 SourceLocation EllipsisLoc) {
4102 if (EllipsisLoc.isValid()) {
4103 FixItHint Insertion;
4104 if (!D.getEllipsisLoc().isValid()) {
4105 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4106 D.setEllipsisLoc(EllipsisLoc);
4107 }
4108 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4109 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4110 }
4111}
4112
Reid Spencer5f016e22007-07-11 17:01:13 +00004113/// ParseDirectDeclarator
4114/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004115/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004116/// '(' declarator ')'
4117/// [GNU] '(' attributes declarator ')'
4118/// [C90] direct-declarator '[' constant-expression[opt] ']'
4119/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4120/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4121/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4122/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004123/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4124/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004125/// direct-declarator '(' parameter-type-list ')'
4126/// direct-declarator '(' identifier-list[opt] ')'
4127/// [GNU] direct-declarator '(' parameter-forward-declarations
4128/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004129/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4130/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004131/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4132/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4133/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004134/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004135/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004136///
4137/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004138/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004139/// '::'[opt] nested-name-specifier[opt] type-name
4140///
4141/// id-expression: [C++ 5.1]
4142/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004143/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004144///
4145/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004146/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004147/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004148/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004149/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004150/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004151///
Richard Smith5d8388c2012-03-27 01:42:32 +00004152/// Note, any additional constructs added here may need corresponding changes
4153/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004154void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004155 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004156
David Blaikie4e4d0842012-03-11 07:00:24 +00004157 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004158 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004159 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004160 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4161 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004162 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004163 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004164 }
4165
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004166 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004167 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004168 // Change the declaration context for name lookup, until this function
4169 // is exited (and the declarator has been parsed).
4170 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004171 }
4172
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004173 // C++0x [dcl.fct]p14:
4174 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004175 // of a parameter-declaration-clause without a preceding comma. In
4176 // this case, the ellipsis is parsed as part of the
4177 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004178 // parameter pack that has not been expanded; otherwise, it is parsed
4179 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004180 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004181 !((D.getContext() == Declarator::PrototypeContext ||
4182 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004183 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00004184 !Actions.containsUnexpandedParameterPacks(D))) {
4185 SourceLocation EllipsisLoc = ConsumeToken();
4186 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4187 // The ellipsis was put in the wrong place. Recover, and explain to
4188 // the user what they should have done.
4189 ParseDeclarator(D);
4190 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4191 return;
4192 } else
4193 D.setEllipsisLoc(EllipsisLoc);
4194
4195 // The ellipsis can't be followed by a parenthesized declarator. We
4196 // check for that in ParseParenDeclarator, after we have disambiguated
4197 // the l_paren token.
4198 }
4199
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004200 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4201 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4202 // We found something that indicates the start of an unqualified-id.
4203 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004204 bool AllowConstructorName;
4205 if (D.getDeclSpec().hasTypeSpecifier())
4206 AllowConstructorName = false;
4207 else if (D.getCXXScopeSpec().isSet())
4208 AllowConstructorName =
4209 (D.getContext() == Declarator::FileContext ||
4210 (D.getContext() == Declarator::MemberContext &&
4211 D.getDeclSpec().isFriendSpecified()));
4212 else
4213 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4214
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004215 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004216 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4217 /*EnteringContext=*/true,
4218 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004219 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004220 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004221 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004222 D.getName()) ||
4223 // Once we're past the identifier, if the scope was bad, mark the
4224 // whole declarator bad.
4225 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004226 D.SetIdentifier(0, Tok.getLocation());
4227 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004228 } else {
4229 // Parsed the unqualified-id; update range information and move along.
4230 if (D.getSourceRange().getBegin().isInvalid())
4231 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4232 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004233 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004234 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004235 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004236 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004237 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004238 "There's a C++-specific check for tok::identifier above");
4239 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4240 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4241 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004242 goto PastIdentifier;
4243 }
Richard Smith9988f282012-03-29 01:16:42 +00004244
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004245 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004246 // direct-declarator: '(' declarator ')'
4247 // direct-declarator: '(' attributes declarator ')'
4248 // Example: 'char (*X)' or 'int (*XX)(void)'
4249 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004250
4251 // If the declarator was parenthesized, we entered the declarator
4252 // scope when parsing the parenthesized declarator, then exited
4253 // the scope already. Re-enter the scope, if we need to.
4254 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004255 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004256 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004257 if (!D.isInvalidType() &&
4258 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004259 // Change the declaration context for name lookup, until this function
4260 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004261 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004262 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004263 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004264 // This could be something simple like "int" (in which case the declarator
4265 // portion is empty), if an abstract-declarator is allowed.
4266 D.SetIdentifier(0, Tok.getLocation());
4267 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004268 if (Tok.getKind() == tok::annot_pragma_parser_crash)
4269 *(volatile int*) 0x11 = 0;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004270 if (D.getContext() == Declarator::MemberContext)
4271 Diag(Tok, diag::err_expected_member_name_or_semi)
4272 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004273 else if (getLangOpts().CPlusPlus)
4274 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004275 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004276 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004277 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004278 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004279 }
Mike Stump1eb44332009-09-09 15:08:12 +00004280
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004281 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004282 assert(D.isPastIdentifier() &&
4283 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004284
Richard Smith6ee326a2012-04-10 01:32:12 +00004285 // Don't parse attributes unless we have parsed an unparenthesized name.
4286 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004287 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004288
Reid Spencer5f016e22007-07-11 17:01:13 +00004289 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004290 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004291 // Enter function-declaration scope, limiting any declarators to the
4292 // function prototype scope, including parameter declarators.
4293 ParseScope PrototypeScope(this,
4294 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004295 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4296 // In such a case, check if we actually have a function declarator; if it
4297 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00004298 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00004299 // When not in file scope, warn for ambiguous function declarators, just
4300 // in case the author intended it as a variable definition.
4301 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4302 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4303 break;
4304 }
John McCall0b7e6782011-03-24 11:26:52 +00004305 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004306 BalancedDelimiterTracker T(*this, tok::l_paren);
4307 T.consumeOpen();
4308 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004309 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004310 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004311 ParseBracketDeclarator(D);
4312 } else {
4313 break;
4314 }
4315 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004316}
Reid Spencer5f016e22007-07-11 17:01:13 +00004317
Chris Lattneref4715c2008-04-06 05:45:57 +00004318/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4319/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004320/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004321/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4322///
4323/// direct-declarator:
4324/// '(' declarator ')'
4325/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004326/// direct-declarator '(' parameter-type-list ')'
4327/// direct-declarator '(' identifier-list[opt] ')'
4328/// [GNU] direct-declarator '(' parameter-forward-declarations
4329/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004330///
4331void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004332 BalancedDelimiterTracker T(*this, tok::l_paren);
4333 T.consumeOpen();
4334
Chris Lattneref4715c2008-04-06 05:45:57 +00004335 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004336
Chris Lattner7399ee02008-10-20 02:05:46 +00004337 // Eat any attributes before we look at whether this is a grouping or function
4338 // declarator paren. If this is a grouping paren, the attribute applies to
4339 // the type being built up, for example:
4340 // int (__attribute__(()) *x)(long y)
4341 // If this ends up not being a grouping paren, the attribute applies to the
4342 // first argument, for example:
4343 // int (__attribute__(()) int x)
4344 // In either case, we need to eat any attributes to be able to determine what
4345 // sort of paren this is.
4346 //
John McCall0b7e6782011-03-24 11:26:52 +00004347 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004348 bool RequiresArg = false;
4349 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004350 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004351
Chris Lattner7399ee02008-10-20 02:05:46 +00004352 // We require that the argument list (if this is a non-grouping paren) be
4353 // present even if the attribute list was empty.
4354 RequiresArg = true;
4355 }
Steve Naroff239f0732008-12-25 14:16:32 +00004356 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004357 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004358 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004359 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004360 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004361 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004362 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004363 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004364 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004365 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004366
Chris Lattneref4715c2008-04-06 05:45:57 +00004367 // If we haven't past the identifier yet (or where the identifier would be
4368 // stored, if this is an abstract declarator), then this is probably just
4369 // grouping parens. However, if this could be an abstract-declarator, then
4370 // this could also be the start of function arguments (consider 'void()').
4371 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004372
Chris Lattneref4715c2008-04-06 05:45:57 +00004373 if (!D.mayOmitIdentifier()) {
4374 // If this can't be an abstract-declarator, this *must* be a grouping
4375 // paren, because we haven't seen the identifier yet.
4376 isGrouping = true;
4377 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004378 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4379 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004380 isDeclarationSpecifier() || // 'int(int)' is a function.
4381 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004382 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4383 // considered to be a type, not a K&R identifier-list.
4384 isGrouping = false;
4385 } else {
4386 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4387 isGrouping = true;
4388 }
Mike Stump1eb44332009-09-09 15:08:12 +00004389
Chris Lattneref4715c2008-04-06 05:45:57 +00004390 // If this is a grouping paren, handle:
4391 // direct-declarator: '(' declarator ')'
4392 // direct-declarator: '(' attributes declarator ')'
4393 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004394 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4395 D.setEllipsisLoc(SourceLocation());
4396
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004397 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004398 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004399 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004400 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004401 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004402 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004403 T.getCloseLocation()),
4404 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004405
4406 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004407
4408 // An ellipsis cannot be placed outside parentheses.
4409 if (EllipsisLoc.isValid())
4410 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4411
Chris Lattneref4715c2008-04-06 05:45:57 +00004412 return;
4413 }
Mike Stump1eb44332009-09-09 15:08:12 +00004414
Chris Lattneref4715c2008-04-06 05:45:57 +00004415 // Okay, if this wasn't a grouping paren, it must be the start of a function
4416 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004417 // identifier (and remember where it would have been), then call into
4418 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004419 D.SetIdentifier(0, Tok.getLocation());
4420
David Blaikie42d6d0c2011-12-04 05:04:18 +00004421 // Enter function-declaration scope, limiting any declarators to the
4422 // function prototype scope, including parameter declarators.
4423 ParseScope PrototypeScope(this,
4424 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004425 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004426 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004427}
4428
4429/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4430/// declarator D up to a paren, which indicates that we are parsing function
4431/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004432///
Richard Smith6ee326a2012-04-10 01:32:12 +00004433/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4434/// immediately after the open paren - they should be considered to be the
4435/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004436///
Richard Smith6ee326a2012-04-10 01:32:12 +00004437/// If RequiresArg is true, then the first argument of the function is required
4438/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004439///
Richard Smith6ee326a2012-04-10 01:32:12 +00004440/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4441/// (C++11) ref-qualifier[opt], exception-specification[opt],
4442/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4443///
4444/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004445/// dynamic-exception-specification
4446/// noexcept-specification
4447///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004448void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004449 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004450 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004451 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004452 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004453 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004454 // lparen is already consumed!
4455 assert(D.isPastIdentifier() && "Should not call before identifier!");
4456
4457 // This should be true when the function has typed arguments.
4458 // Otherwise, it is treated as a K&R-style function.
4459 bool HasProto = false;
4460 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004461 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004462 // Remember where we see an ellipsis, if any.
4463 SourceLocation EllipsisLoc;
4464
4465 DeclSpec DS(AttrFactory);
4466 bool RefQualifierIsLValueRef = true;
4467 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004468 SourceLocation ConstQualifierLoc;
4469 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004470 ExceptionSpecificationType ESpecType = EST_None;
4471 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004472 SmallVector<ParsedType, 2> DynamicExceptions;
4473 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004474 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004475 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004476 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004477
James Molloy16f1f712012-02-29 10:24:19 +00004478 Actions.ActOnStartFunctionDeclarator();
4479
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004480 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004481 if (isFunctionDeclaratorIdentifierList()) {
4482 if (RequiresArg)
4483 Diag(Tok, diag::err_argument_required_after_attribute);
4484
4485 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4486
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004487 Tracker.consumeClose();
4488 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004489 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004490 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004491 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004492 else if (RequiresArg)
4493 Diag(Tok, diag::err_argument_required_after_attribute);
4494
David Blaikie4e4d0842012-03-11 07:00:24 +00004495 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004496
4497 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004498 Tracker.consumeClose();
4499 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004500
David Blaikie4e4d0842012-03-11 07:00:24 +00004501 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004502 // FIXME: Accept these components in any order, and produce fixits to
4503 // correct the order if the user gets it wrong. Ideally we should deal
4504 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004505
4506 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004507 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4508 if (!DS.getSourceRange().getEnd().isInvalid()) {
4509 EndLoc = DS.getSourceRange().getEnd();
4510 ConstQualifierLoc = DS.getConstSpecLoc();
4511 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4512 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004513
4514 // Parse ref-qualifier[opt].
4515 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004516 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004517 diag::warn_cxx98_compat_ref_qualifier :
4518 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004519
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004520 RefQualifierIsLValueRef = Tok.is(tok::amp);
4521 RefQualifierLoc = ConsumeToken();
4522 EndLoc = RefQualifierLoc;
4523 }
4524
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004525 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00004526 // If a declaration declares a member function or member function
4527 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004528 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00004529 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004530 // declarator.
Chad Rosier8decdee2012-06-26 22:30:43 +00004531 bool IsCXX11MemberFunction =
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004532 getLangOpts().CPlusPlus0x &&
4533 (D.getContext() == Declarator::MemberContext ||
4534 (D.getContext() == Declarator::FileContext &&
Chad Rosier8decdee2012-06-26 22:30:43 +00004535 D.getCXXScopeSpec().isValid() &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004536 Actions.CurContext->isRecord()));
4537 Sema::CXXThisScopeRAII ThisScope(Actions,
4538 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4539 DS.getTypeQualifiers(),
4540 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004541
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004542 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004543 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004544 DynamicExceptions,
4545 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004546 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004547 if (ESpecType != EST_None)
4548 EndLoc = ESpecRange.getEnd();
4549
Richard Smith6ee326a2012-04-10 01:32:12 +00004550 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4551 // after the exception-specification.
4552 MaybeParseCXX0XAttributes(FnAttrs);
4553
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004554 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004555 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004556 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004557 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004558 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004559 if (Range.getEnd().isValid())
4560 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004561 }
4562 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004563 }
4564
4565 // Remember that we parsed a function type, and remember the attributes.
4566 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4567 /*isVariadic=*/EllipsisLoc.isValid(),
4568 EllipsisLoc,
4569 ParamInfo.data(), ParamInfo.size(),
4570 DS.getTypeQualifiers(),
4571 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004572 RefQualifierLoc, ConstQualifierLoc,
4573 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004574 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004575 ESpecType, ESpecRange.getBegin(),
4576 DynamicExceptions.data(),
4577 DynamicExceptionRanges.data(),
4578 DynamicExceptions.size(),
4579 NoexceptExpr.isUsable() ?
4580 NoexceptExpr.get() : 0,
Chad Rosier8decdee2012-06-26 22:30:43 +00004581 Tracker.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004582 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004583 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004584 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004585
4586 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004587}
4588
4589/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4590/// identifier list form for a K&R-style function: void foo(a,b,c)
4591///
4592/// Note that identifier-lists are only allowed for normal declarators, not for
4593/// abstract-declarators.
4594bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004595 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004596 && Tok.is(tok::identifier)
4597 && !TryAltiVecVectorToken()
4598 // K&R identifier lists can't have typedefs as identifiers, per C99
4599 // 6.7.5.3p11.
4600 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4601 // Identifier lists follow a really simple grammar: the identifiers can
4602 // be followed *only* by a ", identifier" or ")". However, K&R
4603 // identifier lists are really rare in the brave new modern world, and
4604 // it is very common for someone to typo a type in a non-K&R style
4605 // list. If we are presented with something like: "void foo(intptr x,
4606 // float y)", we don't want to start parsing the function declarator as
4607 // though it is a K&R style declarator just because intptr is an
4608 // invalid type.
4609 //
4610 // To handle this, we check to see if the token after the first
4611 // identifier is a "," or ")". Only then do we parse it as an
4612 // identifier list.
4613 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4614}
4615
4616/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4617/// we found a K&R-style identifier list instead of a typed parameter list.
4618///
4619/// After returning, ParamInfo will hold the parsed parameters.
4620///
4621/// identifier-list: [C99 6.7.5]
4622/// identifier
4623/// identifier-list ',' identifier
4624///
4625void Parser::ParseFunctionDeclaratorIdentifierList(
4626 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004627 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004628 // If there was no identifier specified for the declarator, either we are in
4629 // an abstract-declarator, or we are in a parameter declarator which was found
4630 // to be abstract. In abstract-declarators, identifier lists are not valid:
4631 // diagnose this.
4632 if (!D.getIdentifier())
4633 Diag(Tok, diag::ext_ident_list_in_param);
4634
4635 // Maintain an efficient lookup of params we have seen so far.
4636 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4637
4638 while (1) {
4639 // If this isn't an identifier, report the error and skip until ')'.
4640 if (Tok.isNot(tok::identifier)) {
4641 Diag(Tok, diag::err_expected_ident);
4642 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4643 // Forget we parsed anything.
4644 ParamInfo.clear();
4645 return;
4646 }
4647
4648 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4649
4650 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4651 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4652 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4653
4654 // Verify that the argument identifier has not already been mentioned.
4655 if (!ParamsSoFar.insert(ParmII)) {
4656 Diag(Tok, diag::err_param_redefinition) << ParmII;
4657 } else {
4658 // Remember this identifier in ParamInfo.
4659 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4660 Tok.getLocation(),
4661 0));
4662 }
4663
4664 // Eat the identifier.
4665 ConsumeToken();
4666
4667 // The list continues if we see a comma.
4668 if (Tok.isNot(tok::comma))
4669 break;
4670 ConsumeToken();
4671 }
4672}
4673
4674/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4675/// after the opening parenthesis. This function will not parse a K&R-style
4676/// identifier list.
4677///
Richard Smith6ce48a72012-04-11 04:01:28 +00004678/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4679/// caller parsed those arguments immediately after the open paren - they should
4680/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004681///
4682/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4683/// be the location of the ellipsis, if any was parsed.
4684///
Reid Spencer5f016e22007-07-11 17:01:13 +00004685/// parameter-type-list: [C99 6.7.5]
4686/// parameter-list
4687/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004688/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004689///
4690/// parameter-list: [C99 6.7.5]
4691/// parameter-declaration
4692/// parameter-list ',' parameter-declaration
4693///
4694/// parameter-declaration: [C99 6.7.5]
4695/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004696/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004697/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004698/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004699/// declaration-specifiers abstract-declarator[opt]
4700/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004701/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004702/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004703/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004704///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004705void Parser::ParseParameterDeclarationClause(
4706 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004707 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004708 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004709 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004710
Chris Lattnerf97409f2008-04-06 06:57:35 +00004711 while (1) {
4712 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004713 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4714 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004715 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004716 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004717 }
Mike Stump1eb44332009-09-09 15:08:12 +00004718
Chris Lattnerf97409f2008-04-06 06:57:35 +00004719 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004720 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004721 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004722
Richard Smith6ce48a72012-04-11 04:01:28 +00004723 // Parse any C++11 attributes.
4724 MaybeParseCXX0XAttributes(DS.getAttributes());
4725
John McCall7f040a92010-12-24 02:08:15 +00004726 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004727 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004728 ParseMicrosoftAttributes(DS.getAttributes());
4729
4730 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004731
4732 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004733 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004734 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004735 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4736 // too much hassle.
4737 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004738
Chris Lattnere64c5492009-02-27 18:38:20 +00004739 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004740
Chris Lattnerf97409f2008-04-06 06:57:35 +00004741 // Parse the declarator. This is "PrototypeContext", because we must
4742 // accept either 'declarator' or 'abstract-declarator' here.
4743 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4744 ParseDeclarator(ParmDecl);
4745
4746 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004747 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004748
Chris Lattnerf97409f2008-04-06 06:57:35 +00004749 // Remember this parsed parameter in ParamInfo.
4750 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004751
Douglas Gregor72b505b2008-12-16 21:30:33 +00004752 // DefArgToks is used when the parsing of default arguments needs
4753 // to be delayed.
4754 CachedTokens *DefArgToks = 0;
4755
Chris Lattnerf97409f2008-04-06 06:57:35 +00004756 // If no parameter was specified, verify that *something* was specified,
4757 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004758 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4759 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004760 // Completely missing, emit error.
4761 Diag(DSStart, diag::err_missing_param);
4762 } else {
4763 // Otherwise, we have something. Add it and let semantic analysis try
4764 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004765
Chris Lattnerf97409f2008-04-06 06:57:35 +00004766 // Inform the actions module about the parameter declarator, so it gets
4767 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004768 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004769
4770 // Parse the default argument, if any. We parse the default
4771 // arguments in all dialects; the semantic analysis in
4772 // ActOnParamDefaultArgument will reject the default argument in
4773 // C.
4774 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004775 SourceLocation EqualLoc = Tok.getLocation();
4776
Chris Lattner04421082008-04-08 04:40:51 +00004777 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004778 if (D.getContext() == Declarator::MemberContext) {
4779 // If we're inside a class definition, cache the tokens
4780 // corresponding to the default argument. We'll actually parse
4781 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004782 // FIXME: Can we use a smart pointer for Toks?
4783 DefArgToks = new CachedTokens;
4784
Mike Stump1eb44332009-09-09 15:08:12 +00004785 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004786 /*StopAtSemi=*/true,
4787 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004788 delete DefArgToks;
4789 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004790 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004791 } else {
4792 // Mark the end of the default argument so that we know when to
4793 // stop when we parse it later on.
4794 Token DefArgEnd;
4795 DefArgEnd.startToken();
4796 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4797 DefArgEnd.setLocation(Tok.getLocation());
4798 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004799 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004800 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004801 }
Chris Lattner04421082008-04-08 04:40:51 +00004802 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004803 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004804 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004805
Chad Rosier8decdee2012-06-26 22:30:43 +00004806 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004807 // used.
4808 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004809 Sema::PotentiallyEvaluatedIfUsed,
4810 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004811
Sebastian Redl84407ba2012-03-14 15:54:00 +00004812 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004813 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4814 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004815 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004816 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004817 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004818 if (DefArgResult.isInvalid()) {
4819 Actions.ActOnParamDefaultArgumentError(Param);
4820 SkipUntil(tok::comma, tok::r_paren, true, true);
4821 } else {
4822 // Inform the actions module about the default argument
4823 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004824 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004825 }
Chris Lattner04421082008-04-08 04:40:51 +00004826 }
4827 }
Mike Stump1eb44332009-09-09 15:08:12 +00004828
4829 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4830 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004831 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004832 }
4833
4834 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004835 if (Tok.isNot(tok::comma)) {
4836 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004837 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00004838
David Blaikie4e4d0842012-03-11 07:00:24 +00004839 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004840 // We have ellipsis without a preceding ',', which is ill-formed
4841 // in C. Complain and provide the fix.
4842 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004843 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004844 }
4845 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004846
Douglas Gregored5d6512009-09-22 21:41:40 +00004847 break;
4848 }
Mike Stump1eb44332009-09-09 15:08:12 +00004849
Chris Lattnerf97409f2008-04-06 06:57:35 +00004850 // Consume the comma.
4851 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004852 }
Mike Stump1eb44332009-09-09 15:08:12 +00004853
Chris Lattner66d28652008-04-06 06:34:08 +00004854}
Chris Lattneref4715c2008-04-06 05:45:57 +00004855
Reid Spencer5f016e22007-07-11 17:01:13 +00004856/// [C90] direct-declarator '[' constant-expression[opt] ']'
4857/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4858/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4859/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4860/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004861/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4862/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004863void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004864 if (CheckProhibitedCXX11Attribute())
4865 return;
4866
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004867 BalancedDelimiterTracker T(*this, tok::l_square);
4868 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004869
Chris Lattner378c7e42008-12-18 07:27:21 +00004870 // C array syntax has many features, but by-far the most common is [] and [4].
4871 // This code does a fast path to handle some of the most obvious cases.
4872 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004873 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004874 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004875 MaybeParseCXX0XAttributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00004876
Chris Lattner378c7e42008-12-18 07:27:21 +00004877 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004878 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004879 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004880 T.getOpenLocation(),
4881 T.getCloseLocation()),
4882 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004883 return;
4884 } else if (Tok.getKind() == tok::numeric_constant &&
4885 GetLookAheadToken(1).is(tok::r_square)) {
4886 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004887 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004888 ConsumeToken();
4889
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004890 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004891 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004892 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004893
Chris Lattner378c7e42008-12-18 07:27:21 +00004894 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004895 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004896 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004897 T.getOpenLocation(),
4898 T.getCloseLocation()),
4899 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004900 return;
4901 }
Mike Stump1eb44332009-09-09 15:08:12 +00004902
Reid Spencer5f016e22007-07-11 17:01:13 +00004903 // If valid, this location is the position where we read the 'static' keyword.
4904 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004905 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004906 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004907
Reid Spencer5f016e22007-07-11 17:01:13 +00004908 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004909 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004910 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004911 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004912
Reid Spencer5f016e22007-07-11 17:01:13 +00004913 // If we haven't already read 'static', check to see if there is one after the
4914 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004915 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004916 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004917
Reid Spencer5f016e22007-07-11 17:01:13 +00004918 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4919 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004920 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004921
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004922 // Handle the case where we have '[*]' as the array size. However, a leading
4923 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4924 // the the token after the star is a ']'. Since stars in arrays are
4925 // infrequent, use of lookahead is not costly here.
4926 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004927 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004928
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004929 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004930 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004931 StaticLoc = SourceLocation(); // Drop the static.
4932 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004933 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004934 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004935 // Note, in C89, this production uses the constant-expr production instead
4936 // of assignment-expr. The only difference is that assignment-expr allows
4937 // things like '=' and '*='. Sema rejects these in C89 mode because they
4938 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004939
Douglas Gregore0762c92009-06-19 23:52:42 +00004940 // Parse the constant-expression or assignment-expression now (depending
4941 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004942 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004943 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004944 } else {
4945 EnterExpressionEvaluationContext Unevaluated(Actions,
4946 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004947 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004948 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004949 }
Mike Stump1eb44332009-09-09 15:08:12 +00004950
Reid Spencer5f016e22007-07-11 17:01:13 +00004951 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004952 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004953 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004954 // If the expression was invalid, skip it.
4955 SkipUntil(tok::r_square);
4956 return;
4957 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004958
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004959 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004960
John McCall0b7e6782011-03-24 11:26:52 +00004961 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004962 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004963
Chris Lattner378c7e42008-12-18 07:27:21 +00004964 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004965 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004966 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004967 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004968 T.getOpenLocation(),
4969 T.getCloseLocation()),
4970 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004971}
4972
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004973/// [GNU] typeof-specifier:
4974/// typeof ( expressions )
4975/// typeof ( type-name )
4976/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004977///
4978void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004979 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004980 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004981 SourceLocation StartLoc = ConsumeToken();
4982
John McCallcfb708c2010-01-13 20:03:27 +00004983 const bool hasParens = Tok.is(tok::l_paren);
4984
Eli Friedman71b8fb52012-01-21 01:01:51 +00004985 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4986
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004987 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004988 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004989 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004990 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4991 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004992 if (hasParens)
4993 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004994
4995 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004996 // FIXME: Not accurate, the range gets one token more than it should.
4997 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004998 else
4999 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005000
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005001 if (isCastExpr) {
5002 if (!CastTy) {
5003 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005004 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005005 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005006
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005007 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005008 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005009 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5010 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005011 DiagID, CastTy))
5012 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005013 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005014 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005015
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005016 // If we get here, the operand to the typeof was an expresion.
5017 if (Operand.isInvalid()) {
5018 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005019 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005020 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005021
Eli Friedman71b8fb52012-01-21 01:01:51 +00005022 // We might need to transform the operand if it is potentially evaluated.
5023 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5024 if (Operand.isInvalid()) {
5025 DS.SetTypeSpecError();
5026 return;
5027 }
5028
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005029 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005030 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005031 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5032 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005033 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005034 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005035}
Chris Lattner1b492422010-02-28 18:33:55 +00005036
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005037/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005038/// _Atomic ( type-name )
5039///
5040void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5041 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5042
5043 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005044 BalancedDelimiterTracker T(*this, tok::l_paren);
5045 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00005046 SkipUntil(tok::r_paren);
5047 return;
5048 }
5049
5050 TypeResult Result = ParseTypeName();
5051 if (Result.isInvalid()) {
5052 SkipUntil(tok::r_paren);
5053 return;
5054 }
5055
5056 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005057 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005058
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005059 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005060 return;
5061
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005062 DS.setTypeofParensRange(T.getRange());
5063 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005064
5065 const char *PrevSpec = 0;
5066 unsigned DiagID;
5067 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5068 DiagID, Result.release()))
5069 Diag(StartLoc, DiagID) << PrevSpec;
5070}
5071
Chris Lattner1b492422010-02-28 18:33:55 +00005072
5073/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5074/// from TryAltiVecVectorToken.
5075bool Parser::TryAltiVecVectorTokenOutOfLine() {
5076 Token Next = NextToken();
5077 switch (Next.getKind()) {
5078 default: return false;
5079 case tok::kw_short:
5080 case tok::kw_long:
5081 case tok::kw_signed:
5082 case tok::kw_unsigned:
5083 case tok::kw_void:
5084 case tok::kw_char:
5085 case tok::kw_int:
5086 case tok::kw_float:
5087 case tok::kw_double:
5088 case tok::kw_bool:
5089 case tok::kw___pixel:
5090 Tok.setKind(tok::kw___vector);
5091 return true;
5092 case tok::identifier:
5093 if (Next.getIdentifierInfo() == Ident_pixel) {
5094 Tok.setKind(tok::kw___vector);
5095 return true;
5096 }
5097 return false;
5098 }
5099}
5100
5101bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5102 const char *&PrevSpec, unsigned &DiagID,
5103 bool &isInvalid) {
5104 if (Tok.getIdentifierInfo() == Ident_vector) {
5105 Token Next = NextToken();
5106 switch (Next.getKind()) {
5107 case tok::kw_short:
5108 case tok::kw_long:
5109 case tok::kw_signed:
5110 case tok::kw_unsigned:
5111 case tok::kw_void:
5112 case tok::kw_char:
5113 case tok::kw_int:
5114 case tok::kw_float:
5115 case tok::kw_double:
5116 case tok::kw_bool:
5117 case tok::kw___pixel:
5118 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5119 return true;
5120 case tok::identifier:
5121 if (Next.getIdentifierInfo() == Ident_pixel) {
5122 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5123 return true;
5124 }
5125 break;
5126 default:
5127 break;
5128 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005129 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005130 DS.isTypeAltiVecVector()) {
5131 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5132 return true;
5133 }
5134 return false;
5135}