blob: 1979bb12cdf12ae7f67e06b9c41326878d618b05 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Benjamin Kramer9852f582012-12-01 16:35:25 +000016#include "clang/Basic/AddressSpaces.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000017#include "clang/Basic/CharInfo.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000018#include "clang/Basic/OpenCL.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000020#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000021#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000022#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000026#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// C99 6.7: Declarations.
31//===----------------------------------------------------------------------===//
32
33/// ParseTypeName
34/// type-name: [C99 6.7.6]
35/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000036///
37/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000038TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000039 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000040 AccessSpecifier AS,
Richard Smith6b3d3e52013-02-20 19:22:51 +000041 Decl **OwnedType,
42 ParsedAttributes *Attrs) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000043 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000044 if (DSC == DSC_normal)
45 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000046
Reid Spencer5f016e22007-07-11 17:01:13 +000047 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000048 DeclSpec DS(AttrFactory);
Richard Smith6b3d3e52013-02-20 19:22:51 +000049 if (Attrs)
50 DS.addAttributes(Attrs->getList());
Richard Smith7796eb52012-03-12 08:56:40 +000051 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000052 if (OwnedType)
53 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000054
Reid Spencer5f016e22007-07-11 17:01:13 +000055 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000056 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000057 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000058 if (Range)
59 *Range = DeclaratorInfo.getSourceRange();
60
Chris Lattnereaaebc72009-04-25 08:06:05 +000061 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000062 return true;
63
Douglas Gregor23c94db2010-07-02 17:43:08 +000064 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000065}
66
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000067
68/// isAttributeLateParsed - Return true if the attribute has arguments that
69/// require late parsing.
70static bool isAttributeLateParsed(const IdentifierInfo &II) {
71 return llvm::StringSwitch<bool>(II.getName())
72#include "clang/Parse/AttrLateParsed.inc"
73 .Default(false);
74}
75
Sean Huntbbd37c62009-11-21 08:43:09 +000076/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000077///
78/// [GNU] attributes:
79/// attribute
80/// attributes attribute
81///
82/// [GNU] attribute:
83/// '__attribute__' '(' '(' attribute-list ')' ')'
84///
85/// [GNU] attribute-list:
86/// attrib
87/// attribute_list ',' attrib
88///
89/// [GNU] attrib:
90/// empty
91/// attrib-name
92/// attrib-name '(' identifier ')'
93/// attrib-name '(' identifier ',' nonempty-expr-list ')'
94/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
95///
96/// [GNU] attrib-name:
97/// identifier
98/// typespec
99/// typequal
100/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +0000101///
Reid Spencer5f016e22007-07-11 17:01:13 +0000102/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +0000103/// token lookahead. Comment from gcc: "If they start with an identifier
104/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +0000105/// start with that identifier; otherwise they are an expression list."
106///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000107/// GCC does not require the ',' between attribs in an attribute-list.
108///
Reid Spencer5f016e22007-07-11 17:01:13 +0000109/// At the moment, I am not doing 2 token lookahead. I am also unaware of
110/// any attributes that don't work (based on my limited testing). Most
111/// attributes are very simple in practice. Until we find a bug, I don't see
112/// a pressing need to implement the 2 token lookahead.
113
John McCall7f040a92010-12-24 02:08:15 +0000114void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000115 SourceLocation *endLoc,
116 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000117 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Chris Lattner04d66662007-10-09 17:33:22 +0000119 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 ConsumeToken();
121 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
122 "attribute")) {
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 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
127 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000128 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 }
130 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000131 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
132 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000133 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
135 ConsumeToken();
136 continue;
137 }
138 // we have an identifier or declaration specifier (const, int, etc.)
139 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
140 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000142 if (Tok.is(tok::l_paren)) {
143 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000144 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000145 LateParsedAttribute *LA =
146 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
147 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000148
Bill Wendlingad017fa2012-12-20 19:22:21 +0000149 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000150 // with other late-parsed declarations.
DeLesley Hutchins161db022012-11-02 21:44:32 +0000151 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000152 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000154 // consume everything up to and including the matching right parens
155 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000157 Token Eof;
158 Eof.startToken();
159 Eof.setLocation(Tok.getLocation());
160 LA->Toks.push_back(Eof);
161 } else {
Michael Han6880f492012-10-03 01:56:22 +0000162 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000163 0, SourceLocation(), AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 }
165 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000166 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000167 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 }
169 }
170 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000172 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000173 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
174 SkipUntil(tok::r_paren, false);
175 }
John McCall7f040a92010-12-24 02:08:15 +0000176 if (endLoc)
177 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000179}
180
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000181
Michael Han6880f492012-10-03 01:56:22 +0000182/// Parse the arguments to a parameterized GNU attribute or
183/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000184void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
185 SourceLocation AttrNameLoc,
186 ParsedAttributes &Attrs,
Michael Han6880f492012-10-03 01:56:22 +0000187 SourceLocation *EndLoc,
188 IdentifierInfo *ScopeName,
189 SourceLocation ScopeLoc,
190 AttributeList::Syntax Syntax) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000191
192 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
193
194 // Availability attributes have their own grammar.
195 if (AttrName->isStr("availability")) {
196 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
197 return;
198 }
199 // Thread safety attributes fit into the FIXME case above, so we
200 // just parse the arguments as a list of expressions
201 if (IsThreadSafetyAttribute(AttrName->getName())) {
202 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
203 return;
204 }
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000205 // Type safety attributes have their own grammar.
206 if (AttrName->isStr("type_tag_for_datatype")) {
207 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
208 return;
209 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000210
211 ConsumeParen(); // ignore the left paren loc for now
212
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000213 IdentifierInfo *ParmName = 0;
214 SourceLocation ParmLoc;
215 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000216
Joey Gouly37453b92013-03-08 09:42:32 +0000217 TypeResult T;
218 SourceRange TypeRange;
219 bool TypeParsed = false;
220
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000221 switch (Tok.getKind()) {
222 case tok::kw_char:
223 case tok::kw_wchar_t:
224 case tok::kw_char16_t:
225 case tok::kw_char32_t:
226 case tok::kw_bool:
227 case tok::kw_short:
228 case tok::kw_int:
229 case tok::kw_long:
230 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000231 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000232 case tok::kw_signed:
233 case tok::kw_unsigned:
234 case tok::kw_float:
235 case tok::kw_double:
236 case tok::kw_void:
237 case tok::kw_typeof:
238 // __attribute__(( vec_type_hint(char) ))
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000239 BuiltinType = true;
Joey Gouly37453b92013-03-08 09:42:32 +0000240 T = ParseTypeName(&TypeRange);
241 TypeParsed = true;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000242 break;
243
244 case tok::identifier:
Joey Gouly37453b92013-03-08 09:42:32 +0000245 if (AttrName->isStr("vec_type_hint")) {
246 T = ParseTypeName(&TypeRange);
247 TypeParsed = true;
248 break;
249 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000250 ParmName = Tok.getIdentifierInfo();
251 ParmLoc = ConsumeToken();
252 break;
253
254 default:
255 break;
256 }
257
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +0000258 ExprVector ArgExprs;
Joey Gouly37453b92013-03-08 09:42:32 +0000259 bool isInvalid = false;
260 bool isParmType = false;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000261
Joey Gouly37453b92013-03-08 09:42:32 +0000262 if (!BuiltinType && !AttrName->isStr("vec_type_hint") &&
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000263 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
264 // Eat the comma.
265 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000266 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000267
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000268 // Parse the non-empty comma-separated list of expressions.
269 while (1) {
270 ExprResult ArgExpr(ParseAssignmentExpression());
271 if (ArgExpr.isInvalid()) {
272 SkipUntil(tok::r_paren);
273 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000274 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000275 ArgExprs.push_back(ArgExpr.release());
276 if (Tok.isNot(tok::comma))
277 break;
278 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000279 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000280 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000281 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
282 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
283 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000284 while (Tok.is(tok::identifier)) {
285 ConsumeToken();
286 if (Tok.is(tok::greater))
287 break;
288 if (Tok.is(tok::comma)) {
289 ConsumeToken();
290 continue;
291 }
292 }
293 if (Tok.isNot(tok::greater))
294 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000295 SkipUntil(tok::r_paren, false, true); // skip until ')'
296 }
Joey Gouly37453b92013-03-08 09:42:32 +0000297 } else if (AttrName->isStr("vec_type_hint")) {
298 if (T.get() && !T.isInvalid())
299 isParmType = true;
300 else {
301 if (Tok.is(tok::identifier))
302 ConsumeToken();
303 if (TypeParsed)
304 isInvalid = true;
305 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000306 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000307
308 SourceLocation RParen = Tok.getLocation();
Joey Gouly37453b92013-03-08 09:42:32 +0000309 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen) &&
310 !isInvalid) {
Michael Han45bed132012-10-04 16:42:52 +0000311 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Joey Gouly37453b92013-03-08 09:42:32 +0000312 if (isParmType) {
Joey Gouly37453b92013-03-08 09:42:32 +0000313 Attrs.addNewTypeAttr(AttrName, SourceRange(AttrLoc, RParen), ScopeName,
314 ScopeLoc, ParmName, ParmLoc, T.get(), Syntax);
315 } else {
316 AttributeList *attr = Attrs.addNew(
317 AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, ParmName,
318 ParmLoc, ArgExprs.data(), ArgExprs.size(), Syntax);
319 if (BuiltinType &&
320 attr->getKind() == AttributeList::AT_IBOutletCollection)
321 Diag(Tok, diag::err_iboutletcollection_builtintype);
322 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000323 }
324}
325
Chad Rosier8decdee2012-06-26 22:30:43 +0000326/// \brief Parses a single argument for a declspec, including the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000327/// surrounding parens.
Chad Rosier8decdee2012-06-26 22:30:43 +0000328void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000329 SourceLocation AttrNameLoc,
330 ParsedAttributes &Attrs)
331{
332 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000333 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000334 AttrName->getNameStart(), tok::r_paren))
335 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000336
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000337 ExprResult ArgExpr(ParseConstantExpression());
338 if (ArgExpr.isInvalid()) {
339 T.skipToEnd();
340 return;
341 }
342 Expr *ExprList = ArgExpr.take();
Chad Rosier8decdee2012-06-26 22:30:43 +0000343 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000344 &ExprList, 1, AttributeList::AS_Declspec);
345
346 T.consumeClose();
347}
348
Chad Rosier8decdee2012-06-26 22:30:43 +0000349/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000350/// arguments.
351bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
352 return llvm::StringSwitch<bool>(Ident->getName())
353 .Case("dllimport", true)
354 .Case("dllexport", true)
355 .Case("noreturn", true)
356 .Case("nothrow", true)
357 .Case("noinline", true)
358 .Case("naked", true)
359 .Case("appdomain", true)
360 .Case("process", true)
361 .Case("jitintrinsic", true)
362 .Case("noalias", true)
363 .Case("restrict", true)
364 .Case("novtable", true)
365 .Case("selectany", true)
366 .Case("thread", true)
367 .Default(false);
368}
369
Chad Rosier8decdee2012-06-26 22:30:43 +0000370/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000371/// parameters). Will return false if we properly handled the declspec, or
372/// true if it is an unknown declspec.
Chad Rosier8decdee2012-06-26 22:30:43 +0000373void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000374 SourceLocation Loc,
375 ParsedAttributes &Attrs) {
376 // Try to handle the easy case first -- these declspecs all take a single
377 // parameter as their argument.
378 if (llvm::StringSwitch<bool>(Ident->getName())
379 .Case("uuid", true)
380 .Case("align", true)
381 .Case("allocate", true)
382 .Default(false)) {
383 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
384 } else if (Ident->getName() == "deprecated") {
Chad Rosier8decdee2012-06-26 22:30:43 +0000385 // The deprecated declspec has an optional single argument, so we will
386 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000387 // not.
388 if (Tok.getKind() == tok::l_paren)
389 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
390 else
Chad Rosier8decdee2012-06-26 22:30:43 +0000391 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000392 AttributeList::AS_Declspec);
393 } else if (Ident->getName() == "property") {
394 // The property declspec is more complex in that it can take one or two
Chad Rosier8decdee2012-06-26 22:30:43 +0000395 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000396 // must be named get or put.
397 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000398 // For right now, we will just skip to the closing right paren of the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000399 // property expression.
400 //
401 // FIXME: we should deal with __declspec(property) at some point because it
402 // is used in the platform SDK headers for the Parallel Patterns Library
403 // and ATL.
404 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000405 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000406 Ident->getNameStart(), tok::r_paren))
407 return;
408 T.skipToEnd();
409 } else {
410 // We don't recognize this as a valid declspec, but instead of creating the
411 // attribute and allowing sema to warn about it, we will warn here instead.
412 // This is because some attributes have multiple spellings, but we need to
413 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosier8decdee2012-06-26 22:30:43 +0000414 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000415 // both locations.
416 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
417
418 // If there's an open paren, we should eat the open and close parens under
419 // the assumption that this unknown declspec has parameters.
420 BalancedDelimiterTracker T(*this, tok::l_paren);
421 if (!T.consumeOpen())
422 T.skipToEnd();
423 }
424}
425
Eli Friedmana23b4852009-06-08 07:21:15 +0000426/// [MS] decl-specifier:
427/// __declspec ( extended-decl-modifier-seq )
428///
429/// [MS] extended-decl-modifier-seq:
430/// extended-decl-modifier[opt]
431/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000432void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000433 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000434
Steve Narofff59e17e2008-12-24 20:59:21 +0000435 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000436 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000437 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000438 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000439 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000440
Chad Rosier8decdee2012-06-26 22:30:43 +0000441 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000442 // you can specify multiple attributes per declspec.
443 while (Tok.getKind() != tok::r_paren) {
444 // We expect either a well-known identifier or a generic string. Anything
445 // else is a malformed declspec.
446 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosier8decdee2012-06-26 22:30:43 +0000447 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000448 Tok.getKind() != tok::kw_restrict) {
449 Diag(Tok, diag::err_ms_declspec_type);
450 T.skipToEnd();
451 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000452 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000453
454 IdentifierInfo *AttrName;
455 SourceLocation AttrNameLoc;
456 if (IsString) {
457 SmallString<8> StrBuffer;
458 bool Invalid = false;
459 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
460 if (Invalid) {
461 T.skipToEnd();
462 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000463 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000464 AttrName = PP.getIdentifierInfo(Str);
465 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000466 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000467 AttrName = Tok.getIdentifierInfo();
468 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000469 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000470
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000471 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosier8decdee2012-06-26 22:30:43 +0000472 // If we have a generic string, we will allow it because there is no
473 // documented list of allowable string declspecs, but we know they exist
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000474 // (for instance, SAL declspecs in older versions of MSVC).
475 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000476 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000477 // arguments and can be turned into an attribute directly.
Chad Rosier8decdee2012-06-26 22:30:43 +0000478 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000479 0, 0, AttributeList::AS_Declspec);
480 else
481 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000482 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000483 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000484}
485
John McCall7f040a92010-12-24 02:08:15 +0000486void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000487 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000488 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000489 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000490 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Chad Rosierccbb4022012-12-21 21:27:13 +0000491 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000492 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
493 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000494 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith5cd532c2013-01-29 01:24:26 +0000495 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Eli Friedman290eeb02009-06-08 23:27:34 +0000496 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000497}
498
John McCall7f040a92010-12-24 02:08:15 +0000499void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000500 // Treat these like attributes
501 while (Tok.is(tok::kw___pascal)) {
502 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
503 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000504 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith5cd532c2013-01-29 01:24:26 +0000505 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000506 }
John McCall7f040a92010-12-24 02:08:15 +0000507}
508
Peter Collingbournef315fa82011-02-14 01:42:53 +0000509void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
510 // Treat these like attributes
511 while (Tok.is(tok::kw___kernel)) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000512 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbournef315fa82011-02-14 01:42:53 +0000513 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith5cd532c2013-01-29 01:24:26 +0000514 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
515 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000516 }
517}
518
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000519void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000520 // FIXME: The mapping from attribute spelling to semantics should be
521 // performed in Sema, not here.
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000522 SourceLocation Loc = Tok.getLocation();
523 switch(Tok.getKind()) {
524 // OpenCL qualifiers:
525 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000526 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000527 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000528 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000529 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000530 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000531
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000532 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000533 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000534 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000535 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000536 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000537
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000538 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000539 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000540 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000541 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000542 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000543
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000544 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000545 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000546 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000547 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000548 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000549
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000550 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000551 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000552 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000553 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000554 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000555
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000556 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000557 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000558 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000559 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000560 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000561
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000562 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000563 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000564 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000565 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000566 break;
567 default: break;
568 }
569}
570
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000571/// \brief Parse a version number.
572///
573/// version:
574/// simple-integer
575/// simple-integer ',' simple-integer
576/// simple-integer ',' simple-integer ',' simple-integer
577VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
578 Range = Tok.getLocation();
579
580 if (!Tok.is(tok::numeric_constant)) {
581 Diag(Tok, diag::err_expected_version);
582 SkipUntil(tok::comma, tok::r_paren, true, true, true);
583 return VersionTuple();
584 }
585
586 // Parse the major (and possibly minor and subminor) versions, which
587 // are stored in the numeric constant. We utilize a quirk of the
588 // lexer, which is that it handles something like 1.2.3 as a single
589 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000590 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000591 Buffer.resize(Tok.getLength()+1);
592 const char *ThisTokBegin = &Buffer[0];
593
594 // Get the spelling of the token, which eliminates trigraphs, etc.
595 bool Invalid = false;
596 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
597 if (Invalid)
598 return VersionTuple();
599
600 // Parse the major version.
601 unsigned AfterMajor = 0;
602 unsigned Major = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000603 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000604 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
605 ++AfterMajor;
606 }
607
608 if (AfterMajor == 0) {
609 Diag(Tok, diag::err_expected_version);
610 SkipUntil(tok::comma, tok::r_paren, true, true, true);
611 return VersionTuple();
612 }
613
614 if (AfterMajor == ActualLength) {
615 ConsumeToken();
616
617 // We only had a single version component.
618 if (Major == 0) {
619 Diag(Tok, diag::err_zero_version);
620 return VersionTuple();
621 }
622
623 return VersionTuple(Major);
624 }
625
626 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
627 Diag(Tok, diag::err_expected_version);
628 SkipUntil(tok::comma, tok::r_paren, true, true, true);
629 return VersionTuple();
630 }
631
632 // Parse the minor version.
633 unsigned AfterMinor = AfterMajor + 1;
634 unsigned Minor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000635 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000636 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
637 ++AfterMinor;
638 }
639
640 if (AfterMinor == ActualLength) {
641 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000642
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000643 // We had major.minor.
644 if (Major == 0 && Minor == 0) {
645 Diag(Tok, diag::err_zero_version);
646 return VersionTuple();
647 }
648
Chad Rosier8decdee2012-06-26 22:30:43 +0000649 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000650 }
651
652 // If what follows is not a '.', we have a problem.
653 if (ThisTokBegin[AfterMinor] != '.') {
654 Diag(Tok, diag::err_expected_version);
655 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosier8decdee2012-06-26 22:30:43 +0000656 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000657 }
658
659 // Parse the subminor version.
660 unsigned AfterSubminor = AfterMinor + 1;
661 unsigned Subminor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000662 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000663 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
664 ++AfterSubminor;
665 }
666
667 if (AfterSubminor != ActualLength) {
668 Diag(Tok, diag::err_expected_version);
669 SkipUntil(tok::comma, tok::r_paren, true, true, true);
670 return VersionTuple();
671 }
672 ConsumeToken();
673 return VersionTuple(Major, Minor, Subminor);
674}
675
676/// \brief Parse the contents of the "availability" attribute.
677///
678/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000679/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000680///
681/// platform:
682/// identifier
683///
684/// version-arg-list:
685/// version-arg
686/// version-arg ',' version-arg-list
687///
688/// version-arg:
689/// 'introduced' '=' version
690/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000691/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000692/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000693/// opt-message:
694/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000695void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
696 SourceLocation AvailabilityLoc,
697 ParsedAttributes &attrs,
698 SourceLocation *endLoc) {
699 SourceLocation PlatformLoc;
700 IdentifierInfo *Platform = 0;
701
702 enum { Introduced, Deprecated, Obsoleted, Unknown };
703 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000704 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000705
706 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000707 BalancedDelimiterTracker T(*this, tok::l_paren);
708 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000709 Diag(Tok, diag::err_expected_lparen);
710 return;
711 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000712
713 // Parse the platform name,
714 if (Tok.isNot(tok::identifier)) {
715 Diag(Tok, diag::err_availability_expected_platform);
716 SkipUntil(tok::r_paren);
717 return;
718 }
719 Platform = Tok.getIdentifierInfo();
720 PlatformLoc = ConsumeToken();
721
722 // Parse the ',' following the platform name.
723 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
724 return;
725
726 // If we haven't grabbed the pointers for the identifiers
727 // "introduced", "deprecated", and "obsoleted", do so now.
728 if (!Ident_introduced) {
729 Ident_introduced = PP.getIdentifierInfo("introduced");
730 Ident_deprecated = PP.getIdentifierInfo("deprecated");
731 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000732 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000733 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000734 }
735
736 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000737 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000738 do {
739 if (Tok.isNot(tok::identifier)) {
740 Diag(Tok, diag::err_availability_expected_change);
741 SkipUntil(tok::r_paren);
742 return;
743 }
744 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
745 SourceLocation KeywordLoc = ConsumeToken();
746
Douglas Gregorb53e4172011-03-26 03:35:55 +0000747 if (Keyword == Ident_unavailable) {
748 if (UnavailableLoc.isValid()) {
749 Diag(KeywordLoc, diag::err_availability_redundant)
750 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000751 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000752 UnavailableLoc = KeywordLoc;
753
754 if (Tok.isNot(tok::comma))
755 break;
756
757 ConsumeToken();
758 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000759 }
760
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000761 if (Tok.isNot(tok::equal)) {
762 Diag(Tok, diag::err_expected_equal_after)
763 << Keyword;
764 SkipUntil(tok::r_paren);
765 return;
766 }
767 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000768 if (Keyword == Ident_message) {
769 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000770 Diag(Tok, diag::err_expected_string_literal)
771 << /*Source='availability attribute'*/2;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000772 SkipUntil(tok::r_paren);
773 return;
774 }
775 MessageExpr = ParseStringLiteralExpression();
776 break;
777 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000778
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000779 SourceRange VersionRange;
780 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000781
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000782 if (Version.empty()) {
783 SkipUntil(tok::r_paren);
784 return;
785 }
786
787 unsigned Index;
788 if (Keyword == Ident_introduced)
789 Index = Introduced;
790 else if (Keyword == Ident_deprecated)
791 Index = Deprecated;
792 else if (Keyword == Ident_obsoleted)
793 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000794 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000795 Index = Unknown;
796
797 if (Index < Unknown) {
798 if (!Changes[Index].KeywordLoc.isInvalid()) {
799 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000800 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000801 << SourceRange(Changes[Index].KeywordLoc,
802 Changes[Index].VersionRange.getEnd());
803 }
804
805 Changes[Index].KeywordLoc = KeywordLoc;
806 Changes[Index].Version = Version;
807 Changes[Index].VersionRange = VersionRange;
808 } else {
809 Diag(KeywordLoc, diag::err_availability_unknown_change)
810 << Keyword << VersionRange;
811 }
812
813 if (Tok.isNot(tok::comma))
814 break;
815
816 ConsumeToken();
817 } while (true);
818
819 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000820 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000821 return;
822
823 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000824 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000825
Douglas Gregorb53e4172011-03-26 03:35:55 +0000826 // The 'unavailable' availability cannot be combined with any other
827 // availability changes. Make sure that hasn't happened.
828 if (UnavailableLoc.isValid()) {
829 bool Complained = false;
830 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
831 if (Changes[Index].KeywordLoc.isValid()) {
832 if (!Complained) {
833 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
834 << SourceRange(Changes[Index].KeywordLoc,
835 Changes[Index].VersionRange.getEnd());
836 Complained = true;
837 }
838
839 // Clear out the availability.
840 Changes[Index] = AvailabilityChange();
841 }
842 }
843 }
844
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000845 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000846 attrs.addNew(&Availability,
847 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000848 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000849 Platform, PlatformLoc,
850 Changes[Introduced],
851 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000852 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000853 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000854 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000855}
856
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000857
Bill Wendlingad017fa2012-12-20 19:22:21 +0000858// Late Parsed Attributes:
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000859// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
860
861void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
862
863void Parser::LateParsedClass::ParseLexedAttributes() {
864 Self->ParseLexedAttributes(*Class);
865}
866
867void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000868 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000869}
870
871/// Wrapper class which calls ParseLexedAttribute, after setting up the
872/// scope appropriately.
873void Parser::ParseLexedAttributes(ParsingClass &Class) {
874 // Deal with templates
875 // FIXME: Test cases to make sure this does the right thing for templates.
876 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
877 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
878 HasTemplateScope);
879 if (HasTemplateScope)
880 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
881
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000882 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000883 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000884 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000885 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
886 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
887
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000888 // Enter the scope of nested classes
889 if (!AlreadyHasClassScope)
890 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
891 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000892 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000893 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
894 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
895 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000896 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000897
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000898 if (!AlreadyHasClassScope)
899 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
900 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000901}
902
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000903
904/// \brief Parse all attributes in LAs, and attach them to Decl D.
905void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
906 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +0000907 assert(LAs.parseSoon() &&
908 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000909 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +0000910 if (D)
911 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000912 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000913 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000914 }
915 LAs.clear();
916}
917
918
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000919/// \brief Finish parsing an attribute for which parsing was delayed.
920/// This will be called at the end of parsing a class declaration
921/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +0000922/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000923/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000924void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
925 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000926 // Save the current token position.
927 SourceLocation OrigLoc = Tok.getLocation();
928
929 // Append the current token at the end of the new token stream so that it
930 // doesn't get lost.
931 LA.Toks.push_back(Tok);
932 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
933 // Consume the previously pushed token.
Argyrios Kyrtzidisab2d09b2013-03-27 23:58:17 +0000934 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000935
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000936 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smithcd8ab512013-01-17 01:30:42 +0000937 // FIXME: Do not warn on C++11 attributes, once we start supporting
938 // them here.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000939 Diag(Tok, diag::warn_attribute_on_function_definition)
940 << LA.AttrName.getName();
941 }
942
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000943 ParsedAttributes Attrs(AttrFactory);
944 SourceLocation endLoc;
945
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000946 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000947 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000948 NamedDecl *ND = dyn_cast<NamedDecl>(D);
949 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000950
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000951 // Allow 'this' within late-parsed attributes.
952 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
953 /*TypeQuals=*/0,
954 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000955
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000956 if (LA.Decls.size() == 1) {
957 // If the Decl is templatized, add template parameters to scope.
958 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
959 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
960 if (HasTemplateScope)
961 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000962
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000963 // If the Decl is on a function, add function parameters to the scope.
964 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
965 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
966 if (HasFunScope)
967 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000968
Michael Han6880f492012-10-03 01:56:22 +0000969 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000970 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000971
972 if (HasFunScope) {
973 Actions.ActOnExitFunctionContext();
974 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
975 }
976 if (HasTemplateScope) {
977 TempScope.Exit();
978 }
979 } else {
980 // If there are multiple decls, then the decl cannot be within the
981 // function scope.
Michael Han6880f492012-10-03 01:56:22 +0000982 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000983 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000984 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000985 } else {
986 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000987 }
988
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000989 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
990 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
991 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000992
993 if (Tok.getLocation() != OrigLoc) {
994 // Due to a parsing error, we either went over the cached tokens or
995 // there are still cached tokens left, so we skip the leftover tokens.
996 // Since this is an uncommon situation that should be avoided, use the
997 // expensive isBeforeInTranslationUnit call.
998 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
999 OrigLoc))
1000 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001001 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001002 }
1003}
1004
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001005/// \brief Wrapper around a case statement checking if AttrName is
1006/// one of the thread safety attributes
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001007bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001008 return llvm::StringSwitch<bool>(AttrName)
1009 .Case("guarded_by", true)
1010 .Case("guarded_var", true)
1011 .Case("pt_guarded_by", true)
1012 .Case("pt_guarded_var", true)
1013 .Case("lockable", true)
1014 .Case("scoped_lockable", true)
1015 .Case("no_thread_safety_analysis", true)
1016 .Case("acquired_after", true)
1017 .Case("acquired_before", true)
1018 .Case("exclusive_lock_function", true)
1019 .Case("shared_lock_function", true)
1020 .Case("exclusive_trylock_function", true)
1021 .Case("shared_trylock_function", true)
1022 .Case("unlock_function", true)
1023 .Case("lock_returned", true)
1024 .Case("locks_excluded", true)
1025 .Case("exclusive_locks_required", true)
1026 .Case("shared_locks_required", true)
1027 .Default(false);
1028}
1029
1030/// \brief Parse the contents of thread safety attributes. These
1031/// should always be parsed as an expression list.
1032///
1033/// We need to special case the parsing due to the fact that if the first token
1034/// of the first argument is an identifier, the main parse loop will store
1035/// that token as a "parameter" and the rest of
1036/// the arguments will be added to a list of "arguments". However,
1037/// subsequent tokens in the first argument are lost. We instead parse each
1038/// argument as an expression and add all arguments to the list of "arguments".
1039/// In future, we will take advantage of this special case to also
1040/// deal with some argument scoping issues here (for example, referring to a
1041/// function parameter in the attribute on that function).
1042void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1043 SourceLocation AttrNameLoc,
1044 ParsedAttributes &Attrs,
1045 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001046 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001047
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001048 BalancedDelimiterTracker T(*this, tok::l_paren);
1049 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001050
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001051 ExprVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001052 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001053
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001054 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001055 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinsed4330b2013-02-07 19:01:07 +00001056 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001057 ExprResult ArgExpr(ParseAssignmentExpression());
1058 if (ArgExpr.isInvalid()) {
1059 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001060 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001061 break;
1062 } else {
1063 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001064 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001065 if (Tok.isNot(tok::comma))
1066 break;
1067 ConsumeToken(); // Eat the comma, move to the next argument
1068 }
1069 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001070 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001071 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001072 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001073 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001074 if (EndLoc)
1075 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001076}
1077
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001078void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1079 SourceLocation AttrNameLoc,
1080 ParsedAttributes &Attrs,
1081 SourceLocation *EndLoc) {
1082 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1083
1084 BalancedDelimiterTracker T(*this, tok::l_paren);
1085 T.consumeOpen();
1086
1087 if (Tok.isNot(tok::identifier)) {
1088 Diag(Tok, diag::err_expected_ident);
1089 T.skipToEnd();
1090 return;
1091 }
1092 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1093 SourceLocation ArgumentKindLoc = ConsumeToken();
1094
1095 if (Tok.isNot(tok::comma)) {
1096 Diag(Tok, diag::err_expected_comma);
1097 T.skipToEnd();
1098 return;
1099 }
1100 ConsumeToken();
1101
1102 SourceRange MatchingCTypeRange;
1103 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1104 if (MatchingCType.isInvalid()) {
1105 T.skipToEnd();
1106 return;
1107 }
1108
1109 bool LayoutCompatible = false;
1110 bool MustBeNull = false;
1111 while (Tok.is(tok::comma)) {
1112 ConsumeToken();
1113 if (Tok.isNot(tok::identifier)) {
1114 Diag(Tok, diag::err_expected_ident);
1115 T.skipToEnd();
1116 return;
1117 }
1118 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1119 if (Flag->isStr("layout_compatible"))
1120 LayoutCompatible = true;
1121 else if (Flag->isStr("must_be_null"))
1122 MustBeNull = true;
1123 else {
1124 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1125 T.skipToEnd();
1126 return;
1127 }
1128 ConsumeToken(); // consume flag
1129 }
1130
1131 if (!T.consumeClose()) {
1132 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1133 ArgumentKind, ArgumentKindLoc,
1134 MatchingCType.release(), LayoutCompatible,
1135 MustBeNull, AttributeList::AS_GNU);
1136 }
1137
1138 if (EndLoc)
1139 *EndLoc = T.getCloseLocation();
1140}
1141
Richard Smith6ee326a2012-04-10 01:32:12 +00001142/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1143/// of a C++11 attribute-specifier in a location where an attribute is not
1144/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1145/// situation.
1146///
1147/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1148/// this doesn't appear to actually be an attribute-specifier, and the caller
1149/// should try to parse it.
1150bool Parser::DiagnoseProhibitedCXX11Attribute() {
1151 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1152
1153 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1154 case CAK_NotAttributeSpecifier:
1155 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1156 return false;
1157
1158 case CAK_InvalidAttributeSpecifier:
1159 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1160 return false;
1161
1162 case CAK_AttributeSpecifier:
1163 // Parse and discard the attributes.
1164 SourceLocation BeginLoc = ConsumeBracket();
1165 ConsumeBracket();
1166 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1167 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1168 SourceLocation EndLoc = ConsumeBracket();
1169 Diag(BeginLoc, diag::err_attributes_not_allowed)
1170 << SourceRange(BeginLoc, EndLoc);
1171 return true;
1172 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001173 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001174}
1175
Richard Smith975d52c2013-02-20 01:17:14 +00001176/// \brief We have found the opening square brackets of a C++11
1177/// attribute-specifier in a location where an attribute is not permitted, but
1178/// we know where the attributes ought to be written. Parse them anyway, and
1179/// provide a fixit moving them to the right place.
Richard Smith05321402013-02-19 23:47:15 +00001180void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1181 SourceLocation CorrectLocation) {
1182 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1183 Tok.is(tok::kw_alignas));
1184
1185 // Consume the attributes.
1186 SourceLocation Loc = Tok.getLocation();
1187 ParseCXX11Attributes(Attrs);
1188 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1189
1190 Diag(Loc, diag::err_attributes_not_allowed)
1191 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1192 << FixItHint::CreateRemoval(AttrRange);
1193}
1194
John McCall7f040a92010-12-24 02:08:15 +00001195void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1196 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1197 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001198}
1199
Michael Hanf64231e2012-11-06 19:34:54 +00001200void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1201 AttributeList *AttrList = attrs.getList();
1202 while (AttrList) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001203 if (AttrList->isCXX11Attribute()) {
Richard Smithd03de6a2013-01-29 10:02:16 +00001204 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Hanf64231e2012-11-06 19:34:54 +00001205 << AttrList->getName();
1206 AttrList->setInvalid();
1207 }
1208 AttrList = AttrList->getNext();
1209 }
1210}
1211
Reid Spencer5f016e22007-07-11 17:01:13 +00001212/// ParseDeclaration - Parse a full 'declaration', which consists of
1213/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001214/// 'Context' should be a Declarator::TheContext value. This returns the
1215/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001216///
1217/// declaration: [C99 6.7]
1218/// block-declaration ->
1219/// simple-declaration
1220/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001221/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001222/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001223/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001224/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001225/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001226/// others... [FIXME]
1227///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001228Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1229 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001230 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001231 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001232 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001233 // Must temporarily exit the objective-c container scope for
1234 // parsing c none objective-c decls.
1235 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001236
John McCalld226f652010-08-21 09:40:31 +00001237 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001238 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001239 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001240 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001241 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001242 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001243 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001244 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001245 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001246 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001247 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001248 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001249 SourceLocation InlineLoc = ConsumeToken();
1250 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1251 break;
1252 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001253 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001254 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001255 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001256 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001257 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001258 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001259 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001260 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001261 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001262 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001263 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001264 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001265 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001266 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001267 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001268 default:
John McCall7f040a92010-12-24 02:08:15 +00001269 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001270 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001271
Chris Lattner682bf922009-03-29 16:50:03 +00001272 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001273 // single decl, convert it now. Alias declarations can also declare a type;
1274 // include that too if it is present.
1275 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001276}
1277
1278/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1279/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001280/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1281/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001282///[C90/C++]init-declarator-list ';' [TODO]
1283/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001284///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001285/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001286/// attribute-specifier-seq[opt] type-specifier-seq declarator
1287///
Chris Lattnercd147752009-03-29 17:27:48 +00001288/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001289/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001290///
1291/// If FRI is non-null, we might be parsing a for-range-declaration instead
1292/// of a simple-declaration. If we find that we are, we also parse the
1293/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001294Parser::DeclGroupPtrTy
1295Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1296 SourceLocation &DeclEnd,
Richard Smith68ea3ae2013-02-22 09:06:26 +00001297 ParsedAttributesWithRange &Attrs,
Sean Hunt2edf0a22012-06-23 05:07:58 +00001298 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001299 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001300 ParsingDeclSpec DS(*this);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001301
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001302 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001303 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001304
Reid Spencer5f016e22007-07-11 17:01:13 +00001305 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1306 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001307 if (Tok.is(tok::semi)) {
Richard Smith68ea3ae2013-02-22 09:06:26 +00001308 ProhibitAttributes(Attrs);
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001309 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001310 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001311 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001312 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001313 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001314 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001316
Richard Smith68ea3ae2013-02-22 09:06:26 +00001317 DS.takeAttributesFrom(Attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00001318 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001319}
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Richard Smith0706df42011-10-19 21:33:05 +00001321/// Returns true if this might be the start of a declarator, or a common typo
1322/// for a declarator.
1323bool Parser::MightBeDeclarator(unsigned Context) {
1324 switch (Tok.getKind()) {
1325 case tok::annot_cxxscope:
1326 case tok::annot_template_id:
1327 case tok::caret:
1328 case tok::code_completion:
1329 case tok::coloncolon:
1330 case tok::ellipsis:
1331 case tok::kw___attribute:
1332 case tok::kw_operator:
1333 case tok::l_paren:
1334 case tok::star:
1335 return true;
1336
1337 case tok::amp:
1338 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001339 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001340
Richard Smith1c94c162012-01-09 22:31:44 +00001341 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith80ad52f2013-01-02 11:42:31 +00001342 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smith1c94c162012-01-09 22:31:44 +00001343 NextToken().is(tok::l_square);
1344
1345 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001346 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001347
Richard Smith0706df42011-10-19 21:33:05 +00001348 case tok::identifier:
1349 switch (NextToken().getKind()) {
1350 case tok::code_completion:
1351 case tok::coloncolon:
1352 case tok::comma:
1353 case tok::equal:
1354 case tok::equalequal: // Might be a typo for '='.
1355 case tok::kw_alignas:
1356 case tok::kw_asm:
1357 case tok::kw___attribute:
1358 case tok::l_brace:
1359 case tok::l_paren:
1360 case tok::l_square:
1361 case tok::less:
1362 case tok::r_brace:
1363 case tok::r_paren:
1364 case tok::r_square:
1365 case tok::semi:
1366 return true;
1367
1368 case tok::colon:
1369 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001370 // and in block scope it's probably a label. Inside a class definition,
1371 // this is a bit-field.
1372 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001373 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001374
1375 case tok::identifier: // Possible virt-specifier.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001376 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001377
1378 default:
1379 return false;
1380 }
1381
1382 default:
1383 return false;
1384 }
1385}
1386
Richard Smith994d73f2012-04-11 20:59:20 +00001387/// Skip until we reach something which seems like a sensible place to pick
1388/// up parsing after a malformed declaration. This will sometimes stop sooner
1389/// than SkipUntil(tok::r_brace) would, but will never stop later.
1390void Parser::SkipMalformedDecl() {
1391 while (true) {
1392 switch (Tok.getKind()) {
1393 case tok::l_brace:
1394 // Skip until matching }, then stop. We've probably skipped over
1395 // a malformed class or function definition or similar.
1396 ConsumeBrace();
1397 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1398 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1399 // This declaration isn't over yet. Keep skipping.
1400 continue;
1401 }
1402 if (Tok.is(tok::semi))
1403 ConsumeToken();
1404 return;
1405
1406 case tok::l_square:
1407 ConsumeBracket();
1408 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1409 continue;
1410
1411 case tok::l_paren:
1412 ConsumeParen();
1413 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1414 continue;
1415
1416 case tok::r_brace:
1417 return;
1418
1419 case tok::semi:
1420 ConsumeToken();
1421 return;
1422
1423 case tok::kw_inline:
1424 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001425 // a good place to pick back up parsing, except in an Objective-C
1426 // @interface context.
1427 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1428 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001429 return;
1430 break;
1431
1432 case tok::kw_namespace:
1433 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001434 // place to pick back up parsing, except in an Objective-C
1435 // @interface context.
1436 if (Tok.isAtStartOfLine() &&
1437 (!ParsingInObjCContainer || CurParsedObjCImpl))
1438 return;
1439 break;
1440
1441 case tok::at:
1442 // @end is very much like } in Objective-C contexts.
1443 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1444 ParsingInObjCContainer)
1445 return;
1446 break;
1447
1448 case tok::minus:
1449 case tok::plus:
1450 // - and + probably start new method declarations in Objective-C contexts.
1451 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001452 return;
1453 break;
1454
1455 case tok::eof:
1456 return;
1457
1458 default:
1459 break;
1460 }
1461
1462 ConsumeAnyToken();
1463 }
1464}
1465
John McCalld8ac0572009-11-03 19:26:08 +00001466/// ParseDeclGroup - Having concluded that this is either a function
1467/// definition or a group of object declarations, actually parse the
1468/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001469Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1470 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001471 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001472 SourceLocation *DeclEnd,
1473 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001474 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001475 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001476 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001477
John McCalld8ac0572009-11-03 19:26:08 +00001478 // Bail out if the first declarator didn't seem well-formed.
1479 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001480 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001481 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001482 }
Mike Stump1eb44332009-09-09 15:08:12 +00001483
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001484 // Save late-parsed attributes for now; they need to be parsed in the
1485 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001486 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1487 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001488 if (D.isFunctionDeclarator())
1489 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1490
Chris Lattnerc82daef2010-07-11 22:24:20 +00001491 // Check to see if we have a function *definition* which must have a body.
1492 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1493 // Look at the next token to make sure that this isn't a function
1494 // declaration. We have to check this because __attribute__ might be the
1495 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001496 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001497
Chris Lattner004659a2010-07-11 22:42:07 +00001498 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001499 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1500 Diag(Tok, diag::err_function_declared_typedef);
1501
1502 // Recover by treating the 'typedef' as spurious.
1503 DS.ClearStorageClassSpecs();
1504 }
1505
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001506 Decl *TheDecl =
1507 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001508 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001509 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001510
Chris Lattner004659a2010-07-11 22:42:07 +00001511 if (isDeclarationSpecifier()) {
1512 // If there is an invalid declaration specifier right after the function
1513 // prototype, then we must be in a missing semicolon case where this isn't
1514 // actually a body. Just fall through into the code that handles it as a
1515 // prototype, and let the top-level code handle the erroneous declspec
1516 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001517 } else {
1518 Diag(Tok, diag::err_expected_fn_body);
1519 SkipUntil(tok::semi);
1520 return DeclGroupPtrTy();
1521 }
1522 }
1523
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001524 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001525 return DeclGroupPtrTy();
1526
1527 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1528 // must parse and analyze the for-range-initializer before the declaration is
1529 // analyzed.
Douglas Gregor12849d02013-04-08 20:52:24 +00001530 //
1531 // Handle the Objective-C for-in loop variable similarly, although we
1532 // don't need to parse the container in advance.
1533 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1534 bool IsForRangeLoop = false;
1535 if (Tok.is(tok::colon)) {
1536 IsForRangeLoop = true;
1537 FRI->ColonLoc = ConsumeToken();
1538 if (Tok.is(tok::l_brace))
1539 FRI->RangeExpr = ParseBraceInitializer();
1540 else
1541 FRI->RangeExpr = ParseExpression();
1542 }
1543
Richard Smithad762fc2011-04-14 22:09:26 +00001544 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor12849d02013-04-08 20:52:24 +00001545 if (IsForRangeLoop)
1546 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001547 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001548 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001549 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1550 }
1551
Chris Lattner5f9e2722011-07-23 10:55:15 +00001552 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001553 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001554 if (LateParsedAttrs.size() > 0)
1555 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001556 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001557 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001558 DeclsInGroup.push_back(FirstDecl);
1559
Richard Smith0706df42011-10-19 21:33:05 +00001560 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001561
John McCalld8ac0572009-11-03 19:26:08 +00001562 // If we don't have a comma, it is either the end of the list (a ';') or an
1563 // error, bail out.
1564 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001565 SourceLocation CommaLoc = ConsumeToken();
1566
1567 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1568 // This comma was followed by a line-break and something which can't be
1569 // the start of a declarator. The comma was probably a typo for a
1570 // semicolon.
1571 Diag(CommaLoc, diag::err_expected_semi_declaration)
1572 << FixItHint::CreateReplacement(CommaLoc, ";");
1573 ExpectSemi = false;
1574 break;
1575 }
John McCalld8ac0572009-11-03 19:26:08 +00001576
1577 // Parse the next declarator.
1578 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001579 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001580
1581 // Accept attributes in an init-declarator. In the first declarator in a
1582 // declaration, these would be part of the declspec. In subsequent
1583 // declarators, they become part of the declarator itself, so that they
1584 // don't apply to declarators after *this* one. Examples:
1585 // short __attribute__((common)) var; -> declspec
1586 // short var __attribute__((common)); -> declarator
1587 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001588 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001589
1590 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001591 if (!D.isInvalidType()) {
1592 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1593 D.complete(ThisDecl);
1594 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001595 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001596 }
John McCalld8ac0572009-11-03 19:26:08 +00001597 }
1598
1599 if (DeclEnd)
1600 *DeclEnd = Tok.getLocation();
1601
Richard Smith0706df42011-10-19 21:33:05 +00001602 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001603 ExpectAndConsumeSemi(Context == Declarator::FileContext
1604 ? diag::err_invalid_token_after_toplevel_declarator
1605 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001606 // Okay, there was no semicolon and one was expected. If we see a
1607 // declaration specifier, just assume it was missing and continue parsing.
1608 // Otherwise things are very confused and we skip to recover.
1609 if (!isDeclarationSpecifier()) {
1610 SkipUntil(tok::r_brace, true, true);
1611 if (Tok.is(tok::semi))
1612 ConsumeToken();
1613 }
John McCalld8ac0572009-11-03 19:26:08 +00001614 }
1615
Douglas Gregor23c94db2010-07-02 17:43:08 +00001616 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001617 DeclsInGroup.data(),
1618 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001619}
1620
Richard Smithad762fc2011-04-14 22:09:26 +00001621/// Parse an optional simple-asm-expr and attributes, and attach them to a
1622/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001623bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001624 // If a simple-asm-expr is present, parse it.
1625 if (Tok.is(tok::kw_asm)) {
1626 SourceLocation Loc;
1627 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1628 if (AsmLabel.isInvalid()) {
1629 SkipUntil(tok::semi, true, true);
1630 return true;
1631 }
1632
1633 D.setAsmLabel(AsmLabel.release());
1634 D.SetRangeEnd(Loc);
1635 }
1636
1637 MaybeParseGNUAttributes(D);
1638 return false;
1639}
1640
Douglas Gregor1426e532009-05-12 21:31:51 +00001641/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1642/// declarator'. This method parses the remainder of the declaration
1643/// (including any attributes or initializer, among other things) and
1644/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001645///
Reid Spencer5f016e22007-07-11 17:01:13 +00001646/// init-declarator: [C99 6.7]
1647/// declarator
1648/// declarator '=' initializer
1649/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1650/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001651/// [C++] declarator initializer[opt]
1652///
1653/// [C++] initializer:
1654/// [C++] '=' initializer-clause
1655/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001656/// [C++0x] '=' 'default' [TODO]
1657/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001658/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001659///
1660/// According to the standard grammar, =default and =delete are function
1661/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001662///
John McCalld226f652010-08-21 09:40:31 +00001663Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001664 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001665 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001666 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001667
Richard Smithad762fc2011-04-14 22:09:26 +00001668 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1669}
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Richard Smithad762fc2011-04-14 22:09:26 +00001671Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1672 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001673 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001674 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001675 switch (TemplateInfo.Kind) {
1676 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001677 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001678 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001679
Douglas Gregord5a423b2009-09-25 18:43:00 +00001680 case ParsedTemplateInfo::Template:
1681 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001682 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001683 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001684 D);
1685 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001686
Douglas Gregord5a423b2009-09-25 18:43:00 +00001687 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001688 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001689 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001690 TemplateInfo.ExternLoc,
1691 TemplateInfo.TemplateLoc,
1692 D);
1693 if (ThisRes.isInvalid()) {
1694 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001695 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001696 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001697
Douglas Gregord5a423b2009-09-25 18:43:00 +00001698 ThisDecl = ThisRes.get();
1699 break;
1700 }
1701 }
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Richard Smith34b41d92011-02-20 03:19:35 +00001703 bool TypeContainsAuto =
1704 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1705
Douglas Gregor1426e532009-05-12 21:31:51 +00001706 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001707 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001708 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001709 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001710 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001711 if (D.isFunctionDeclarator())
1712 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1713 << 1 /* delete */;
1714 else
1715 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001716 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001717 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001718 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1719 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001720 else
1721 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001722 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001723 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001724 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001725 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001726 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001727
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001728 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001729 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001730 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001731 cutOffParsing();
1732 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001733 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001734
John McCall60d7b3a2010-08-24 06:29:42 +00001735 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001736
David Blaikie4e4d0842012-03-11 07:00:24 +00001737 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001738 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001739 ExitScope();
1740 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001741
Douglas Gregor1426e532009-05-12 21:31:51 +00001742 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001743 SkipUntil(tok::comma, true, true);
1744 Actions.ActOnInitializerError(ThisDecl);
1745 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001746 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1747 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001748 }
1749 } else if (Tok.is(tok::l_paren)) {
1750 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001751 BalancedDelimiterTracker T(*this, tok::l_paren);
1752 T.consumeOpen();
1753
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001754 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001755 CommaLocsTy CommaLocs;
1756
David Blaikie4e4d0842012-03-11 07:00:24 +00001757 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001758 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001759 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001760 }
1761
Douglas Gregor1426e532009-05-12 21:31:51 +00001762 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001763 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001764 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001765
David Blaikie4e4d0842012-03-11 07:00:24 +00001766 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001767 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001768 ExitScope();
1769 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001770 } else {
1771 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001772 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001773
1774 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1775 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001776
David Blaikie4e4d0842012-03-11 07:00:24 +00001777 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001778 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001779 ExitScope();
1780 }
1781
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001782 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1783 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001784 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001785 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1786 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001787 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001788 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001789 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001790 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001791 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1792
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001793 if (D.getCXXScopeSpec().isSet()) {
1794 EnterScope(0);
1795 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1796 }
1797
1798 ExprResult Init(ParseBraceInitializer());
1799
1800 if (D.getCXXScopeSpec().isSet()) {
1801 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1802 ExitScope();
1803 }
1804
1805 if (Init.isInvalid()) {
1806 Actions.ActOnInitializerError(ThisDecl);
1807 } else
1808 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1809 /*DirectInit=*/true, TypeContainsAuto);
1810
Douglas Gregor1426e532009-05-12 21:31:51 +00001811 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001812 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001813 }
1814
Richard Smith483b9f32011-02-21 20:05:19 +00001815 Actions.FinalizeDeclaration(ThisDecl);
1816
Douglas Gregor1426e532009-05-12 21:31:51 +00001817 return ThisDecl;
1818}
1819
Reid Spencer5f016e22007-07-11 17:01:13 +00001820/// ParseSpecifierQualifierList
1821/// specifier-qualifier-list:
1822/// type-specifier specifier-qualifier-list[opt]
1823/// type-qualifier specifier-qualifier-list[opt]
1824/// [GNU] attributes specifier-qualifier-list[opt]
1825///
Richard Smith69730c12012-03-12 07:56:15 +00001826void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1827 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001828 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1829 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001830 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001831 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Reid Spencer5f016e22007-07-11 17:01:13 +00001833 // Validate declspec for type-name.
1834 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001835 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1836 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001837 Diag(Tok, diag::err_expected_type);
1838 DS.SetTypeSpecError();
1839 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1840 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001841 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001842 if (!DS.hasTypeSpecifier())
1843 DS.SetTypeSpecError();
1844 }
Mike Stump1eb44332009-09-09 15:08:12 +00001845
Reid Spencer5f016e22007-07-11 17:01:13 +00001846 // Issue diagnostic and remove storage class if present.
1847 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1848 if (DS.getStorageClassSpecLoc().isValid())
1849 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1850 else
1851 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1852 DS.ClearStorageClassSpecs();
1853 }
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Reid Spencer5f016e22007-07-11 17:01:13 +00001855 // Issue diagnostic and remove function specfier if present.
1856 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001857 if (DS.isInlineSpecified())
1858 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1859 if (DS.isVirtualSpecified())
1860 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1861 if (DS.isExplicitSpecified())
1862 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001863 DS.ClearFunctionSpecs();
1864 }
Richard Smith69730c12012-03-12 07:56:15 +00001865
1866 // Issue diagnostic and remove constexpr specfier if present.
1867 if (DS.isConstexprSpecified()) {
1868 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1869 DS.ClearConstexprSpec();
1870 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001871}
1872
Chris Lattnerc199ab32009-04-12 20:42:31 +00001873/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1874/// specified token is valid after the identifier in a declarator which
1875/// immediately follows the declspec. For example, these things are valid:
1876///
1877/// int x [ 4]; // direct-declarator
1878/// int x ( int y); // direct-declarator
1879/// int(int x ) // direct-declarator
1880/// int x ; // simple-declaration
1881/// int x = 17; // init-declarator-list
1882/// int x , y; // init-declarator-list
1883/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001884/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001885/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001886///
1887/// This is not, because 'x' does not immediately follow the declspec (though
1888/// ')' happens to be valid anyway).
1889/// int (x)
1890///
1891static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1892 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1893 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001894 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001895}
1896
Chris Lattnere40c2952009-04-14 21:34:55 +00001897
1898/// ParseImplicitInt - This method is called when we have an non-typename
1899/// identifier in a declspec (which normally terminates the decl spec) when
1900/// the declspec has no type specifier. In this case, the declspec is either
1901/// malformed or is "implicit int" (in K&R and C89).
1902///
1903/// This method handles diagnosing this prettily and returns false if the
1904/// declspec is done being processed. If it recovers and thinks there may be
1905/// other pieces of declspec after it, it returns true.
1906///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001907bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001908 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00001909 AccessSpecifier AS, DeclSpecContext DSC,
1910 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001911 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001912
Chris Lattnere40c2952009-04-14 21:34:55 +00001913 SourceLocation Loc = Tok.getLocation();
1914 // If we see an identifier that is not a type name, we normally would
1915 // parse it as the identifer being declared. However, when a typename
1916 // is typo'd or the definition is not included, this will incorrectly
1917 // parse the typename as the identifier name and fall over misparsing
1918 // later parts of the diagnostic.
1919 //
1920 // As such, we try to do some look-ahead in cases where this would
1921 // otherwise be an "implicit-int" case to see if this is invalid. For
1922 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1923 // an identifier with implicit int, we'd get a parse error because the
1924 // next token is obviously invalid for a type. Parse these as a case
1925 // with an invalid type specifier.
1926 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Chris Lattnere40c2952009-04-14 21:34:55 +00001928 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001929 // error, do lookahead to try to do better recovery. This never applies
1930 // within a type specifier. Outside of C++, we allow this even if the
1931 // language doesn't "officially" support implicit int -- we support
1932 // implicit int as an extension in C99 and C11. Allegedly, MS also
1933 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001934 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001935 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001936 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001937 // If this token is valid for implicit int, e.g. "static x = 4", then
1938 // we just avoid eating the identifier, so it will be parsed as the
1939 // identifier in the declarator.
1940 return false;
1941 }
Mike Stump1eb44332009-09-09 15:08:12 +00001942
Richard Smith827adaf2012-05-15 21:01:51 +00001943 if (getLangOpts().CPlusPlus &&
1944 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1945 // Don't require a type specifier if we have the 'auto' storage class
1946 // specifier in C++98 -- we'll promote it to a type specifier.
1947 return false;
1948 }
1949
Chris Lattnere40c2952009-04-14 21:34:55 +00001950 // Otherwise, if we don't consume this token, we are going to emit an
1951 // error anyway. Try to recover from various common problems. Check
1952 // to see if this was a reference to a tag name without a tag specified.
1953 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001954 //
1955 // C++ doesn't need this, and isTagName doesn't take SS.
1956 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001957 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001958 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001959
Douglas Gregor23c94db2010-07-02 17:43:08 +00001960 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001961 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001962 case DeclSpec::TST_enum:
1963 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1964 case DeclSpec::TST_union:
1965 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1966 case DeclSpec::TST_struct:
1967 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00001968 case DeclSpec::TST_interface:
1969 TagName="__interface"; FixitTagName = "__interface ";
1970 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001971 case DeclSpec::TST_class:
1972 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001973 }
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Chris Lattnerf4382f52009-04-14 22:17:06 +00001975 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001976 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1977 LookupResult R(Actions, TokenName, SourceLocation(),
1978 Sema::LookupOrdinaryName);
1979
Chris Lattnerf4382f52009-04-14 22:17:06 +00001980 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001981 << TokenName << TagName << getLangOpts().CPlusPlus
1982 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1983
1984 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1985 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1986 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001987 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001988 << TokenName << TagName;
1989 }
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Chris Lattnerf4382f52009-04-14 22:17:06 +00001991 // Parse this as a tag as if the missing tag were present.
1992 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001993 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001994 else
Richard Smith69730c12012-03-12 07:56:15 +00001995 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00001996 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001997 return true;
1998 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001999 }
Mike Stump1eb44332009-09-09 15:08:12 +00002000
Richard Smith8f0a7e72012-05-15 21:29:55 +00002001 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00002002 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00002003 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2004 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00002005 // Look ahead to the next token to try to figure out what this declaration
2006 // was supposed to be.
2007 switch (NextToken().getKind()) {
2008 case tok::comma:
2009 case tok::equal:
2010 case tok::kw_asm:
2011 case tok::l_brace:
2012 case tok::l_square:
2013 case tok::semi:
2014 // This looks like a variable declaration. The type is probably missing.
2015 // We're done parsing decl-specifiers.
2016 return false;
2017
2018 case tok::l_paren: {
2019 // static x(4); // 'x' is not a type
2020 // x(int n); // 'x' is not a type
2021 // x (*p)[]; // 'x' is a type
2022 //
2023 // Since we're in an error case (or the rare 'implicit int in C++' MS
2024 // extension), we can afford to perform a tentative parse to determine
2025 // which case we're in.
2026 TentativeParsingAction PA(*this);
2027 ConsumeToken();
2028 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2029 PA.Revert();
2030 if (TPR == TPResult::False())
2031 return false;
2032 // The identifier is followed by a parenthesized declarator.
2033 // It's supposed to be a type.
2034 break;
2035 }
2036
2037 default:
2038 // This is probably supposed to be a type. This includes cases like:
2039 // int f(itn);
2040 // struct S { unsinged : 4; };
2041 break;
2042 }
2043 }
2044
Chad Rosier8decdee2012-06-26 22:30:43 +00002045 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00002046 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00002047 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002048 IdentifierInfo *II = Tok.getIdentifierInfo();
2049 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00002050 // The action emitted a diagnostic, so we don't have to.
2051 if (T) {
2052 // The action has suggested that the type T could be used. Set that as
2053 // the type in the declaration specifiers, consume the would-be type
2054 // name token, and we're done.
2055 const char *PrevSpec;
2056 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00002057 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00002058 DS.SetRangeEnd(Tok.getLocation());
2059 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002060 // There may be other declaration specifiers after this.
2061 return true;
2062 } else if (II != Tok.getIdentifierInfo()) {
2063 // If no type was suggested, the correction is to a keyword
2064 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002065 // There may be other declaration specifiers after this.
2066 return true;
2067 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002068
Douglas Gregora786fdb2009-10-13 23:27:22 +00002069 // Fall through; the action had no suggestion for us.
2070 } else {
2071 // The action did not emit a diagnostic, so emit one now.
2072 SourceRange R;
2073 if (SS) R = SS->getRange();
2074 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2075 }
Mike Stump1eb44332009-09-09 15:08:12 +00002076
Douglas Gregora786fdb2009-10-13 23:27:22 +00002077 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002078 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002079 DS.SetRangeEnd(Tok.getLocation());
2080 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002081
Chris Lattnere40c2952009-04-14 21:34:55 +00002082 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2083 // avoid rippling error messages on subsequent uses of the same type,
2084 // could be useful if #include was forgotten.
2085 return false;
2086}
2087
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002088/// \brief Determine the declaration specifier context from the declarator
2089/// context.
2090///
2091/// \param Context the declarator context, which is one of the
2092/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002093Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002094Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2095 if (Context == Declarator::MemberContext)
2096 return DSC_class;
2097 if (Context == Declarator::FileContext)
2098 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002099 if (Context == Declarator::TrailingReturnContext)
2100 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002101 return DSC_normal;
2102}
2103
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002104/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2105///
2106/// FIXME: Simply returns an alignof() expression if the argument is a
2107/// type. Ideally, the type should be propagated directly into Sema.
2108///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002109/// [C11] type-id
2110/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002111/// [C++0x] type-id ...[opt]
2112/// [C++0x] assignment-expression ...[opt]
2113ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2114 SourceLocation &EllipsisLoc) {
2115 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002116 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002117 SourceLocation TypeLoc = Tok.getLocation();
2118 ParsedType Ty = ParseTypeName().get();
2119 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002120 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2121 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002122 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002123 ER = ParseConstantExpression();
2124
Richard Smith80ad52f2013-01-02 11:42:31 +00002125 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002126 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002127
2128 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002129}
2130
2131/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2132/// attribute to Attrs.
2133///
2134/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002135/// [C11] '_Alignas' '(' type-id ')'
2136/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smith33f04a22013-01-29 01:48:07 +00002137/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2138/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002139void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smithf6565a92013-02-22 08:32:16 +00002140 SourceLocation *EndLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002141 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2142 "Not an alignment-specifier!");
2143
Richard Smith33f04a22013-01-29 01:48:07 +00002144 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2145 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002146
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002147 BalancedDelimiterTracker T(*this, tok::l_paren);
2148 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002149 return;
2150
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002151 SourceLocation EllipsisLoc;
2152 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002153 if (ArgExpr.isInvalid()) {
2154 SkipUntil(tok::r_paren);
2155 return;
2156 }
2157
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002158 T.consumeClose();
Richard Smithf6565a92013-02-22 08:32:16 +00002159 if (EndLoc)
2160 *EndLoc = T.getCloseLocation();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002161
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002162 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002163 ArgExprs.push_back(ArgExpr.release());
Richard Smith33f04a22013-01-29 01:48:07 +00002164 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
Richard Smithf6565a92013-02-22 08:32:16 +00002165 ArgExprs.data(), 1, AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002166}
2167
Reid Spencer5f016e22007-07-11 17:01:13 +00002168/// ParseDeclarationSpecifiers
2169/// declaration-specifiers: [C99 6.7]
2170/// storage-class-specifier declaration-specifiers[opt]
2171/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002172/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002173/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002174/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002175/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002176///
2177/// storage-class-specifier: [C99 6.7.1]
2178/// 'typedef'
2179/// 'extern'
2180/// 'static'
2181/// 'auto'
2182/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002183/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002184/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002185/// function-specifier: [C99 6.7.4]
2186/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002187/// [C++] 'virtual'
2188/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002189/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002190/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002191/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002192
Reid Spencer5f016e22007-07-11 17:01:13 +00002193///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002194void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002195 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002196 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002197 DeclSpecContext DSContext,
2198 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002199 if (DS.getSourceRange().isInvalid()) {
2200 DS.SetRangeStart(Tok.getLocation());
2201 DS.SetRangeEnd(Tok.getLocation());
2202 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002203
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002204 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002205 bool AttrsLastTime = false;
2206 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002207 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002208 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002209 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002210 unsigned DiagID = 0;
2211
Reid Spencer5f016e22007-07-11 17:01:13 +00002212 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002213
Reid Spencer5f016e22007-07-11 17:01:13 +00002214 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002215 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002216 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002217 if (!AttrsLastTime)
2218 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002219 else {
2220 // Reject C++11 attributes that appertain to decl specifiers as
2221 // we don't support any C++11 attributes that appertain to decl
2222 // specifiers. This also conforms to what g++ 4.8 is doing.
2223 ProhibitCXX11Attributes(attrs);
2224
Sean Hunt2edf0a22012-06-23 05:07:58 +00002225 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002226 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002227
Reid Spencer5f016e22007-07-11 17:01:13 +00002228 // If this is not a declaration specifier token, we're done reading decl
2229 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002230 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002231 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002232
Sean Hunt2edf0a22012-06-23 05:07:58 +00002233 case tok::l_square:
2234 case tok::kw_alignas:
Richard Smith672edb02013-02-22 09:15:49 +00002235 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Sean Hunt2edf0a22012-06-23 05:07:58 +00002236 goto DoneWithDeclSpec;
2237
2238 ProhibitAttributes(attrs);
2239 // FIXME: It would be good to recover by accepting the attributes,
2240 // but attempting to do that now would cause serious
2241 // madness in terms of diagnostics.
2242 attrs.clear();
2243 attrs.Range = SourceRange();
2244
2245 ParseCXX11Attributes(attrs);
2246 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002247 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002248
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002249 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002250 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002251 if (DS.hasTypeSpecifier()) {
2252 bool AllowNonIdentifiers
2253 = (getCurScope()->getFlags() & (Scope::ControlScope |
2254 Scope::BlockScope |
2255 Scope::TemplateParamScope |
2256 Scope::FunctionPrototypeScope |
2257 Scope::AtCatchScope)) == 0;
2258 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002259 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002260 (DSContext == DSC_class && DS.isFriendSpecified());
2261
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002262 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002263 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002264 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002265 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002266 }
2267
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002268 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2269 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2270 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002271 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002272 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002273 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002274 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002275 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002276 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002277
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002278 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002279 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002280 }
2281
Chris Lattner5e02c472009-01-05 00:07:25 +00002282 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002283 // C++ scope specifier. Annotate and loop, or bail out on error.
2284 if (TryAnnotateCXXScopeToken(true)) {
2285 if (!DS.hasTypeSpecifier())
2286 DS.SetTypeSpecError();
2287 goto DoneWithDeclSpec;
2288 }
John McCall2e0a7152010-03-01 18:20:46 +00002289 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2290 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002291 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002292
2293 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002294 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002295 goto DoneWithDeclSpec;
2296
John McCallaa87d332009-12-12 11:40:51 +00002297 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002298 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2299 Tok.getAnnotationRange(),
2300 SS);
John McCallaa87d332009-12-12 11:40:51 +00002301
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002302 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002303 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002304 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002305 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002306 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002307 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002308
2309 // C++ [class.qual]p2:
2310 // In a lookup in which the constructor is an acceptable lookup
2311 // result and the nested-name-specifier nominates a class C:
2312 //
2313 // - if the name specified after the
2314 // nested-name-specifier, when looked up in C, is the
2315 // injected-class-name of C (Clause 9), or
2316 //
2317 // - if the name specified after the nested-name-specifier
2318 // is the same as the identifier or the
2319 // simple-template-id's template-name in the last
2320 // component of the nested-name-specifier,
2321 //
2322 // the name is instead considered to name the constructor of
2323 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002324 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002325 // Thus, if the template-name is actually the constructor
2326 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002327 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002328 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002329 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCallba9d8532010-04-13 06:39:49 +00002330 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002331 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002332 if (isConstructorDeclarator()) {
2333 // The user meant this to be an out-of-line constructor
2334 // definition, but template arguments are not allowed
2335 // there. Just allow this as a constructor; we'll
2336 // complain about it later.
2337 goto DoneWithDeclSpec;
2338 }
2339
2340 // The user meant this to name a type, but it actually names
2341 // a constructor with some extraneous template
2342 // arguments. Complain, then parse it as a type as the user
2343 // intended.
2344 Diag(TemplateId->TemplateNameLoc,
2345 diag::err_out_of_line_template_id_names_constructor)
2346 << TemplateId->Name;
2347 }
2348
John McCallaa87d332009-12-12 11:40:51 +00002349 DS.getTypeSpecScope() = SS;
2350 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002351 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002352 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002353 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002354 continue;
2355 }
2356
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002357 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002358 DS.getTypeSpecScope() = SS;
2359 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002360 if (Tok.getAnnotationValue()) {
2361 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002362 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002363 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002364 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002365 if (isInvalid)
2366 break;
John McCallb3d87482010-08-24 05:47:05 +00002367 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002368 else
2369 DS.SetTypeSpecError();
2370 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2371 ConsumeToken(); // The typename
2372 }
2373
Douglas Gregor9135c722009-03-25 15:40:00 +00002374 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002375 goto DoneWithDeclSpec;
2376
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002377 // If we're in a context where the identifier could be a class name,
2378 // check whether this is a constructor declaration.
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002379 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002380 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002381 &SS)) {
2382 if (isConstructorDeclarator())
2383 goto DoneWithDeclSpec;
2384
2385 // As noted in C++ [class.qual]p2 (cited above), when the name
2386 // of the class is qualified in a context where it could name
2387 // a constructor, its a constructor name. However, we've
2388 // looked at the declarator, and the user probably meant this
2389 // to be a type. Complain that it isn't supposed to be treated
2390 // as a type, then proceed to parse it as a type.
2391 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2392 << Next.getIdentifierInfo();
2393 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002394
John McCallb3d87482010-08-24 05:47:05 +00002395 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2396 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002397 getCurScope(), &SS,
2398 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002399 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002400 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002401
Chris Lattnerf4382f52009-04-14 22:17:06 +00002402 // If the referenced identifier is not a type, then this declspec is
2403 // erroneous: We already checked about that it has no type specifier, and
2404 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002405 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002406 if (TypeRep == 0) {
2407 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002408 ParsedAttributesWithRange Attrs(AttrFactory);
2409 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2410 if (!Attrs.empty()) {
2411 AttrsLastTime = true;
2412 attrs.takeAllFrom(Attrs);
2413 }
2414 continue;
2415 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002416 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002417 }
Mike Stump1eb44332009-09-09 15:08:12 +00002418
John McCallaa87d332009-12-12 11:40:51 +00002419 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002420 ConsumeToken(); // The C++ scope.
2421
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002422 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002423 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002424 if (isInvalid)
2425 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002426
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002427 DS.SetRangeEnd(Tok.getLocation());
2428 ConsumeToken(); // The typename.
2429
2430 continue;
2431 }
Mike Stump1eb44332009-09-09 15:08:12 +00002432
Chris Lattner80d0c892009-01-21 19:48:37 +00002433 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002434 if (Tok.getAnnotationValue()) {
2435 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002436 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002437 DiagID, T);
2438 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002439 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002440
Chris Lattner5c5db552010-04-05 18:18:31 +00002441 if (isInvalid)
2442 break;
2443
Chris Lattner80d0c892009-01-21 19:48:37 +00002444 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2445 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002446
Chris Lattner80d0c892009-01-21 19:48:37 +00002447 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2448 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002449 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002450 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002451 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002452
Chris Lattner80d0c892009-01-21 19:48:37 +00002453 continue;
2454 }
Mike Stump1eb44332009-09-09 15:08:12 +00002455
Douglas Gregorbfad9152011-04-28 15:48:45 +00002456 case tok::kw___is_signed:
2457 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2458 // typically treats it as a trait. If we see __is_signed as it appears
2459 // in libstdc++, e.g.,
2460 //
2461 // static const bool __is_signed;
2462 //
2463 // then treat __is_signed as an identifier rather than as a keyword.
2464 if (DS.getTypeSpecType() == TST_bool &&
2465 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2466 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2467 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2468 Tok.setKind(tok::identifier);
2469 }
2470
2471 // We're done with the declaration-specifiers.
2472 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002473
Chris Lattner3bd934a2008-07-26 01:18:38 +00002474 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002475 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002476 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002477 // In C++, check to see if this is a scope specifier like foo::bar::, if
2478 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002479 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002480 if (TryAnnotateCXXScopeToken(true)) {
2481 if (!DS.hasTypeSpecifier())
2482 DS.SetTypeSpecError();
2483 goto DoneWithDeclSpec;
2484 }
2485 if (!Tok.is(tok::identifier))
2486 continue;
2487 }
Mike Stump1eb44332009-09-09 15:08:12 +00002488
Chris Lattner3bd934a2008-07-26 01:18:38 +00002489 // This identifier can only be a typedef name if we haven't already seen
2490 // a type-specifier. Without this check we misparse:
2491 // typedef int X; struct Y { short X; }; as 'short int'.
2492 if (DS.hasTypeSpecifier())
2493 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002494
John Thompson82287d12010-02-05 00:12:22 +00002495 // Check for need to substitute AltiVec keyword tokens.
2496 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2497 break;
2498
Richard Smithf63eee72012-05-09 18:56:43 +00002499 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2500 // allow the use of a typedef name as a type specifier.
2501 if (DS.isTypeAltiVecVector())
2502 goto DoneWithDeclSpec;
2503
John McCallb3d87482010-08-24 05:47:05 +00002504 ParsedType TypeRep =
2505 Actions.getTypeName(*Tok.getIdentifierInfo(),
2506 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002507
Chris Lattnerc199ab32009-04-12 20:42:31 +00002508 // If this is not a typedef name, don't parse it as part of the declspec,
2509 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002510 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002511 ParsedAttributesWithRange Attrs(AttrFactory);
2512 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2513 if (!Attrs.empty()) {
2514 AttrsLastTime = true;
2515 attrs.takeAllFrom(Attrs);
2516 }
2517 continue;
2518 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002519 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002520 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002521
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002522 // If we're in a context where the identifier could be a class name,
2523 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002524 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002525 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002526 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002527 goto DoneWithDeclSpec;
2528
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002529 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002530 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002531 if (isInvalid)
2532 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002533
Chris Lattner3bd934a2008-07-26 01:18:38 +00002534 DS.SetRangeEnd(Tok.getLocation());
2535 ConsumeToken(); // The identifier
2536
2537 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2538 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002539 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002540 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002541 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002542
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002543 // Need to support trailing type qualifiers (e.g. "id<p> const").
2544 // If a type specifier follows, it will be diagnosed elsewhere.
2545 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002546 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002547
2548 // type-name
2549 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002550 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002551 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002552 // This template-id does not refer to a type name, so we're
2553 // done with the type-specifiers.
2554 goto DoneWithDeclSpec;
2555 }
2556
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002557 // If we're in a context where the template-id could be a
2558 // constructor name or specialization, check whether this is a
2559 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002560 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002561 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002562 isConstructorDeclarator())
2563 goto DoneWithDeclSpec;
2564
Douglas Gregor39a8de12009-02-25 19:37:18 +00002565 // Turn the template-id annotation token into a type annotation
2566 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002567 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002568 continue;
2569 }
2570
Reid Spencer5f016e22007-07-11 17:01:13 +00002571 // GNU attributes support.
2572 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002573 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002574 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002575
2576 // Microsoft declspec support.
2577 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002578 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002579 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002580
Steve Naroff239f0732008-12-25 14:16:32 +00002581 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002582 case tok::kw___forceinline: {
Chad Rosier22aa6902012-12-21 22:24:43 +00002583 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002584 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002585 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002586 // FIXME: This does not work correctly if it is set to be a declspec
2587 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002588 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002589 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002590 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002591 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002592
2593 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002594 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002595 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002596 case tok::kw___cdecl:
2597 case tok::kw___stdcall:
2598 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002599 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002600 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002601 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002602 continue;
2603
Dawn Perchik52fc3142010-09-03 01:29:35 +00002604 // Borland single token adornments.
2605 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002606 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002607 continue;
2608
Peter Collingbournef315fa82011-02-14 01:42:53 +00002609 // OpenCL single token adornments.
2610 case tok::kw___kernel:
2611 ParseOpenCLAttributes(DS.getAttributes());
2612 continue;
2613
Reid Spencer5f016e22007-07-11 17:01:13 +00002614 // storage-class-specifier
2615 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002616 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2617 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002618 break;
2619 case tok::kw_extern:
2620 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002621 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002622 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2623 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002624 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002625 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002626 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2627 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002628 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002629 case tok::kw_static:
2630 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002631 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002632 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2633 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002634 break;
2635 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002636 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002637 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002638 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2639 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002640 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002641 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002642 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002643 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002644 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2645 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002646 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002647 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2648 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002649 break;
2650 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002651 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2652 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002653 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002654 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002655 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2656 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002657 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002658 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002659 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002660 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002661
Reid Spencer5f016e22007-07-11 17:01:13 +00002662 // function-specifier
2663 case tok::kw_inline:
Chad Rosier22aa6902012-12-21 22:24:43 +00002664 isInvalid = DS.setFunctionSpecInline(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002665 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002666 case tok::kw_virtual:
Chad Rosier22aa6902012-12-21 22:24:43 +00002667 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002668 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002669 case tok::kw_explicit:
Chad Rosier22aa6902012-12-21 22:24:43 +00002670 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002671 break;
Richard Smithde03c152013-01-17 22:16:11 +00002672 case tok::kw__Noreturn:
2673 if (!getLangOpts().C11)
2674 Diag(Loc, diag::ext_c11_noreturn);
2675 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2676 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002677
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002678 // alignment-specifier
2679 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002680 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002681 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002682 ParseAlignmentSpecifier(DS.getAttributes());
2683 continue;
2684
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002685 // friend
2686 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002687 if (DSContext == DSC_class)
2688 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2689 else {
2690 PrevSpec = ""; // not actually used by the diagnostic
2691 DiagID = diag::err_friend_invalid_in_context;
2692 isInvalid = true;
2693 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002694 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002695
Douglas Gregor8d267c52011-09-09 02:06:17 +00002696 // Modules
2697 case tok::kw___module_private__:
2698 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2699 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002700
Sebastian Redl2ac67232009-11-05 15:47:02 +00002701 // constexpr
2702 case tok::kw_constexpr:
2703 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2704 break;
2705
Chris Lattner80d0c892009-01-21 19:48:37 +00002706 // type-specifier
2707 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002708 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2709 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002710 break;
2711 case tok::kw_long:
2712 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002713 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2714 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002715 else
John McCallfec54012009-08-03 20:12:06 +00002716 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2717 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002718 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002719 case tok::kw___int64:
2720 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2721 DiagID);
2722 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002723 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002724 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2725 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002726 break;
2727 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002728 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2729 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002730 break;
2731 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002732 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2733 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002734 break;
2735 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002736 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2737 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002738 break;
2739 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002740 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2741 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002742 break;
2743 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002744 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2745 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002746 break;
2747 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2749 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002750 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002751 case tok::kw___int128:
2752 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2753 DiagID);
2754 break;
2755 case tok::kw_half:
2756 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2757 DiagID);
2758 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002759 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002760 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2761 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002762 break;
2763 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002764 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2765 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002766 break;
2767 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002768 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2769 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002770 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002771 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002772 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2773 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002774 break;
2775 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002776 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2777 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002778 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002779 case tok::kw_bool:
2780 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002781 if (Tok.is(tok::kw_bool) &&
2782 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2783 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2784 PrevSpec = ""; // Not used by the diagnostic.
2785 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002786 // For better error recovery.
2787 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002788 isInvalid = true;
2789 } else {
2790 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2791 DiagID);
2792 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002793 break;
2794 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002795 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2796 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002797 break;
2798 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002799 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2800 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002801 break;
2802 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002803 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2804 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002805 break;
John Thompson82287d12010-02-05 00:12:22 +00002806 case tok::kw___vector:
2807 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2808 break;
2809 case tok::kw___pixel:
2810 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2811 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00002812 case tok::kw_image1d_t:
2813 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2814 PrevSpec, DiagID);
2815 break;
2816 case tok::kw_image1d_array_t:
2817 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2818 PrevSpec, DiagID);
2819 break;
2820 case tok::kw_image1d_buffer_t:
2821 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2822 PrevSpec, DiagID);
2823 break;
2824 case tok::kw_image2d_t:
2825 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2826 PrevSpec, DiagID);
2827 break;
2828 case tok::kw_image2d_array_t:
2829 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2830 PrevSpec, DiagID);
2831 break;
2832 case tok::kw_image3d_t:
2833 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2834 PrevSpec, DiagID);
2835 break;
Guy Benyei21f18c42013-02-07 10:55:47 +00002836 case tok::kw_sampler_t:
2837 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2838 PrevSpec, DiagID);
2839 break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00002840 case tok::kw_event_t:
2841 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2842 PrevSpec, DiagID);
2843 break;
John McCalla5fc4722011-04-09 22:50:59 +00002844 case tok::kw___unknown_anytype:
2845 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2846 PrevSpec, DiagID);
2847 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002848
2849 // class-specifier:
2850 case tok::kw_class:
2851 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00002852 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00002853 case tok::kw_union: {
2854 tok::TokenKind Kind = Tok.getKind();
2855 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00002856
2857 // These are attributes following class specifiers.
2858 // To produce better diagnostic, we parse them when
2859 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002860 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00002861 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002862 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002863
2864 // If there are attributes following class specifier,
2865 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002866 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00002867 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00002868 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002869 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002870 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002871 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002872
2873 // enum-specifier:
2874 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002875 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002876 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002877 continue;
2878
2879 // cv-qualifier:
2880 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002881 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002882 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002883 break;
2884 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002885 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002886 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002887 break;
2888 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002889 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002890 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002891 break;
2892
Douglas Gregord57959a2009-03-27 23:10:48 +00002893 // C++ typename-specifier:
2894 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002895 if (TryAnnotateTypeOrScopeToken()) {
2896 DS.SetTypeSpecError();
2897 goto DoneWithDeclSpec;
2898 }
2899 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002900 continue;
2901 break;
2902
Chris Lattner80d0c892009-01-21 19:48:37 +00002903 // GNU typeof support.
2904 case tok::kw_typeof:
2905 ParseTypeofSpecifier(DS);
2906 continue;
2907
David Blaikie42d6d0c2011-12-04 05:04:18 +00002908 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002909 ParseDecltypeSpecifier(DS);
2910 continue;
2911
Sean Huntdb5d44b2011-05-19 05:37:45 +00002912 case tok::kw___underlying_type:
2913 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002914 continue;
2915
2916 case tok::kw__Atomic:
Richard Smith4cf4a5e2013-03-28 01:55:44 +00002917 // C11 6.7.2.4/4:
2918 // If the _Atomic keyword is immediately followed by a left parenthesis,
2919 // it is interpreted as a type specifier (with a type name), not as a
2920 // type qualifier.
2921 if (NextToken().is(tok::l_paren)) {
2922 ParseAtomicSpecifier(DS);
2923 continue;
2924 }
2925 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
2926 getLangOpts());
2927 break;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002928
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002929 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00002930 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002931 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002932 goto DoneWithDeclSpec;
2933 case tok::kw___private:
2934 case tok::kw___global:
2935 case tok::kw___local:
2936 case tok::kw___constant:
2937 case tok::kw___read_only:
2938 case tok::kw___write_only:
2939 case tok::kw___read_write:
2940 ParseOpenCLQualifiers(DS);
2941 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002942
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002943 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002944 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002945 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2946 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002947 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002948 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002949
Douglas Gregor46f936e2010-11-19 17:10:50 +00002950 if (!ParseObjCProtocolQualifiers(DS))
2951 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2952 << FixItHint::CreateInsertion(Loc, "id")
2953 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00002954
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002955 // Need to support trailing type qualifiers (e.g. "id<p> const").
2956 // If a type specifier follows, it will be diagnosed elsewhere.
2957 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002958 }
John McCallfec54012009-08-03 20:12:06 +00002959 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002960 if (isInvalid) {
2961 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002962 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00002963
Douglas Gregorae2fb142010-08-23 14:34:43 +00002964 if (DiagID == diag::ext_duplicate_declspec)
2965 Diag(Tok, DiagID)
2966 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2967 else
2968 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002969 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002970
Chris Lattner81c018d2008-03-13 06:29:04 +00002971 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002972 if (DiagID != diag::err_bool_redeclaration)
2973 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002974
2975 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002976 }
2977}
Douglas Gregoradcac882008-12-01 23:54:00 +00002978
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002979/// ParseStructDeclaration - Parse a struct declaration without the terminating
2980/// semicolon.
2981///
Reid Spencer5f016e22007-07-11 17:01:13 +00002982/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002983/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002984/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002985/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002986/// struct-declarator-list:
2987/// struct-declarator
2988/// struct-declarator-list ',' struct-declarator
2989/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2990/// struct-declarator:
2991/// declarator
2992/// [GNU] declarator attributes[opt]
2993/// declarator[opt] ':' constant-expression
2994/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2995///
Chris Lattnere1359422008-04-10 06:46:29 +00002996void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002997ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00002998
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002999 if (Tok.is(tok::kw___extension__)) {
3000 // __extension__ silences extension warnings in the subexpression.
3001 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00003002 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00003003 return ParseStructDeclaration(DS, Fields);
3004 }
Mike Stump1eb44332009-09-09 15:08:12 +00003005
Steve Naroff28a7ca82007-08-20 22:28:22 +00003006 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00003007 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003008
Douglas Gregor4920f1f2009-01-12 22:49:06 +00003009 // If there are no declarators, this is a free-standing declaration
3010 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00003011 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003012 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3013 DS);
3014 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00003015 return;
3016 }
3017
3018 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00003019 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00003020 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003021 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003022 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00003023 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00003024
Bill Wendlingad017fa2012-12-20 19:22:21 +00003025 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00003026 if (!FirstDeclarator)
3027 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00003028
Steve Naroff28a7ca82007-08-20 22:28:22 +00003029 /// struct-declarator: declarator
3030 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003031 if (Tok.isNot(tok::colon)) {
3032 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3033 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00003034 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003035 }
Mike Stump1eb44332009-09-09 15:08:12 +00003036
Chris Lattner04d66662007-10-09 17:33:22 +00003037 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00003038 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00003039 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003040 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00003041 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00003042 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00003043 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00003044 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003045
Steve Naroff28a7ca82007-08-20 22:28:22 +00003046 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003047 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003048
John McCallbdd563e2009-11-03 02:38:08 +00003049 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00003050 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00003051
Steve Naroff28a7ca82007-08-20 22:28:22 +00003052 // If we don't have a comma, it is either the end of the list (a ';')
3053 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00003054 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003055 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00003056
Steve Naroff28a7ca82007-08-20 22:28:22 +00003057 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00003058 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003059
John McCallbdd563e2009-11-03 02:38:08 +00003060 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003061 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00003062}
3063
3064/// ParseStructUnionBody
3065/// struct-contents:
3066/// struct-declaration-list
3067/// [EXT] empty
3068/// [GNU] "struct-declaration-list" without terminatoring ';'
3069/// struct-declaration-list:
3070/// struct-declaration
3071/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003072/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003073///
Reid Spencer5f016e22007-07-11 17:01:13 +00003074void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003075 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003076 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3077 "parsing struct/union body");
Andy Gibbsf50f3f72013-04-03 09:31:19 +00003078 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump1eb44332009-09-09 15:08:12 +00003079
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003080 BalancedDelimiterTracker T(*this, tok::l_brace);
3081 if (T.consumeOpen())
3082 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003083
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003084 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003085 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003086
Andy Gibbsf50f3f72013-04-03 09:31:19 +00003087 // Empty structs are an extension in C (C99 6.7.2.1p7).
3088 if (Tok.is(tok::r_brace)) {
Richard Smithd7c56e12011-12-29 21:57:33 +00003089 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3090 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3091 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003092
Chris Lattner5f9e2722011-07-23 10:55:15 +00003093 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003094
Reid Spencer5f016e22007-07-11 17:01:13 +00003095 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003096 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003097 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003098
Reid Spencer5f016e22007-07-11 17:01:13 +00003099 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003100 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003101 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003102 continue;
3103 }
Chris Lattnere1359422008-04-10 06:46:29 +00003104
Andy Gibbs74b9fa12013-04-03 09:46:04 +00003105 // Parse _Static_assert declaration.
3106 if (Tok.is(tok::kw__Static_assert)) {
3107 SourceLocation DeclEnd;
3108 ParseStaticAssertDeclaration(DeclEnd);
3109 continue;
3110 }
3111
John McCallbdd563e2009-11-03 02:38:08 +00003112 if (!Tok.is(tok::at)) {
3113 struct CFieldCallback : FieldCallback {
3114 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003115 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003116 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003117
John McCalld226f652010-08-21 09:40:31 +00003118 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003119 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003120 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3121
Eli Friedmandcdff462012-08-08 23:53:27 +00003122 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003123 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003124 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003125 FD.D.getDeclSpec().getSourceRange().getBegin(),
3126 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003127 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003128 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003129 }
John McCallbdd563e2009-11-03 02:38:08 +00003130 } Callback(*this, TagDecl, FieldDecls);
3131
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003132 // Parse all the comma separated declarators.
3133 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003134 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003135 } else { // Handle @defs
3136 ConsumeToken();
3137 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3138 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003139 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003140 continue;
3141 }
3142 ConsumeToken();
3143 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3144 if (!Tok.is(tok::identifier)) {
3145 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003146 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003147 continue;
3148 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003149 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003150 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003151 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003152 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3153 ConsumeToken();
3154 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003155 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003156
Chris Lattner04d66662007-10-09 17:33:22 +00003157 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003158 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003159 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003160 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003161 break;
3162 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003163 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3164 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003165 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003166 // If we stopped at a ';', eat it.
3167 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003168 }
3169 }
Mike Stump1eb44332009-09-09 15:08:12 +00003170
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003171 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003172
John McCall0b7e6782011-03-24 11:26:52 +00003173 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003174 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003175 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003176
Douglas Gregor23c94db2010-07-02 17:43:08 +00003177 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003178 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003179 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003180 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003181 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003182 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3183 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003184}
3185
Reid Spencer5f016e22007-07-11 17:01:13 +00003186/// ParseEnumSpecifier
3187/// enum-specifier: [C99 6.7.2.2]
3188/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003189///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003190/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3191/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003192/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3193/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003194/// 'enum' identifier
3195/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003196///
Richard Smith1af83c42012-03-23 03:33:32 +00003197/// [C++11] enum-head '{' enumerator-list[opt] '}'
3198/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003199///
Richard Smith1af83c42012-03-23 03:33:32 +00003200/// enum-head: [C++11]
3201/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3202/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3203/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003204///
Richard Smith1af83c42012-03-23 03:33:32 +00003205/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003206/// 'enum'
3207/// 'enum' 'class'
3208/// 'enum' 'struct'
3209///
Richard Smith1af83c42012-03-23 03:33:32 +00003210/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003211/// ':' type-specifier-seq
3212///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003213/// [C++] elaborated-type-specifier:
3214/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3215///
Chris Lattner4c97d762009-04-12 21:49:30 +00003216void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003217 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003218 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003219 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003220 if (Tok.is(tok::code_completion)) {
3221 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003222 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003223 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003224 }
John McCall57c13002011-07-06 05:58:41 +00003225
Sean Hunt2edf0a22012-06-23 05:07:58 +00003226 // If attributes exist after tag, parse them.
3227 ParsedAttributesWithRange attrs(AttrFactory);
3228 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003229 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003230
3231 // If declspecs exist after tag, parse them.
3232 while (Tok.is(tok::kw___declspec))
3233 ParseMicrosoftDeclSpec(attrs);
3234
Richard Smithbdad7a22012-01-10 01:33:14 +00003235 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003236 bool IsScopedUsingClassTag = false;
3237
John McCall1e12b3d2012-06-23 22:30:04 +00003238 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Smith80ad52f2013-01-02 11:42:31 +00003239 if (getLangOpts().CPlusPlus11 &&
John McCall57c13002011-07-06 05:58:41 +00003240 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00003241 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003242 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003243 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003244
Bill Wendlingad017fa2012-12-20 19:22:21 +00003245 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003246 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003247 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003248
3249 // They are allowed afterwards, though.
3250 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003251 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003252 while (Tok.is(tok::kw___declspec))
3253 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003254 }
Richard Smith1af83c42012-03-23 03:33:32 +00003255
John McCall13489672012-05-07 06:16:58 +00003256 // C++11 [temp.explicit]p12:
3257 // The usual access controls do not apply to names used to specify
3258 // explicit instantiations.
3259 // We extend this to also cover explicit specializations. Note that
3260 // we don't suppress if this turns out to be an elaborated type
3261 // specifier.
3262 bool shouldDelayDiagsInTag =
3263 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3264 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3265 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003266
Richard Smith7796eb52012-03-12 08:56:40 +00003267 // Enum definitions should not be parsed in a trailing-return-type.
3268 bool AllowDeclaration = DSC != DSC_trailing;
3269
3270 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003271 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003272 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003273
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003274 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003275 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003276 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3277 // if a fixed underlying type is allowed.
3278 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003279
3280 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith725fe0e2013-04-01 21:43:41 +00003281 /*EnteringContext=*/true))
John McCall9ba61662010-02-26 08:45:28 +00003282 return;
3283
3284 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003285 Diag(Tok, diag::err_expected_ident);
3286 if (Tok.isNot(tok::l_brace)) {
3287 // Has no name and is not a definition.
3288 // Skip the rest of this declarator, up until the comma or semicolon.
3289 SkipUntil(tok::comma, true);
3290 return;
3291 }
3292 }
3293 }
Mike Stump1eb44332009-09-09 15:08:12 +00003294
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003295 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003296 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003297 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003298 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003299
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003300 // Skip the rest of this declarator, up until the comma or semicolon.
3301 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003302 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003303 }
Mike Stump1eb44332009-09-09 15:08:12 +00003304
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003305 // If an identifier is present, consume and remember it.
3306 IdentifierInfo *Name = 0;
3307 SourceLocation NameLoc;
3308 if (Tok.is(tok::identifier)) {
3309 Name = Tok.getIdentifierInfo();
3310 NameLoc = ConsumeToken();
3311 }
Mike Stump1eb44332009-09-09 15:08:12 +00003312
Richard Smithbdad7a22012-01-10 01:33:14 +00003313 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003314 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3315 // declaration of a scoped enumeration.
3316 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003317 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003318 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003319 }
3320
John McCall13489672012-05-07 06:16:58 +00003321 // Okay, end the suppression area. We'll decide whether to emit the
3322 // diagnostics in a second.
3323 if (shouldDelayDiagsInTag)
3324 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003325
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003326 TypeResult BaseType;
3327
Douglas Gregora61b3e72010-12-01 17:42:47 +00003328 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003329 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003330 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003331 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003332 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003333 // If we're in class scope, this can either be an enum declaration with
3334 // an underlying type, or a declaration of a bitfield member. We try to
3335 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003336 // (integer literal, sizeof); if it's still ambiguous, we then consider
3337 // anything that's a simple-type-specifier followed by '(' as an
3338 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003339 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003340 EnterExpressionEvaluationContext Unevaluated(Actions,
3341 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003342 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003343 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003344 // bit-field. This is the common case.
3345 if (TPR == TPResult::True())
3346 PossibleBitfield = true;
3347 // If the next token starts a type-specifier-seq, it may be either a
3348 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003349 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003350 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003351 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003352 GetLookAheadToken(2).getKind() == tok::semi) {
3353 // Consume the ':'.
3354 ConsumeToken();
3355 } else {
3356 // We have the start of a type-specifier-seq, so we have to perform
3357 // tentative parsing to determine whether we have an expression or a
3358 // type.
3359 TentativeParsingAction TPA(*this);
3360
3361 // Consume the ':'.
3362 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003363
3364 // If we see a type specifier followed by an open-brace, we have an
3365 // ambiguity between an underlying type and a C++11 braced
3366 // function-style cast. Resolve this by always treating it as an
3367 // underlying type.
3368 // FIXME: The standard is not entirely clear on how to disambiguate in
3369 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003370 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003371 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003372 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003373 // We'll parse this as a bitfield later.
3374 PossibleBitfield = true;
3375 TPA.Revert();
3376 } else {
3377 // We have a type-specifier-seq.
3378 TPA.Commit();
3379 }
3380 }
3381 } else {
3382 // Consume the ':'.
3383 ConsumeToken();
3384 }
3385
3386 if (!PossibleBitfield) {
3387 SourceRange Range;
3388 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003389
Richard Smith80ad52f2013-01-02 11:42:31 +00003390 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003391 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003392 } else if (!getLangOpts().ObjC2) {
3393 if (getLangOpts().CPlusPlus)
3394 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3395 else
3396 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3397 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003398 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003399 }
3400
Richard Smithbdad7a22012-01-10 01:33:14 +00003401 // There are four options here. If we have 'friend enum foo;' then this is a
3402 // friend declaration, and cannot have an accompanying definition. If we have
3403 // 'enum foo;', then this is a forward declaration. If we have
3404 // 'enum foo {...' then this is a definition. Otherwise we have something
3405 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003406 //
3407 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3408 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3409 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3410 //
John McCallf312b1e2010-08-26 23:41:50 +00003411 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003412 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003413 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003414 } else if (Tok.is(tok::l_brace)) {
3415 if (DS.isFriendSpecified()) {
3416 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3417 << SourceRange(DS.getFriendSpecLoc());
3418 ConsumeBrace();
3419 SkipUntil(tok::r_brace);
3420 TUK = Sema::TUK_Friend;
3421 } else {
3422 TUK = Sema::TUK_Definition;
3423 }
Richard Smithc9f35172012-06-25 21:37:02 +00003424 } else if (DSC != DSC_type_specifier &&
3425 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003426 (Tok.isAtStartOfLine() &&
3427 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003428 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3429 if (Tok.isNot(tok::semi)) {
3430 // A semicolon was missing after this declaration. Diagnose and recover.
3431 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3432 "enum");
3433 PP.EnterToken(Tok);
3434 Tok.setKind(tok::semi);
3435 }
John McCall13489672012-05-07 06:16:58 +00003436 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003437 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003438 }
3439
3440 // If this is an elaborated type specifier, and we delayed
3441 // diagnostics before, just merge them into the current pool.
3442 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3443 diagsFromTag.redelay();
3444 }
Richard Smith1af83c42012-03-23 03:33:32 +00003445
3446 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003447 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003448 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003449 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003450 // Skip the rest of this declarator, up until the comma or semicolon.
3451 Diag(Tok, diag::err_enum_template);
3452 SkipUntil(tok::comma, true);
3453 return;
3454 }
3455
3456 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3457 // Enumerations can't be explicitly instantiated.
3458 DS.SetTypeSpecError();
3459 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3460 return;
3461 }
3462
3463 assert(TemplateInfo.TemplateParams && "no template parameters");
3464 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3465 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003466 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003467
Sean Hunt2edf0a22012-06-23 05:07:58 +00003468 if (TUK == Sema::TUK_Reference)
3469 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003470
Douglas Gregorb9075602011-02-22 02:55:24 +00003471 if (!Name && TUK != Sema::TUK_Definition) {
3472 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003473
Douglas Gregorb9075602011-02-22 02:55:24 +00003474 // Skip the rest of this declarator, up until the comma or semicolon.
3475 SkipUntil(tok::comma, true);
3476 return;
3477 }
Richard Smith1af83c42012-03-23 03:33:32 +00003478
Douglas Gregor402abb52009-05-28 23:31:59 +00003479 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003480 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003481 const char *PrevSpec = 0;
3482 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003483 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003484 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003485 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003486 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003487 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003488
Douglas Gregor48c89f42010-04-24 16:38:41 +00003489 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003490 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003491 // dependent tag.
3492 if (!Name) {
3493 DS.SetTypeSpecError();
3494 Diag(Tok, diag::err_expected_type_name_after_typename);
3495 return;
3496 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003497
Douglas Gregor23c94db2010-07-02 17:43:08 +00003498 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003499 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003500 NameLoc);
3501 if (Type.isInvalid()) {
3502 DS.SetTypeSpecError();
3503 return;
3504 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003505
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003506 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3507 NameLoc.isValid() ? NameLoc : StartLoc,
3508 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003509 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003510
Douglas Gregor48c89f42010-04-24 16:38:41 +00003511 return;
3512 }
Mike Stump1eb44332009-09-09 15:08:12 +00003513
John McCalld226f652010-08-21 09:40:31 +00003514 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003515 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003516 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003517 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003518 ConsumeBrace();
3519 SkipUntil(tok::r_brace);
3520 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003521
Douglas Gregor48c89f42010-04-24 16:38:41 +00003522 DS.SetTypeSpecError();
3523 return;
3524 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003525
Richard Smithc9f35172012-06-25 21:37:02 +00003526 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003527 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003528
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003529 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3530 NameLoc.isValid() ? NameLoc : StartLoc,
3531 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003532 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003533}
3534
3535/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3536/// enumerator-list:
3537/// enumerator
3538/// enumerator-list ',' enumerator
3539/// enumerator:
3540/// enumeration-constant
3541/// enumeration-constant '=' constant-expression
3542/// enumeration-constant:
3543/// identifier
3544///
John McCalld226f652010-08-21 09:40:31 +00003545void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003546 // Enter the scope of the enum body and start the definition.
3547 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003548 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003549
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003550 BalancedDelimiterTracker T(*this, tok::l_brace);
3551 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003552
Chris Lattner7946dd32007-08-27 17:24:30 +00003553 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003554 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003555 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003556
Chris Lattner5f9e2722011-07-23 10:55:15 +00003557 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003558
John McCalld226f652010-08-21 09:40:31 +00003559 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003560
Reid Spencer5f016e22007-07-11 17:01:13 +00003561 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003562 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003563 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3564 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003565
John McCall5b629aa2010-10-22 23:36:17 +00003566 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003567 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003568 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003569 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003570 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003571
Reid Spencer5f016e22007-07-11 17:01:13 +00003572 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003573 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003574 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003575
Chris Lattner04d66662007-10-09 17:33:22 +00003576 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003577 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003578 AssignedVal = ParseConstantExpression();
3579 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003580 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003581 }
Mike Stump1eb44332009-09-09 15:08:12 +00003582
Reid Spencer5f016e22007-07-11 17:01:13 +00003583 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003584 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3585 LastEnumConstDecl,
3586 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003587 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003588 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003589 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003590
Reid Spencer5f016e22007-07-11 17:01:13 +00003591 EnumConstantDecls.push_back(EnumConstDecl);
3592 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003593
Douglas Gregor751f6922010-09-07 14:51:08 +00003594 if (Tok.is(tok::identifier)) {
3595 // We're missing a comma between enumerators.
3596 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003597 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003598 << FixItHint::CreateInsertion(Loc, ", ");
3599 continue;
3600 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003601
Chris Lattner04d66662007-10-09 17:33:22 +00003602 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003603 break;
3604 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003605
Richard Smith7fe62082011-10-15 05:09:34 +00003606 if (Tok.isNot(tok::identifier)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003607 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003608 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3609 diag::ext_enumerator_list_comma_cxx :
3610 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003611 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003612 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003613 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3614 << FixItHint::CreateRemoval(CommaLoc);
3615 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003616 }
Mike Stump1eb44332009-09-09 15:08:12 +00003617
Reid Spencer5f016e22007-07-11 17:01:13 +00003618 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003619 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003620
Reid Spencer5f016e22007-07-11 17:01:13 +00003621 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003622 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003623 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003624
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003625 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3626 EnumDecl, EnumConstantDecls.data(),
3627 EnumConstantDecls.size(), getCurScope(),
3628 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003629
Douglas Gregor72de6672009-01-08 20:45:30 +00003630 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003631 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3632 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003633
3634 // The next token must be valid after an enum definition. If not, a ';'
3635 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003636 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3637 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003638 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3639 // Push this token back into the preprocessor and change our current token
3640 // to ';' so that the rest of the code recovers as though there were an
3641 // ';' after the definition.
3642 PP.EnterToken(Tok);
3643 Tok.setKind(tok::semi);
3644 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003645}
3646
3647/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003648/// start of a type-qualifier-list.
3649bool Parser::isTypeQualifier() const {
3650 switch (Tok.getKind()) {
3651 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003652
3653 // type-qualifier only in OpenCL
3654 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003655 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003656
Steve Naroff5f8aa692008-02-11 23:15:56 +00003657 // type-qualifier
3658 case tok::kw_const:
3659 case tok::kw_volatile:
3660 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003661 case tok::kw___private:
3662 case tok::kw___local:
3663 case tok::kw___global:
3664 case tok::kw___constant:
3665 case tok::kw___read_only:
3666 case tok::kw___read_write:
3667 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003668 return true;
3669 }
3670}
3671
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003672/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3673/// is definitely a type-specifier. Return false if it isn't part of a type
3674/// specifier or if we're not sure.
3675bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3676 switch (Tok.getKind()) {
3677 default: return false;
3678 // type-specifiers
3679 case tok::kw_short:
3680 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003681 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003682 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003683 case tok::kw_signed:
3684 case tok::kw_unsigned:
3685 case tok::kw__Complex:
3686 case tok::kw__Imaginary:
3687 case tok::kw_void:
3688 case tok::kw_char:
3689 case tok::kw_wchar_t:
3690 case tok::kw_char16_t:
3691 case tok::kw_char32_t:
3692 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003693 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003694 case tok::kw_float:
3695 case tok::kw_double:
3696 case tok::kw_bool:
3697 case tok::kw__Bool:
3698 case tok::kw__Decimal32:
3699 case tok::kw__Decimal64:
3700 case tok::kw__Decimal128:
3701 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003702
Guy Benyeib13621d2012-12-18 14:38:23 +00003703 // OpenCL specific types:
3704 case tok::kw_image1d_t:
3705 case tok::kw_image1d_array_t:
3706 case tok::kw_image1d_buffer_t:
3707 case tok::kw_image2d_t:
3708 case tok::kw_image2d_array_t:
3709 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003710 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003711 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003712
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003713 // struct-or-union-specifier (C99) or class-specifier (C++)
3714 case tok::kw_class:
3715 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003716 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003717 case tok::kw_union:
3718 // enum-specifier
3719 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003720
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003721 // typedef-name
3722 case tok::annot_typename:
3723 return true;
3724 }
3725}
3726
Steve Naroff5f8aa692008-02-11 23:15:56 +00003727/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003728/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003729bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003730 switch (Tok.getKind()) {
3731 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003732
Chris Lattner166a8fc2009-01-04 23:41:41 +00003733 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003734 if (TryAltiVecVectorToken())
3735 return true;
3736 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003737 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003738 // Annotate typenames and C++ scope specifiers. If we get one, just
3739 // recurse to handle whatever we get.
3740 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003741 return true;
3742 if (Tok.is(tok::identifier))
3743 return false;
3744 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003745
Chris Lattner166a8fc2009-01-04 23:41:41 +00003746 case tok::coloncolon: // ::foo::bar
3747 if (NextToken().is(tok::kw_new) || // ::new
3748 NextToken().is(tok::kw_delete)) // ::delete
3749 return false;
3750
Chris Lattner166a8fc2009-01-04 23:41:41 +00003751 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003752 return true;
3753 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003754
Reid Spencer5f016e22007-07-11 17:01:13 +00003755 // GNU attributes support.
3756 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003757 // GNU typeof support.
3758 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003759
Reid Spencer5f016e22007-07-11 17:01:13 +00003760 // type-specifiers
3761 case tok::kw_short:
3762 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003763 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003764 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003765 case tok::kw_signed:
3766 case tok::kw_unsigned:
3767 case tok::kw__Complex:
3768 case tok::kw__Imaginary:
3769 case tok::kw_void:
3770 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003771 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003772 case tok::kw_char16_t:
3773 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003774 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003775 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003776 case tok::kw_float:
3777 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003778 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003779 case tok::kw__Bool:
3780 case tok::kw__Decimal32:
3781 case tok::kw__Decimal64:
3782 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003783 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003784
Guy Benyeib13621d2012-12-18 14:38:23 +00003785 // OpenCL specific types:
3786 case tok::kw_image1d_t:
3787 case tok::kw_image1d_array_t:
3788 case tok::kw_image1d_buffer_t:
3789 case tok::kw_image2d_t:
3790 case tok::kw_image2d_array_t:
3791 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003792 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003793 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003794
Chris Lattner99dc9142008-04-13 18:59:07 +00003795 // struct-or-union-specifier (C99) or class-specifier (C++)
3796 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003797 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003798 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003799 case tok::kw_union:
3800 // enum-specifier
3801 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003802
Reid Spencer5f016e22007-07-11 17:01:13 +00003803 // type-qualifier
3804 case tok::kw_const:
3805 case tok::kw_volatile:
3806 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003807
John McCallb8a8de32012-11-14 00:49:39 +00003808 // Debugger support.
3809 case tok::kw___unknown_anytype:
3810
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003811 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003812 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003813 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003814
Chris Lattner7c186be2008-10-20 00:25:30 +00003815 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3816 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003817 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003818
Steve Naroff239f0732008-12-25 14:16:32 +00003819 case tok::kw___cdecl:
3820 case tok::kw___stdcall:
3821 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003822 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003823 case tok::kw___w64:
3824 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003825 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003826 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003827 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003828
3829 case tok::kw___private:
3830 case tok::kw___local:
3831 case tok::kw___global:
3832 case tok::kw___constant:
3833 case tok::kw___read_only:
3834 case tok::kw___read_write:
3835 case tok::kw___write_only:
3836
Eli Friedman290eeb02009-06-08 23:27:34 +00003837 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003838
3839 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003840 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003841
Richard Smith4cf4a5e2013-03-28 01:55:44 +00003842 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00003843 case tok::kw__Atomic:
3844 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003845 }
3846}
3847
3848/// isDeclarationSpecifier() - Return true if the current token is part of a
3849/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003850///
3851/// \param DisambiguatingWithExpression True to indicate that the purpose of
3852/// this check is to disambiguate between an expression and a declaration.
3853bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003854 switch (Tok.getKind()) {
3855 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003856
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003857 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003858 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003859
Chris Lattner166a8fc2009-01-04 23:41:41 +00003860 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003861 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003862 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003863 return false;
John Thompson82287d12010-02-05 00:12:22 +00003864 if (TryAltiVecVectorToken())
3865 return true;
3866 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003867 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003868 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003869 // Annotate typenames and C++ scope specifiers. If we get one, just
3870 // recurse to handle whatever we get.
3871 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003872 return true;
3873 if (Tok.is(tok::identifier))
3874 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003875
Douglas Gregor9497a732010-09-16 01:51:54 +00003876 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00003877 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00003878 // expression is permitted, then this is probably a class message send
3879 // missing the initial '['. In this case, we won't consider this to be
3880 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00003881 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00003882 isStartOfObjCClassMessageMissingOpenBracket())
3883 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003884
John McCall9ba61662010-02-26 08:45:28 +00003885 return isDeclarationSpecifier();
3886
Chris Lattner166a8fc2009-01-04 23:41:41 +00003887 case tok::coloncolon: // ::foo::bar
3888 if (NextToken().is(tok::kw_new) || // ::new
3889 NextToken().is(tok::kw_delete)) // ::delete
3890 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003891
Chris Lattner166a8fc2009-01-04 23:41:41 +00003892 // Annotate typenames and C++ scope specifiers. If we get one, just
3893 // recurse to handle whatever we get.
3894 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003895 return true;
3896 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003897
Reid Spencer5f016e22007-07-11 17:01:13 +00003898 // storage-class-specifier
3899 case tok::kw_typedef:
3900 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003901 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003902 case tok::kw_static:
3903 case tok::kw_auto:
3904 case tok::kw_register:
3905 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003906
Douglas Gregor8d267c52011-09-09 02:06:17 +00003907 // Modules
3908 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00003909
John McCallb8a8de32012-11-14 00:49:39 +00003910 // Debugger support
3911 case tok::kw___unknown_anytype:
3912
Reid Spencer5f016e22007-07-11 17:01:13 +00003913 // type-specifiers
3914 case tok::kw_short:
3915 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003916 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003917 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003918 case tok::kw_signed:
3919 case tok::kw_unsigned:
3920 case tok::kw__Complex:
3921 case tok::kw__Imaginary:
3922 case tok::kw_void:
3923 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003924 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003925 case tok::kw_char16_t:
3926 case tok::kw_char32_t:
3927
Reid Spencer5f016e22007-07-11 17:01:13 +00003928 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003929 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003930 case tok::kw_float:
3931 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003932 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003933 case tok::kw__Bool:
3934 case tok::kw__Decimal32:
3935 case tok::kw__Decimal64:
3936 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003937 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003938
Guy Benyeib13621d2012-12-18 14:38:23 +00003939 // OpenCL specific types:
3940 case tok::kw_image1d_t:
3941 case tok::kw_image1d_array_t:
3942 case tok::kw_image1d_buffer_t:
3943 case tok::kw_image2d_t:
3944 case tok::kw_image2d_array_t:
3945 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003946 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003947 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003948
Chris Lattner99dc9142008-04-13 18:59:07 +00003949 // struct-or-union-specifier (C99) or class-specifier (C++)
3950 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003951 case tok::kw_struct:
3952 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00003953 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003954 // enum-specifier
3955 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003956
Reid Spencer5f016e22007-07-11 17:01:13 +00003957 // type-qualifier
3958 case tok::kw_const:
3959 case tok::kw_volatile:
3960 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003961
Reid Spencer5f016e22007-07-11 17:01:13 +00003962 // function-specifier
3963 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003964 case tok::kw_virtual:
3965 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00003966 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003967
Richard Smith4cd81c52013-01-29 09:02:09 +00003968 // alignment-specifier
3969 case tok::kw__Alignas:
3970
Richard Smith53aec2a2012-10-25 00:00:53 +00003971 // friend keyword.
3972 case tok::kw_friend:
3973
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003974 // static_assert-declaration
3975 case tok::kw__Static_assert:
3976
Chris Lattner1ef08762007-08-09 17:01:07 +00003977 // GNU typeof support.
3978 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003979
Chris Lattner1ef08762007-08-09 17:01:07 +00003980 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003981 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00003982
Richard Smith53aec2a2012-10-25 00:00:53 +00003983 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003984 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00003985 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00003986
Richard Smith4cf4a5e2013-03-28 01:55:44 +00003987 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00003988 case tok::kw__Atomic:
3989 return true;
3990
Chris Lattnerf3948c42008-07-26 03:38:44 +00003991 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3992 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003993 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003994
Douglas Gregord9d75e52011-04-27 05:41:15 +00003995 // typedef-name
3996 case tok::annot_typename:
3997 return !DisambiguatingWithExpression ||
3998 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00003999
Steve Naroff47f52092009-01-06 19:34:12 +00004000 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00004001 case tok::kw___cdecl:
4002 case tok::kw___stdcall:
4003 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004004 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00004005 case tok::kw___w64:
4006 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004007 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00004008 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004009 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004010 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004011
4012 case tok::kw___private:
4013 case tok::kw___local:
4014 case tok::kw___global:
4015 case tok::kw___constant:
4016 case tok::kw___read_only:
4017 case tok::kw___read_write:
4018 case tok::kw___write_only:
4019
Eli Friedman290eeb02009-06-08 23:27:34 +00004020 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00004021 }
4022}
4023
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004024bool Parser::isConstructorDeclarator() {
4025 TentativeParsingAction TPA(*this);
4026
4027 // Parse the C++ scope specifier.
4028 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00004029 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004030 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00004031 TPA.Revert();
4032 return false;
4033 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004034
4035 // Parse the constructor name.
4036 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4037 // We already know that we have a constructor name; just consume
4038 // the token.
4039 ConsumeToken();
4040 } else {
4041 TPA.Revert();
4042 return false;
4043 }
4044
Richard Smith22592862012-03-27 23:05:05 +00004045 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004046 if (Tok.isNot(tok::l_paren)) {
4047 TPA.Revert();
4048 return false;
4049 }
4050 ConsumeParen();
4051
Richard Smith22592862012-03-27 23:05:05 +00004052 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4053 // that we have a constructor.
4054 if (Tok.is(tok::r_paren) ||
4055 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004056 TPA.Revert();
4057 return true;
4058 }
4059
4060 // If we need to, enter the specified scope.
4061 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00004062 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004063 DeclScopeObj.EnterDeclaratorScope();
4064
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004065 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00004066 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004067 MaybeParseMicrosoftAttributes(Attrs);
4068
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004069 // Check whether the next token(s) are part of a declaration
4070 // specifier, in which case we have the start of a parameter and,
4071 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00004072 bool IsConstructor = false;
4073 if (isDeclarationSpecifier())
4074 IsConstructor = true;
4075 else if (Tok.is(tok::identifier) ||
4076 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4077 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4078 // This might be a parenthesized member name, but is more likely to
4079 // be a constructor declaration with an invalid argument type. Keep
4080 // looking.
4081 if (Tok.is(tok::annot_cxxscope))
4082 ConsumeToken();
4083 ConsumeToken();
4084
4085 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004086 // which must have one of the following syntactic forms (see the
4087 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004088 switch (Tok.getKind()) {
4089 case tok::l_paren:
4090 // C(X ( int));
4091 case tok::l_square:
4092 // C(X [ 5]);
4093 // C(X [ [attribute]]);
4094 case tok::coloncolon:
4095 // C(X :: Y);
4096 // C(X :: *p);
4097 case tok::r_paren:
4098 // C(X )
4099 // Assume this isn't a constructor, rather than assuming it's a
4100 // constructor with an unnamed parameter of an ill-formed type.
4101 break;
4102
4103 default:
4104 IsConstructor = true;
4105 break;
4106 }
4107 }
4108
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004109 TPA.Revert();
4110 return IsConstructor;
4111}
Reid Spencer5f016e22007-07-11 17:01:13 +00004112
4113/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004114/// type-qualifier-list: [C99 6.7.5]
4115/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004116/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004117/// [ only if VendorAttributesAllowed=true ]
4118/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004119/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004120/// [ only if VendorAttributesAllowed=true ]
4121/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004122/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004123/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004124///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004125void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4126 bool VendorAttributesAllowed,
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004127 bool CXX11AttributesAllowed,
4128 bool AtomicAllowed) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004129 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004130 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004131 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004132 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004133 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004134 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004135
4136 SourceLocation EndLoc;
4137
Reid Spencer5f016e22007-07-11 17:01:13 +00004138 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004139 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004140 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004141 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004142 SourceLocation Loc = Tok.getLocation();
4143
4144 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004145 case tok::code_completion:
4146 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004147 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004148
Reid Spencer5f016e22007-07-11 17:01:13 +00004149 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004150 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004151 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004152 break;
4153 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004154 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004155 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004156 break;
4157 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004158 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004159 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004160 break;
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004161 case tok::kw__Atomic:
4162 if (!AtomicAllowed)
4163 goto DoneWithTypeQuals;
4164 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4165 getLangOpts());
4166 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004167
4168 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004169 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004170 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004171 goto DoneWithTypeQuals;
4172 case tok::kw___private:
4173 case tok::kw___global:
4174 case tok::kw___local:
4175 case tok::kw___constant:
4176 case tok::kw___read_only:
4177 case tok::kw___write_only:
4178 case tok::kw___read_write:
4179 ParseOpenCLQualifiers(DS);
4180 break;
4181
Eli Friedman290eeb02009-06-08 23:27:34 +00004182 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004183 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004184 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004185 case tok::kw___cdecl:
4186 case tok::kw___stdcall:
4187 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004188 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004189 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004190 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004191 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004192 continue;
4193 }
4194 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004195 case tok::kw___pascal:
4196 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004197 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004198 continue;
4199 }
4200 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004201 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004202 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004203 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004204 continue; // do *not* consume the next token!
4205 }
4206 // otherwise, FALL THROUGH!
4207 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004208 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004209 // If this is not a type-qualifier token, we're done reading type
4210 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004211 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004212 if (EndLoc.isValid())
4213 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004214 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004215 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004216
Reid Spencer5f016e22007-07-11 17:01:13 +00004217 // If the specifier combination wasn't legal, issue a diagnostic.
4218 if (isInvalid) {
4219 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004220 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004221 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004222 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004223 }
4224}
4225
4226
4227/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4228///
4229void Parser::ParseDeclarator(Declarator &D) {
4230 /// This implements the 'declarator' production in the C grammar, then checks
4231 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004232 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004233}
4234
Richard Smith9988f282012-03-29 01:16:42 +00004235static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4236 if (Kind == tok::star || Kind == tok::caret)
4237 return true;
4238
4239 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4240 if (!Lang.CPlusPlus)
4241 return false;
4242
4243 return Kind == tok::amp || Kind == tok::ampamp;
4244}
4245
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004246/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4247/// is parsed by the function passed to it. Pass null, and the direct-declarator
4248/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004249/// ptr-operator production.
4250///
Richard Smith0706df42011-10-19 21:33:05 +00004251/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004252/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4253/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004254///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004255/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4256/// [C] pointer[opt] direct-declarator
4257/// [C++] direct-declarator
4258/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004259///
4260/// pointer: [C99 6.7.5]
4261/// '*' type-qualifier-list[opt]
4262/// '*' type-qualifier-list[opt] pointer
4263///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004264/// ptr-operator:
4265/// '*' cv-qualifier-seq[opt]
4266/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004267/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004268/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004269/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004270/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004271void Parser::ParseDeclaratorInternal(Declarator &D,
4272 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004273 if (Diags.hasAllExtensionsSilenced())
4274 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004275
Sebastian Redlf30208a2009-01-24 21:16:55 +00004276 // C++ member pointers start with a '::' or a nested-name.
4277 // Member pointers get special handling, since there's no place for the
4278 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004279 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004280 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4281 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004282 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4283 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004284 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004285 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004286
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004287 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004288 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004289 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004290 if (D.mayHaveIdentifier())
4291 D.getCXXScopeSpec() = SS;
4292 else
4293 AnnotateScopeToken(SS, true);
4294
Sebastian Redlf30208a2009-01-24 21:16:55 +00004295 if (DirectDeclParser)
4296 (this->*DirectDeclParser)(D);
4297 return;
4298 }
4299
4300 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004301 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004302 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004303 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004304 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004305
4306 // Recurse to parse whatever is left.
4307 ParseDeclaratorInternal(D, DirectDeclParser);
4308
4309 // Sema will have to catch (syntactically invalid) pointers into global
4310 // scope. It has to catch pointers into namespace scope anyway.
4311 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004312 Loc),
4313 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004314 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004315 return;
4316 }
4317 }
4318
4319 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004320 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004321 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004322 if (DirectDeclParser)
4323 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004324 return;
4325 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004326
Sebastian Redl05532f22009-03-15 22:02:01 +00004327 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4328 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004329 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004330 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004331
Chris Lattner9af55002009-03-27 04:18:06 +00004332 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004333 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004334 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004335
Richard Smith6ee326a2012-04-10 01:32:12 +00004336 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004337 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004338 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004339
Reid Spencer5f016e22007-07-11 17:01:13 +00004340 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004341 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004342 if (Kind == tok::star)
4343 // Remember that we parsed a pointer type, and remember the type-quals.
4344 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004345 DS.getConstSpecLoc(),
4346 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004347 DS.getRestrictSpecLoc()),
4348 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004349 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004350 else
4351 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004352 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004353 Loc),
4354 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004355 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004356 } else {
4357 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004358 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004359
Sebastian Redl743de1f2009-03-23 00:00:23 +00004360 // Complain about rvalue references in C++03, but then go on and build
4361 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004362 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004363 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004364 diag::warn_cxx98_compat_rvalue_reference :
4365 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004366
Richard Smith6ee326a2012-04-10 01:32:12 +00004367 // GNU-style and C++11 attributes are allowed here, as is restrict.
4368 ParseTypeQualifierListOpt(DS);
4369 D.ExtendWithDeclSpec(DS);
4370
Reid Spencer5f016e22007-07-11 17:01:13 +00004371 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4372 // cv-qualifiers are introduced through the use of a typedef or of a
4373 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004374 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4375 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4376 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004377 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004378 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4379 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004380 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004381 // 'restrict' is permitted as an extension.
4382 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4383 Diag(DS.getAtomicSpecLoc(),
4384 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Reid Spencer5f016e22007-07-11 17:01:13 +00004385 }
4386
4387 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004388 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004389
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004390 if (D.getNumTypeObjects() > 0) {
4391 // C++ [dcl.ref]p4: There shall be no references to references.
4392 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4393 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004394 if (const IdentifierInfo *II = D.getIdentifier())
4395 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4396 << II;
4397 else
4398 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4399 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004400
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004401 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004402 // can go ahead and build the (technically ill-formed)
4403 // declarator: reference collapsing will take care of it.
4404 }
4405 }
4406
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004407 // Remember that we parsed a reference type.
Chris Lattner76549142008-02-21 01:32:26 +00004408 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004409 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004410 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004411 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004412 }
4413}
4414
Richard Smith9988f282012-03-29 01:16:42 +00004415static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4416 SourceLocation EllipsisLoc) {
4417 if (EllipsisLoc.isValid()) {
4418 FixItHint Insertion;
4419 if (!D.getEllipsisLoc().isValid()) {
4420 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4421 D.setEllipsisLoc(EllipsisLoc);
4422 }
4423 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4424 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4425 }
4426}
4427
Reid Spencer5f016e22007-07-11 17:01:13 +00004428/// ParseDirectDeclarator
4429/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004430/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004431/// '(' declarator ')'
4432/// [GNU] '(' attributes declarator ')'
4433/// [C90] direct-declarator '[' constant-expression[opt] ']'
4434/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4435/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4436/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4437/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004438/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4439/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004440/// direct-declarator '(' parameter-type-list ')'
4441/// direct-declarator '(' identifier-list[opt] ')'
4442/// [GNU] direct-declarator '(' parameter-forward-declarations
4443/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004444/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4445/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004446/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4447/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4448/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004449/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004450/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004451///
4452/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004453/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004454/// '::'[opt] nested-name-specifier[opt] type-name
4455///
4456/// id-expression: [C++ 5.1]
4457/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004458/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004459///
4460/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004461/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004462/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004463/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004464/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004465/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004466///
Richard Smith5d8388c2012-03-27 01:42:32 +00004467/// Note, any additional constructs added here may need corresponding changes
4468/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004469void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004470 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004471
David Blaikie4e4d0842012-03-11 07:00:24 +00004472 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004473 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004474 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004475 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4476 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004477 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004478 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004479 }
4480
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004481 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004482 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004483 // Change the declaration context for name lookup, until this function
4484 // is exited (and the declarator has been parsed).
4485 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004486 }
4487
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004488 // C++0x [dcl.fct]p14:
4489 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004490 // of a parameter-declaration-clause without a preceding comma. In
4491 // this case, the ellipsis is parsed as part of the
4492 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004493 // parameter pack that has not been expanded; otherwise, it is parsed
4494 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004495 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004496 !((D.getContext() == Declarator::PrototypeContext ||
4497 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004498 NextToken().is(tok::r_paren) &&
Richard Smith30f2a742013-02-20 20:19:27 +00004499 !D.hasGroupingParens() &&
Richard Smith9988f282012-03-29 01:16:42 +00004500 !Actions.containsUnexpandedParameterPacks(D))) {
4501 SourceLocation EllipsisLoc = ConsumeToken();
4502 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4503 // The ellipsis was put in the wrong place. Recover, and explain to
4504 // the user what they should have done.
4505 ParseDeclarator(D);
4506 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4507 return;
4508 } else
4509 D.setEllipsisLoc(EllipsisLoc);
4510
4511 // The ellipsis can't be followed by a parenthesized declarator. We
4512 // check for that in ParseParenDeclarator, after we have disambiguated
4513 // the l_paren token.
4514 }
4515
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004516 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4517 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4518 // We found something that indicates the start of an unqualified-id.
4519 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004520 bool AllowConstructorName;
4521 if (D.getDeclSpec().hasTypeSpecifier())
4522 AllowConstructorName = false;
4523 else if (D.getCXXScopeSpec().isSet())
4524 AllowConstructorName =
4525 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00004526 D.getContext() == Declarator::MemberContext);
John McCallba9d8532010-04-13 06:39:49 +00004527 else
4528 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4529
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004530 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004531 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4532 /*EnteringContext=*/true,
4533 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004534 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004535 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004536 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004537 D.getName()) ||
4538 // Once we're past the identifier, if the scope was bad, mark the
4539 // whole declarator bad.
4540 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004541 D.SetIdentifier(0, Tok.getLocation());
4542 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004543 } else {
4544 // Parsed the unqualified-id; update range information and move along.
4545 if (D.getSourceRange().getBegin().isInvalid())
4546 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4547 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004548 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004549 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004550 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004551 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004552 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004553 "There's a C++-specific check for tok::identifier above");
4554 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4555 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4556 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004557 goto PastIdentifier;
4558 }
Richard Smith9988f282012-03-29 01:16:42 +00004559
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004560 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004561 // direct-declarator: '(' declarator ')'
4562 // direct-declarator: '(' attributes declarator ')'
4563 // Example: 'char (*X)' or 'int (*XX)(void)'
4564 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004565
4566 // If the declarator was parenthesized, we entered the declarator
4567 // scope when parsing the parenthesized declarator, then exited
4568 // the scope already. Re-enter the scope, if we need to.
4569 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004570 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004571 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004572 if (!D.isInvalidType() &&
4573 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004574 // Change the declaration context for name lookup, until this function
4575 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004576 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004577 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004578 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004579 // This could be something simple like "int" (in which case the declarator
4580 // portion is empty), if an abstract-declarator is allowed.
4581 D.SetIdentifier(0, Tok.getLocation());
Richard Smith30f2a742013-02-20 20:19:27 +00004582
4583 // The grammar for abstract-pack-declarator does not allow grouping parens.
4584 // FIXME: Revisit this once core issue 1488 is resolved.
4585 if (D.hasEllipsis() && D.hasGroupingParens())
4586 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4587 diag::ext_abstract_pack_declarator_parens);
Reid Spencer5f016e22007-07-11 17:01:13 +00004588 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004589 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004590 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004591 if (D.getContext() == Declarator::MemberContext)
4592 Diag(Tok, diag::err_expected_member_name_or_semi)
4593 << D.getDeclSpec().getSourceRange();
Richard Trieudb55c04c2013-01-26 02:31:38 +00004594 else if (getLangOpts().CPlusPlus) {
4595 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4596 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4597 else
4598 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4599 } else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004600 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004601 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004602 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004603 }
Mike Stump1eb44332009-09-09 15:08:12 +00004604
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004605 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004606 assert(D.isPastIdentifier() &&
4607 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004608
Richard Smith6ee326a2012-04-10 01:32:12 +00004609 // Don't parse attributes unless we have parsed an unparenthesized name.
4610 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004611 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004612
Reid Spencer5f016e22007-07-11 17:01:13 +00004613 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004614 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004615 // Enter function-declaration scope, limiting any declarators to the
4616 // function prototype scope, including parameter declarators.
4617 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004618 Scope::FunctionPrototypeScope|Scope::DeclScope|
4619 (D.isFunctionDeclaratorAFunctionDeclaration()
4620 ? Scope::FunctionDeclarationScope : 0));
4621
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004622 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4623 // In such a case, check if we actually have a function declarator; if it
4624 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004625 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004626 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4627 // The name of the declarator, if any, is tentatively declared within
4628 // a possible direct initializer.
4629 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4630 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4631 TentativelyDeclaredIdentifiers.pop_back();
4632 if (!IsFunctionDecl)
4633 break;
4634 }
John McCall0b7e6782011-03-24 11:26:52 +00004635 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004636 BalancedDelimiterTracker T(*this, tok::l_paren);
4637 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004638 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004639 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004640 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004641 ParseBracketDeclarator(D);
4642 } else {
4643 break;
4644 }
4645 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004646}
Reid Spencer5f016e22007-07-11 17:01:13 +00004647
Chris Lattneref4715c2008-04-06 05:45:57 +00004648/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4649/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004650/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004651/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4652///
4653/// direct-declarator:
4654/// '(' declarator ')'
4655/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004656/// direct-declarator '(' parameter-type-list ')'
4657/// direct-declarator '(' identifier-list[opt] ')'
4658/// [GNU] direct-declarator '(' parameter-forward-declarations
4659/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004660///
4661void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004662 BalancedDelimiterTracker T(*this, tok::l_paren);
4663 T.consumeOpen();
4664
Chris Lattneref4715c2008-04-06 05:45:57 +00004665 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004666
Chris Lattner7399ee02008-10-20 02:05:46 +00004667 // Eat any attributes before we look at whether this is a grouping or function
4668 // declarator paren. If this is a grouping paren, the attribute applies to
4669 // the type being built up, for example:
4670 // int (__attribute__(()) *x)(long y)
4671 // If this ends up not being a grouping paren, the attribute applies to the
4672 // first argument, for example:
4673 // int (__attribute__(()) int x)
4674 // In either case, we need to eat any attributes to be able to determine what
4675 // sort of paren this is.
4676 //
John McCall0b7e6782011-03-24 11:26:52 +00004677 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004678 bool RequiresArg = false;
4679 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004680 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004681
Chris Lattner7399ee02008-10-20 02:05:46 +00004682 // We require that the argument list (if this is a non-grouping paren) be
4683 // present even if the attribute list was empty.
4684 RequiresArg = true;
4685 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004686
Steve Naroff239f0732008-12-25 14:16:32 +00004687 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004688 ParseMicrosoftTypeAttributes(attrs);
4689
Dawn Perchik52fc3142010-09-03 01:29:35 +00004690 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004691 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004692 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004693
Chris Lattneref4715c2008-04-06 05:45:57 +00004694 // If we haven't past the identifier yet (or where the identifier would be
4695 // stored, if this is an abstract declarator), then this is probably just
4696 // grouping parens. However, if this could be an abstract-declarator, then
4697 // this could also be the start of function arguments (consider 'void()').
4698 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004699
Chris Lattneref4715c2008-04-06 05:45:57 +00004700 if (!D.mayOmitIdentifier()) {
4701 // If this can't be an abstract-declarator, this *must* be a grouping
4702 // paren, because we haven't seen the identifier yet.
4703 isGrouping = true;
4704 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004705 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4706 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004707 isDeclarationSpecifier() || // 'int(int)' is a function.
4708 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004709 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4710 // considered to be a type, not a K&R identifier-list.
4711 isGrouping = false;
4712 } else {
4713 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4714 isGrouping = true;
4715 }
Mike Stump1eb44332009-09-09 15:08:12 +00004716
Chris Lattneref4715c2008-04-06 05:45:57 +00004717 // If this is a grouping paren, handle:
4718 // direct-declarator: '(' declarator ')'
4719 // direct-declarator: '(' attributes declarator ')'
4720 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004721 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4722 D.setEllipsisLoc(SourceLocation());
4723
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004724 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004725 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004726 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004727 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004728 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004729 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004730 T.getCloseLocation()),
4731 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004732
4733 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004734
4735 // An ellipsis cannot be placed outside parentheses.
4736 if (EllipsisLoc.isValid())
4737 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4738
Chris Lattneref4715c2008-04-06 05:45:57 +00004739 return;
4740 }
Mike Stump1eb44332009-09-09 15:08:12 +00004741
Chris Lattneref4715c2008-04-06 05:45:57 +00004742 // Okay, if this wasn't a grouping paren, it must be the start of a function
4743 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004744 // identifier (and remember where it would have been), then call into
4745 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004746 D.SetIdentifier(0, Tok.getLocation());
4747
David Blaikie42d6d0c2011-12-04 05:04:18 +00004748 // Enter function-declaration scope, limiting any declarators to the
4749 // function prototype scope, including parameter declarators.
4750 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004751 Scope::FunctionPrototypeScope | Scope::DeclScope |
4752 (D.isFunctionDeclaratorAFunctionDeclaration()
4753 ? Scope::FunctionDeclarationScope : 0));
Richard Smithb9c62612012-07-30 21:30:52 +00004754 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004755 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004756}
4757
4758/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4759/// declarator D up to a paren, which indicates that we are parsing function
4760/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004761///
Richard Smith6ee326a2012-04-10 01:32:12 +00004762/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4763/// immediately after the open paren - they should be considered to be the
4764/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004765///
Richard Smith6ee326a2012-04-10 01:32:12 +00004766/// If RequiresArg is true, then the first argument of the function is required
4767/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004768///
Richard Smith6ee326a2012-04-10 01:32:12 +00004769/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4770/// (C++11) ref-qualifier[opt], exception-specification[opt],
4771/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4772///
4773/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004774/// dynamic-exception-specification
4775/// noexcept-specification
4776///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004777void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004778 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004779 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004780 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004781 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004782 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004783 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004784 // lparen is already consumed!
4785 assert(D.isPastIdentifier() && "Should not call before identifier!");
4786
4787 // This should be true when the function has typed arguments.
4788 // Otherwise, it is treated as a K&R-style function.
4789 bool HasProto = false;
4790 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004791 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004792 // Remember where we see an ellipsis, if any.
4793 SourceLocation EllipsisLoc;
4794
4795 DeclSpec DS(AttrFactory);
4796 bool RefQualifierIsLValueRef = true;
4797 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004798 SourceLocation ConstQualifierLoc;
4799 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004800 ExceptionSpecificationType ESpecType = EST_None;
4801 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004802 SmallVector<ParsedType, 2> DynamicExceptions;
4803 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004804 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004805 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004806 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004807
James Molloy16f1f712012-02-29 10:24:19 +00004808 Actions.ActOnStartFunctionDeclarator();
4809
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004810 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4811 EndLoc is the end location for the function declarator.
4812 They differ for trailing return types. */
4813 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004814 SourceLocation LParenLoc, RParenLoc;
4815 LParenLoc = Tracker.getOpenLocation();
4816 StartLoc = LParenLoc;
4817
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004818 if (isFunctionDeclaratorIdentifierList()) {
4819 if (RequiresArg)
4820 Diag(Tok, diag::err_argument_required_after_attribute);
4821
4822 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4823
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004824 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004825 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004826 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004827 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004828 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004829 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004830 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004831 else if (RequiresArg)
4832 Diag(Tok, diag::err_argument_required_after_attribute);
4833
David Blaikie4e4d0842012-03-11 07:00:24 +00004834 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004835
4836 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004837 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004838 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004839 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004840 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004841
David Blaikie4e4d0842012-03-11 07:00:24 +00004842 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004843 // FIXME: Accept these components in any order, and produce fixits to
4844 // correct the order if the user gets it wrong. Ideally we should deal
4845 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004846
4847 // Parse cv-qualifier-seq[opt].
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004848 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
4849 /*CXX11AttributesAllowed*/ false,
4850 /*AtomicAllowed*/ false);
Richard Smith6ee326a2012-04-10 01:32:12 +00004851 if (!DS.getSourceRange().getEnd().isInvalid()) {
4852 EndLoc = DS.getSourceRange().getEnd();
4853 ConstQualifierLoc = DS.getConstSpecLoc();
4854 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4855 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004856
4857 // Parse ref-qualifier[opt].
4858 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004859 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004860 diag::warn_cxx98_compat_ref_qualifier :
4861 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004862
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004863 RefQualifierIsLValueRef = Tok.is(tok::amp);
4864 RefQualifierLoc = ConsumeToken();
4865 EndLoc = RefQualifierLoc;
4866 }
4867
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004868 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00004869 // If a declaration declares a member function or member function
4870 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004871 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00004872 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004873 // declarator.
Richard Smithd9227792013-03-15 00:41:52 +00004874 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosier8decdee2012-06-26 22:30:43 +00004875 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00004876 getLangOpts().CPlusPlus11 &&
Richard Smithd9227792013-03-15 00:41:52 +00004877 (D.getContext() == Declarator::MemberContext
4878 ? !D.getDeclSpec().isFriendSpecified()
4879 : D.getContext() == Declarator::FileContext &&
4880 D.getCXXScopeSpec().isValid() &&
4881 Actions.CurContext->isRecord());
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004882 Sema::CXXThisScopeRAII ThisScope(Actions,
4883 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00004884 DS.getTypeQualifiers() |
4885 (D.getDeclSpec().isConstexprSpecified()
4886 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004887 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004888
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004889 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004890 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004891 DynamicExceptions,
4892 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004893 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004894 if (ESpecType != EST_None)
4895 EndLoc = ESpecRange.getEnd();
4896
Richard Smith6ee326a2012-04-10 01:32:12 +00004897 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4898 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00004899 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004900
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004901 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004902 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00004903 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004904 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004905 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4906 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004907 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00004908 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004909 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004910 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004911 }
4912 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004913 }
4914
4915 // Remember that we parsed a function type, and remember the attributes.
4916 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004917 IsAmbiguous,
4918 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004919 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004920 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004921 DS.getTypeQualifiers(),
4922 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004923 RefQualifierLoc, ConstQualifierLoc,
4924 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004925 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004926 ESpecType, ESpecRange.getBegin(),
4927 DynamicExceptions.data(),
4928 DynamicExceptionRanges.data(),
4929 DynamicExceptions.size(),
4930 NoexceptExpr.isUsable() ?
4931 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004932 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004933 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004934 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004935
4936 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004937}
4938
4939/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4940/// identifier list form for a K&R-style function: void foo(a,b,c)
4941///
4942/// Note that identifier-lists are only allowed for normal declarators, not for
4943/// abstract-declarators.
4944bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004945 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004946 && Tok.is(tok::identifier)
4947 && !TryAltiVecVectorToken()
4948 // K&R identifier lists can't have typedefs as identifiers, per C99
4949 // 6.7.5.3p11.
4950 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4951 // Identifier lists follow a really simple grammar: the identifiers can
4952 // be followed *only* by a ", identifier" or ")". However, K&R
4953 // identifier lists are really rare in the brave new modern world, and
4954 // it is very common for someone to typo a type in a non-K&R style
4955 // list. If we are presented with something like: "void foo(intptr x,
4956 // float y)", we don't want to start parsing the function declarator as
4957 // though it is a K&R style declarator just because intptr is an
4958 // invalid type.
4959 //
4960 // To handle this, we check to see if the token after the first
4961 // identifier is a "," or ")". Only then do we parse it as an
4962 // identifier list.
4963 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4964}
4965
4966/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4967/// we found a K&R-style identifier list instead of a typed parameter list.
4968///
4969/// After returning, ParamInfo will hold the parsed parameters.
4970///
4971/// identifier-list: [C99 6.7.5]
4972/// identifier
4973/// identifier-list ',' identifier
4974///
4975void Parser::ParseFunctionDeclaratorIdentifierList(
4976 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004977 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004978 // If there was no identifier specified for the declarator, either we are in
4979 // an abstract-declarator, or we are in a parameter declarator which was found
4980 // to be abstract. In abstract-declarators, identifier lists are not valid:
4981 // diagnose this.
4982 if (!D.getIdentifier())
4983 Diag(Tok, diag::ext_ident_list_in_param);
4984
4985 // Maintain an efficient lookup of params we have seen so far.
4986 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4987
4988 while (1) {
4989 // If this isn't an identifier, report the error and skip until ')'.
4990 if (Tok.isNot(tok::identifier)) {
4991 Diag(Tok, diag::err_expected_ident);
4992 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4993 // Forget we parsed anything.
4994 ParamInfo.clear();
4995 return;
4996 }
4997
4998 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4999
5000 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5001 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5002 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5003
5004 // Verify that the argument identifier has not already been mentioned.
5005 if (!ParamsSoFar.insert(ParmII)) {
5006 Diag(Tok, diag::err_param_redefinition) << ParmII;
5007 } else {
5008 // Remember this identifier in ParamInfo.
5009 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5010 Tok.getLocation(),
5011 0));
5012 }
5013
5014 // Eat the identifier.
5015 ConsumeToken();
5016
5017 // The list continues if we see a comma.
5018 if (Tok.isNot(tok::comma))
5019 break;
5020 ConsumeToken();
5021 }
5022}
5023
5024/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5025/// after the opening parenthesis. This function will not parse a K&R-style
5026/// identifier list.
5027///
Richard Smith6ce48a72012-04-11 04:01:28 +00005028/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5029/// caller parsed those arguments immediately after the open paren - they should
5030/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005031///
5032/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5033/// be the location of the ellipsis, if any was parsed.
5034///
Reid Spencer5f016e22007-07-11 17:01:13 +00005035/// parameter-type-list: [C99 6.7.5]
5036/// parameter-list
5037/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00005038/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00005039///
5040/// parameter-list: [C99 6.7.5]
5041/// parameter-declaration
5042/// parameter-list ',' parameter-declaration
5043///
5044/// parameter-declaration: [C99 6.7.5]
5045/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00005046/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00005047/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00005048/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00005049/// declaration-specifiers abstract-declarator[opt]
5050/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00005051/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00005052/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00005053/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00005054///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005055void Parser::ParseParameterDeclarationClause(
5056 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00005057 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005058 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005059 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00005060
Chris Lattnerf97409f2008-04-06 06:57:35 +00005061 while (1) {
5062 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00005063 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5064 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00005065 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00005066 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00005067 }
Mike Stump1eb44332009-09-09 15:08:12 +00005068
Chris Lattnerf97409f2008-04-06 06:57:35 +00005069 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00005070 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00005071 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005072
Richard Smith6ce48a72012-04-11 04:01:28 +00005073 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005074 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00005075
John McCall7f040a92010-12-24 02:08:15 +00005076 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00005077 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00005078
5079 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00005080
5081 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00005082 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005083 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00005084 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5085 // too much hassle.
5086 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00005087
Chris Lattnere64c5492009-02-27 18:38:20 +00005088 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00005089
Chris Lattnerf97409f2008-04-06 06:57:35 +00005090 // Parse the declarator. This is "PrototypeContext", because we must
5091 // accept either 'declarator' or 'abstract-declarator' here.
5092 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5093 ParseDeclarator(ParmDecl);
5094
5095 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00005096 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00005097
Chris Lattnerf97409f2008-04-06 06:57:35 +00005098 // Remember this parsed parameter in ParamInfo.
5099 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00005100
Douglas Gregor72b505b2008-12-16 21:30:33 +00005101 // DefArgToks is used when the parsing of default arguments needs
5102 // to be delayed.
5103 CachedTokens *DefArgToks = 0;
5104
Chris Lattnerf97409f2008-04-06 06:57:35 +00005105 // If no parameter was specified, verify that *something* was specified,
5106 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00005107 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5108 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005109 // Completely missing, emit error.
5110 Diag(DSStart, diag::err_missing_param);
5111 } else {
5112 // Otherwise, we have something. Add it and let semantic analysis try
5113 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005114
Chris Lattnerf97409f2008-04-06 06:57:35 +00005115 // Inform the actions module about the parameter declarator, so it gets
5116 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00005117 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00005118
5119 // Parse the default argument, if any. We parse the default
5120 // arguments in all dialects; the semantic analysis in
5121 // ActOnParamDefaultArgument will reject the default argument in
5122 // C.
5123 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005124 SourceLocation EqualLoc = Tok.getLocation();
5125
Chris Lattner04421082008-04-08 04:40:51 +00005126 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005127 if (D.getContext() == Declarator::MemberContext) {
5128 // If we're inside a class definition, cache the tokens
5129 // corresponding to the default argument. We'll actually parse
5130 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005131 // FIXME: Can we use a smart pointer for Toks?
5132 DefArgToks = new CachedTokens;
5133
Mike Stump1eb44332009-09-09 15:08:12 +00005134 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00005135 /*StopAtSemi=*/true,
5136 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005137 delete DefArgToks;
5138 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005139 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005140 } else {
5141 // Mark the end of the default argument so that we know when to
5142 // stop when we parse it later on.
5143 Token DefArgEnd;
5144 DefArgEnd.startToken();
5145 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5146 DefArgEnd.setLocation(Tok.getLocation());
5147 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005148 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005149 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005150 }
Chris Lattner04421082008-04-08 04:40:51 +00005151 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005152 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005153 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005154
Chad Rosier8decdee2012-06-26 22:30:43 +00005155 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005156 // used.
5157 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005158 Sema::PotentiallyEvaluatedIfUsed,
5159 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005160
Sebastian Redl84407ba2012-03-14 15:54:00 +00005161 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005162 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005163 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005164 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005165 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005166 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005167 if (DefArgResult.isInvalid()) {
5168 Actions.ActOnParamDefaultArgumentError(Param);
5169 SkipUntil(tok::comma, tok::r_paren, true, true);
5170 } else {
5171 // Inform the actions module about the default argument
5172 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005173 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005174 }
Chris Lattner04421082008-04-08 04:40:51 +00005175 }
5176 }
Mike Stump1eb44332009-09-09 15:08:12 +00005177
5178 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5179 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00005180 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005181 }
5182
5183 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005184 if (Tok.isNot(tok::comma)) {
5185 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005186 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005187
David Blaikie4e4d0842012-03-11 07:00:24 +00005188 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005189 // We have ellipsis without a preceding ',', which is ill-formed
5190 // in C. Complain and provide the fix.
5191 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005192 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005193 }
5194 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005195
Douglas Gregored5d6512009-09-22 21:41:40 +00005196 break;
5197 }
Mike Stump1eb44332009-09-09 15:08:12 +00005198
Chris Lattnerf97409f2008-04-06 06:57:35 +00005199 // Consume the comma.
5200 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005201 }
Mike Stump1eb44332009-09-09 15:08:12 +00005202
Chris Lattner66d28652008-04-06 06:34:08 +00005203}
Chris Lattneref4715c2008-04-06 05:45:57 +00005204
Reid Spencer5f016e22007-07-11 17:01:13 +00005205/// [C90] direct-declarator '[' constant-expression[opt] ']'
5206/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5207/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5208/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5209/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005210/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5211/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005212void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005213 if (CheckProhibitedCXX11Attribute())
5214 return;
5215
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005216 BalancedDelimiterTracker T(*this, tok::l_square);
5217 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005218
Chris Lattner378c7e42008-12-18 07:27:21 +00005219 // C array syntax has many features, but by-far the most common is [] and [4].
5220 // This code does a fast path to handle some of the most obvious cases.
5221 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005222 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005223 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005224 MaybeParseCXX11Attributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005225
Chris Lattner378c7e42008-12-18 07:27:21 +00005226 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005227 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005228 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005229 T.getOpenLocation(),
5230 T.getCloseLocation()),
5231 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005232 return;
5233 } else if (Tok.getKind() == tok::numeric_constant &&
5234 GetLookAheadToken(1).is(tok::r_square)) {
5235 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005236 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005237 ConsumeToken();
5238
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005239 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005240 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005241 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005242
Chris Lattner378c7e42008-12-18 07:27:21 +00005243 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005244 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005245 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005246 T.getOpenLocation(),
5247 T.getCloseLocation()),
5248 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005249 return;
5250 }
Mike Stump1eb44332009-09-09 15:08:12 +00005251
Reid Spencer5f016e22007-07-11 17:01:13 +00005252 // If valid, this location is the position where we read the 'static' keyword.
5253 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005254 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005255 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005256
Reid Spencer5f016e22007-07-11 17:01:13 +00005257 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005258 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005259 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005260 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005261
Reid Spencer5f016e22007-07-11 17:01:13 +00005262 // If we haven't already read 'static', check to see if there is one after the
5263 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005264 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005265 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005266
Reid Spencer5f016e22007-07-11 17:01:13 +00005267 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5268 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005269 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005270
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005271 // Handle the case where we have '[*]' as the array size. However, a leading
5272 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005273 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005274 // infrequent, use of lookahead is not costly here.
5275 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005276 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005277
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005278 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005279 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005280 StaticLoc = SourceLocation(); // Drop the static.
5281 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005282 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005283 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005284 // Note, in C89, this production uses the constant-expr production instead
5285 // of assignment-expr. The only difference is that assignment-expr allows
5286 // things like '=' and '*='. Sema rejects these in C89 mode because they
5287 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005288
Douglas Gregore0762c92009-06-19 23:52:42 +00005289 // Parse the constant-expression or assignment-expression now (depending
5290 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005291 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005292 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005293 } else {
5294 EnterExpressionEvaluationContext Unevaluated(Actions,
5295 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005296 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005297 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005298 }
Mike Stump1eb44332009-09-09 15:08:12 +00005299
Reid Spencer5f016e22007-07-11 17:01:13 +00005300 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005301 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005302 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005303 // If the expression was invalid, skip it.
5304 SkipUntil(tok::r_square);
5305 return;
5306 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005307
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005308 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005309
John McCall0b7e6782011-03-24 11:26:52 +00005310 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005311 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005312
Chris Lattner378c7e42008-12-18 07:27:21 +00005313 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005314 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005315 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005316 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005317 T.getOpenLocation(),
5318 T.getCloseLocation()),
5319 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005320}
5321
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005322/// [GNU] typeof-specifier:
5323/// typeof ( expressions )
5324/// typeof ( type-name )
5325/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005326///
5327void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005328 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005329 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005330 SourceLocation StartLoc = ConsumeToken();
5331
John McCallcfb708c2010-01-13 20:03:27 +00005332 const bool hasParens = Tok.is(tok::l_paren);
5333
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005334 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5335 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005336
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005337 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005338 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005339 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005340 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5341 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005342 if (hasParens)
5343 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005344
5345 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005346 // FIXME: Not accurate, the range gets one token more than it should.
5347 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005348 else
5349 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005350
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005351 if (isCastExpr) {
5352 if (!CastTy) {
5353 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005354 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005355 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005356
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005357 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005358 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005359 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5360 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005361 DiagID, CastTy))
5362 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005363 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005364 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005365
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005366 // If we get here, the operand to the typeof was an expresion.
5367 if (Operand.isInvalid()) {
5368 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005369 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005370 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005371
Eli Friedman71b8fb52012-01-21 01:01:51 +00005372 // We might need to transform the operand if it is potentially evaluated.
5373 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5374 if (Operand.isInvalid()) {
5375 DS.SetTypeSpecError();
5376 return;
5377 }
5378
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005379 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005380 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005381 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5382 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005383 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005384 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005385}
Chris Lattner1b492422010-02-28 18:33:55 +00005386
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005387/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005388/// _Atomic ( type-name )
5389///
5390void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005391 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5392 "Not an atomic specifier");
Eli Friedmanb001de72011-10-06 23:00:33 +00005393
5394 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005395 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005396 if (T.consumeOpen())
Eli Friedmanb001de72011-10-06 23:00:33 +00005397 return;
Eli Friedmanb001de72011-10-06 23:00:33 +00005398
5399 TypeResult Result = ParseTypeName();
5400 if (Result.isInvalid()) {
5401 SkipUntil(tok::r_paren);
5402 return;
5403 }
5404
5405 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005406 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005407
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005408 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005409 return;
5410
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005411 DS.setTypeofParensRange(T.getRange());
5412 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005413
5414 const char *PrevSpec = 0;
5415 unsigned DiagID;
5416 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5417 DiagID, Result.release()))
5418 Diag(StartLoc, DiagID) << PrevSpec;
5419}
5420
Chris Lattner1b492422010-02-28 18:33:55 +00005421
5422/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5423/// from TryAltiVecVectorToken.
5424bool Parser::TryAltiVecVectorTokenOutOfLine() {
5425 Token Next = NextToken();
5426 switch (Next.getKind()) {
5427 default: return false;
5428 case tok::kw_short:
5429 case tok::kw_long:
5430 case tok::kw_signed:
5431 case tok::kw_unsigned:
5432 case tok::kw_void:
5433 case tok::kw_char:
5434 case tok::kw_int:
5435 case tok::kw_float:
5436 case tok::kw_double:
5437 case tok::kw_bool:
5438 case tok::kw___pixel:
5439 Tok.setKind(tok::kw___vector);
5440 return true;
5441 case tok::identifier:
5442 if (Next.getIdentifierInfo() == Ident_pixel) {
5443 Tok.setKind(tok::kw___vector);
5444 return true;
5445 }
5446 return false;
5447 }
5448}
5449
5450bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5451 const char *&PrevSpec, unsigned &DiagID,
5452 bool &isInvalid) {
5453 if (Tok.getIdentifierInfo() == Ident_vector) {
5454 Token Next = NextToken();
5455 switch (Next.getKind()) {
5456 case tok::kw_short:
5457 case tok::kw_long:
5458 case tok::kw_signed:
5459 case tok::kw_unsigned:
5460 case tok::kw_void:
5461 case tok::kw_char:
5462 case tok::kw_int:
5463 case tok::kw_float:
5464 case tok::kw_double:
5465 case tok::kw_bool:
5466 case tok::kw___pixel:
5467 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5468 return true;
5469 case tok::identifier:
5470 if (Next.getIdentifierInfo() == Ident_pixel) {
5471 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5472 return true;
5473 }
5474 break;
5475 default:
5476 break;
5477 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005478 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005479 DS.isTypeAltiVecVector()) {
5480 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5481 return true;
5482 }
5483 return false;
5484}