blob: 990a9097acf2d2711930886df509bcc7bac1300d [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.
1530 if (FRI && Tok.is(tok::colon)) {
1531 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001532 if (Tok.is(tok::l_brace))
1533 FRI->RangeExpr = ParseBraceInitializer();
1534 else
1535 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001536 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1537 Actions.ActOnCXXForRangeDecl(ThisDecl);
1538 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001539 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001540 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1541 }
1542
Chris Lattner5f9e2722011-07-23 10:55:15 +00001543 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001544 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001545 if (LateParsedAttrs.size() > 0)
1546 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001547 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001548 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001549 DeclsInGroup.push_back(FirstDecl);
1550
Richard Smith0706df42011-10-19 21:33:05 +00001551 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001552
John McCalld8ac0572009-11-03 19:26:08 +00001553 // If we don't have a comma, it is either the end of the list (a ';') or an
1554 // error, bail out.
1555 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001556 SourceLocation CommaLoc = ConsumeToken();
1557
1558 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1559 // This comma was followed by a line-break and something which can't be
1560 // the start of a declarator. The comma was probably a typo for a
1561 // semicolon.
1562 Diag(CommaLoc, diag::err_expected_semi_declaration)
1563 << FixItHint::CreateReplacement(CommaLoc, ";");
1564 ExpectSemi = false;
1565 break;
1566 }
John McCalld8ac0572009-11-03 19:26:08 +00001567
1568 // Parse the next declarator.
1569 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001570 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001571
1572 // Accept attributes in an init-declarator. In the first declarator in a
1573 // declaration, these would be part of the declspec. In subsequent
1574 // declarators, they become part of the declarator itself, so that they
1575 // don't apply to declarators after *this* one. Examples:
1576 // short __attribute__((common)) var; -> declspec
1577 // short var __attribute__((common)); -> declarator
1578 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001579 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001580
1581 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001582 if (!D.isInvalidType()) {
1583 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1584 D.complete(ThisDecl);
1585 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001586 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001587 }
John McCalld8ac0572009-11-03 19:26:08 +00001588 }
1589
1590 if (DeclEnd)
1591 *DeclEnd = Tok.getLocation();
1592
Richard Smith0706df42011-10-19 21:33:05 +00001593 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001594 ExpectAndConsumeSemi(Context == Declarator::FileContext
1595 ? diag::err_invalid_token_after_toplevel_declarator
1596 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001597 // Okay, there was no semicolon and one was expected. If we see a
1598 // declaration specifier, just assume it was missing and continue parsing.
1599 // Otherwise things are very confused and we skip to recover.
1600 if (!isDeclarationSpecifier()) {
1601 SkipUntil(tok::r_brace, true, true);
1602 if (Tok.is(tok::semi))
1603 ConsumeToken();
1604 }
John McCalld8ac0572009-11-03 19:26:08 +00001605 }
1606
Douglas Gregor23c94db2010-07-02 17:43:08 +00001607 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001608 DeclsInGroup.data(),
1609 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001610}
1611
Richard Smithad762fc2011-04-14 22:09:26 +00001612/// Parse an optional simple-asm-expr and attributes, and attach them to a
1613/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001614bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001615 // If a simple-asm-expr is present, parse it.
1616 if (Tok.is(tok::kw_asm)) {
1617 SourceLocation Loc;
1618 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1619 if (AsmLabel.isInvalid()) {
1620 SkipUntil(tok::semi, true, true);
1621 return true;
1622 }
1623
1624 D.setAsmLabel(AsmLabel.release());
1625 D.SetRangeEnd(Loc);
1626 }
1627
1628 MaybeParseGNUAttributes(D);
1629 return false;
1630}
1631
Douglas Gregor1426e532009-05-12 21:31:51 +00001632/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1633/// declarator'. This method parses the remainder of the declaration
1634/// (including any attributes or initializer, among other things) and
1635/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001636///
Reid Spencer5f016e22007-07-11 17:01:13 +00001637/// init-declarator: [C99 6.7]
1638/// declarator
1639/// declarator '=' initializer
1640/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1641/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001642/// [C++] declarator initializer[opt]
1643///
1644/// [C++] initializer:
1645/// [C++] '=' initializer-clause
1646/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001647/// [C++0x] '=' 'default' [TODO]
1648/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001649/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001650///
1651/// According to the standard grammar, =default and =delete are function
1652/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001653///
John McCalld226f652010-08-21 09:40:31 +00001654Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001655 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001656 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001657 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Richard Smithad762fc2011-04-14 22:09:26 +00001659 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1660}
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Richard Smithad762fc2011-04-14 22:09:26 +00001662Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1663 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001664 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001665 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001666 switch (TemplateInfo.Kind) {
1667 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001668 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001669 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001670
Douglas Gregord5a423b2009-09-25 18:43:00 +00001671 case ParsedTemplateInfo::Template:
1672 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001673 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001674 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001675 D);
1676 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001677
Douglas Gregord5a423b2009-09-25 18:43:00 +00001678 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001679 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001680 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001681 TemplateInfo.ExternLoc,
1682 TemplateInfo.TemplateLoc,
1683 D);
1684 if (ThisRes.isInvalid()) {
1685 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001686 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001687 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001688
Douglas Gregord5a423b2009-09-25 18:43:00 +00001689 ThisDecl = ThisRes.get();
1690 break;
1691 }
1692 }
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Richard Smith34b41d92011-02-20 03:19:35 +00001694 bool TypeContainsAuto =
1695 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1696
Douglas Gregor1426e532009-05-12 21:31:51 +00001697 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001698 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001699 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001700 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001701 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001702 if (D.isFunctionDeclarator())
1703 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1704 << 1 /* delete */;
1705 else
1706 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001707 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001708 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001709 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1710 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001711 else
1712 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001713 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001714 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001715 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001716 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001717 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001718
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001719 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001720 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001721 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001722 cutOffParsing();
1723 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001724 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001725
John McCall60d7b3a2010-08-24 06:29:42 +00001726 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001727
David Blaikie4e4d0842012-03-11 07:00:24 +00001728 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001729 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001730 ExitScope();
1731 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001732
Douglas Gregor1426e532009-05-12 21:31:51 +00001733 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001734 SkipUntil(tok::comma, true, true);
1735 Actions.ActOnInitializerError(ThisDecl);
1736 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001737 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1738 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001739 }
1740 } else if (Tok.is(tok::l_paren)) {
1741 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001742 BalancedDelimiterTracker T(*this, tok::l_paren);
1743 T.consumeOpen();
1744
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001745 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001746 CommaLocsTy CommaLocs;
1747
David Blaikie4e4d0842012-03-11 07:00:24 +00001748 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001749 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001750 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001751 }
1752
Douglas Gregor1426e532009-05-12 21:31:51 +00001753 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001754 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001755 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001756
David Blaikie4e4d0842012-03-11 07:00:24 +00001757 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001758 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001759 ExitScope();
1760 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001761 } else {
1762 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001763 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001764
1765 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1766 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001767
David Blaikie4e4d0842012-03-11 07:00:24 +00001768 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001769 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001770 ExitScope();
1771 }
1772
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001773 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1774 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001775 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001776 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1777 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001778 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001779 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001780 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001781 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001782 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1783
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001784 if (D.getCXXScopeSpec().isSet()) {
1785 EnterScope(0);
1786 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1787 }
1788
1789 ExprResult Init(ParseBraceInitializer());
1790
1791 if (D.getCXXScopeSpec().isSet()) {
1792 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1793 ExitScope();
1794 }
1795
1796 if (Init.isInvalid()) {
1797 Actions.ActOnInitializerError(ThisDecl);
1798 } else
1799 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1800 /*DirectInit=*/true, TypeContainsAuto);
1801
Douglas Gregor1426e532009-05-12 21:31:51 +00001802 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001803 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001804 }
1805
Richard Smith483b9f32011-02-21 20:05:19 +00001806 Actions.FinalizeDeclaration(ThisDecl);
1807
Douglas Gregor1426e532009-05-12 21:31:51 +00001808 return ThisDecl;
1809}
1810
Reid Spencer5f016e22007-07-11 17:01:13 +00001811/// ParseSpecifierQualifierList
1812/// specifier-qualifier-list:
1813/// type-specifier specifier-qualifier-list[opt]
1814/// type-qualifier specifier-qualifier-list[opt]
1815/// [GNU] attributes specifier-qualifier-list[opt]
1816///
Richard Smith69730c12012-03-12 07:56:15 +00001817void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1818 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001819 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1820 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001821 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001822 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Reid Spencer5f016e22007-07-11 17:01:13 +00001824 // Validate declspec for type-name.
1825 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001826 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1827 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001828 Diag(Tok, diag::err_expected_type);
1829 DS.SetTypeSpecError();
1830 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1831 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001832 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001833 if (!DS.hasTypeSpecifier())
1834 DS.SetTypeSpecError();
1835 }
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Reid Spencer5f016e22007-07-11 17:01:13 +00001837 // Issue diagnostic and remove storage class if present.
1838 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1839 if (DS.getStorageClassSpecLoc().isValid())
1840 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1841 else
1842 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1843 DS.ClearStorageClassSpecs();
1844 }
Mike Stump1eb44332009-09-09 15:08:12 +00001845
Reid Spencer5f016e22007-07-11 17:01:13 +00001846 // Issue diagnostic and remove function specfier if present.
1847 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001848 if (DS.isInlineSpecified())
1849 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1850 if (DS.isVirtualSpecified())
1851 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1852 if (DS.isExplicitSpecified())
1853 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001854 DS.ClearFunctionSpecs();
1855 }
Richard Smith69730c12012-03-12 07:56:15 +00001856
1857 // Issue diagnostic and remove constexpr specfier if present.
1858 if (DS.isConstexprSpecified()) {
1859 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1860 DS.ClearConstexprSpec();
1861 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001862}
1863
Chris Lattnerc199ab32009-04-12 20:42:31 +00001864/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1865/// specified token is valid after the identifier in a declarator which
1866/// immediately follows the declspec. For example, these things are valid:
1867///
1868/// int x [ 4]; // direct-declarator
1869/// int x ( int y); // direct-declarator
1870/// int(int x ) // direct-declarator
1871/// int x ; // simple-declaration
1872/// int x = 17; // init-declarator-list
1873/// int x , y; // init-declarator-list
1874/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001875/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001876/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001877///
1878/// This is not, because 'x' does not immediately follow the declspec (though
1879/// ')' happens to be valid anyway).
1880/// int (x)
1881///
1882static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1883 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1884 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001885 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001886}
1887
Chris Lattnere40c2952009-04-14 21:34:55 +00001888
1889/// ParseImplicitInt - This method is called when we have an non-typename
1890/// identifier in a declspec (which normally terminates the decl spec) when
1891/// the declspec has no type specifier. In this case, the declspec is either
1892/// malformed or is "implicit int" (in K&R and C89).
1893///
1894/// This method handles diagnosing this prettily and returns false if the
1895/// declspec is done being processed. If it recovers and thinks there may be
1896/// other pieces of declspec after it, it returns true.
1897///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001898bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001899 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00001900 AccessSpecifier AS, DeclSpecContext DSC,
1901 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001902 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001903
Chris Lattnere40c2952009-04-14 21:34:55 +00001904 SourceLocation Loc = Tok.getLocation();
1905 // If we see an identifier that is not a type name, we normally would
1906 // parse it as the identifer being declared. However, when a typename
1907 // is typo'd or the definition is not included, this will incorrectly
1908 // parse the typename as the identifier name and fall over misparsing
1909 // later parts of the diagnostic.
1910 //
1911 // As such, we try to do some look-ahead in cases where this would
1912 // otherwise be an "implicit-int" case to see if this is invalid. For
1913 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1914 // an identifier with implicit int, we'd get a parse error because the
1915 // next token is obviously invalid for a type. Parse these as a case
1916 // with an invalid type specifier.
1917 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001918
Chris Lattnere40c2952009-04-14 21:34:55 +00001919 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001920 // error, do lookahead to try to do better recovery. This never applies
1921 // within a type specifier. Outside of C++, we allow this even if the
1922 // language doesn't "officially" support implicit int -- we support
1923 // implicit int as an extension in C99 and C11. Allegedly, MS also
1924 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001925 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001926 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001927 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001928 // If this token is valid for implicit int, e.g. "static x = 4", then
1929 // we just avoid eating the identifier, so it will be parsed as the
1930 // identifier in the declarator.
1931 return false;
1932 }
Mike Stump1eb44332009-09-09 15:08:12 +00001933
Richard Smith827adaf2012-05-15 21:01:51 +00001934 if (getLangOpts().CPlusPlus &&
1935 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1936 // Don't require a type specifier if we have the 'auto' storage class
1937 // specifier in C++98 -- we'll promote it to a type specifier.
1938 return false;
1939 }
1940
Chris Lattnere40c2952009-04-14 21:34:55 +00001941 // Otherwise, if we don't consume this token, we are going to emit an
1942 // error anyway. Try to recover from various common problems. Check
1943 // to see if this was a reference to a tag name without a tag specified.
1944 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001945 //
1946 // C++ doesn't need this, and isTagName doesn't take SS.
1947 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001948 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001949 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001950
Douglas Gregor23c94db2010-07-02 17:43:08 +00001951 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001952 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001953 case DeclSpec::TST_enum:
1954 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1955 case DeclSpec::TST_union:
1956 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1957 case DeclSpec::TST_struct:
1958 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00001959 case DeclSpec::TST_interface:
1960 TagName="__interface"; FixitTagName = "__interface ";
1961 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001962 case DeclSpec::TST_class:
1963 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001964 }
Mike Stump1eb44332009-09-09 15:08:12 +00001965
Chris Lattnerf4382f52009-04-14 22:17:06 +00001966 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001967 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1968 LookupResult R(Actions, TokenName, SourceLocation(),
1969 Sema::LookupOrdinaryName);
1970
Chris Lattnerf4382f52009-04-14 22:17:06 +00001971 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001972 << TokenName << TagName << getLangOpts().CPlusPlus
1973 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1974
1975 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1976 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1977 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001978 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001979 << TokenName << TagName;
1980 }
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Chris Lattnerf4382f52009-04-14 22:17:06 +00001982 // Parse this as a tag as if the missing tag were present.
1983 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001984 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001985 else
Richard Smith69730c12012-03-12 07:56:15 +00001986 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00001987 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001988 return true;
1989 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001990 }
Mike Stump1eb44332009-09-09 15:08:12 +00001991
Richard Smith8f0a7e72012-05-15 21:29:55 +00001992 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001993 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001994 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1995 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001996 // Look ahead to the next token to try to figure out what this declaration
1997 // was supposed to be.
1998 switch (NextToken().getKind()) {
1999 case tok::comma:
2000 case tok::equal:
2001 case tok::kw_asm:
2002 case tok::l_brace:
2003 case tok::l_square:
2004 case tok::semi:
2005 // This looks like a variable declaration. The type is probably missing.
2006 // We're done parsing decl-specifiers.
2007 return false;
2008
2009 case tok::l_paren: {
2010 // static x(4); // 'x' is not a type
2011 // x(int n); // 'x' is not a type
2012 // x (*p)[]; // 'x' is a type
2013 //
2014 // Since we're in an error case (or the rare 'implicit int in C++' MS
2015 // extension), we can afford to perform a tentative parse to determine
2016 // which case we're in.
2017 TentativeParsingAction PA(*this);
2018 ConsumeToken();
2019 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2020 PA.Revert();
2021 if (TPR == TPResult::False())
2022 return false;
2023 // The identifier is followed by a parenthesized declarator.
2024 // It's supposed to be a type.
2025 break;
2026 }
2027
2028 default:
2029 // This is probably supposed to be a type. This includes cases like:
2030 // int f(itn);
2031 // struct S { unsinged : 4; };
2032 break;
2033 }
2034 }
2035
Chad Rosier8decdee2012-06-26 22:30:43 +00002036 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00002037 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00002038 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002039 IdentifierInfo *II = Tok.getIdentifierInfo();
2040 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00002041 // The action emitted a diagnostic, so we don't have to.
2042 if (T) {
2043 // The action has suggested that the type T could be used. Set that as
2044 // the type in the declaration specifiers, consume the would-be type
2045 // name token, and we're done.
2046 const char *PrevSpec;
2047 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00002048 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00002049 DS.SetRangeEnd(Tok.getLocation());
2050 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002051 // There may be other declaration specifiers after this.
2052 return true;
2053 } else if (II != Tok.getIdentifierInfo()) {
2054 // If no type was suggested, the correction is to a keyword
2055 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002056 // There may be other declaration specifiers after this.
2057 return true;
2058 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002059
Douglas Gregora786fdb2009-10-13 23:27:22 +00002060 // Fall through; the action had no suggestion for us.
2061 } else {
2062 // The action did not emit a diagnostic, so emit one now.
2063 SourceRange R;
2064 if (SS) R = SS->getRange();
2065 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2066 }
Mike Stump1eb44332009-09-09 15:08:12 +00002067
Douglas Gregora786fdb2009-10-13 23:27:22 +00002068 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002069 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002070 DS.SetRangeEnd(Tok.getLocation());
2071 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002072
Chris Lattnere40c2952009-04-14 21:34:55 +00002073 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2074 // avoid rippling error messages on subsequent uses of the same type,
2075 // could be useful if #include was forgotten.
2076 return false;
2077}
2078
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002079/// \brief Determine the declaration specifier context from the declarator
2080/// context.
2081///
2082/// \param Context the declarator context, which is one of the
2083/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002084Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002085Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2086 if (Context == Declarator::MemberContext)
2087 return DSC_class;
2088 if (Context == Declarator::FileContext)
2089 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002090 if (Context == Declarator::TrailingReturnContext)
2091 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002092 return DSC_normal;
2093}
2094
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002095/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2096///
2097/// FIXME: Simply returns an alignof() expression if the argument is a
2098/// type. Ideally, the type should be propagated directly into Sema.
2099///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002100/// [C11] type-id
2101/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002102/// [C++0x] type-id ...[opt]
2103/// [C++0x] assignment-expression ...[opt]
2104ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2105 SourceLocation &EllipsisLoc) {
2106 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002107 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002108 SourceLocation TypeLoc = Tok.getLocation();
2109 ParsedType Ty = ParseTypeName().get();
2110 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002111 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2112 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002113 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002114 ER = ParseConstantExpression();
2115
Richard Smith80ad52f2013-01-02 11:42:31 +00002116 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002117 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002118
2119 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002120}
2121
2122/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2123/// attribute to Attrs.
2124///
2125/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002126/// [C11] '_Alignas' '(' type-id ')'
2127/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smith33f04a22013-01-29 01:48:07 +00002128/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2129/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002130void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smithf6565a92013-02-22 08:32:16 +00002131 SourceLocation *EndLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002132 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2133 "Not an alignment-specifier!");
2134
Richard Smith33f04a22013-01-29 01:48:07 +00002135 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2136 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002137
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002138 BalancedDelimiterTracker T(*this, tok::l_paren);
2139 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002140 return;
2141
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002142 SourceLocation EllipsisLoc;
2143 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002144 if (ArgExpr.isInvalid()) {
2145 SkipUntil(tok::r_paren);
2146 return;
2147 }
2148
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002149 T.consumeClose();
Richard Smithf6565a92013-02-22 08:32:16 +00002150 if (EndLoc)
2151 *EndLoc = T.getCloseLocation();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002152
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002153 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002154 ArgExprs.push_back(ArgExpr.release());
Richard Smith33f04a22013-01-29 01:48:07 +00002155 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
Richard Smithf6565a92013-02-22 08:32:16 +00002156 ArgExprs.data(), 1, AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002157}
2158
Reid Spencer5f016e22007-07-11 17:01:13 +00002159/// ParseDeclarationSpecifiers
2160/// declaration-specifiers: [C99 6.7]
2161/// storage-class-specifier declaration-specifiers[opt]
2162/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002163/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002164/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002165/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002166/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002167///
2168/// storage-class-specifier: [C99 6.7.1]
2169/// 'typedef'
2170/// 'extern'
2171/// 'static'
2172/// 'auto'
2173/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002174/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002175/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002176/// function-specifier: [C99 6.7.4]
2177/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002178/// [C++] 'virtual'
2179/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002180/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002181/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002182/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002183
Reid Spencer5f016e22007-07-11 17:01:13 +00002184///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002185void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002186 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002187 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002188 DeclSpecContext DSContext,
2189 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002190 if (DS.getSourceRange().isInvalid()) {
2191 DS.SetRangeStart(Tok.getLocation());
2192 DS.SetRangeEnd(Tok.getLocation());
2193 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002194
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002195 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002196 bool AttrsLastTime = false;
2197 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002198 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002199 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002200 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002201 unsigned DiagID = 0;
2202
Reid Spencer5f016e22007-07-11 17:01:13 +00002203 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002204
Reid Spencer5f016e22007-07-11 17:01:13 +00002205 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002206 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002207 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002208 if (!AttrsLastTime)
2209 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002210 else {
2211 // Reject C++11 attributes that appertain to decl specifiers as
2212 // we don't support any C++11 attributes that appertain to decl
2213 // specifiers. This also conforms to what g++ 4.8 is doing.
2214 ProhibitCXX11Attributes(attrs);
2215
Sean Hunt2edf0a22012-06-23 05:07:58 +00002216 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002217 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002218
Reid Spencer5f016e22007-07-11 17:01:13 +00002219 // If this is not a declaration specifier token, we're done reading decl
2220 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002221 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002222 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Sean Hunt2edf0a22012-06-23 05:07:58 +00002224 case tok::l_square:
2225 case tok::kw_alignas:
Richard Smith672edb02013-02-22 09:15:49 +00002226 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Sean Hunt2edf0a22012-06-23 05:07:58 +00002227 goto DoneWithDeclSpec;
2228
2229 ProhibitAttributes(attrs);
2230 // FIXME: It would be good to recover by accepting the attributes,
2231 // but attempting to do that now would cause serious
2232 // madness in terms of diagnostics.
2233 attrs.clear();
2234 attrs.Range = SourceRange();
2235
2236 ParseCXX11Attributes(attrs);
2237 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002238 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002239
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002240 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002241 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002242 if (DS.hasTypeSpecifier()) {
2243 bool AllowNonIdentifiers
2244 = (getCurScope()->getFlags() & (Scope::ControlScope |
2245 Scope::BlockScope |
2246 Scope::TemplateParamScope |
2247 Scope::FunctionPrototypeScope |
2248 Scope::AtCatchScope)) == 0;
2249 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002250 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002251 (DSContext == DSC_class && DS.isFriendSpecified());
2252
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002253 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002254 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002255 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002256 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002257 }
2258
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002259 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2260 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2261 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002262 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002263 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002264 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002265 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002266 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002267 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002268
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002269 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002270 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002271 }
2272
Chris Lattner5e02c472009-01-05 00:07:25 +00002273 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002274 // C++ scope specifier. Annotate and loop, or bail out on error.
2275 if (TryAnnotateCXXScopeToken(true)) {
2276 if (!DS.hasTypeSpecifier())
2277 DS.SetTypeSpecError();
2278 goto DoneWithDeclSpec;
2279 }
John McCall2e0a7152010-03-01 18:20:46 +00002280 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2281 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002282 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002283
2284 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002285 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002286 goto DoneWithDeclSpec;
2287
John McCallaa87d332009-12-12 11:40:51 +00002288 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002289 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2290 Tok.getAnnotationRange(),
2291 SS);
John McCallaa87d332009-12-12 11:40:51 +00002292
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002293 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002294 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002295 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002296 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002297 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002298 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002299
2300 // C++ [class.qual]p2:
2301 // In a lookup in which the constructor is an acceptable lookup
2302 // result and the nested-name-specifier nominates a class C:
2303 //
2304 // - if the name specified after the
2305 // nested-name-specifier, when looked up in C, is the
2306 // injected-class-name of C (Clause 9), or
2307 //
2308 // - if the name specified after the nested-name-specifier
2309 // is the same as the identifier or the
2310 // simple-template-id's template-name in the last
2311 // component of the nested-name-specifier,
2312 //
2313 // the name is instead considered to name the constructor of
2314 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002315 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002316 // Thus, if the template-name is actually the constructor
2317 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002318 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002319 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002320 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCallba9d8532010-04-13 06:39:49 +00002321 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002322 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002323 if (isConstructorDeclarator()) {
2324 // The user meant this to be an out-of-line constructor
2325 // definition, but template arguments are not allowed
2326 // there. Just allow this as a constructor; we'll
2327 // complain about it later.
2328 goto DoneWithDeclSpec;
2329 }
2330
2331 // The user meant this to name a type, but it actually names
2332 // a constructor with some extraneous template
2333 // arguments. Complain, then parse it as a type as the user
2334 // intended.
2335 Diag(TemplateId->TemplateNameLoc,
2336 diag::err_out_of_line_template_id_names_constructor)
2337 << TemplateId->Name;
2338 }
2339
John McCallaa87d332009-12-12 11:40:51 +00002340 DS.getTypeSpecScope() = SS;
2341 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002342 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002343 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002344 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002345 continue;
2346 }
2347
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002348 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002349 DS.getTypeSpecScope() = SS;
2350 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002351 if (Tok.getAnnotationValue()) {
2352 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002353 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002354 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002355 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002356 if (isInvalid)
2357 break;
John McCallb3d87482010-08-24 05:47:05 +00002358 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002359 else
2360 DS.SetTypeSpecError();
2361 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2362 ConsumeToken(); // The typename
2363 }
2364
Douglas Gregor9135c722009-03-25 15:40:00 +00002365 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002366 goto DoneWithDeclSpec;
2367
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002368 // If we're in a context where the identifier could be a class name,
2369 // check whether this is a constructor declaration.
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002370 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002371 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002372 &SS)) {
2373 if (isConstructorDeclarator())
2374 goto DoneWithDeclSpec;
2375
2376 // As noted in C++ [class.qual]p2 (cited above), when the name
2377 // of the class is qualified in a context where it could name
2378 // a constructor, its a constructor name. However, we've
2379 // looked at the declarator, and the user probably meant this
2380 // to be a type. Complain that it isn't supposed to be treated
2381 // as a type, then proceed to parse it as a type.
2382 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2383 << Next.getIdentifierInfo();
2384 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002385
John McCallb3d87482010-08-24 05:47:05 +00002386 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2387 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002388 getCurScope(), &SS,
2389 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002390 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002391 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002392
Chris Lattnerf4382f52009-04-14 22:17:06 +00002393 // If the referenced identifier is not a type, then this declspec is
2394 // erroneous: We already checked about that it has no type specifier, and
2395 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002396 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002397 if (TypeRep == 0) {
2398 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002399 ParsedAttributesWithRange Attrs(AttrFactory);
2400 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2401 if (!Attrs.empty()) {
2402 AttrsLastTime = true;
2403 attrs.takeAllFrom(Attrs);
2404 }
2405 continue;
2406 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002407 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002408 }
Mike Stump1eb44332009-09-09 15:08:12 +00002409
John McCallaa87d332009-12-12 11:40:51 +00002410 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002411 ConsumeToken(); // The C++ scope.
2412
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002413 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002414 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002415 if (isInvalid)
2416 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002417
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002418 DS.SetRangeEnd(Tok.getLocation());
2419 ConsumeToken(); // The typename.
2420
2421 continue;
2422 }
Mike Stump1eb44332009-09-09 15:08:12 +00002423
Chris Lattner80d0c892009-01-21 19:48:37 +00002424 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002425 if (Tok.getAnnotationValue()) {
2426 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002427 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002428 DiagID, T);
2429 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002430 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002431
Chris Lattner5c5db552010-04-05 18:18:31 +00002432 if (isInvalid)
2433 break;
2434
Chris Lattner80d0c892009-01-21 19:48:37 +00002435 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2436 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002437
Chris Lattner80d0c892009-01-21 19:48:37 +00002438 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2439 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002440 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002441 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002442 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002443
Chris Lattner80d0c892009-01-21 19:48:37 +00002444 continue;
2445 }
Mike Stump1eb44332009-09-09 15:08:12 +00002446
Douglas Gregorbfad9152011-04-28 15:48:45 +00002447 case tok::kw___is_signed:
2448 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2449 // typically treats it as a trait. If we see __is_signed as it appears
2450 // in libstdc++, e.g.,
2451 //
2452 // static const bool __is_signed;
2453 //
2454 // then treat __is_signed as an identifier rather than as a keyword.
2455 if (DS.getTypeSpecType() == TST_bool &&
2456 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2457 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2458 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2459 Tok.setKind(tok::identifier);
2460 }
2461
2462 // We're done with the declaration-specifiers.
2463 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002464
Chris Lattner3bd934a2008-07-26 01:18:38 +00002465 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002466 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002467 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002468 // In C++, check to see if this is a scope specifier like foo::bar::, if
2469 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002470 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002471 if (TryAnnotateCXXScopeToken(true)) {
2472 if (!DS.hasTypeSpecifier())
2473 DS.SetTypeSpecError();
2474 goto DoneWithDeclSpec;
2475 }
2476 if (!Tok.is(tok::identifier))
2477 continue;
2478 }
Mike Stump1eb44332009-09-09 15:08:12 +00002479
Chris Lattner3bd934a2008-07-26 01:18:38 +00002480 // This identifier can only be a typedef name if we haven't already seen
2481 // a type-specifier. Without this check we misparse:
2482 // typedef int X; struct Y { short X; }; as 'short int'.
2483 if (DS.hasTypeSpecifier())
2484 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002485
John Thompson82287d12010-02-05 00:12:22 +00002486 // Check for need to substitute AltiVec keyword tokens.
2487 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2488 break;
2489
Richard Smithf63eee72012-05-09 18:56:43 +00002490 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2491 // allow the use of a typedef name as a type specifier.
2492 if (DS.isTypeAltiVecVector())
2493 goto DoneWithDeclSpec;
2494
John McCallb3d87482010-08-24 05:47:05 +00002495 ParsedType TypeRep =
2496 Actions.getTypeName(*Tok.getIdentifierInfo(),
2497 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002498
Chris Lattnerc199ab32009-04-12 20:42:31 +00002499 // If this is not a typedef name, don't parse it as part of the declspec,
2500 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002501 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002502 ParsedAttributesWithRange Attrs(AttrFactory);
2503 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2504 if (!Attrs.empty()) {
2505 AttrsLastTime = true;
2506 attrs.takeAllFrom(Attrs);
2507 }
2508 continue;
2509 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002510 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002511 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002512
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002513 // If we're in a context where the identifier could be a class name,
2514 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002515 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002516 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002517 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002518 goto DoneWithDeclSpec;
2519
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002520 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002521 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002522 if (isInvalid)
2523 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002524
Chris Lattner3bd934a2008-07-26 01:18:38 +00002525 DS.SetRangeEnd(Tok.getLocation());
2526 ConsumeToken(); // The identifier
2527
2528 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2529 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002530 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002531 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002532 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002533
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002534 // Need to support trailing type qualifiers (e.g. "id<p> const").
2535 // If a type specifier follows, it will be diagnosed elsewhere.
2536 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002537 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002538
2539 // type-name
2540 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002541 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002542 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002543 // This template-id does not refer to a type name, so we're
2544 // done with the type-specifiers.
2545 goto DoneWithDeclSpec;
2546 }
2547
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002548 // If we're in a context where the template-id could be a
2549 // constructor name or specialization, check whether this is a
2550 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002551 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002552 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002553 isConstructorDeclarator())
2554 goto DoneWithDeclSpec;
2555
Douglas Gregor39a8de12009-02-25 19:37:18 +00002556 // Turn the template-id annotation token into a type annotation
2557 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002558 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002559 continue;
2560 }
2561
Reid Spencer5f016e22007-07-11 17:01:13 +00002562 // GNU attributes support.
2563 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002564 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002565 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002566
2567 // Microsoft declspec support.
2568 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002569 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002570 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002571
Steve Naroff239f0732008-12-25 14:16:32 +00002572 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002573 case tok::kw___forceinline: {
Chad Rosier22aa6902012-12-21 22:24:43 +00002574 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002575 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002576 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002577 // FIXME: This does not work correctly if it is set to be a declspec
2578 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002579 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002580 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002581 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002582 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002583
2584 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002585 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002586 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002587 case tok::kw___cdecl:
2588 case tok::kw___stdcall:
2589 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002590 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002591 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002592 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002593 continue;
2594
Dawn Perchik52fc3142010-09-03 01:29:35 +00002595 // Borland single token adornments.
2596 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002597 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002598 continue;
2599
Peter Collingbournef315fa82011-02-14 01:42:53 +00002600 // OpenCL single token adornments.
2601 case tok::kw___kernel:
2602 ParseOpenCLAttributes(DS.getAttributes());
2603 continue;
2604
Reid Spencer5f016e22007-07-11 17:01:13 +00002605 // storage-class-specifier
2606 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002607 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2608 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002609 break;
2610 case tok::kw_extern:
2611 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002612 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002613 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2614 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002615 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002616 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002617 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2618 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002619 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002620 case tok::kw_static:
2621 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002622 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002623 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2624 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002625 break;
2626 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002627 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002628 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002629 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2630 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002631 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002632 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002633 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002634 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002635 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2636 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002637 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002638 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2639 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002640 break;
2641 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002642 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2643 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002644 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002645 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002646 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2647 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002648 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002649 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002650 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002651 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002652
Reid Spencer5f016e22007-07-11 17:01:13 +00002653 // function-specifier
2654 case tok::kw_inline:
Chad Rosier22aa6902012-12-21 22:24:43 +00002655 isInvalid = DS.setFunctionSpecInline(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002656 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002657 case tok::kw_virtual:
Chad Rosier22aa6902012-12-21 22:24:43 +00002658 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002659 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002660 case tok::kw_explicit:
Chad Rosier22aa6902012-12-21 22:24:43 +00002661 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002662 break;
Richard Smithde03c152013-01-17 22:16:11 +00002663 case tok::kw__Noreturn:
2664 if (!getLangOpts().C11)
2665 Diag(Loc, diag::ext_c11_noreturn);
2666 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2667 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002668
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002669 // alignment-specifier
2670 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002671 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002672 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002673 ParseAlignmentSpecifier(DS.getAttributes());
2674 continue;
2675
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002676 // friend
2677 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002678 if (DSContext == DSC_class)
2679 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2680 else {
2681 PrevSpec = ""; // not actually used by the diagnostic
2682 DiagID = diag::err_friend_invalid_in_context;
2683 isInvalid = true;
2684 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002685 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002686
Douglas Gregor8d267c52011-09-09 02:06:17 +00002687 // Modules
2688 case tok::kw___module_private__:
2689 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2690 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002691
Sebastian Redl2ac67232009-11-05 15:47:02 +00002692 // constexpr
2693 case tok::kw_constexpr:
2694 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2695 break;
2696
Chris Lattner80d0c892009-01-21 19:48:37 +00002697 // type-specifier
2698 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002699 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2700 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002701 break;
2702 case tok::kw_long:
2703 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002704 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2705 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002706 else
John McCallfec54012009-08-03 20:12:06 +00002707 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2708 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002709 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002710 case tok::kw___int64:
2711 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2712 DiagID);
2713 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002714 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002715 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2716 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002717 break;
2718 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002719 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2720 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002721 break;
2722 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002723 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2724 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002725 break;
2726 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002727 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2728 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002729 break;
2730 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002731 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2732 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002733 break;
2734 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002735 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2736 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002737 break;
2738 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002739 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2740 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002741 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002742 case tok::kw___int128:
2743 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2744 DiagID);
2745 break;
2746 case tok::kw_half:
2747 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2748 DiagID);
2749 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002750 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2752 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002753 break;
2754 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002755 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2756 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002757 break;
2758 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002759 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2760 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002761 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002762 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002763 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2764 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002765 break;
2766 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002767 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2768 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002769 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002770 case tok::kw_bool:
2771 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002772 if (Tok.is(tok::kw_bool) &&
2773 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2774 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2775 PrevSpec = ""; // Not used by the diagnostic.
2776 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002777 // For better error recovery.
2778 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002779 isInvalid = true;
2780 } else {
2781 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2782 DiagID);
2783 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002784 break;
2785 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002786 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2787 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002788 break;
2789 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002790 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2791 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002792 break;
2793 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002794 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2795 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002796 break;
John Thompson82287d12010-02-05 00:12:22 +00002797 case tok::kw___vector:
2798 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2799 break;
2800 case tok::kw___pixel:
2801 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2802 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00002803 case tok::kw_image1d_t:
2804 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2805 PrevSpec, DiagID);
2806 break;
2807 case tok::kw_image1d_array_t:
2808 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2809 PrevSpec, DiagID);
2810 break;
2811 case tok::kw_image1d_buffer_t:
2812 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2813 PrevSpec, DiagID);
2814 break;
2815 case tok::kw_image2d_t:
2816 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2817 PrevSpec, DiagID);
2818 break;
2819 case tok::kw_image2d_array_t:
2820 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2821 PrevSpec, DiagID);
2822 break;
2823 case tok::kw_image3d_t:
2824 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2825 PrevSpec, DiagID);
2826 break;
Guy Benyei21f18c42013-02-07 10:55:47 +00002827 case tok::kw_sampler_t:
2828 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2829 PrevSpec, DiagID);
2830 break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00002831 case tok::kw_event_t:
2832 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2833 PrevSpec, DiagID);
2834 break;
John McCalla5fc4722011-04-09 22:50:59 +00002835 case tok::kw___unknown_anytype:
2836 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2837 PrevSpec, DiagID);
2838 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002839
2840 // class-specifier:
2841 case tok::kw_class:
2842 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00002843 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00002844 case tok::kw_union: {
2845 tok::TokenKind Kind = Tok.getKind();
2846 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00002847
2848 // These are attributes following class specifiers.
2849 // To produce better diagnostic, we parse them when
2850 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002851 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00002852 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002853 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002854
2855 // If there are attributes following class specifier,
2856 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002857 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00002858 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00002859 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002860 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002861 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002862 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002863
2864 // enum-specifier:
2865 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002866 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002867 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002868 continue;
2869
2870 // cv-qualifier:
2871 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002872 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002873 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002874 break;
2875 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002876 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002877 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002878 break;
2879 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002880 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002881 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002882 break;
2883
Douglas Gregord57959a2009-03-27 23:10:48 +00002884 // C++ typename-specifier:
2885 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002886 if (TryAnnotateTypeOrScopeToken()) {
2887 DS.SetTypeSpecError();
2888 goto DoneWithDeclSpec;
2889 }
2890 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002891 continue;
2892 break;
2893
Chris Lattner80d0c892009-01-21 19:48:37 +00002894 // GNU typeof support.
2895 case tok::kw_typeof:
2896 ParseTypeofSpecifier(DS);
2897 continue;
2898
David Blaikie42d6d0c2011-12-04 05:04:18 +00002899 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002900 ParseDecltypeSpecifier(DS);
2901 continue;
2902
Sean Huntdb5d44b2011-05-19 05:37:45 +00002903 case tok::kw___underlying_type:
2904 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002905 continue;
2906
2907 case tok::kw__Atomic:
Richard Smith4cf4a5e2013-03-28 01:55:44 +00002908 // C11 6.7.2.4/4:
2909 // If the _Atomic keyword is immediately followed by a left parenthesis,
2910 // it is interpreted as a type specifier (with a type name), not as a
2911 // type qualifier.
2912 if (NextToken().is(tok::l_paren)) {
2913 ParseAtomicSpecifier(DS);
2914 continue;
2915 }
2916 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
2917 getLangOpts());
2918 break;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002919
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002920 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00002921 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002922 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002923 goto DoneWithDeclSpec;
2924 case tok::kw___private:
2925 case tok::kw___global:
2926 case tok::kw___local:
2927 case tok::kw___constant:
2928 case tok::kw___read_only:
2929 case tok::kw___write_only:
2930 case tok::kw___read_write:
2931 ParseOpenCLQualifiers(DS);
2932 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002933
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002934 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002935 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002936 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2937 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002938 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002939 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002940
Douglas Gregor46f936e2010-11-19 17:10:50 +00002941 if (!ParseObjCProtocolQualifiers(DS))
2942 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2943 << FixItHint::CreateInsertion(Loc, "id")
2944 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00002945
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002946 // Need to support trailing type qualifiers (e.g. "id<p> const").
2947 // If a type specifier follows, it will be diagnosed elsewhere.
2948 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002949 }
John McCallfec54012009-08-03 20:12:06 +00002950 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002951 if (isInvalid) {
2952 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002953 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00002954
Douglas Gregorae2fb142010-08-23 14:34:43 +00002955 if (DiagID == diag::ext_duplicate_declspec)
2956 Diag(Tok, DiagID)
2957 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2958 else
2959 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002960 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002961
Chris Lattner81c018d2008-03-13 06:29:04 +00002962 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002963 if (DiagID != diag::err_bool_redeclaration)
2964 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002965
2966 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002967 }
2968}
Douglas Gregoradcac882008-12-01 23:54:00 +00002969
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002970/// ParseStructDeclaration - Parse a struct declaration without the terminating
2971/// semicolon.
2972///
Reid Spencer5f016e22007-07-11 17:01:13 +00002973/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002974/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002975/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002976/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002977/// struct-declarator-list:
2978/// struct-declarator
2979/// struct-declarator-list ',' struct-declarator
2980/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2981/// struct-declarator:
2982/// declarator
2983/// [GNU] declarator attributes[opt]
2984/// declarator[opt] ':' constant-expression
2985/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2986///
Chris Lattnere1359422008-04-10 06:46:29 +00002987void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002988ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00002989
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002990 if (Tok.is(tok::kw___extension__)) {
2991 // __extension__ silences extension warnings in the subexpression.
2992 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002993 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002994 return ParseStructDeclaration(DS, Fields);
2995 }
Mike Stump1eb44332009-09-09 15:08:12 +00002996
Steve Naroff28a7ca82007-08-20 22:28:22 +00002997 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002998 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002999
Douglas Gregor4920f1f2009-01-12 22:49:06 +00003000 // If there are no declarators, this is a free-standing declaration
3001 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00003002 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003003 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3004 DS);
3005 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00003006 return;
3007 }
3008
3009 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00003010 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00003011 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003012 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003013 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00003014 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00003015
Bill Wendlingad017fa2012-12-20 19:22:21 +00003016 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00003017 if (!FirstDeclarator)
3018 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00003019
Steve Naroff28a7ca82007-08-20 22:28:22 +00003020 /// struct-declarator: declarator
3021 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003022 if (Tok.isNot(tok::colon)) {
3023 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3024 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00003025 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003026 }
Mike Stump1eb44332009-09-09 15:08:12 +00003027
Chris Lattner04d66662007-10-09 17:33:22 +00003028 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00003029 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00003030 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003031 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00003032 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00003033 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00003034 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00003035 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003036
Steve Naroff28a7ca82007-08-20 22:28:22 +00003037 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003038 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003039
John McCallbdd563e2009-11-03 02:38:08 +00003040 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00003041 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00003042
Steve Naroff28a7ca82007-08-20 22:28:22 +00003043 // If we don't have a comma, it is either the end of the list (a ';')
3044 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00003045 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003046 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00003047
Steve Naroff28a7ca82007-08-20 22:28:22 +00003048 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00003049 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003050
John McCallbdd563e2009-11-03 02:38:08 +00003051 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003052 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00003053}
3054
3055/// ParseStructUnionBody
3056/// struct-contents:
3057/// struct-declaration-list
3058/// [EXT] empty
3059/// [GNU] "struct-declaration-list" without terminatoring ';'
3060/// struct-declaration-list:
3061/// struct-declaration
3062/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003063/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003064///
Reid Spencer5f016e22007-07-11 17:01:13 +00003065void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003066 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003067 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3068 "parsing struct/union body");
Andy Gibbsf50f3f72013-04-03 09:31:19 +00003069 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump1eb44332009-09-09 15:08:12 +00003070
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003071 BalancedDelimiterTracker T(*this, tok::l_brace);
3072 if (T.consumeOpen())
3073 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003074
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003075 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003076 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003077
Andy Gibbsf50f3f72013-04-03 09:31:19 +00003078 // Empty structs are an extension in C (C99 6.7.2.1p7).
3079 if (Tok.is(tok::r_brace)) {
Richard Smithd7c56e12011-12-29 21:57:33 +00003080 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3081 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3082 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003083
Chris Lattner5f9e2722011-07-23 10:55:15 +00003084 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003085
Reid Spencer5f016e22007-07-11 17:01:13 +00003086 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003087 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003088 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003089
Reid Spencer5f016e22007-07-11 17:01:13 +00003090 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003091 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003092 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003093 continue;
3094 }
Chris Lattnere1359422008-04-10 06:46:29 +00003095
Andy Gibbs74b9fa12013-04-03 09:46:04 +00003096 // Parse _Static_assert declaration.
3097 if (Tok.is(tok::kw__Static_assert)) {
3098 SourceLocation DeclEnd;
3099 ParseStaticAssertDeclaration(DeclEnd);
3100 continue;
3101 }
3102
John McCallbdd563e2009-11-03 02:38:08 +00003103 if (!Tok.is(tok::at)) {
3104 struct CFieldCallback : FieldCallback {
3105 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003106 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003107 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003108
John McCalld226f652010-08-21 09:40:31 +00003109 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003110 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003111 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3112
Eli Friedmandcdff462012-08-08 23:53:27 +00003113 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003114 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003115 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003116 FD.D.getDeclSpec().getSourceRange().getBegin(),
3117 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003118 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003119 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003120 }
John McCallbdd563e2009-11-03 02:38:08 +00003121 } Callback(*this, TagDecl, FieldDecls);
3122
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003123 // Parse all the comma separated declarators.
3124 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003125 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003126 } else { // Handle @defs
3127 ConsumeToken();
3128 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3129 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003130 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003131 continue;
3132 }
3133 ConsumeToken();
3134 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3135 if (!Tok.is(tok::identifier)) {
3136 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003137 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003138 continue;
3139 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003140 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003141 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003142 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003143 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3144 ConsumeToken();
3145 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003146 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003147
Chris Lattner04d66662007-10-09 17:33:22 +00003148 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003149 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003150 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003151 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003152 break;
3153 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003154 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3155 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003156 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003157 // If we stopped at a ';', eat it.
3158 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003159 }
3160 }
Mike Stump1eb44332009-09-09 15:08:12 +00003161
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003162 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003163
John McCall0b7e6782011-03-24 11:26:52 +00003164 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003165 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003166 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003167
Douglas Gregor23c94db2010-07-02 17:43:08 +00003168 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003169 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003170 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003171 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003172 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003173 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3174 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003175}
3176
Reid Spencer5f016e22007-07-11 17:01:13 +00003177/// ParseEnumSpecifier
3178/// enum-specifier: [C99 6.7.2.2]
3179/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003180///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003181/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3182/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003183/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3184/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003185/// 'enum' identifier
3186/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003187///
Richard Smith1af83c42012-03-23 03:33:32 +00003188/// [C++11] enum-head '{' enumerator-list[opt] '}'
3189/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003190///
Richard Smith1af83c42012-03-23 03:33:32 +00003191/// enum-head: [C++11]
3192/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3193/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3194/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003195///
Richard Smith1af83c42012-03-23 03:33:32 +00003196/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003197/// 'enum'
3198/// 'enum' 'class'
3199/// 'enum' 'struct'
3200///
Richard Smith1af83c42012-03-23 03:33:32 +00003201/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003202/// ':' type-specifier-seq
3203///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003204/// [C++] elaborated-type-specifier:
3205/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3206///
Chris Lattner4c97d762009-04-12 21:49:30 +00003207void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003208 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003209 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003210 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003211 if (Tok.is(tok::code_completion)) {
3212 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003213 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003214 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003215 }
John McCall57c13002011-07-06 05:58:41 +00003216
Sean Hunt2edf0a22012-06-23 05:07:58 +00003217 // If attributes exist after tag, parse them.
3218 ParsedAttributesWithRange attrs(AttrFactory);
3219 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003220 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003221
3222 // If declspecs exist after tag, parse them.
3223 while (Tok.is(tok::kw___declspec))
3224 ParseMicrosoftDeclSpec(attrs);
3225
Richard Smithbdad7a22012-01-10 01:33:14 +00003226 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003227 bool IsScopedUsingClassTag = false;
3228
John McCall1e12b3d2012-06-23 22:30:04 +00003229 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Smith80ad52f2013-01-02 11:42:31 +00003230 if (getLangOpts().CPlusPlus11 &&
John McCall57c13002011-07-06 05:58:41 +00003231 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00003232 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003233 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003234 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003235
Bill Wendlingad017fa2012-12-20 19:22:21 +00003236 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003237 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003238 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003239
3240 // They are allowed afterwards, though.
3241 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003242 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003243 while (Tok.is(tok::kw___declspec))
3244 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003245 }
Richard Smith1af83c42012-03-23 03:33:32 +00003246
John McCall13489672012-05-07 06:16:58 +00003247 // C++11 [temp.explicit]p12:
3248 // The usual access controls do not apply to names used to specify
3249 // explicit instantiations.
3250 // We extend this to also cover explicit specializations. Note that
3251 // we don't suppress if this turns out to be an elaborated type
3252 // specifier.
3253 bool shouldDelayDiagsInTag =
3254 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3255 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3256 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003257
Richard Smith7796eb52012-03-12 08:56:40 +00003258 // Enum definitions should not be parsed in a trailing-return-type.
3259 bool AllowDeclaration = DSC != DSC_trailing;
3260
3261 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003262 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003263 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003264
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003265 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003266 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003267 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3268 // if a fixed underlying type is allowed.
3269 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003270
3271 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith725fe0e2013-04-01 21:43:41 +00003272 /*EnteringContext=*/true))
John McCall9ba61662010-02-26 08:45:28 +00003273 return;
3274
3275 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003276 Diag(Tok, diag::err_expected_ident);
3277 if (Tok.isNot(tok::l_brace)) {
3278 // Has no name and is not a definition.
3279 // Skip the rest of this declarator, up until the comma or semicolon.
3280 SkipUntil(tok::comma, true);
3281 return;
3282 }
3283 }
3284 }
Mike Stump1eb44332009-09-09 15:08:12 +00003285
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003286 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003287 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003288 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003289 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003290
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003291 // Skip the rest of this declarator, up until the comma or semicolon.
3292 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003293 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003294 }
Mike Stump1eb44332009-09-09 15:08:12 +00003295
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003296 // If an identifier is present, consume and remember it.
3297 IdentifierInfo *Name = 0;
3298 SourceLocation NameLoc;
3299 if (Tok.is(tok::identifier)) {
3300 Name = Tok.getIdentifierInfo();
3301 NameLoc = ConsumeToken();
3302 }
Mike Stump1eb44332009-09-09 15:08:12 +00003303
Richard Smithbdad7a22012-01-10 01:33:14 +00003304 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003305 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3306 // declaration of a scoped enumeration.
3307 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003308 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003309 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003310 }
3311
John McCall13489672012-05-07 06:16:58 +00003312 // Okay, end the suppression area. We'll decide whether to emit the
3313 // diagnostics in a second.
3314 if (shouldDelayDiagsInTag)
3315 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003316
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003317 TypeResult BaseType;
3318
Douglas Gregora61b3e72010-12-01 17:42:47 +00003319 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003320 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003321 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003322 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003323 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003324 // If we're in class scope, this can either be an enum declaration with
3325 // an underlying type, or a declaration of a bitfield member. We try to
3326 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003327 // (integer literal, sizeof); if it's still ambiguous, we then consider
3328 // anything that's a simple-type-specifier followed by '(' as an
3329 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003330 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003331 EnterExpressionEvaluationContext Unevaluated(Actions,
3332 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003333 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003334 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003335 // bit-field. This is the common case.
3336 if (TPR == TPResult::True())
3337 PossibleBitfield = true;
3338 // If the next token starts a type-specifier-seq, it may be either a
3339 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003340 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003341 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003342 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003343 GetLookAheadToken(2).getKind() == tok::semi) {
3344 // Consume the ':'.
3345 ConsumeToken();
3346 } else {
3347 // We have the start of a type-specifier-seq, so we have to perform
3348 // tentative parsing to determine whether we have an expression or a
3349 // type.
3350 TentativeParsingAction TPA(*this);
3351
3352 // Consume the ':'.
3353 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003354
3355 // If we see a type specifier followed by an open-brace, we have an
3356 // ambiguity between an underlying type and a C++11 braced
3357 // function-style cast. Resolve this by always treating it as an
3358 // underlying type.
3359 // FIXME: The standard is not entirely clear on how to disambiguate in
3360 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003361 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003362 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003363 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003364 // We'll parse this as a bitfield later.
3365 PossibleBitfield = true;
3366 TPA.Revert();
3367 } else {
3368 // We have a type-specifier-seq.
3369 TPA.Commit();
3370 }
3371 }
3372 } else {
3373 // Consume the ':'.
3374 ConsumeToken();
3375 }
3376
3377 if (!PossibleBitfield) {
3378 SourceRange Range;
3379 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003380
Richard Smith80ad52f2013-01-02 11:42:31 +00003381 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003382 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003383 } else if (!getLangOpts().ObjC2) {
3384 if (getLangOpts().CPlusPlus)
3385 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3386 else
3387 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3388 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003389 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003390 }
3391
Richard Smithbdad7a22012-01-10 01:33:14 +00003392 // There are four options here. If we have 'friend enum foo;' then this is a
3393 // friend declaration, and cannot have an accompanying definition. If we have
3394 // 'enum foo;', then this is a forward declaration. If we have
3395 // 'enum foo {...' then this is a definition. Otherwise we have something
3396 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003397 //
3398 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3399 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3400 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3401 //
John McCallf312b1e2010-08-26 23:41:50 +00003402 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003403 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003404 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003405 } else if (Tok.is(tok::l_brace)) {
3406 if (DS.isFriendSpecified()) {
3407 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3408 << SourceRange(DS.getFriendSpecLoc());
3409 ConsumeBrace();
3410 SkipUntil(tok::r_brace);
3411 TUK = Sema::TUK_Friend;
3412 } else {
3413 TUK = Sema::TUK_Definition;
3414 }
Richard Smithc9f35172012-06-25 21:37:02 +00003415 } else if (DSC != DSC_type_specifier &&
3416 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003417 (Tok.isAtStartOfLine() &&
3418 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003419 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3420 if (Tok.isNot(tok::semi)) {
3421 // A semicolon was missing after this declaration. Diagnose and recover.
3422 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3423 "enum");
3424 PP.EnterToken(Tok);
3425 Tok.setKind(tok::semi);
3426 }
John McCall13489672012-05-07 06:16:58 +00003427 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003428 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003429 }
3430
3431 // If this is an elaborated type specifier, and we delayed
3432 // diagnostics before, just merge them into the current pool.
3433 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3434 diagsFromTag.redelay();
3435 }
Richard Smith1af83c42012-03-23 03:33:32 +00003436
3437 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003438 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003439 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003440 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003441 // Skip the rest of this declarator, up until the comma or semicolon.
3442 Diag(Tok, diag::err_enum_template);
3443 SkipUntil(tok::comma, true);
3444 return;
3445 }
3446
3447 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3448 // Enumerations can't be explicitly instantiated.
3449 DS.SetTypeSpecError();
3450 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3451 return;
3452 }
3453
3454 assert(TemplateInfo.TemplateParams && "no template parameters");
3455 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3456 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003457 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003458
Sean Hunt2edf0a22012-06-23 05:07:58 +00003459 if (TUK == Sema::TUK_Reference)
3460 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003461
Douglas Gregorb9075602011-02-22 02:55:24 +00003462 if (!Name && TUK != Sema::TUK_Definition) {
3463 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003464
Douglas Gregorb9075602011-02-22 02:55:24 +00003465 // Skip the rest of this declarator, up until the comma or semicolon.
3466 SkipUntil(tok::comma, true);
3467 return;
3468 }
Richard Smith1af83c42012-03-23 03:33:32 +00003469
Douglas Gregor402abb52009-05-28 23:31:59 +00003470 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003471 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003472 const char *PrevSpec = 0;
3473 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003474 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003475 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003476 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003477 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003478 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003479
Douglas Gregor48c89f42010-04-24 16:38:41 +00003480 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003481 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003482 // dependent tag.
3483 if (!Name) {
3484 DS.SetTypeSpecError();
3485 Diag(Tok, diag::err_expected_type_name_after_typename);
3486 return;
3487 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003488
Douglas Gregor23c94db2010-07-02 17:43:08 +00003489 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003490 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003491 NameLoc);
3492 if (Type.isInvalid()) {
3493 DS.SetTypeSpecError();
3494 return;
3495 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003496
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003497 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3498 NameLoc.isValid() ? NameLoc : StartLoc,
3499 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003500 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003501
Douglas Gregor48c89f42010-04-24 16:38:41 +00003502 return;
3503 }
Mike Stump1eb44332009-09-09 15:08:12 +00003504
John McCalld226f652010-08-21 09:40:31 +00003505 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003506 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003507 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003508 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003509 ConsumeBrace();
3510 SkipUntil(tok::r_brace);
3511 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003512
Douglas Gregor48c89f42010-04-24 16:38:41 +00003513 DS.SetTypeSpecError();
3514 return;
3515 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003516
Richard Smithc9f35172012-06-25 21:37:02 +00003517 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003518 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003519
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003520 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3521 NameLoc.isValid() ? NameLoc : StartLoc,
3522 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003523 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003524}
3525
3526/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3527/// enumerator-list:
3528/// enumerator
3529/// enumerator-list ',' enumerator
3530/// enumerator:
3531/// enumeration-constant
3532/// enumeration-constant '=' constant-expression
3533/// enumeration-constant:
3534/// identifier
3535///
John McCalld226f652010-08-21 09:40:31 +00003536void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003537 // Enter the scope of the enum body and start the definition.
3538 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003539 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003540
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003541 BalancedDelimiterTracker T(*this, tok::l_brace);
3542 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003543
Chris Lattner7946dd32007-08-27 17:24:30 +00003544 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003545 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003546 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003547
Chris Lattner5f9e2722011-07-23 10:55:15 +00003548 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003549
John McCalld226f652010-08-21 09:40:31 +00003550 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003551
Reid Spencer5f016e22007-07-11 17:01:13 +00003552 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003553 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003554 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3555 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003556
John McCall5b629aa2010-10-22 23:36:17 +00003557 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003558 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003559 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003560 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003561 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003562
Reid Spencer5f016e22007-07-11 17:01:13 +00003563 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003564 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003565 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003566
Chris Lattner04d66662007-10-09 17:33:22 +00003567 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003568 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003569 AssignedVal = ParseConstantExpression();
3570 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003571 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003572 }
Mike Stump1eb44332009-09-09 15:08:12 +00003573
Reid Spencer5f016e22007-07-11 17:01:13 +00003574 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003575 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3576 LastEnumConstDecl,
3577 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003578 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003579 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003580 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003581
Reid Spencer5f016e22007-07-11 17:01:13 +00003582 EnumConstantDecls.push_back(EnumConstDecl);
3583 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003584
Douglas Gregor751f6922010-09-07 14:51:08 +00003585 if (Tok.is(tok::identifier)) {
3586 // We're missing a comma between enumerators.
3587 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003588 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003589 << FixItHint::CreateInsertion(Loc, ", ");
3590 continue;
3591 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003592
Chris Lattner04d66662007-10-09 17:33:22 +00003593 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003594 break;
3595 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003596
Richard Smith7fe62082011-10-15 05:09:34 +00003597 if (Tok.isNot(tok::identifier)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003598 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003599 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3600 diag::ext_enumerator_list_comma_cxx :
3601 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003602 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003603 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003604 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3605 << FixItHint::CreateRemoval(CommaLoc);
3606 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003607 }
Mike Stump1eb44332009-09-09 15:08:12 +00003608
Reid Spencer5f016e22007-07-11 17:01:13 +00003609 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003610 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003611
Reid Spencer5f016e22007-07-11 17:01:13 +00003612 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003613 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003614 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003615
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003616 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3617 EnumDecl, EnumConstantDecls.data(),
3618 EnumConstantDecls.size(), getCurScope(),
3619 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003620
Douglas Gregor72de6672009-01-08 20:45:30 +00003621 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003622 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3623 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003624
3625 // The next token must be valid after an enum definition. If not, a ';'
3626 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003627 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3628 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003629 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3630 // Push this token back into the preprocessor and change our current token
3631 // to ';' so that the rest of the code recovers as though there were an
3632 // ';' after the definition.
3633 PP.EnterToken(Tok);
3634 Tok.setKind(tok::semi);
3635 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003636}
3637
3638/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003639/// start of a type-qualifier-list.
3640bool Parser::isTypeQualifier() const {
3641 switch (Tok.getKind()) {
3642 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003643
3644 // type-qualifier only in OpenCL
3645 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003646 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003647
Steve Naroff5f8aa692008-02-11 23:15:56 +00003648 // type-qualifier
3649 case tok::kw_const:
3650 case tok::kw_volatile:
3651 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003652 case tok::kw___private:
3653 case tok::kw___local:
3654 case tok::kw___global:
3655 case tok::kw___constant:
3656 case tok::kw___read_only:
3657 case tok::kw___read_write:
3658 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003659 return true;
3660 }
3661}
3662
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003663/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3664/// is definitely a type-specifier. Return false if it isn't part of a type
3665/// specifier or if we're not sure.
3666bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3667 switch (Tok.getKind()) {
3668 default: return false;
3669 // type-specifiers
3670 case tok::kw_short:
3671 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003672 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003673 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003674 case tok::kw_signed:
3675 case tok::kw_unsigned:
3676 case tok::kw__Complex:
3677 case tok::kw__Imaginary:
3678 case tok::kw_void:
3679 case tok::kw_char:
3680 case tok::kw_wchar_t:
3681 case tok::kw_char16_t:
3682 case tok::kw_char32_t:
3683 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003684 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003685 case tok::kw_float:
3686 case tok::kw_double:
3687 case tok::kw_bool:
3688 case tok::kw__Bool:
3689 case tok::kw__Decimal32:
3690 case tok::kw__Decimal64:
3691 case tok::kw__Decimal128:
3692 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003693
Guy Benyeib13621d2012-12-18 14:38:23 +00003694 // OpenCL specific types:
3695 case tok::kw_image1d_t:
3696 case tok::kw_image1d_array_t:
3697 case tok::kw_image1d_buffer_t:
3698 case tok::kw_image2d_t:
3699 case tok::kw_image2d_array_t:
3700 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003701 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003702 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003703
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003704 // struct-or-union-specifier (C99) or class-specifier (C++)
3705 case tok::kw_class:
3706 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003707 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003708 case tok::kw_union:
3709 // enum-specifier
3710 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003711
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003712 // typedef-name
3713 case tok::annot_typename:
3714 return true;
3715 }
3716}
3717
Steve Naroff5f8aa692008-02-11 23:15:56 +00003718/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003719/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003720bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003721 switch (Tok.getKind()) {
3722 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003723
Chris Lattner166a8fc2009-01-04 23:41:41 +00003724 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003725 if (TryAltiVecVectorToken())
3726 return true;
3727 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003728 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003729 // Annotate typenames and C++ scope specifiers. If we get one, just
3730 // recurse to handle whatever we get.
3731 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003732 return true;
3733 if (Tok.is(tok::identifier))
3734 return false;
3735 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003736
Chris Lattner166a8fc2009-01-04 23:41:41 +00003737 case tok::coloncolon: // ::foo::bar
3738 if (NextToken().is(tok::kw_new) || // ::new
3739 NextToken().is(tok::kw_delete)) // ::delete
3740 return false;
3741
Chris Lattner166a8fc2009-01-04 23:41:41 +00003742 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003743 return true;
3744 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003745
Reid Spencer5f016e22007-07-11 17:01:13 +00003746 // GNU attributes support.
3747 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003748 // GNU typeof support.
3749 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003750
Reid Spencer5f016e22007-07-11 17:01:13 +00003751 // type-specifiers
3752 case tok::kw_short:
3753 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003754 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003755 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003756 case tok::kw_signed:
3757 case tok::kw_unsigned:
3758 case tok::kw__Complex:
3759 case tok::kw__Imaginary:
3760 case tok::kw_void:
3761 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003762 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003763 case tok::kw_char16_t:
3764 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003765 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003766 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003767 case tok::kw_float:
3768 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003769 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003770 case tok::kw__Bool:
3771 case tok::kw__Decimal32:
3772 case tok::kw__Decimal64:
3773 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003774 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003775
Guy Benyeib13621d2012-12-18 14:38:23 +00003776 // OpenCL specific types:
3777 case tok::kw_image1d_t:
3778 case tok::kw_image1d_array_t:
3779 case tok::kw_image1d_buffer_t:
3780 case tok::kw_image2d_t:
3781 case tok::kw_image2d_array_t:
3782 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003783 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003784 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003785
Chris Lattner99dc9142008-04-13 18:59:07 +00003786 // struct-or-union-specifier (C99) or class-specifier (C++)
3787 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003788 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003789 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003790 case tok::kw_union:
3791 // enum-specifier
3792 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003793
Reid Spencer5f016e22007-07-11 17:01:13 +00003794 // type-qualifier
3795 case tok::kw_const:
3796 case tok::kw_volatile:
3797 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003798
John McCallb8a8de32012-11-14 00:49:39 +00003799 // Debugger support.
3800 case tok::kw___unknown_anytype:
3801
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003802 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003803 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003804 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003805
Chris Lattner7c186be2008-10-20 00:25:30 +00003806 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3807 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003808 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003809
Steve Naroff239f0732008-12-25 14:16:32 +00003810 case tok::kw___cdecl:
3811 case tok::kw___stdcall:
3812 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003813 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003814 case tok::kw___w64:
3815 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003816 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003817 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003818 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003819
3820 case tok::kw___private:
3821 case tok::kw___local:
3822 case tok::kw___global:
3823 case tok::kw___constant:
3824 case tok::kw___read_only:
3825 case tok::kw___read_write:
3826 case tok::kw___write_only:
3827
Eli Friedman290eeb02009-06-08 23:27:34 +00003828 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003829
3830 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003831 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003832
Richard Smith4cf4a5e2013-03-28 01:55:44 +00003833 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00003834 case tok::kw__Atomic:
3835 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003836 }
3837}
3838
3839/// isDeclarationSpecifier() - Return true if the current token is part of a
3840/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003841///
3842/// \param DisambiguatingWithExpression True to indicate that the purpose of
3843/// this check is to disambiguate between an expression and a declaration.
3844bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003845 switch (Tok.getKind()) {
3846 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003847
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003848 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003849 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003850
Chris Lattner166a8fc2009-01-04 23:41:41 +00003851 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003852 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003853 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003854 return false;
John Thompson82287d12010-02-05 00:12:22 +00003855 if (TryAltiVecVectorToken())
3856 return true;
3857 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003858 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003859 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003860 // Annotate typenames and C++ scope specifiers. If we get one, just
3861 // recurse to handle whatever we get.
3862 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003863 return true;
3864 if (Tok.is(tok::identifier))
3865 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003866
Douglas Gregor9497a732010-09-16 01:51:54 +00003867 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00003868 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00003869 // expression is permitted, then this is probably a class message send
3870 // missing the initial '['. In this case, we won't consider this to be
3871 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00003872 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00003873 isStartOfObjCClassMessageMissingOpenBracket())
3874 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003875
John McCall9ba61662010-02-26 08:45:28 +00003876 return isDeclarationSpecifier();
3877
Chris Lattner166a8fc2009-01-04 23:41:41 +00003878 case tok::coloncolon: // ::foo::bar
3879 if (NextToken().is(tok::kw_new) || // ::new
3880 NextToken().is(tok::kw_delete)) // ::delete
3881 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003882
Chris Lattner166a8fc2009-01-04 23:41:41 +00003883 // Annotate typenames and C++ scope specifiers. If we get one, just
3884 // recurse to handle whatever we get.
3885 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003886 return true;
3887 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003888
Reid Spencer5f016e22007-07-11 17:01:13 +00003889 // storage-class-specifier
3890 case tok::kw_typedef:
3891 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003892 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003893 case tok::kw_static:
3894 case tok::kw_auto:
3895 case tok::kw_register:
3896 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003897
Douglas Gregor8d267c52011-09-09 02:06:17 +00003898 // Modules
3899 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00003900
John McCallb8a8de32012-11-14 00:49:39 +00003901 // Debugger support
3902 case tok::kw___unknown_anytype:
3903
Reid Spencer5f016e22007-07-11 17:01:13 +00003904 // type-specifiers
3905 case tok::kw_short:
3906 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003907 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003908 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003909 case tok::kw_signed:
3910 case tok::kw_unsigned:
3911 case tok::kw__Complex:
3912 case tok::kw__Imaginary:
3913 case tok::kw_void:
3914 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003915 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003916 case tok::kw_char16_t:
3917 case tok::kw_char32_t:
3918
Reid Spencer5f016e22007-07-11 17:01:13 +00003919 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003920 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003921 case tok::kw_float:
3922 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003923 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003924 case tok::kw__Bool:
3925 case tok::kw__Decimal32:
3926 case tok::kw__Decimal64:
3927 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003928 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003929
Guy Benyeib13621d2012-12-18 14:38:23 +00003930 // OpenCL specific types:
3931 case tok::kw_image1d_t:
3932 case tok::kw_image1d_array_t:
3933 case tok::kw_image1d_buffer_t:
3934 case tok::kw_image2d_t:
3935 case tok::kw_image2d_array_t:
3936 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003937 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003938 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003939
Chris Lattner99dc9142008-04-13 18:59:07 +00003940 // struct-or-union-specifier (C99) or class-specifier (C++)
3941 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003942 case tok::kw_struct:
3943 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00003944 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003945 // enum-specifier
3946 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003947
Reid Spencer5f016e22007-07-11 17:01:13 +00003948 // type-qualifier
3949 case tok::kw_const:
3950 case tok::kw_volatile:
3951 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003952
Reid Spencer5f016e22007-07-11 17:01:13 +00003953 // function-specifier
3954 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003955 case tok::kw_virtual:
3956 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00003957 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003958
Richard Smith4cd81c52013-01-29 09:02:09 +00003959 // alignment-specifier
3960 case tok::kw__Alignas:
3961
Richard Smith53aec2a2012-10-25 00:00:53 +00003962 // friend keyword.
3963 case tok::kw_friend:
3964
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003965 // static_assert-declaration
3966 case tok::kw__Static_assert:
3967
Chris Lattner1ef08762007-08-09 17:01:07 +00003968 // GNU typeof support.
3969 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003970
Chris Lattner1ef08762007-08-09 17:01:07 +00003971 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003972 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00003973
Richard Smith53aec2a2012-10-25 00:00:53 +00003974 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003975 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00003976 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00003977
Richard Smith4cf4a5e2013-03-28 01:55:44 +00003978 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00003979 case tok::kw__Atomic:
3980 return true;
3981
Chris Lattnerf3948c42008-07-26 03:38:44 +00003982 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3983 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003984 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003985
Douglas Gregord9d75e52011-04-27 05:41:15 +00003986 // typedef-name
3987 case tok::annot_typename:
3988 return !DisambiguatingWithExpression ||
3989 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00003990
Steve Naroff47f52092009-01-06 19:34:12 +00003991 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003992 case tok::kw___cdecl:
3993 case tok::kw___stdcall:
3994 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003995 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003996 case tok::kw___w64:
3997 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003998 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003999 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004000 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004001 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004002
4003 case tok::kw___private:
4004 case tok::kw___local:
4005 case tok::kw___global:
4006 case tok::kw___constant:
4007 case tok::kw___read_only:
4008 case tok::kw___read_write:
4009 case tok::kw___write_only:
4010
Eli Friedman290eeb02009-06-08 23:27:34 +00004011 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00004012 }
4013}
4014
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004015bool Parser::isConstructorDeclarator() {
4016 TentativeParsingAction TPA(*this);
4017
4018 // Parse the C++ scope specifier.
4019 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00004020 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004021 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00004022 TPA.Revert();
4023 return false;
4024 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004025
4026 // Parse the constructor name.
4027 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4028 // We already know that we have a constructor name; just consume
4029 // the token.
4030 ConsumeToken();
4031 } else {
4032 TPA.Revert();
4033 return false;
4034 }
4035
Richard Smith22592862012-03-27 23:05:05 +00004036 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004037 if (Tok.isNot(tok::l_paren)) {
4038 TPA.Revert();
4039 return false;
4040 }
4041 ConsumeParen();
4042
Richard Smith22592862012-03-27 23:05:05 +00004043 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4044 // that we have a constructor.
4045 if (Tok.is(tok::r_paren) ||
4046 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004047 TPA.Revert();
4048 return true;
4049 }
4050
4051 // If we need to, enter the specified scope.
4052 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00004053 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004054 DeclScopeObj.EnterDeclaratorScope();
4055
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004056 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00004057 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004058 MaybeParseMicrosoftAttributes(Attrs);
4059
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004060 // Check whether the next token(s) are part of a declaration
4061 // specifier, in which case we have the start of a parameter and,
4062 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00004063 bool IsConstructor = false;
4064 if (isDeclarationSpecifier())
4065 IsConstructor = true;
4066 else if (Tok.is(tok::identifier) ||
4067 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4068 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4069 // This might be a parenthesized member name, but is more likely to
4070 // be a constructor declaration with an invalid argument type. Keep
4071 // looking.
4072 if (Tok.is(tok::annot_cxxscope))
4073 ConsumeToken();
4074 ConsumeToken();
4075
4076 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004077 // which must have one of the following syntactic forms (see the
4078 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004079 switch (Tok.getKind()) {
4080 case tok::l_paren:
4081 // C(X ( int));
4082 case tok::l_square:
4083 // C(X [ 5]);
4084 // C(X [ [attribute]]);
4085 case tok::coloncolon:
4086 // C(X :: Y);
4087 // C(X :: *p);
4088 case tok::r_paren:
4089 // C(X )
4090 // Assume this isn't a constructor, rather than assuming it's a
4091 // constructor with an unnamed parameter of an ill-formed type.
4092 break;
4093
4094 default:
4095 IsConstructor = true;
4096 break;
4097 }
4098 }
4099
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004100 TPA.Revert();
4101 return IsConstructor;
4102}
Reid Spencer5f016e22007-07-11 17:01:13 +00004103
4104/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004105/// type-qualifier-list: [C99 6.7.5]
4106/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004107/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004108/// [ only if VendorAttributesAllowed=true ]
4109/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004110/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004111/// [ only if VendorAttributesAllowed=true ]
4112/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004113/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004114/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004115///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004116void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4117 bool VendorAttributesAllowed,
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004118 bool CXX11AttributesAllowed,
4119 bool AtomicAllowed) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004120 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004121 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004122 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004123 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004124 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004125 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004126
4127 SourceLocation EndLoc;
4128
Reid Spencer5f016e22007-07-11 17:01:13 +00004129 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004130 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004131 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004132 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004133 SourceLocation Loc = Tok.getLocation();
4134
4135 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004136 case tok::code_completion:
4137 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004138 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004139
Reid Spencer5f016e22007-07-11 17:01:13 +00004140 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004141 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004142 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004143 break;
4144 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004145 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004146 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004147 break;
4148 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004149 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004150 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004151 break;
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004152 case tok::kw__Atomic:
4153 if (!AtomicAllowed)
4154 goto DoneWithTypeQuals;
4155 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4156 getLangOpts());
4157 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004158
4159 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004160 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004161 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004162 goto DoneWithTypeQuals;
4163 case tok::kw___private:
4164 case tok::kw___global:
4165 case tok::kw___local:
4166 case tok::kw___constant:
4167 case tok::kw___read_only:
4168 case tok::kw___write_only:
4169 case tok::kw___read_write:
4170 ParseOpenCLQualifiers(DS);
4171 break;
4172
Eli Friedman290eeb02009-06-08 23:27:34 +00004173 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004174 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004175 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004176 case tok::kw___cdecl:
4177 case tok::kw___stdcall:
4178 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004179 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004180 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004181 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004182 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004183 continue;
4184 }
4185 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004186 case tok::kw___pascal:
4187 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004188 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004189 continue;
4190 }
4191 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004192 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004193 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004194 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004195 continue; // do *not* consume the next token!
4196 }
4197 // otherwise, FALL THROUGH!
4198 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004199 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004200 // If this is not a type-qualifier token, we're done reading type
4201 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004202 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004203 if (EndLoc.isValid())
4204 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004205 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004206 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004207
Reid Spencer5f016e22007-07-11 17:01:13 +00004208 // If the specifier combination wasn't legal, issue a diagnostic.
4209 if (isInvalid) {
4210 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004211 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004212 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004213 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004214 }
4215}
4216
4217
4218/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4219///
4220void Parser::ParseDeclarator(Declarator &D) {
4221 /// This implements the 'declarator' production in the C grammar, then checks
4222 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004223 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004224}
4225
Richard Smith9988f282012-03-29 01:16:42 +00004226static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4227 if (Kind == tok::star || Kind == tok::caret)
4228 return true;
4229
4230 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4231 if (!Lang.CPlusPlus)
4232 return false;
4233
4234 return Kind == tok::amp || Kind == tok::ampamp;
4235}
4236
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004237/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4238/// is parsed by the function passed to it. Pass null, and the direct-declarator
4239/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004240/// ptr-operator production.
4241///
Richard Smith0706df42011-10-19 21:33:05 +00004242/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004243/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4244/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004245///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004246/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4247/// [C] pointer[opt] direct-declarator
4248/// [C++] direct-declarator
4249/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004250///
4251/// pointer: [C99 6.7.5]
4252/// '*' type-qualifier-list[opt]
4253/// '*' type-qualifier-list[opt] pointer
4254///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004255/// ptr-operator:
4256/// '*' cv-qualifier-seq[opt]
4257/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004258/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004259/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004260/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004261/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004262void Parser::ParseDeclaratorInternal(Declarator &D,
4263 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004264 if (Diags.hasAllExtensionsSilenced())
4265 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004266
Sebastian Redlf30208a2009-01-24 21:16:55 +00004267 // C++ member pointers start with a '::' or a nested-name.
4268 // Member pointers get special handling, since there's no place for the
4269 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004270 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004271 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4272 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004273 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4274 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004275 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004276 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004277
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004278 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004279 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004280 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004281 if (D.mayHaveIdentifier())
4282 D.getCXXScopeSpec() = SS;
4283 else
4284 AnnotateScopeToken(SS, true);
4285
Sebastian Redlf30208a2009-01-24 21:16:55 +00004286 if (DirectDeclParser)
4287 (this->*DirectDeclParser)(D);
4288 return;
4289 }
4290
4291 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004292 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004293 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004294 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004295 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004296
4297 // Recurse to parse whatever is left.
4298 ParseDeclaratorInternal(D, DirectDeclParser);
4299
4300 // Sema will have to catch (syntactically invalid) pointers into global
4301 // scope. It has to catch pointers into namespace scope anyway.
4302 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004303 Loc),
4304 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004305 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004306 return;
4307 }
4308 }
4309
4310 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004311 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004312 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004313 if (DirectDeclParser)
4314 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004315 return;
4316 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004317
Sebastian Redl05532f22009-03-15 22:02:01 +00004318 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4319 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004320 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004321 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004322
Chris Lattner9af55002009-03-27 04:18:06 +00004323 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004324 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004325 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004326
Richard Smith6ee326a2012-04-10 01:32:12 +00004327 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004328 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004329 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004330
Reid Spencer5f016e22007-07-11 17:01:13 +00004331 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004332 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004333 if (Kind == tok::star)
4334 // Remember that we parsed a pointer type, and remember the type-quals.
4335 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004336 DS.getConstSpecLoc(),
4337 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004338 DS.getRestrictSpecLoc()),
4339 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004340 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004341 else
4342 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004343 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004344 Loc),
4345 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004346 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004347 } else {
4348 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004349 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004350
Sebastian Redl743de1f2009-03-23 00:00:23 +00004351 // Complain about rvalue references in C++03, but then go on and build
4352 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004353 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004354 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004355 diag::warn_cxx98_compat_rvalue_reference :
4356 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004357
Richard Smith6ee326a2012-04-10 01:32:12 +00004358 // GNU-style and C++11 attributes are allowed here, as is restrict.
4359 ParseTypeQualifierListOpt(DS);
4360 D.ExtendWithDeclSpec(DS);
4361
Reid Spencer5f016e22007-07-11 17:01:13 +00004362 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4363 // cv-qualifiers are introduced through the use of a typedef or of a
4364 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004365 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4366 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4367 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004368 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004369 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4370 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004371 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004372 // 'restrict' is permitted as an extension.
4373 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4374 Diag(DS.getAtomicSpecLoc(),
4375 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Reid Spencer5f016e22007-07-11 17:01:13 +00004376 }
4377
4378 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004379 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004380
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004381 if (D.getNumTypeObjects() > 0) {
4382 // C++ [dcl.ref]p4: There shall be no references to references.
4383 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4384 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004385 if (const IdentifierInfo *II = D.getIdentifier())
4386 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4387 << II;
4388 else
4389 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4390 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004391
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004392 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004393 // can go ahead and build the (technically ill-formed)
4394 // declarator: reference collapsing will take care of it.
4395 }
4396 }
4397
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004398 // Remember that we parsed a reference type.
Chris Lattner76549142008-02-21 01:32:26 +00004399 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004400 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004401 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004402 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004403 }
4404}
4405
Richard Smith9988f282012-03-29 01:16:42 +00004406static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4407 SourceLocation EllipsisLoc) {
4408 if (EllipsisLoc.isValid()) {
4409 FixItHint Insertion;
4410 if (!D.getEllipsisLoc().isValid()) {
4411 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4412 D.setEllipsisLoc(EllipsisLoc);
4413 }
4414 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4415 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4416 }
4417}
4418
Reid Spencer5f016e22007-07-11 17:01:13 +00004419/// ParseDirectDeclarator
4420/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004421/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004422/// '(' declarator ')'
4423/// [GNU] '(' attributes declarator ')'
4424/// [C90] direct-declarator '[' constant-expression[opt] ']'
4425/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4426/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4427/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4428/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004429/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4430/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004431/// direct-declarator '(' parameter-type-list ')'
4432/// direct-declarator '(' identifier-list[opt] ')'
4433/// [GNU] direct-declarator '(' parameter-forward-declarations
4434/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004435/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4436/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004437/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4438/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4439/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004440/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004441/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004442///
4443/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004444/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004445/// '::'[opt] nested-name-specifier[opt] type-name
4446///
4447/// id-expression: [C++ 5.1]
4448/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004449/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004450///
4451/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004452/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004453/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004454/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004455/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004456/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004457///
Richard Smith5d8388c2012-03-27 01:42:32 +00004458/// Note, any additional constructs added here may need corresponding changes
4459/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004460void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004461 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004462
David Blaikie4e4d0842012-03-11 07:00:24 +00004463 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004464 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004465 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004466 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4467 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004468 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004469 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004470 }
4471
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004472 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004473 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004474 // Change the declaration context for name lookup, until this function
4475 // is exited (and the declarator has been parsed).
4476 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004477 }
4478
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004479 // C++0x [dcl.fct]p14:
4480 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004481 // of a parameter-declaration-clause without a preceding comma. In
4482 // this case, the ellipsis is parsed as part of the
4483 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004484 // parameter pack that has not been expanded; otherwise, it is parsed
4485 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004486 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004487 !((D.getContext() == Declarator::PrototypeContext ||
4488 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004489 NextToken().is(tok::r_paren) &&
Richard Smith30f2a742013-02-20 20:19:27 +00004490 !D.hasGroupingParens() &&
Richard Smith9988f282012-03-29 01:16:42 +00004491 !Actions.containsUnexpandedParameterPacks(D))) {
4492 SourceLocation EllipsisLoc = ConsumeToken();
4493 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4494 // The ellipsis was put in the wrong place. Recover, and explain to
4495 // the user what they should have done.
4496 ParseDeclarator(D);
4497 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4498 return;
4499 } else
4500 D.setEllipsisLoc(EllipsisLoc);
4501
4502 // The ellipsis can't be followed by a parenthesized declarator. We
4503 // check for that in ParseParenDeclarator, after we have disambiguated
4504 // the l_paren token.
4505 }
4506
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004507 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4508 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4509 // We found something that indicates the start of an unqualified-id.
4510 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004511 bool AllowConstructorName;
4512 if (D.getDeclSpec().hasTypeSpecifier())
4513 AllowConstructorName = false;
4514 else if (D.getCXXScopeSpec().isSet())
4515 AllowConstructorName =
4516 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00004517 D.getContext() == Declarator::MemberContext);
John McCallba9d8532010-04-13 06:39:49 +00004518 else
4519 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4520
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004521 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004522 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4523 /*EnteringContext=*/true,
4524 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004525 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004526 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004527 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004528 D.getName()) ||
4529 // Once we're past the identifier, if the scope was bad, mark the
4530 // whole declarator bad.
4531 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004532 D.SetIdentifier(0, Tok.getLocation());
4533 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004534 } else {
4535 // Parsed the unqualified-id; update range information and move along.
4536 if (D.getSourceRange().getBegin().isInvalid())
4537 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4538 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004539 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004540 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004541 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004542 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004543 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004544 "There's a C++-specific check for tok::identifier above");
4545 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4546 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4547 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004548 goto PastIdentifier;
4549 }
Richard Smith9988f282012-03-29 01:16:42 +00004550
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004551 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004552 // direct-declarator: '(' declarator ')'
4553 // direct-declarator: '(' attributes declarator ')'
4554 // Example: 'char (*X)' or 'int (*XX)(void)'
4555 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004556
4557 // If the declarator was parenthesized, we entered the declarator
4558 // scope when parsing the parenthesized declarator, then exited
4559 // the scope already. Re-enter the scope, if we need to.
4560 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004561 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004562 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004563 if (!D.isInvalidType() &&
4564 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004565 // Change the declaration context for name lookup, until this function
4566 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004567 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004568 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004569 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004570 // This could be something simple like "int" (in which case the declarator
4571 // portion is empty), if an abstract-declarator is allowed.
4572 D.SetIdentifier(0, Tok.getLocation());
Richard Smith30f2a742013-02-20 20:19:27 +00004573
4574 // The grammar for abstract-pack-declarator does not allow grouping parens.
4575 // FIXME: Revisit this once core issue 1488 is resolved.
4576 if (D.hasEllipsis() && D.hasGroupingParens())
4577 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4578 diag::ext_abstract_pack_declarator_parens);
Reid Spencer5f016e22007-07-11 17:01:13 +00004579 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004580 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004581 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004582 if (D.getContext() == Declarator::MemberContext)
4583 Diag(Tok, diag::err_expected_member_name_or_semi)
4584 << D.getDeclSpec().getSourceRange();
Richard Trieudb55c04c2013-01-26 02:31:38 +00004585 else if (getLangOpts().CPlusPlus) {
4586 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4587 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4588 else
4589 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4590 } else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004591 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004592 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004593 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004594 }
Mike Stump1eb44332009-09-09 15:08:12 +00004595
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004596 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004597 assert(D.isPastIdentifier() &&
4598 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004599
Richard Smith6ee326a2012-04-10 01:32:12 +00004600 // Don't parse attributes unless we have parsed an unparenthesized name.
4601 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004602 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004603
Reid Spencer5f016e22007-07-11 17:01:13 +00004604 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004605 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004606 // Enter function-declaration scope, limiting any declarators to the
4607 // function prototype scope, including parameter declarators.
4608 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004609 Scope::FunctionPrototypeScope|Scope::DeclScope|
4610 (D.isFunctionDeclaratorAFunctionDeclaration()
4611 ? Scope::FunctionDeclarationScope : 0));
4612
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004613 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4614 // In such a case, check if we actually have a function declarator; if it
4615 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004616 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004617 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4618 // The name of the declarator, if any, is tentatively declared within
4619 // a possible direct initializer.
4620 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4621 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4622 TentativelyDeclaredIdentifiers.pop_back();
4623 if (!IsFunctionDecl)
4624 break;
4625 }
John McCall0b7e6782011-03-24 11:26:52 +00004626 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004627 BalancedDelimiterTracker T(*this, tok::l_paren);
4628 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004629 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004630 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004631 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004632 ParseBracketDeclarator(D);
4633 } else {
4634 break;
4635 }
4636 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004637}
Reid Spencer5f016e22007-07-11 17:01:13 +00004638
Chris Lattneref4715c2008-04-06 05:45:57 +00004639/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4640/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004641/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004642/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4643///
4644/// direct-declarator:
4645/// '(' declarator ')'
4646/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004647/// direct-declarator '(' parameter-type-list ')'
4648/// direct-declarator '(' identifier-list[opt] ')'
4649/// [GNU] direct-declarator '(' parameter-forward-declarations
4650/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004651///
4652void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004653 BalancedDelimiterTracker T(*this, tok::l_paren);
4654 T.consumeOpen();
4655
Chris Lattneref4715c2008-04-06 05:45:57 +00004656 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004657
Chris Lattner7399ee02008-10-20 02:05:46 +00004658 // Eat any attributes before we look at whether this is a grouping or function
4659 // declarator paren. If this is a grouping paren, the attribute applies to
4660 // the type being built up, for example:
4661 // int (__attribute__(()) *x)(long y)
4662 // If this ends up not being a grouping paren, the attribute applies to the
4663 // first argument, for example:
4664 // int (__attribute__(()) int x)
4665 // In either case, we need to eat any attributes to be able to determine what
4666 // sort of paren this is.
4667 //
John McCall0b7e6782011-03-24 11:26:52 +00004668 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004669 bool RequiresArg = false;
4670 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004671 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004672
Chris Lattner7399ee02008-10-20 02:05:46 +00004673 // We require that the argument list (if this is a non-grouping paren) be
4674 // present even if the attribute list was empty.
4675 RequiresArg = true;
4676 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004677
Steve Naroff239f0732008-12-25 14:16:32 +00004678 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004679 ParseMicrosoftTypeAttributes(attrs);
4680
Dawn Perchik52fc3142010-09-03 01:29:35 +00004681 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004682 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004683 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004684
Chris Lattneref4715c2008-04-06 05:45:57 +00004685 // If we haven't past the identifier yet (or where the identifier would be
4686 // stored, if this is an abstract declarator), then this is probably just
4687 // grouping parens. However, if this could be an abstract-declarator, then
4688 // this could also be the start of function arguments (consider 'void()').
4689 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004690
Chris Lattneref4715c2008-04-06 05:45:57 +00004691 if (!D.mayOmitIdentifier()) {
4692 // If this can't be an abstract-declarator, this *must* be a grouping
4693 // paren, because we haven't seen the identifier yet.
4694 isGrouping = true;
4695 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004696 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4697 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004698 isDeclarationSpecifier() || // 'int(int)' is a function.
4699 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004700 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4701 // considered to be a type, not a K&R identifier-list.
4702 isGrouping = false;
4703 } else {
4704 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4705 isGrouping = true;
4706 }
Mike Stump1eb44332009-09-09 15:08:12 +00004707
Chris Lattneref4715c2008-04-06 05:45:57 +00004708 // If this is a grouping paren, handle:
4709 // direct-declarator: '(' declarator ')'
4710 // direct-declarator: '(' attributes declarator ')'
4711 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004712 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4713 D.setEllipsisLoc(SourceLocation());
4714
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004715 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004716 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004717 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004718 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004719 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004720 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004721 T.getCloseLocation()),
4722 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004723
4724 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004725
4726 // An ellipsis cannot be placed outside parentheses.
4727 if (EllipsisLoc.isValid())
4728 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4729
Chris Lattneref4715c2008-04-06 05:45:57 +00004730 return;
4731 }
Mike Stump1eb44332009-09-09 15:08:12 +00004732
Chris Lattneref4715c2008-04-06 05:45:57 +00004733 // Okay, if this wasn't a grouping paren, it must be the start of a function
4734 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004735 // identifier (and remember where it would have been), then call into
4736 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004737 D.SetIdentifier(0, Tok.getLocation());
4738
David Blaikie42d6d0c2011-12-04 05:04:18 +00004739 // Enter function-declaration scope, limiting any declarators to the
4740 // function prototype scope, including parameter declarators.
4741 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004742 Scope::FunctionPrototypeScope | Scope::DeclScope |
4743 (D.isFunctionDeclaratorAFunctionDeclaration()
4744 ? Scope::FunctionDeclarationScope : 0));
Richard Smithb9c62612012-07-30 21:30:52 +00004745 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004746 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004747}
4748
4749/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4750/// declarator D up to a paren, which indicates that we are parsing function
4751/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004752///
Richard Smith6ee326a2012-04-10 01:32:12 +00004753/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4754/// immediately after the open paren - they should be considered to be the
4755/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004756///
Richard Smith6ee326a2012-04-10 01:32:12 +00004757/// If RequiresArg is true, then the first argument of the function is required
4758/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004759///
Richard Smith6ee326a2012-04-10 01:32:12 +00004760/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4761/// (C++11) ref-qualifier[opt], exception-specification[opt],
4762/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4763///
4764/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004765/// dynamic-exception-specification
4766/// noexcept-specification
4767///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004768void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004769 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004770 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004771 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004772 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004773 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004774 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004775 // lparen is already consumed!
4776 assert(D.isPastIdentifier() && "Should not call before identifier!");
4777
4778 // This should be true when the function has typed arguments.
4779 // Otherwise, it is treated as a K&R-style function.
4780 bool HasProto = false;
4781 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004782 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004783 // Remember where we see an ellipsis, if any.
4784 SourceLocation EllipsisLoc;
4785
4786 DeclSpec DS(AttrFactory);
4787 bool RefQualifierIsLValueRef = true;
4788 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004789 SourceLocation ConstQualifierLoc;
4790 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004791 ExceptionSpecificationType ESpecType = EST_None;
4792 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004793 SmallVector<ParsedType, 2> DynamicExceptions;
4794 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004795 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004796 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004797 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004798
James Molloy16f1f712012-02-29 10:24:19 +00004799 Actions.ActOnStartFunctionDeclarator();
4800
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004801 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4802 EndLoc is the end location for the function declarator.
4803 They differ for trailing return types. */
4804 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004805 SourceLocation LParenLoc, RParenLoc;
4806 LParenLoc = Tracker.getOpenLocation();
4807 StartLoc = LParenLoc;
4808
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004809 if (isFunctionDeclaratorIdentifierList()) {
4810 if (RequiresArg)
4811 Diag(Tok, diag::err_argument_required_after_attribute);
4812
4813 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4814
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004815 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004816 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004817 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004818 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004819 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004820 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004821 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004822 else if (RequiresArg)
4823 Diag(Tok, diag::err_argument_required_after_attribute);
4824
David Blaikie4e4d0842012-03-11 07:00:24 +00004825 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004826
4827 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004828 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004829 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004830 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004831 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004832
David Blaikie4e4d0842012-03-11 07:00:24 +00004833 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004834 // FIXME: Accept these components in any order, and produce fixits to
4835 // correct the order if the user gets it wrong. Ideally we should deal
4836 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004837
4838 // Parse cv-qualifier-seq[opt].
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004839 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
4840 /*CXX11AttributesAllowed*/ false,
4841 /*AtomicAllowed*/ false);
Richard Smith6ee326a2012-04-10 01:32:12 +00004842 if (!DS.getSourceRange().getEnd().isInvalid()) {
4843 EndLoc = DS.getSourceRange().getEnd();
4844 ConstQualifierLoc = DS.getConstSpecLoc();
4845 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4846 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004847
4848 // Parse ref-qualifier[opt].
4849 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004850 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004851 diag::warn_cxx98_compat_ref_qualifier :
4852 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004853
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004854 RefQualifierIsLValueRef = Tok.is(tok::amp);
4855 RefQualifierLoc = ConsumeToken();
4856 EndLoc = RefQualifierLoc;
4857 }
4858
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004859 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00004860 // If a declaration declares a member function or member function
4861 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004862 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00004863 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004864 // declarator.
Richard Smithd9227792013-03-15 00:41:52 +00004865 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosier8decdee2012-06-26 22:30:43 +00004866 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00004867 getLangOpts().CPlusPlus11 &&
Richard Smithd9227792013-03-15 00:41:52 +00004868 (D.getContext() == Declarator::MemberContext
4869 ? !D.getDeclSpec().isFriendSpecified()
4870 : D.getContext() == Declarator::FileContext &&
4871 D.getCXXScopeSpec().isValid() &&
4872 Actions.CurContext->isRecord());
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004873 Sema::CXXThisScopeRAII ThisScope(Actions,
4874 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00004875 DS.getTypeQualifiers() |
4876 (D.getDeclSpec().isConstexprSpecified()
4877 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004878 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004879
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004880 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004881 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004882 DynamicExceptions,
4883 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004884 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004885 if (ESpecType != EST_None)
4886 EndLoc = ESpecRange.getEnd();
4887
Richard Smith6ee326a2012-04-10 01:32:12 +00004888 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4889 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00004890 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004891
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004892 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004893 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00004894 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004895 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004896 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4897 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004898 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00004899 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004900 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004901 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004902 }
4903 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004904 }
4905
4906 // Remember that we parsed a function type, and remember the attributes.
4907 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004908 IsAmbiguous,
4909 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004910 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004911 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004912 DS.getTypeQualifiers(),
4913 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004914 RefQualifierLoc, ConstQualifierLoc,
4915 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004916 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004917 ESpecType, ESpecRange.getBegin(),
4918 DynamicExceptions.data(),
4919 DynamicExceptionRanges.data(),
4920 DynamicExceptions.size(),
4921 NoexceptExpr.isUsable() ?
4922 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004923 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004924 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004925 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004926
4927 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004928}
4929
4930/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4931/// identifier list form for a K&R-style function: void foo(a,b,c)
4932///
4933/// Note that identifier-lists are only allowed for normal declarators, not for
4934/// abstract-declarators.
4935bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004936 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004937 && Tok.is(tok::identifier)
4938 && !TryAltiVecVectorToken()
4939 // K&R identifier lists can't have typedefs as identifiers, per C99
4940 // 6.7.5.3p11.
4941 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4942 // Identifier lists follow a really simple grammar: the identifiers can
4943 // be followed *only* by a ", identifier" or ")". However, K&R
4944 // identifier lists are really rare in the brave new modern world, and
4945 // it is very common for someone to typo a type in a non-K&R style
4946 // list. If we are presented with something like: "void foo(intptr x,
4947 // float y)", we don't want to start parsing the function declarator as
4948 // though it is a K&R style declarator just because intptr is an
4949 // invalid type.
4950 //
4951 // To handle this, we check to see if the token after the first
4952 // identifier is a "," or ")". Only then do we parse it as an
4953 // identifier list.
4954 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4955}
4956
4957/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4958/// we found a K&R-style identifier list instead of a typed parameter list.
4959///
4960/// After returning, ParamInfo will hold the parsed parameters.
4961///
4962/// identifier-list: [C99 6.7.5]
4963/// identifier
4964/// identifier-list ',' identifier
4965///
4966void Parser::ParseFunctionDeclaratorIdentifierList(
4967 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004968 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004969 // If there was no identifier specified for the declarator, either we are in
4970 // an abstract-declarator, or we are in a parameter declarator which was found
4971 // to be abstract. In abstract-declarators, identifier lists are not valid:
4972 // diagnose this.
4973 if (!D.getIdentifier())
4974 Diag(Tok, diag::ext_ident_list_in_param);
4975
4976 // Maintain an efficient lookup of params we have seen so far.
4977 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4978
4979 while (1) {
4980 // If this isn't an identifier, report the error and skip until ')'.
4981 if (Tok.isNot(tok::identifier)) {
4982 Diag(Tok, diag::err_expected_ident);
4983 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4984 // Forget we parsed anything.
4985 ParamInfo.clear();
4986 return;
4987 }
4988
4989 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4990
4991 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4992 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4993 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4994
4995 // Verify that the argument identifier has not already been mentioned.
4996 if (!ParamsSoFar.insert(ParmII)) {
4997 Diag(Tok, diag::err_param_redefinition) << ParmII;
4998 } else {
4999 // Remember this identifier in ParamInfo.
5000 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5001 Tok.getLocation(),
5002 0));
5003 }
5004
5005 // Eat the identifier.
5006 ConsumeToken();
5007
5008 // The list continues if we see a comma.
5009 if (Tok.isNot(tok::comma))
5010 break;
5011 ConsumeToken();
5012 }
5013}
5014
5015/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5016/// after the opening parenthesis. This function will not parse a K&R-style
5017/// identifier list.
5018///
Richard Smith6ce48a72012-04-11 04:01:28 +00005019/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5020/// caller parsed those arguments immediately after the open paren - they should
5021/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005022///
5023/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5024/// be the location of the ellipsis, if any was parsed.
5025///
Reid Spencer5f016e22007-07-11 17:01:13 +00005026/// parameter-type-list: [C99 6.7.5]
5027/// parameter-list
5028/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00005029/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00005030///
5031/// parameter-list: [C99 6.7.5]
5032/// parameter-declaration
5033/// parameter-list ',' parameter-declaration
5034///
5035/// parameter-declaration: [C99 6.7.5]
5036/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00005037/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00005038/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00005039/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00005040/// declaration-specifiers abstract-declarator[opt]
5041/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00005042/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00005043/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00005044/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00005045///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005046void Parser::ParseParameterDeclarationClause(
5047 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00005048 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005049 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005050 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00005051
Chris Lattnerf97409f2008-04-06 06:57:35 +00005052 while (1) {
5053 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00005054 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5055 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00005056 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00005057 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00005058 }
Mike Stump1eb44332009-09-09 15:08:12 +00005059
Chris Lattnerf97409f2008-04-06 06:57:35 +00005060 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00005061 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00005062 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005063
Richard Smith6ce48a72012-04-11 04:01:28 +00005064 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005065 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00005066
John McCall7f040a92010-12-24 02:08:15 +00005067 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00005068 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00005069
5070 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00005071
5072 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00005073 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005074 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00005075 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5076 // too much hassle.
5077 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00005078
Chris Lattnere64c5492009-02-27 18:38:20 +00005079 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00005080
Chris Lattnerf97409f2008-04-06 06:57:35 +00005081 // Parse the declarator. This is "PrototypeContext", because we must
5082 // accept either 'declarator' or 'abstract-declarator' here.
5083 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5084 ParseDeclarator(ParmDecl);
5085
5086 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00005087 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00005088
Chris Lattnerf97409f2008-04-06 06:57:35 +00005089 // Remember this parsed parameter in ParamInfo.
5090 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00005091
Douglas Gregor72b505b2008-12-16 21:30:33 +00005092 // DefArgToks is used when the parsing of default arguments needs
5093 // to be delayed.
5094 CachedTokens *DefArgToks = 0;
5095
Chris Lattnerf97409f2008-04-06 06:57:35 +00005096 // If no parameter was specified, verify that *something* was specified,
5097 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00005098 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5099 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005100 // Completely missing, emit error.
5101 Diag(DSStart, diag::err_missing_param);
5102 } else {
5103 // Otherwise, we have something. Add it and let semantic analysis try
5104 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005105
Chris Lattnerf97409f2008-04-06 06:57:35 +00005106 // Inform the actions module about the parameter declarator, so it gets
5107 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00005108 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00005109
5110 // Parse the default argument, if any. We parse the default
5111 // arguments in all dialects; the semantic analysis in
5112 // ActOnParamDefaultArgument will reject the default argument in
5113 // C.
5114 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005115 SourceLocation EqualLoc = Tok.getLocation();
5116
Chris Lattner04421082008-04-08 04:40:51 +00005117 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005118 if (D.getContext() == Declarator::MemberContext) {
5119 // If we're inside a class definition, cache the tokens
5120 // corresponding to the default argument. We'll actually parse
5121 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005122 // FIXME: Can we use a smart pointer for Toks?
5123 DefArgToks = new CachedTokens;
5124
Mike Stump1eb44332009-09-09 15:08:12 +00005125 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00005126 /*StopAtSemi=*/true,
5127 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005128 delete DefArgToks;
5129 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005130 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005131 } else {
5132 // Mark the end of the default argument so that we know when to
5133 // stop when we parse it later on.
5134 Token DefArgEnd;
5135 DefArgEnd.startToken();
5136 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5137 DefArgEnd.setLocation(Tok.getLocation());
5138 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005139 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005140 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005141 }
Chris Lattner04421082008-04-08 04:40:51 +00005142 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005143 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005144 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005145
Chad Rosier8decdee2012-06-26 22:30:43 +00005146 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005147 // used.
5148 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005149 Sema::PotentiallyEvaluatedIfUsed,
5150 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005151
Sebastian Redl84407ba2012-03-14 15:54:00 +00005152 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005153 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005154 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005155 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005156 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005157 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005158 if (DefArgResult.isInvalid()) {
5159 Actions.ActOnParamDefaultArgumentError(Param);
5160 SkipUntil(tok::comma, tok::r_paren, true, true);
5161 } else {
5162 // Inform the actions module about the default argument
5163 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005164 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005165 }
Chris Lattner04421082008-04-08 04:40:51 +00005166 }
5167 }
Mike Stump1eb44332009-09-09 15:08:12 +00005168
5169 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5170 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00005171 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005172 }
5173
5174 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005175 if (Tok.isNot(tok::comma)) {
5176 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005177 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005178
David Blaikie4e4d0842012-03-11 07:00:24 +00005179 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005180 // We have ellipsis without a preceding ',', which is ill-formed
5181 // in C. Complain and provide the fix.
5182 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005183 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005184 }
5185 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005186
Douglas Gregored5d6512009-09-22 21:41:40 +00005187 break;
5188 }
Mike Stump1eb44332009-09-09 15:08:12 +00005189
Chris Lattnerf97409f2008-04-06 06:57:35 +00005190 // Consume the comma.
5191 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005192 }
Mike Stump1eb44332009-09-09 15:08:12 +00005193
Chris Lattner66d28652008-04-06 06:34:08 +00005194}
Chris Lattneref4715c2008-04-06 05:45:57 +00005195
Reid Spencer5f016e22007-07-11 17:01:13 +00005196/// [C90] direct-declarator '[' constant-expression[opt] ']'
5197/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5198/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5199/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5200/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005201/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5202/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005203void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005204 if (CheckProhibitedCXX11Attribute())
5205 return;
5206
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005207 BalancedDelimiterTracker T(*this, tok::l_square);
5208 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005209
Chris Lattner378c7e42008-12-18 07:27:21 +00005210 // C array syntax has many features, but by-far the most common is [] and [4].
5211 // This code does a fast path to handle some of the most obvious cases.
5212 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005213 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005214 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005215 MaybeParseCXX11Attributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005216
Chris Lattner378c7e42008-12-18 07:27:21 +00005217 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005218 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005219 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005220 T.getOpenLocation(),
5221 T.getCloseLocation()),
5222 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005223 return;
5224 } else if (Tok.getKind() == tok::numeric_constant &&
5225 GetLookAheadToken(1).is(tok::r_square)) {
5226 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005227 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005228 ConsumeToken();
5229
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005230 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005231 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005232 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005233
Chris Lattner378c7e42008-12-18 07:27:21 +00005234 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005235 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005236 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005237 T.getOpenLocation(),
5238 T.getCloseLocation()),
5239 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005240 return;
5241 }
Mike Stump1eb44332009-09-09 15:08:12 +00005242
Reid Spencer5f016e22007-07-11 17:01:13 +00005243 // If valid, this location is the position where we read the 'static' keyword.
5244 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005245 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005246 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005247
Reid Spencer5f016e22007-07-11 17:01:13 +00005248 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005249 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005250 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005251 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005252
Reid Spencer5f016e22007-07-11 17:01:13 +00005253 // If we haven't already read 'static', check to see if there is one after the
5254 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005255 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005256 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005257
Reid Spencer5f016e22007-07-11 17:01:13 +00005258 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5259 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005260 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005261
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005262 // Handle the case where we have '[*]' as the array size. However, a leading
5263 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005264 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005265 // infrequent, use of lookahead is not costly here.
5266 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005267 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005268
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005269 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005270 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005271 StaticLoc = SourceLocation(); // Drop the static.
5272 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005273 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005274 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005275 // Note, in C89, this production uses the constant-expr production instead
5276 // of assignment-expr. The only difference is that assignment-expr allows
5277 // things like '=' and '*='. Sema rejects these in C89 mode because they
5278 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005279
Douglas Gregore0762c92009-06-19 23:52:42 +00005280 // Parse the constant-expression or assignment-expression now (depending
5281 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005282 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005283 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005284 } else {
5285 EnterExpressionEvaluationContext Unevaluated(Actions,
5286 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005287 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005288 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005289 }
Mike Stump1eb44332009-09-09 15:08:12 +00005290
Reid Spencer5f016e22007-07-11 17:01:13 +00005291 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005292 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005293 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005294 // If the expression was invalid, skip it.
5295 SkipUntil(tok::r_square);
5296 return;
5297 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005298
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005299 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005300
John McCall0b7e6782011-03-24 11:26:52 +00005301 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005302 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005303
Chris Lattner378c7e42008-12-18 07:27:21 +00005304 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005305 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005306 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005307 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005308 T.getOpenLocation(),
5309 T.getCloseLocation()),
5310 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005311}
5312
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005313/// [GNU] typeof-specifier:
5314/// typeof ( expressions )
5315/// typeof ( type-name )
5316/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005317///
5318void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005319 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005320 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005321 SourceLocation StartLoc = ConsumeToken();
5322
John McCallcfb708c2010-01-13 20:03:27 +00005323 const bool hasParens = Tok.is(tok::l_paren);
5324
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005325 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5326 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005327
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005328 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005329 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005330 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005331 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5332 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005333 if (hasParens)
5334 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005335
5336 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005337 // FIXME: Not accurate, the range gets one token more than it should.
5338 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005339 else
5340 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005341
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005342 if (isCastExpr) {
5343 if (!CastTy) {
5344 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005345 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005346 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005347
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005348 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005349 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005350 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5351 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005352 DiagID, CastTy))
5353 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005354 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005355 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005356
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005357 // If we get here, the operand to the typeof was an expresion.
5358 if (Operand.isInvalid()) {
5359 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005360 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005361 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005362
Eli Friedman71b8fb52012-01-21 01:01:51 +00005363 // We might need to transform the operand if it is potentially evaluated.
5364 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5365 if (Operand.isInvalid()) {
5366 DS.SetTypeSpecError();
5367 return;
5368 }
5369
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005370 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005371 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005372 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5373 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005374 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005375 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005376}
Chris Lattner1b492422010-02-28 18:33:55 +00005377
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005378/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005379/// _Atomic ( type-name )
5380///
5381void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005382 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5383 "Not an atomic specifier");
Eli Friedmanb001de72011-10-06 23:00:33 +00005384
5385 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005386 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005387 if (T.consumeOpen())
Eli Friedmanb001de72011-10-06 23:00:33 +00005388 return;
Eli Friedmanb001de72011-10-06 23:00:33 +00005389
5390 TypeResult Result = ParseTypeName();
5391 if (Result.isInvalid()) {
5392 SkipUntil(tok::r_paren);
5393 return;
5394 }
5395
5396 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005397 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005398
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005399 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005400 return;
5401
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005402 DS.setTypeofParensRange(T.getRange());
5403 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005404
5405 const char *PrevSpec = 0;
5406 unsigned DiagID;
5407 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5408 DiagID, Result.release()))
5409 Diag(StartLoc, DiagID) << PrevSpec;
5410}
5411
Chris Lattner1b492422010-02-28 18:33:55 +00005412
5413/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5414/// from TryAltiVecVectorToken.
5415bool Parser::TryAltiVecVectorTokenOutOfLine() {
5416 Token Next = NextToken();
5417 switch (Next.getKind()) {
5418 default: return false;
5419 case tok::kw_short:
5420 case tok::kw_long:
5421 case tok::kw_signed:
5422 case tok::kw_unsigned:
5423 case tok::kw_void:
5424 case tok::kw_char:
5425 case tok::kw_int:
5426 case tok::kw_float:
5427 case tok::kw_double:
5428 case tok::kw_bool:
5429 case tok::kw___pixel:
5430 Tok.setKind(tok::kw___vector);
5431 return true;
5432 case tok::identifier:
5433 if (Next.getIdentifierInfo() == Ident_pixel) {
5434 Tok.setKind(tok::kw___vector);
5435 return true;
5436 }
5437 return false;
5438 }
5439}
5440
5441bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5442 const char *&PrevSpec, unsigned &DiagID,
5443 bool &isInvalid) {
5444 if (Tok.getIdentifierInfo() == Ident_vector) {
5445 Token Next = NextToken();
5446 switch (Next.getKind()) {
5447 case tok::kw_short:
5448 case tok::kw_long:
5449 case tok::kw_signed:
5450 case tok::kw_unsigned:
5451 case tok::kw_void:
5452 case tok::kw_char:
5453 case tok::kw_int:
5454 case tok::kw_float:
5455 case tok::kw_double:
5456 case tok::kw_bool:
5457 case tok::kw___pixel:
5458 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5459 return true;
5460 case tok::identifier:
5461 if (Next.getIdentifierInfo() == Ident_pixel) {
5462 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5463 return true;
5464 }
5465 break;
5466 default:
5467 break;
5468 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005469 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005470 DS.isTypeAltiVecVector()) {
5471 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5472 return true;
5473 }
5474 return false;
5475}