blob: 697e95a73ef2b0e4afd88b5cbf617ca0c3aeafdb [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.
John McCall76da55d2013-04-16 07:28:30 +0000397 if (Tok.isNot(tok::l_paren)) {
398 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
399 << Ident->getNameStart();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000400 return;
John McCall76da55d2013-04-16 07:28:30 +0000401 }
402 BalancedDelimiterTracker T(*this, tok::l_paren);
403 T.expectAndConsume(diag::err_expected_lparen_after,
404 Ident->getNameStart(), tok::r_paren);
405
406 enum AccessorKind {
407 AK_Invalid = -1,
408 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
409 };
410 IdentifierInfo *AccessorNames[] = { 0, 0 };
411 bool HasInvalidAccessor = false;
412
413 // Parse the accessor specifications.
414 while (true) {
415 // Stop if this doesn't look like an accessor spec.
416 if (!Tok.is(tok::identifier)) {
417 // If the user wrote a completely empty list, use a special diagnostic.
418 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
419 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
420 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
421 break;
422 }
423
424 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
425 break;
426 }
427
428 AccessorKind Kind;
429 SourceLocation KindLoc = Tok.getLocation();
430 StringRef KindStr = Tok.getIdentifierInfo()->getName();
431 if (KindStr == "get") {
432 Kind = AK_Get;
433 } else if (KindStr == "put") {
434 Kind = AK_Put;
435
436 // Recover from the common mistake of using 'set' instead of 'put'.
437 } else if (KindStr == "set") {
438 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
439 << FixItHint::CreateReplacement(KindLoc, "put");
440 Kind = AK_Put;
441
442 // Handle the mistake of forgetting the accessor kind by skipping
443 // this accessor.
444 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
445 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
446 ConsumeToken();
447 HasInvalidAccessor = true;
448 goto next_property_accessor;
449
450 // Otherwise, complain about the unknown accessor kind.
451 } else {
452 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
453 HasInvalidAccessor = true;
454 Kind = AK_Invalid;
455
456 // Try to keep parsing unless it doesn't look like an accessor spec.
457 if (!NextToken().is(tok::equal)) break;
458 }
459
460 // Consume the identifier.
461 ConsumeToken();
462
463 // Consume the '='.
464 if (Tok.is(tok::equal)) {
465 ConsumeToken();
466 } else {
467 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
468 << KindStr;
469 break;
470 }
471
472 // Expect the method name.
473 if (!Tok.is(tok::identifier)) {
474 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
475 break;
476 }
477
478 if (Kind == AK_Invalid) {
479 // Just drop invalid accessors.
480 } else if (AccessorNames[Kind] != NULL) {
481 // Complain about the repeated accessor, ignore it, and keep parsing.
482 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
483 } else {
484 AccessorNames[Kind] = Tok.getIdentifierInfo();
485 }
486 ConsumeToken();
487
488 next_property_accessor:
489 // Keep processing accessors until we run out.
490 if (Tok.is(tok::comma)) {
491 ConsumeAnyToken();
492 continue;
493
494 // If we run into the ')', stop without consuming it.
495 } else if (Tok.is(tok::r_paren)) {
496 break;
497 } else {
498 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
499 break;
500 }
501 }
502
503 // Only add the property attribute if it was well-formed.
504 if (!HasInvalidAccessor) {
505 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(), 0,
506 SourceLocation(),
507 AccessorNames[AK_Get], AccessorNames[AK_Put],
508 AttributeList::AS_Declspec);
509 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000510 T.skipToEnd();
511 } else {
512 // We don't recognize this as a valid declspec, but instead of creating the
513 // attribute and allowing sema to warn about it, we will warn here instead.
514 // This is because some attributes have multiple spellings, but we need to
515 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosier8decdee2012-06-26 22:30:43 +0000516 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000517 // both locations.
518 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
519
520 // If there's an open paren, we should eat the open and close parens under
521 // the assumption that this unknown declspec has parameters.
522 BalancedDelimiterTracker T(*this, tok::l_paren);
523 if (!T.consumeOpen())
524 T.skipToEnd();
525 }
526}
527
Eli Friedmana23b4852009-06-08 07:21:15 +0000528/// [MS] decl-specifier:
529/// __declspec ( extended-decl-modifier-seq )
530///
531/// [MS] extended-decl-modifier-seq:
532/// extended-decl-modifier[opt]
533/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000534void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000535 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000536
Steve Narofff59e17e2008-12-24 20:59:21 +0000537 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000538 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000539 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000540 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000541 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000542
Chad Rosier8decdee2012-06-26 22:30:43 +0000543 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000544 // you can specify multiple attributes per declspec.
545 while (Tok.getKind() != tok::r_paren) {
546 // We expect either a well-known identifier or a generic string. Anything
547 // else is a malformed declspec.
548 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosier8decdee2012-06-26 22:30:43 +0000549 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000550 Tok.getKind() != tok::kw_restrict) {
551 Diag(Tok, diag::err_ms_declspec_type);
552 T.skipToEnd();
553 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000554 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000555
556 IdentifierInfo *AttrName;
557 SourceLocation AttrNameLoc;
558 if (IsString) {
559 SmallString<8> StrBuffer;
560 bool Invalid = false;
561 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
562 if (Invalid) {
563 T.skipToEnd();
564 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000565 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000566 AttrName = PP.getIdentifierInfo(Str);
567 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000568 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000569 AttrName = Tok.getIdentifierInfo();
570 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000571 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000572
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000573 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosier8decdee2012-06-26 22:30:43 +0000574 // If we have a generic string, we will allow it because there is no
575 // documented list of allowable string declspecs, but we know they exist
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000576 // (for instance, SAL declspecs in older versions of MSVC).
577 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000578 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000579 // arguments and can be turned into an attribute directly.
Chad Rosier8decdee2012-06-26 22:30:43 +0000580 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000581 0, 0, AttributeList::AS_Declspec);
582 else
583 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000584 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000585 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000586}
587
John McCall7f040a92010-12-24 02:08:15 +0000588void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000589 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000590 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000591 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000592 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Chad Rosierccbb4022012-12-21 21:27:13 +0000593 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000594 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
595 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000596 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith5cd532c2013-01-29 01:24:26 +0000597 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Eli Friedman290eeb02009-06-08 23:27:34 +0000598 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000599}
600
John McCall7f040a92010-12-24 02:08:15 +0000601void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000602 // Treat these like attributes
603 while (Tok.is(tok::kw___pascal)) {
604 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
605 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000606 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith5cd532c2013-01-29 01:24:26 +0000607 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000608 }
John McCall7f040a92010-12-24 02:08:15 +0000609}
610
Peter Collingbournef315fa82011-02-14 01:42:53 +0000611void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
612 // Treat these like attributes
613 while (Tok.is(tok::kw___kernel)) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000614 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbournef315fa82011-02-14 01:42:53 +0000615 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith5cd532c2013-01-29 01:24:26 +0000616 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
617 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000618 }
619}
620
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000621void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000622 // FIXME: The mapping from attribute spelling to semantics should be
623 // performed in Sema, not here.
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000624 SourceLocation Loc = Tok.getLocation();
625 switch(Tok.getKind()) {
626 // OpenCL qualifiers:
627 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000628 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000629 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000630 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000631 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000632 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000633
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000634 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000635 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000636 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000637 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000638 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000639
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000640 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000641 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000642 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000643 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000644 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000645
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000646 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000647 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000648 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000649 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000650 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000651
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000652 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000653 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000654 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000655 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000656 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000657
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000658 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000659 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000660 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000661 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000662 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000663
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000664 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000665 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000666 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000667 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000668 break;
669 default: break;
670 }
671}
672
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000673/// \brief Parse a version number.
674///
675/// version:
676/// simple-integer
677/// simple-integer ',' simple-integer
678/// simple-integer ',' simple-integer ',' simple-integer
679VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
680 Range = Tok.getLocation();
681
682 if (!Tok.is(tok::numeric_constant)) {
683 Diag(Tok, diag::err_expected_version);
684 SkipUntil(tok::comma, tok::r_paren, true, true, true);
685 return VersionTuple();
686 }
687
688 // Parse the major (and possibly minor and subminor) versions, which
689 // are stored in the numeric constant. We utilize a quirk of the
690 // lexer, which is that it handles something like 1.2.3 as a single
691 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000692 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000693 Buffer.resize(Tok.getLength()+1);
694 const char *ThisTokBegin = &Buffer[0];
695
696 // Get the spelling of the token, which eliminates trigraphs, etc.
697 bool Invalid = false;
698 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
699 if (Invalid)
700 return VersionTuple();
701
702 // Parse the major version.
703 unsigned AfterMajor = 0;
704 unsigned Major = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000705 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000706 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
707 ++AfterMajor;
708 }
709
710 if (AfterMajor == 0) {
711 Diag(Tok, diag::err_expected_version);
712 SkipUntil(tok::comma, tok::r_paren, true, true, true);
713 return VersionTuple();
714 }
715
716 if (AfterMajor == ActualLength) {
717 ConsumeToken();
718
719 // We only had a single version component.
720 if (Major == 0) {
721 Diag(Tok, diag::err_zero_version);
722 return VersionTuple();
723 }
724
725 return VersionTuple(Major);
726 }
727
728 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
729 Diag(Tok, diag::err_expected_version);
730 SkipUntil(tok::comma, tok::r_paren, true, true, true);
731 return VersionTuple();
732 }
733
734 // Parse the minor version.
735 unsigned AfterMinor = AfterMajor + 1;
736 unsigned Minor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000737 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000738 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
739 ++AfterMinor;
740 }
741
742 if (AfterMinor == ActualLength) {
743 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000744
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000745 // We had major.minor.
746 if (Major == 0 && Minor == 0) {
747 Diag(Tok, diag::err_zero_version);
748 return VersionTuple();
749 }
750
Chad Rosier8decdee2012-06-26 22:30:43 +0000751 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000752 }
753
754 // If what follows is not a '.', we have a problem.
755 if (ThisTokBegin[AfterMinor] != '.') {
756 Diag(Tok, diag::err_expected_version);
757 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosier8decdee2012-06-26 22:30:43 +0000758 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000759 }
760
761 // Parse the subminor version.
762 unsigned AfterSubminor = AfterMinor + 1;
763 unsigned Subminor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000764 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000765 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
766 ++AfterSubminor;
767 }
768
769 if (AfterSubminor != ActualLength) {
770 Diag(Tok, diag::err_expected_version);
771 SkipUntil(tok::comma, tok::r_paren, true, true, true);
772 return VersionTuple();
773 }
774 ConsumeToken();
775 return VersionTuple(Major, Minor, Subminor);
776}
777
778/// \brief Parse the contents of the "availability" attribute.
779///
780/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000781/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000782///
783/// platform:
784/// identifier
785///
786/// version-arg-list:
787/// version-arg
788/// version-arg ',' version-arg-list
789///
790/// version-arg:
791/// 'introduced' '=' version
792/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000793/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000794/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000795/// opt-message:
796/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000797void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
798 SourceLocation AvailabilityLoc,
799 ParsedAttributes &attrs,
800 SourceLocation *endLoc) {
801 SourceLocation PlatformLoc;
802 IdentifierInfo *Platform = 0;
803
804 enum { Introduced, Deprecated, Obsoleted, Unknown };
805 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000806 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000807
808 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000809 BalancedDelimiterTracker T(*this, tok::l_paren);
810 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000811 Diag(Tok, diag::err_expected_lparen);
812 return;
813 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000814
815 // Parse the platform name,
816 if (Tok.isNot(tok::identifier)) {
817 Diag(Tok, diag::err_availability_expected_platform);
818 SkipUntil(tok::r_paren);
819 return;
820 }
821 Platform = Tok.getIdentifierInfo();
822 PlatformLoc = ConsumeToken();
823
824 // Parse the ',' following the platform name.
825 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
826 return;
827
828 // If we haven't grabbed the pointers for the identifiers
829 // "introduced", "deprecated", and "obsoleted", do so now.
830 if (!Ident_introduced) {
831 Ident_introduced = PP.getIdentifierInfo("introduced");
832 Ident_deprecated = PP.getIdentifierInfo("deprecated");
833 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000834 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000835 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000836 }
837
838 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000839 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000840 do {
841 if (Tok.isNot(tok::identifier)) {
842 Diag(Tok, diag::err_availability_expected_change);
843 SkipUntil(tok::r_paren);
844 return;
845 }
846 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
847 SourceLocation KeywordLoc = ConsumeToken();
848
Douglas Gregorb53e4172011-03-26 03:35:55 +0000849 if (Keyword == Ident_unavailable) {
850 if (UnavailableLoc.isValid()) {
851 Diag(KeywordLoc, diag::err_availability_redundant)
852 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000853 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000854 UnavailableLoc = KeywordLoc;
855
856 if (Tok.isNot(tok::comma))
857 break;
858
859 ConsumeToken();
860 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000861 }
862
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000863 if (Tok.isNot(tok::equal)) {
864 Diag(Tok, diag::err_expected_equal_after)
865 << Keyword;
866 SkipUntil(tok::r_paren);
867 return;
868 }
869 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000870 if (Keyword == Ident_message) {
871 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000872 Diag(Tok, diag::err_expected_string_literal)
873 << /*Source='availability attribute'*/2;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000874 SkipUntil(tok::r_paren);
875 return;
876 }
877 MessageExpr = ParseStringLiteralExpression();
878 break;
879 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000880
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000881 SourceRange VersionRange;
882 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000883
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000884 if (Version.empty()) {
885 SkipUntil(tok::r_paren);
886 return;
887 }
888
889 unsigned Index;
890 if (Keyword == Ident_introduced)
891 Index = Introduced;
892 else if (Keyword == Ident_deprecated)
893 Index = Deprecated;
894 else if (Keyword == Ident_obsoleted)
895 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000896 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000897 Index = Unknown;
898
899 if (Index < Unknown) {
900 if (!Changes[Index].KeywordLoc.isInvalid()) {
901 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000902 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000903 << SourceRange(Changes[Index].KeywordLoc,
904 Changes[Index].VersionRange.getEnd());
905 }
906
907 Changes[Index].KeywordLoc = KeywordLoc;
908 Changes[Index].Version = Version;
909 Changes[Index].VersionRange = VersionRange;
910 } else {
911 Diag(KeywordLoc, diag::err_availability_unknown_change)
912 << Keyword << VersionRange;
913 }
914
915 if (Tok.isNot(tok::comma))
916 break;
917
918 ConsumeToken();
919 } while (true);
920
921 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000922 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000923 return;
924
925 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000926 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000927
Douglas Gregorb53e4172011-03-26 03:35:55 +0000928 // The 'unavailable' availability cannot be combined with any other
929 // availability changes. Make sure that hasn't happened.
930 if (UnavailableLoc.isValid()) {
931 bool Complained = false;
932 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
933 if (Changes[Index].KeywordLoc.isValid()) {
934 if (!Complained) {
935 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
936 << SourceRange(Changes[Index].KeywordLoc,
937 Changes[Index].VersionRange.getEnd());
938 Complained = true;
939 }
940
941 // Clear out the availability.
942 Changes[Index] = AvailabilityChange();
943 }
944 }
945 }
946
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000947 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000948 attrs.addNew(&Availability,
949 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000950 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000951 Platform, PlatformLoc,
952 Changes[Introduced],
953 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000954 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000955 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000956 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000957}
958
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000959
Bill Wendlingad017fa2012-12-20 19:22:21 +0000960// Late Parsed Attributes:
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000961// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
962
963void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
964
965void Parser::LateParsedClass::ParseLexedAttributes() {
966 Self->ParseLexedAttributes(*Class);
967}
968
969void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000970 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000971}
972
973/// Wrapper class which calls ParseLexedAttribute, after setting up the
974/// scope appropriately.
975void Parser::ParseLexedAttributes(ParsingClass &Class) {
976 // Deal with templates
977 // FIXME: Test cases to make sure this does the right thing for templates.
978 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
979 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
980 HasTemplateScope);
981 if (HasTemplateScope)
982 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
983
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000984 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000985 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000986 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000987 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
988 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
989
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000990 // Enter the scope of nested classes
991 if (!AlreadyHasClassScope)
992 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
993 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000994 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000995 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
996 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
997 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000998 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000999
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +00001000 if (!AlreadyHasClassScope)
1001 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1002 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001003}
1004
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001005
1006/// \brief Parse all attributes in LAs, and attach them to Decl D.
1007void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1008 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +00001009 assert(LAs.parseSoon() &&
1010 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001011 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +00001012 if (D)
1013 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001014 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +00001015 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001016 }
1017 LAs.clear();
1018}
1019
1020
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001021/// \brief Finish parsing an attribute for which parsing was delayed.
1022/// This will be called at the end of parsing a class declaration
1023/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +00001024/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001025/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001026void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1027 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001028 // Save the current token position.
1029 SourceLocation OrigLoc = Tok.getLocation();
1030
1031 // Append the current token at the end of the new token stream so that it
1032 // doesn't get lost.
1033 LA.Toks.push_back(Tok);
1034 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1035 // Consume the previously pushed token.
Argyrios Kyrtzidisab2d09b2013-03-27 23:58:17 +00001036 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001037
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001038 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smithcd8ab512013-01-17 01:30:42 +00001039 // FIXME: Do not warn on C++11 attributes, once we start supporting
1040 // them here.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001041 Diag(Tok, diag::warn_attribute_on_function_definition)
1042 << LA.AttrName.getName();
1043 }
1044
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001045 ParsedAttributes Attrs(AttrFactory);
1046 SourceLocation endLoc;
1047
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001048 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001049 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001050 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1051 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +00001052
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001053 // Allow 'this' within late-parsed attributes.
1054 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
1055 /*TypeQuals=*/0,
1056 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001057
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001058 if (LA.Decls.size() == 1) {
1059 // If the Decl is templatized, add template parameters to scope.
1060 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1061 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1062 if (HasTemplateScope)
1063 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001064
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001065 // If the Decl is on a function, add function parameters to the scope.
1066 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1067 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1068 if (HasFunScope)
1069 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001070
Michael Han6880f492012-10-03 01:56:22 +00001071 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +00001072 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001073
1074 if (HasFunScope) {
1075 Actions.ActOnExitFunctionContext();
1076 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1077 }
1078 if (HasTemplateScope) {
1079 TempScope.Exit();
1080 }
1081 } else {
1082 // If there are multiple decls, then the decl cannot be within the
1083 // function scope.
Michael Han6880f492012-10-03 01:56:22 +00001084 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +00001085 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001086 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +00001087 } else {
1088 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +00001089 }
1090
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001091 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1092 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1093 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001094
1095 if (Tok.getLocation() != OrigLoc) {
1096 // Due to a parsing error, we either went over the cached tokens or
1097 // there are still cached tokens left, so we skip the leftover tokens.
1098 // Since this is an uncommon situation that should be avoided, use the
1099 // expensive isBeforeInTranslationUnit call.
1100 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1101 OrigLoc))
1102 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001103 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001104 }
1105}
1106
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001107/// \brief Wrapper around a case statement checking if AttrName is
1108/// one of the thread safety attributes
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001109bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001110 return llvm::StringSwitch<bool>(AttrName)
1111 .Case("guarded_by", true)
1112 .Case("guarded_var", true)
1113 .Case("pt_guarded_by", true)
1114 .Case("pt_guarded_var", true)
1115 .Case("lockable", true)
1116 .Case("scoped_lockable", true)
1117 .Case("no_thread_safety_analysis", true)
1118 .Case("acquired_after", true)
1119 .Case("acquired_before", true)
1120 .Case("exclusive_lock_function", true)
1121 .Case("shared_lock_function", true)
1122 .Case("exclusive_trylock_function", true)
1123 .Case("shared_trylock_function", true)
1124 .Case("unlock_function", true)
1125 .Case("lock_returned", true)
1126 .Case("locks_excluded", true)
1127 .Case("exclusive_locks_required", true)
1128 .Case("shared_locks_required", true)
1129 .Default(false);
1130}
1131
1132/// \brief Parse the contents of thread safety attributes. These
1133/// should always be parsed as an expression list.
1134///
1135/// We need to special case the parsing due to the fact that if the first token
1136/// of the first argument is an identifier, the main parse loop will store
1137/// that token as a "parameter" and the rest of
1138/// the arguments will be added to a list of "arguments". However,
1139/// subsequent tokens in the first argument are lost. We instead parse each
1140/// argument as an expression and add all arguments to the list of "arguments".
1141/// In future, we will take advantage of this special case to also
1142/// deal with some argument scoping issues here (for example, referring to a
1143/// function parameter in the attribute on that function).
1144void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1145 SourceLocation AttrNameLoc,
1146 ParsedAttributes &Attrs,
1147 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001148 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001149
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001150 BalancedDelimiterTracker T(*this, tok::l_paren);
1151 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001152
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001153 ExprVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001154 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001155
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001156 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001157 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinsed4330b2013-02-07 19:01:07 +00001158 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001159 ExprResult ArgExpr(ParseAssignmentExpression());
1160 if (ArgExpr.isInvalid()) {
1161 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001162 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001163 break;
1164 } else {
1165 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001166 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001167 if (Tok.isNot(tok::comma))
1168 break;
1169 ConsumeToken(); // Eat the comma, move to the next argument
1170 }
1171 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001172 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001173 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001174 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001175 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001176 if (EndLoc)
1177 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001178}
1179
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001180void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1181 SourceLocation AttrNameLoc,
1182 ParsedAttributes &Attrs,
1183 SourceLocation *EndLoc) {
1184 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1185
1186 BalancedDelimiterTracker T(*this, tok::l_paren);
1187 T.consumeOpen();
1188
1189 if (Tok.isNot(tok::identifier)) {
1190 Diag(Tok, diag::err_expected_ident);
1191 T.skipToEnd();
1192 return;
1193 }
1194 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1195 SourceLocation ArgumentKindLoc = ConsumeToken();
1196
1197 if (Tok.isNot(tok::comma)) {
1198 Diag(Tok, diag::err_expected_comma);
1199 T.skipToEnd();
1200 return;
1201 }
1202 ConsumeToken();
1203
1204 SourceRange MatchingCTypeRange;
1205 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1206 if (MatchingCType.isInvalid()) {
1207 T.skipToEnd();
1208 return;
1209 }
1210
1211 bool LayoutCompatible = false;
1212 bool MustBeNull = false;
1213 while (Tok.is(tok::comma)) {
1214 ConsumeToken();
1215 if (Tok.isNot(tok::identifier)) {
1216 Diag(Tok, diag::err_expected_ident);
1217 T.skipToEnd();
1218 return;
1219 }
1220 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1221 if (Flag->isStr("layout_compatible"))
1222 LayoutCompatible = true;
1223 else if (Flag->isStr("must_be_null"))
1224 MustBeNull = true;
1225 else {
1226 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1227 T.skipToEnd();
1228 return;
1229 }
1230 ConsumeToken(); // consume flag
1231 }
1232
1233 if (!T.consumeClose()) {
1234 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1235 ArgumentKind, ArgumentKindLoc,
1236 MatchingCType.release(), LayoutCompatible,
1237 MustBeNull, AttributeList::AS_GNU);
1238 }
1239
1240 if (EndLoc)
1241 *EndLoc = T.getCloseLocation();
1242}
1243
Richard Smith6ee326a2012-04-10 01:32:12 +00001244/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1245/// of a C++11 attribute-specifier in a location where an attribute is not
1246/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1247/// situation.
1248///
1249/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1250/// this doesn't appear to actually be an attribute-specifier, and the caller
1251/// should try to parse it.
1252bool Parser::DiagnoseProhibitedCXX11Attribute() {
1253 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1254
1255 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1256 case CAK_NotAttributeSpecifier:
1257 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1258 return false;
1259
1260 case CAK_InvalidAttributeSpecifier:
1261 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1262 return false;
1263
1264 case CAK_AttributeSpecifier:
1265 // Parse and discard the attributes.
1266 SourceLocation BeginLoc = ConsumeBracket();
1267 ConsumeBracket();
1268 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1269 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1270 SourceLocation EndLoc = ConsumeBracket();
1271 Diag(BeginLoc, diag::err_attributes_not_allowed)
1272 << SourceRange(BeginLoc, EndLoc);
1273 return true;
1274 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001275 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001276}
1277
Richard Smith975d52c2013-02-20 01:17:14 +00001278/// \brief We have found the opening square brackets of a C++11
1279/// attribute-specifier in a location where an attribute is not permitted, but
1280/// we know where the attributes ought to be written. Parse them anyway, and
1281/// provide a fixit moving them to the right place.
Richard Smith05321402013-02-19 23:47:15 +00001282void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1283 SourceLocation CorrectLocation) {
1284 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1285 Tok.is(tok::kw_alignas));
1286
1287 // Consume the attributes.
1288 SourceLocation Loc = Tok.getLocation();
1289 ParseCXX11Attributes(Attrs);
1290 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1291
1292 Diag(Loc, diag::err_attributes_not_allowed)
1293 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1294 << FixItHint::CreateRemoval(AttrRange);
1295}
1296
John McCall7f040a92010-12-24 02:08:15 +00001297void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1298 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1299 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001300}
1301
Michael Hanf64231e2012-11-06 19:34:54 +00001302void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1303 AttributeList *AttrList = attrs.getList();
1304 while (AttrList) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001305 if (AttrList->isCXX11Attribute()) {
Richard Smithd03de6a2013-01-29 10:02:16 +00001306 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Hanf64231e2012-11-06 19:34:54 +00001307 << AttrList->getName();
1308 AttrList->setInvalid();
1309 }
1310 AttrList = AttrList->getNext();
1311 }
1312}
1313
Reid Spencer5f016e22007-07-11 17:01:13 +00001314/// ParseDeclaration - Parse a full 'declaration', which consists of
1315/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001316/// 'Context' should be a Declarator::TheContext value. This returns the
1317/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001318///
1319/// declaration: [C99 6.7]
1320/// block-declaration ->
1321/// simple-declaration
1322/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001323/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001324/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001325/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001326/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001327/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001328/// others... [FIXME]
1329///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001330Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1331 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001332 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001333 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001334 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001335 // Must temporarily exit the objective-c container scope for
1336 // parsing c none objective-c decls.
1337 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001338
John McCalld226f652010-08-21 09:40:31 +00001339 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001340 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001341 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001342 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001343 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001344 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001345 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001346 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001347 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001348 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001349 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001350 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001351 SourceLocation InlineLoc = ConsumeToken();
1352 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1353 break;
1354 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001355 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001356 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001357 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001358 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001359 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001360 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001361 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001362 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001363 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001364 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001365 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001366 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001367 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001368 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001369 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001370 default:
John McCall7f040a92010-12-24 02:08:15 +00001371 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001372 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001373
Chris Lattner682bf922009-03-29 16:50:03 +00001374 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001375 // single decl, convert it now. Alias declarations can also declare a type;
1376 // include that too if it is present.
1377 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001378}
1379
1380/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1381/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001382/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1383/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001384///[C90/C++]init-declarator-list ';' [TODO]
1385/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001386///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001387/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001388/// attribute-specifier-seq[opt] type-specifier-seq declarator
1389///
Chris Lattnercd147752009-03-29 17:27:48 +00001390/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001391/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001392///
1393/// If FRI is non-null, we might be parsing a for-range-declaration instead
1394/// of a simple-declaration. If we find that we are, we also parse the
1395/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001396Parser::DeclGroupPtrTy
1397Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1398 SourceLocation &DeclEnd,
Richard Smith68ea3ae2013-02-22 09:06:26 +00001399 ParsedAttributesWithRange &Attrs,
Sean Hunt2edf0a22012-06-23 05:07:58 +00001400 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001402 ParsingDeclSpec DS(*this);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001403
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001404 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001405 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001406
Reid Spencer5f016e22007-07-11 17:01:13 +00001407 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1408 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001409 if (Tok.is(tok::semi)) {
Richard Smith68ea3ae2013-02-22 09:06:26 +00001410 ProhibitAttributes(Attrs);
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001411 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001412 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001413 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001414 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001415 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001416 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001417 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001418
Richard Smith68ea3ae2013-02-22 09:06:26 +00001419 DS.takeAttributesFrom(Attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00001420 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001421}
Mike Stump1eb44332009-09-09 15:08:12 +00001422
Richard Smith0706df42011-10-19 21:33:05 +00001423/// Returns true if this might be the start of a declarator, or a common typo
1424/// for a declarator.
1425bool Parser::MightBeDeclarator(unsigned Context) {
1426 switch (Tok.getKind()) {
1427 case tok::annot_cxxscope:
1428 case tok::annot_template_id:
1429 case tok::caret:
1430 case tok::code_completion:
1431 case tok::coloncolon:
1432 case tok::ellipsis:
1433 case tok::kw___attribute:
1434 case tok::kw_operator:
1435 case tok::l_paren:
1436 case tok::star:
1437 return true;
1438
1439 case tok::amp:
1440 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001441 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001442
Richard Smith1c94c162012-01-09 22:31:44 +00001443 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith80ad52f2013-01-02 11:42:31 +00001444 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smith1c94c162012-01-09 22:31:44 +00001445 NextToken().is(tok::l_square);
1446
1447 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001448 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001449
Richard Smith0706df42011-10-19 21:33:05 +00001450 case tok::identifier:
1451 switch (NextToken().getKind()) {
1452 case tok::code_completion:
1453 case tok::coloncolon:
1454 case tok::comma:
1455 case tok::equal:
1456 case tok::equalequal: // Might be a typo for '='.
1457 case tok::kw_alignas:
1458 case tok::kw_asm:
1459 case tok::kw___attribute:
1460 case tok::l_brace:
1461 case tok::l_paren:
1462 case tok::l_square:
1463 case tok::less:
1464 case tok::r_brace:
1465 case tok::r_paren:
1466 case tok::r_square:
1467 case tok::semi:
1468 return true;
1469
1470 case tok::colon:
1471 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001472 // and in block scope it's probably a label. Inside a class definition,
1473 // this is a bit-field.
1474 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001475 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001476
1477 case tok::identifier: // Possible virt-specifier.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001478 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001479
1480 default:
1481 return false;
1482 }
1483
1484 default:
1485 return false;
1486 }
1487}
1488
Richard Smith994d73f2012-04-11 20:59:20 +00001489/// Skip until we reach something which seems like a sensible place to pick
1490/// up parsing after a malformed declaration. This will sometimes stop sooner
1491/// than SkipUntil(tok::r_brace) would, but will never stop later.
1492void Parser::SkipMalformedDecl() {
1493 while (true) {
1494 switch (Tok.getKind()) {
1495 case tok::l_brace:
1496 // Skip until matching }, then stop. We've probably skipped over
1497 // a malformed class or function definition or similar.
1498 ConsumeBrace();
1499 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1500 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1501 // This declaration isn't over yet. Keep skipping.
1502 continue;
1503 }
1504 if (Tok.is(tok::semi))
1505 ConsumeToken();
1506 return;
1507
1508 case tok::l_square:
1509 ConsumeBracket();
1510 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1511 continue;
1512
1513 case tok::l_paren:
1514 ConsumeParen();
1515 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1516 continue;
1517
1518 case tok::r_brace:
1519 return;
1520
1521 case tok::semi:
1522 ConsumeToken();
1523 return;
1524
1525 case tok::kw_inline:
1526 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001527 // a good place to pick back up parsing, except in an Objective-C
1528 // @interface context.
1529 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1530 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001531 return;
1532 break;
1533
1534 case tok::kw_namespace:
1535 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001536 // place to pick back up parsing, except in an Objective-C
1537 // @interface context.
1538 if (Tok.isAtStartOfLine() &&
1539 (!ParsingInObjCContainer || CurParsedObjCImpl))
1540 return;
1541 break;
1542
1543 case tok::at:
1544 // @end is very much like } in Objective-C contexts.
1545 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1546 ParsingInObjCContainer)
1547 return;
1548 break;
1549
1550 case tok::minus:
1551 case tok::plus:
1552 // - and + probably start new method declarations in Objective-C contexts.
1553 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001554 return;
1555 break;
1556
1557 case tok::eof:
1558 return;
1559
1560 default:
1561 break;
1562 }
1563
1564 ConsumeAnyToken();
1565 }
1566}
1567
John McCalld8ac0572009-11-03 19:26:08 +00001568/// ParseDeclGroup - Having concluded that this is either a function
1569/// definition or a group of object declarations, actually parse the
1570/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001571Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1572 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001573 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001574 SourceLocation *DeclEnd,
1575 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001576 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001577 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001578 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001579
John McCalld8ac0572009-11-03 19:26:08 +00001580 // Bail out if the first declarator didn't seem well-formed.
1581 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001582 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001583 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001584 }
Mike Stump1eb44332009-09-09 15:08:12 +00001585
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001586 // Save late-parsed attributes for now; they need to be parsed in the
1587 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001588 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1589 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001590 if (D.isFunctionDeclarator())
1591 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1592
Chris Lattnerc82daef2010-07-11 22:24:20 +00001593 // Check to see if we have a function *definition* which must have a body.
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001594 if (D.isFunctionDeclarator() &&
Chris Lattnerc82daef2010-07-11 22:24:20 +00001595 // Look at the next token to make sure that this isn't a function
1596 // declaration. We have to check this because __attribute__ might be the
1597 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001598 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001599
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001600 if (AllowFunctionDefinitions) {
1601 if (isStartOfFunctionDefinition(D)) {
1602 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1603 Diag(Tok, diag::err_function_declared_typedef);
John McCalld8ac0572009-11-03 19:26:08 +00001604
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001605 // Recover by treating the 'typedef' as spurious.
1606 DS.ClearStorageClassSpecs();
1607 }
1608
1609 Decl *TheDecl =
1610 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1611 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld8ac0572009-11-03 19:26:08 +00001612 }
1613
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001614 if (isDeclarationSpecifier()) {
1615 // If there is an invalid declaration specifier right after the function
1616 // prototype, then we must be in a missing semicolon case where this isn't
1617 // actually a body. Just fall through into the code that handles it as a
1618 // prototype, and let the top-level code handle the erroneous declspec
1619 // where it would otherwise expect a comma or semicolon.
1620 } else {
1621 Diag(Tok, diag::err_expected_fn_body);
1622 SkipUntil(tok::semi);
1623 return DeclGroupPtrTy();
1624 }
John McCalld8ac0572009-11-03 19:26:08 +00001625 } else {
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001626 if (Tok.is(tok::l_brace)) {
1627 Diag(Tok, diag::err_function_definition_not_allowed);
1628 SkipUntil(tok::r_brace, true, true);
1629 }
John McCalld8ac0572009-11-03 19:26:08 +00001630 }
1631 }
1632
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001633 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001634 return DeclGroupPtrTy();
1635
1636 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1637 // must parse and analyze the for-range-initializer before the declaration is
1638 // analyzed.
Douglas Gregor12849d02013-04-08 20:52:24 +00001639 //
1640 // Handle the Objective-C for-in loop variable similarly, although we
1641 // don't need to parse the container in advance.
1642 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1643 bool IsForRangeLoop = false;
1644 if (Tok.is(tok::colon)) {
1645 IsForRangeLoop = true;
1646 FRI->ColonLoc = ConsumeToken();
1647 if (Tok.is(tok::l_brace))
1648 FRI->RangeExpr = ParseBraceInitializer();
1649 else
1650 FRI->RangeExpr = ParseExpression();
1651 }
1652
Richard Smithad762fc2011-04-14 22:09:26 +00001653 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor12849d02013-04-08 20:52:24 +00001654 if (IsForRangeLoop)
1655 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001656 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001657 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001658 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1659 }
1660
Chris Lattner5f9e2722011-07-23 10:55:15 +00001661 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001662 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001663 if (LateParsedAttrs.size() > 0)
1664 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001665 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001666 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001667 DeclsInGroup.push_back(FirstDecl);
1668
Richard Smith0706df42011-10-19 21:33:05 +00001669 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001670
John McCalld8ac0572009-11-03 19:26:08 +00001671 // If we don't have a comma, it is either the end of the list (a ';') or an
1672 // error, bail out.
1673 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001674 SourceLocation CommaLoc = ConsumeToken();
1675
1676 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1677 // This comma was followed by a line-break and something which can't be
1678 // the start of a declarator. The comma was probably a typo for a
1679 // semicolon.
1680 Diag(CommaLoc, diag::err_expected_semi_declaration)
1681 << FixItHint::CreateReplacement(CommaLoc, ";");
1682 ExpectSemi = false;
1683 break;
1684 }
John McCalld8ac0572009-11-03 19:26:08 +00001685
1686 // Parse the next declarator.
1687 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001688 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001689
1690 // Accept attributes in an init-declarator. In the first declarator in a
1691 // declaration, these would be part of the declspec. In subsequent
1692 // declarators, they become part of the declarator itself, so that they
1693 // don't apply to declarators after *this* one. Examples:
1694 // short __attribute__((common)) var; -> declspec
1695 // short var __attribute__((common)); -> declarator
1696 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001697 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001698
1699 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001700 if (!D.isInvalidType()) {
1701 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1702 D.complete(ThisDecl);
1703 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001704 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001705 }
John McCalld8ac0572009-11-03 19:26:08 +00001706 }
1707
1708 if (DeclEnd)
1709 *DeclEnd = Tok.getLocation();
1710
Richard Smith0706df42011-10-19 21:33:05 +00001711 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001712 ExpectAndConsumeSemi(Context == Declarator::FileContext
1713 ? diag::err_invalid_token_after_toplevel_declarator
1714 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001715 // Okay, there was no semicolon and one was expected. If we see a
1716 // declaration specifier, just assume it was missing and continue parsing.
1717 // Otherwise things are very confused and we skip to recover.
1718 if (!isDeclarationSpecifier()) {
1719 SkipUntil(tok::r_brace, true, true);
1720 if (Tok.is(tok::semi))
1721 ConsumeToken();
1722 }
John McCalld8ac0572009-11-03 19:26:08 +00001723 }
1724
Douglas Gregor23c94db2010-07-02 17:43:08 +00001725 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001726 DeclsInGroup.data(),
1727 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001728}
1729
Richard Smithad762fc2011-04-14 22:09:26 +00001730/// Parse an optional simple-asm-expr and attributes, and attach them to a
1731/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001732bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001733 // If a simple-asm-expr is present, parse it.
1734 if (Tok.is(tok::kw_asm)) {
1735 SourceLocation Loc;
1736 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1737 if (AsmLabel.isInvalid()) {
1738 SkipUntil(tok::semi, true, true);
1739 return true;
1740 }
1741
1742 D.setAsmLabel(AsmLabel.release());
1743 D.SetRangeEnd(Loc);
1744 }
1745
1746 MaybeParseGNUAttributes(D);
1747 return false;
1748}
1749
Douglas Gregor1426e532009-05-12 21:31:51 +00001750/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1751/// declarator'. This method parses the remainder of the declaration
1752/// (including any attributes or initializer, among other things) and
1753/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001754///
Reid Spencer5f016e22007-07-11 17:01:13 +00001755/// init-declarator: [C99 6.7]
1756/// declarator
1757/// declarator '=' initializer
1758/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1759/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001760/// [C++] declarator initializer[opt]
1761///
1762/// [C++] initializer:
1763/// [C++] '=' initializer-clause
1764/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001765/// [C++0x] '=' 'default' [TODO]
1766/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001767/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001768///
1769/// According to the standard grammar, =default and =delete are function
1770/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001771///
John McCalld226f652010-08-21 09:40:31 +00001772Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001773 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001774 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001775 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001776
Richard Smithad762fc2011-04-14 22:09:26 +00001777 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1778}
Mike Stump1eb44332009-09-09 15:08:12 +00001779
Richard Smithad762fc2011-04-14 22:09:26 +00001780Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1781 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001782 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001783 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001784 switch (TemplateInfo.Kind) {
1785 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001786 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001787 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001788
Douglas Gregord5a423b2009-09-25 18:43:00 +00001789 case ParsedTemplateInfo::Template:
1790 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001791 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001792 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001793 D);
1794 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001795
Douglas Gregord5a423b2009-09-25 18:43:00 +00001796 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001797 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001798 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001799 TemplateInfo.ExternLoc,
1800 TemplateInfo.TemplateLoc,
1801 D);
1802 if (ThisRes.isInvalid()) {
1803 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001804 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001805 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001806
Douglas Gregord5a423b2009-09-25 18:43:00 +00001807 ThisDecl = ThisRes.get();
1808 break;
1809 }
1810 }
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Richard Smith34b41d92011-02-20 03:19:35 +00001812 bool TypeContainsAuto =
1813 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1814
Douglas Gregor1426e532009-05-12 21:31:51 +00001815 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001816 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001817 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001818 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001819 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001820 if (D.isFunctionDeclarator())
1821 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1822 << 1 /* delete */;
1823 else
1824 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001825 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001826 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001827 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1828 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001829 else
1830 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001831 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001832 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001833 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001834 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001835 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001836
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001837 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001838 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001839 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001840 cutOffParsing();
1841 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001842 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001843
John McCall60d7b3a2010-08-24 06:29:42 +00001844 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001845
David Blaikie4e4d0842012-03-11 07:00:24 +00001846 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001847 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001848 ExitScope();
1849 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001850
Douglas Gregor1426e532009-05-12 21:31:51 +00001851 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001852 SkipUntil(tok::comma, true, true);
1853 Actions.ActOnInitializerError(ThisDecl);
1854 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001855 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1856 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001857 }
1858 } else if (Tok.is(tok::l_paren)) {
1859 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001860 BalancedDelimiterTracker T(*this, tok::l_paren);
1861 T.consumeOpen();
1862
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001863 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001864 CommaLocsTy CommaLocs;
1865
David Blaikie4e4d0842012-03-11 07:00:24 +00001866 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001867 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001868 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001869 }
1870
Douglas Gregor1426e532009-05-12 21:31:51 +00001871 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001872 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001873 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001874
David Blaikie4e4d0842012-03-11 07:00:24 +00001875 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001876 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001877 ExitScope();
1878 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001879 } else {
1880 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001881 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001882
1883 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1884 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001885
David Blaikie4e4d0842012-03-11 07:00:24 +00001886 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001887 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001888 ExitScope();
1889 }
1890
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001891 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1892 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001893 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001894 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1895 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001896 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001897 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001898 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001899 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001900 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1901
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001902 if (D.getCXXScopeSpec().isSet()) {
1903 EnterScope(0);
1904 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1905 }
1906
1907 ExprResult Init(ParseBraceInitializer());
1908
1909 if (D.getCXXScopeSpec().isSet()) {
1910 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1911 ExitScope();
1912 }
1913
1914 if (Init.isInvalid()) {
1915 Actions.ActOnInitializerError(ThisDecl);
1916 } else
1917 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1918 /*DirectInit=*/true, TypeContainsAuto);
1919
Douglas Gregor1426e532009-05-12 21:31:51 +00001920 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001921 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001922 }
1923
Richard Smith483b9f32011-02-21 20:05:19 +00001924 Actions.FinalizeDeclaration(ThisDecl);
1925
Douglas Gregor1426e532009-05-12 21:31:51 +00001926 return ThisDecl;
1927}
1928
Reid Spencer5f016e22007-07-11 17:01:13 +00001929/// ParseSpecifierQualifierList
1930/// specifier-qualifier-list:
1931/// type-specifier specifier-qualifier-list[opt]
1932/// type-qualifier specifier-qualifier-list[opt]
1933/// [GNU] attributes specifier-qualifier-list[opt]
1934///
Richard Smith69730c12012-03-12 07:56:15 +00001935void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1936 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001937 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1938 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001939 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001940 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Reid Spencer5f016e22007-07-11 17:01:13 +00001942 // Validate declspec for type-name.
1943 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001944 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1945 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001946 Diag(Tok, diag::err_expected_type);
1947 DS.SetTypeSpecError();
1948 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1949 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001950 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001951 if (!DS.hasTypeSpecifier())
1952 DS.SetTypeSpecError();
1953 }
Mike Stump1eb44332009-09-09 15:08:12 +00001954
Reid Spencer5f016e22007-07-11 17:01:13 +00001955 // Issue diagnostic and remove storage class if present.
1956 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1957 if (DS.getStorageClassSpecLoc().isValid())
1958 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1959 else
Richard Smithec642442013-04-12 22:46:28 +00001960 Diag(DS.getThreadStorageClassSpecLoc(),
1961 diag::err_typename_invalid_storageclass);
Reid Spencer5f016e22007-07-11 17:01:13 +00001962 DS.ClearStorageClassSpecs();
1963 }
Mike Stump1eb44332009-09-09 15:08:12 +00001964
Reid Spencer5f016e22007-07-11 17:01:13 +00001965 // Issue diagnostic and remove function specfier if present.
1966 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001967 if (DS.isInlineSpecified())
1968 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1969 if (DS.isVirtualSpecified())
1970 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1971 if (DS.isExplicitSpecified())
1972 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001973 DS.ClearFunctionSpecs();
1974 }
Richard Smith69730c12012-03-12 07:56:15 +00001975
1976 // Issue diagnostic and remove constexpr specfier if present.
1977 if (DS.isConstexprSpecified()) {
1978 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1979 DS.ClearConstexprSpec();
1980 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001981}
1982
Chris Lattnerc199ab32009-04-12 20:42:31 +00001983/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1984/// specified token is valid after the identifier in a declarator which
1985/// immediately follows the declspec. For example, these things are valid:
1986///
1987/// int x [ 4]; // direct-declarator
1988/// int x ( int y); // direct-declarator
1989/// int(int x ) // direct-declarator
1990/// int x ; // simple-declaration
1991/// int x = 17; // init-declarator-list
1992/// int x , y; // init-declarator-list
1993/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001994/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001995/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001996///
1997/// This is not, because 'x' does not immediately follow the declspec (though
1998/// ')' happens to be valid anyway).
1999/// int (x)
2000///
2001static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2002 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2003 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00002004 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00002005}
2006
Chris Lattnere40c2952009-04-14 21:34:55 +00002007
2008/// ParseImplicitInt - This method is called when we have an non-typename
2009/// identifier in a declspec (which normally terminates the decl spec) when
2010/// the declspec has no type specifier. In this case, the declspec is either
2011/// malformed or is "implicit int" (in K&R and C89).
2012///
2013/// This method handles diagnosing this prettily and returns false if the
2014/// declspec is done being processed. If it recovers and thinks there may be
2015/// other pieces of declspec after it, it returns true.
2016///
Chris Lattnerf4382f52009-04-14 22:17:06 +00002017bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002018 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00002019 AccessSpecifier AS, DeclSpecContext DSC,
2020 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00002021 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002022
Chris Lattnere40c2952009-04-14 21:34:55 +00002023 SourceLocation Loc = Tok.getLocation();
2024 // If we see an identifier that is not a type name, we normally would
2025 // parse it as the identifer being declared. However, when a typename
2026 // is typo'd or the definition is not included, this will incorrectly
2027 // parse the typename as the identifier name and fall over misparsing
2028 // later parts of the diagnostic.
2029 //
2030 // As such, we try to do some look-ahead in cases where this would
2031 // otherwise be an "implicit-int" case to see if this is invalid. For
2032 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2033 // an identifier with implicit int, we'd get a parse error because the
2034 // next token is obviously invalid for a type. Parse these as a case
2035 // with an invalid type specifier.
2036 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00002037
Chris Lattnere40c2952009-04-14 21:34:55 +00002038 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00002039 // error, do lookahead to try to do better recovery. This never applies
2040 // within a type specifier. Outside of C++, we allow this even if the
2041 // language doesn't "officially" support implicit int -- we support
2042 // implicit int as an extension in C99 and C11. Allegedly, MS also
2043 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00002044 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00002045 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00002046 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00002047 // If this token is valid for implicit int, e.g. "static x = 4", then
2048 // we just avoid eating the identifier, so it will be parsed as the
2049 // identifier in the declarator.
2050 return false;
2051 }
Mike Stump1eb44332009-09-09 15:08:12 +00002052
Richard Smith827adaf2012-05-15 21:01:51 +00002053 if (getLangOpts().CPlusPlus &&
2054 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2055 // Don't require a type specifier if we have the 'auto' storage class
2056 // specifier in C++98 -- we'll promote it to a type specifier.
2057 return false;
2058 }
2059
Chris Lattnere40c2952009-04-14 21:34:55 +00002060 // Otherwise, if we don't consume this token, we are going to emit an
2061 // error anyway. Try to recover from various common problems. Check
2062 // to see if this was a reference to a tag name without a tag specified.
2063 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00002064 //
2065 // C++ doesn't need this, and isTagName doesn't take SS.
2066 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002067 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002068 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Douglas Gregor23c94db2010-07-02 17:43:08 +00002070 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00002071 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002072 case DeclSpec::TST_enum:
2073 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2074 case DeclSpec::TST_union:
2075 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2076 case DeclSpec::TST_struct:
2077 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00002078 case DeclSpec::TST_interface:
2079 TagName="__interface"; FixitTagName = "__interface ";
2080 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002081 case DeclSpec::TST_class:
2082 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00002083 }
Mike Stump1eb44332009-09-09 15:08:12 +00002084
Chris Lattnerf4382f52009-04-14 22:17:06 +00002085 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002086 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2087 LookupResult R(Actions, TokenName, SourceLocation(),
2088 Sema::LookupOrdinaryName);
2089
Chris Lattnerf4382f52009-04-14 22:17:06 +00002090 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002091 << TokenName << TagName << getLangOpts().CPlusPlus
2092 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2093
2094 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2095 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2096 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00002097 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002098 << TokenName << TagName;
2099 }
Mike Stump1eb44332009-09-09 15:08:12 +00002100
Chris Lattnerf4382f52009-04-14 22:17:06 +00002101 // Parse this as a tag as if the missing tag were present.
2102 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00002103 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00002104 else
Richard Smith69730c12012-03-12 07:56:15 +00002105 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00002106 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00002107 return true;
2108 }
Chris Lattnere40c2952009-04-14 21:34:55 +00002109 }
Mike Stump1eb44332009-09-09 15:08:12 +00002110
Richard Smith8f0a7e72012-05-15 21:29:55 +00002111 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00002112 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00002113 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2114 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00002115 // Look ahead to the next token to try to figure out what this declaration
2116 // was supposed to be.
2117 switch (NextToken().getKind()) {
2118 case tok::comma:
2119 case tok::equal:
2120 case tok::kw_asm:
2121 case tok::l_brace:
2122 case tok::l_square:
2123 case tok::semi:
2124 // This looks like a variable declaration. The type is probably missing.
2125 // We're done parsing decl-specifiers.
2126 return false;
2127
2128 case tok::l_paren: {
2129 // static x(4); // 'x' is not a type
2130 // x(int n); // 'x' is not a type
2131 // x (*p)[]; // 'x' is a type
2132 //
2133 // Since we're in an error case (or the rare 'implicit int in C++' MS
2134 // extension), we can afford to perform a tentative parse to determine
2135 // which case we're in.
2136 TentativeParsingAction PA(*this);
2137 ConsumeToken();
2138 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2139 PA.Revert();
2140 if (TPR == TPResult::False())
2141 return false;
2142 // The identifier is followed by a parenthesized declarator.
2143 // It's supposed to be a type.
2144 break;
2145 }
2146
2147 default:
2148 // This is probably supposed to be a type. This includes cases like:
2149 // int f(itn);
2150 // struct S { unsinged : 4; };
2151 break;
2152 }
2153 }
2154
Chad Rosier8decdee2012-06-26 22:30:43 +00002155 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00002156 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00002157 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002158 IdentifierInfo *II = Tok.getIdentifierInfo();
2159 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00002160 // The action emitted a diagnostic, so we don't have to.
2161 if (T) {
2162 // The action has suggested that the type T could be used. Set that as
2163 // the type in the declaration specifiers, consume the would-be type
2164 // name token, and we're done.
2165 const char *PrevSpec;
2166 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00002167 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00002168 DS.SetRangeEnd(Tok.getLocation());
2169 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002170 // There may be other declaration specifiers after this.
2171 return true;
2172 } else if (II != Tok.getIdentifierInfo()) {
2173 // If no type was suggested, the correction is to a keyword
2174 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002175 // There may be other declaration specifiers after this.
2176 return true;
2177 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002178
Douglas Gregora786fdb2009-10-13 23:27:22 +00002179 // Fall through; the action had no suggestion for us.
2180 } else {
2181 // The action did not emit a diagnostic, so emit one now.
2182 SourceRange R;
2183 if (SS) R = SS->getRange();
2184 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2185 }
Mike Stump1eb44332009-09-09 15:08:12 +00002186
Douglas Gregora786fdb2009-10-13 23:27:22 +00002187 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002188 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002189 DS.SetRangeEnd(Tok.getLocation());
2190 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002191
Chris Lattnere40c2952009-04-14 21:34:55 +00002192 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2193 // avoid rippling error messages on subsequent uses of the same type,
2194 // could be useful if #include was forgotten.
2195 return false;
2196}
2197
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002198/// \brief Determine the declaration specifier context from the declarator
2199/// context.
2200///
2201/// \param Context the declarator context, which is one of the
2202/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002203Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002204Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2205 if (Context == Declarator::MemberContext)
2206 return DSC_class;
2207 if (Context == Declarator::FileContext)
2208 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002209 if (Context == Declarator::TrailingReturnContext)
2210 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002211 return DSC_normal;
2212}
2213
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002214/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2215///
2216/// FIXME: Simply returns an alignof() expression if the argument is a
2217/// type. Ideally, the type should be propagated directly into Sema.
2218///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002219/// [C11] type-id
2220/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002221/// [C++0x] type-id ...[opt]
2222/// [C++0x] assignment-expression ...[opt]
2223ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2224 SourceLocation &EllipsisLoc) {
2225 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002226 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002227 SourceLocation TypeLoc = Tok.getLocation();
2228 ParsedType Ty = ParseTypeName().get();
2229 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002230 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2231 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002232 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002233 ER = ParseConstantExpression();
2234
Richard Smith80ad52f2013-01-02 11:42:31 +00002235 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002236 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002237
2238 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002239}
2240
2241/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2242/// attribute to Attrs.
2243///
2244/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002245/// [C11] '_Alignas' '(' type-id ')'
2246/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smith33f04a22013-01-29 01:48:07 +00002247/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2248/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002249void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smithf6565a92013-02-22 08:32:16 +00002250 SourceLocation *EndLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002251 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2252 "Not an alignment-specifier!");
2253
Richard Smith33f04a22013-01-29 01:48:07 +00002254 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2255 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002256
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002257 BalancedDelimiterTracker T(*this, tok::l_paren);
2258 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002259 return;
2260
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002261 SourceLocation EllipsisLoc;
2262 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002263 if (ArgExpr.isInvalid()) {
2264 SkipUntil(tok::r_paren);
2265 return;
2266 }
2267
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002268 T.consumeClose();
Richard Smithf6565a92013-02-22 08:32:16 +00002269 if (EndLoc)
2270 *EndLoc = T.getCloseLocation();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002271
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002272 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002273 ArgExprs.push_back(ArgExpr.release());
Richard Smith33f04a22013-01-29 01:48:07 +00002274 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
Richard Smithf6565a92013-02-22 08:32:16 +00002275 ArgExprs.data(), 1, AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002276}
2277
Reid Spencer5f016e22007-07-11 17:01:13 +00002278/// ParseDeclarationSpecifiers
2279/// declaration-specifiers: [C99 6.7]
2280/// storage-class-specifier declaration-specifiers[opt]
2281/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002282/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002283/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002284/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002285/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002286///
2287/// storage-class-specifier: [C99 6.7.1]
2288/// 'typedef'
2289/// 'extern'
2290/// 'static'
2291/// 'auto'
2292/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002293/// [C++] 'mutable'
Richard Smithec642442013-04-12 22:46:28 +00002294/// [C++11] 'thread_local'
2295/// [C11] '_Thread_local'
Reid Spencer5f016e22007-07-11 17:01:13 +00002296/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002297/// function-specifier: [C99 6.7.4]
2298/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002299/// [C++] 'virtual'
2300/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002301/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002302/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002303/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002304
Reid Spencer5f016e22007-07-11 17:01:13 +00002305///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002306void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002307 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002308 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002309 DeclSpecContext DSContext,
2310 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002311 if (DS.getSourceRange().isInvalid()) {
2312 DS.SetRangeStart(Tok.getLocation());
2313 DS.SetRangeEnd(Tok.getLocation());
2314 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002315
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002316 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002317 bool AttrsLastTime = false;
2318 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002319 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002320 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002321 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002322 unsigned DiagID = 0;
2323
Reid Spencer5f016e22007-07-11 17:01:13 +00002324 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002325
Reid Spencer5f016e22007-07-11 17:01:13 +00002326 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002327 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002328 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002329 if (!AttrsLastTime)
2330 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002331 else {
2332 // Reject C++11 attributes that appertain to decl specifiers as
2333 // we don't support any C++11 attributes that appertain to decl
2334 // specifiers. This also conforms to what g++ 4.8 is doing.
2335 ProhibitCXX11Attributes(attrs);
2336
Sean Hunt2edf0a22012-06-23 05:07:58 +00002337 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002338 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002339
Reid Spencer5f016e22007-07-11 17:01:13 +00002340 // If this is not a declaration specifier token, we're done reading decl
2341 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002342 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002343 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002344
Sean Hunt2edf0a22012-06-23 05:07:58 +00002345 case tok::l_square:
2346 case tok::kw_alignas:
Richard Smith672edb02013-02-22 09:15:49 +00002347 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Sean Hunt2edf0a22012-06-23 05:07:58 +00002348 goto DoneWithDeclSpec;
2349
2350 ProhibitAttributes(attrs);
2351 // FIXME: It would be good to recover by accepting the attributes,
2352 // but attempting to do that now would cause serious
2353 // madness in terms of diagnostics.
2354 attrs.clear();
2355 attrs.Range = SourceRange();
2356
2357 ParseCXX11Attributes(attrs);
2358 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002359 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002360
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002361 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002362 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002363 if (DS.hasTypeSpecifier()) {
2364 bool AllowNonIdentifiers
2365 = (getCurScope()->getFlags() & (Scope::ControlScope |
2366 Scope::BlockScope |
2367 Scope::TemplateParamScope |
2368 Scope::FunctionPrototypeScope |
2369 Scope::AtCatchScope)) == 0;
2370 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002371 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002372 (DSContext == DSC_class && DS.isFriendSpecified());
2373
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002374 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002375 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002376 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002377 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002378 }
2379
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002380 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2381 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2382 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002383 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002384 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002385 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002386 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002387 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002388 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002389
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002390 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002391 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002392 }
2393
Chris Lattner5e02c472009-01-05 00:07:25 +00002394 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002395 // C++ scope specifier. Annotate and loop, or bail out on error.
2396 if (TryAnnotateCXXScopeToken(true)) {
2397 if (!DS.hasTypeSpecifier())
2398 DS.SetTypeSpecError();
2399 goto DoneWithDeclSpec;
2400 }
John McCall2e0a7152010-03-01 18:20:46 +00002401 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2402 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002403 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002404
2405 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002406 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002407 goto DoneWithDeclSpec;
2408
John McCallaa87d332009-12-12 11:40:51 +00002409 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002410 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2411 Tok.getAnnotationRange(),
2412 SS);
John McCallaa87d332009-12-12 11:40:51 +00002413
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002414 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002415 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002416 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002417 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002418 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002419 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002420
2421 // C++ [class.qual]p2:
2422 // In a lookup in which the constructor is an acceptable lookup
2423 // result and the nested-name-specifier nominates a class C:
2424 //
2425 // - if the name specified after the
2426 // nested-name-specifier, when looked up in C, is the
2427 // injected-class-name of C (Clause 9), or
2428 //
2429 // - if the name specified after the nested-name-specifier
2430 // is the same as the identifier or the
2431 // simple-template-id's template-name in the last
2432 // component of the nested-name-specifier,
2433 //
2434 // the name is instead considered to name the constructor of
2435 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002436 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002437 // Thus, if the template-name is actually the constructor
2438 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002439 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002440 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002441 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCallba9d8532010-04-13 06:39:49 +00002442 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002443 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002444 if (isConstructorDeclarator()) {
2445 // The user meant this to be an out-of-line constructor
2446 // definition, but template arguments are not allowed
2447 // there. Just allow this as a constructor; we'll
2448 // complain about it later.
2449 goto DoneWithDeclSpec;
2450 }
2451
2452 // The user meant this to name a type, but it actually names
2453 // a constructor with some extraneous template
2454 // arguments. Complain, then parse it as a type as the user
2455 // intended.
2456 Diag(TemplateId->TemplateNameLoc,
2457 diag::err_out_of_line_template_id_names_constructor)
2458 << TemplateId->Name;
2459 }
2460
John McCallaa87d332009-12-12 11:40:51 +00002461 DS.getTypeSpecScope() = SS;
2462 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002463 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002464 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002465 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002466 continue;
2467 }
2468
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002469 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002470 DS.getTypeSpecScope() = SS;
2471 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002472 if (Tok.getAnnotationValue()) {
2473 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002474 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002475 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002476 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002477 if (isInvalid)
2478 break;
John McCallb3d87482010-08-24 05:47:05 +00002479 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002480 else
2481 DS.SetTypeSpecError();
2482 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2483 ConsumeToken(); // The typename
2484 }
2485
Douglas Gregor9135c722009-03-25 15:40:00 +00002486 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002487 goto DoneWithDeclSpec;
2488
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002489 // If we're in a context where the identifier could be a class name,
2490 // check whether this is a constructor declaration.
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002491 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002492 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002493 &SS)) {
2494 if (isConstructorDeclarator())
2495 goto DoneWithDeclSpec;
2496
2497 // As noted in C++ [class.qual]p2 (cited above), when the name
2498 // of the class is qualified in a context where it could name
2499 // a constructor, its a constructor name. However, we've
2500 // looked at the declarator, and the user probably meant this
2501 // to be a type. Complain that it isn't supposed to be treated
2502 // as a type, then proceed to parse it as a type.
2503 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2504 << Next.getIdentifierInfo();
2505 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002506
John McCallb3d87482010-08-24 05:47:05 +00002507 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2508 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002509 getCurScope(), &SS,
2510 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002511 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002512 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002513
Chris Lattnerf4382f52009-04-14 22:17:06 +00002514 // If the referenced identifier is not a type, then this declspec is
2515 // erroneous: We already checked about that it has no type specifier, and
2516 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002517 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002518 if (TypeRep == 0) {
2519 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002520 ParsedAttributesWithRange Attrs(AttrFactory);
2521 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2522 if (!Attrs.empty()) {
2523 AttrsLastTime = true;
2524 attrs.takeAllFrom(Attrs);
2525 }
2526 continue;
2527 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002528 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002529 }
Mike Stump1eb44332009-09-09 15:08:12 +00002530
John McCallaa87d332009-12-12 11:40:51 +00002531 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002532 ConsumeToken(); // The C++ scope.
2533
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002534 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002535 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002536 if (isInvalid)
2537 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002538
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002539 DS.SetRangeEnd(Tok.getLocation());
2540 ConsumeToken(); // The typename.
2541
2542 continue;
2543 }
Mike Stump1eb44332009-09-09 15:08:12 +00002544
Chris Lattner80d0c892009-01-21 19:48:37 +00002545 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002546 if (Tok.getAnnotationValue()) {
2547 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002548 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002549 DiagID, T);
2550 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002551 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002552
Chris Lattner5c5db552010-04-05 18:18:31 +00002553 if (isInvalid)
2554 break;
2555
Chris Lattner80d0c892009-01-21 19:48:37 +00002556 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2557 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002558
Chris Lattner80d0c892009-01-21 19:48:37 +00002559 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2560 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002561 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002562 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002563 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002564
Chris Lattner80d0c892009-01-21 19:48:37 +00002565 continue;
2566 }
Mike Stump1eb44332009-09-09 15:08:12 +00002567
Douglas Gregorbfad9152011-04-28 15:48:45 +00002568 case tok::kw___is_signed:
2569 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2570 // typically treats it as a trait. If we see __is_signed as it appears
2571 // in libstdc++, e.g.,
2572 //
2573 // static const bool __is_signed;
2574 //
2575 // then treat __is_signed as an identifier rather than as a keyword.
2576 if (DS.getTypeSpecType() == TST_bool &&
2577 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2578 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2579 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2580 Tok.setKind(tok::identifier);
2581 }
2582
2583 // We're done with the declaration-specifiers.
2584 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002585
Chris Lattner3bd934a2008-07-26 01:18:38 +00002586 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002587 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002588 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002589 // In C++, check to see if this is a scope specifier like foo::bar::, if
2590 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002591 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002592 if (TryAnnotateCXXScopeToken(true)) {
2593 if (!DS.hasTypeSpecifier())
2594 DS.SetTypeSpecError();
2595 goto DoneWithDeclSpec;
2596 }
2597 if (!Tok.is(tok::identifier))
2598 continue;
2599 }
Mike Stump1eb44332009-09-09 15:08:12 +00002600
Chris Lattner3bd934a2008-07-26 01:18:38 +00002601 // This identifier can only be a typedef name if we haven't already seen
2602 // a type-specifier. Without this check we misparse:
2603 // typedef int X; struct Y { short X; }; as 'short int'.
2604 if (DS.hasTypeSpecifier())
2605 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002606
John Thompson82287d12010-02-05 00:12:22 +00002607 // Check for need to substitute AltiVec keyword tokens.
2608 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2609 break;
2610
Richard Smithf63eee72012-05-09 18:56:43 +00002611 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2612 // allow the use of a typedef name as a type specifier.
2613 if (DS.isTypeAltiVecVector())
2614 goto DoneWithDeclSpec;
2615
John McCallb3d87482010-08-24 05:47:05 +00002616 ParsedType TypeRep =
2617 Actions.getTypeName(*Tok.getIdentifierInfo(),
2618 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002619
Chris Lattnerc199ab32009-04-12 20:42:31 +00002620 // If this is not a typedef name, don't parse it as part of the declspec,
2621 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002622 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002623 ParsedAttributesWithRange Attrs(AttrFactory);
2624 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2625 if (!Attrs.empty()) {
2626 AttrsLastTime = true;
2627 attrs.takeAllFrom(Attrs);
2628 }
2629 continue;
2630 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002631 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002632 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002633
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002634 // If we're in a context where the identifier could be a class name,
2635 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002636 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002637 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002638 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002639 goto DoneWithDeclSpec;
2640
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002641 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002642 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002643 if (isInvalid)
2644 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002645
Chris Lattner3bd934a2008-07-26 01:18:38 +00002646 DS.SetRangeEnd(Tok.getLocation());
2647 ConsumeToken(); // The identifier
2648
2649 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2650 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002651 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002652 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002653 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002654
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002655 // Need to support trailing type qualifiers (e.g. "id<p> const").
2656 // If a type specifier follows, it will be diagnosed elsewhere.
2657 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002658 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002659
2660 // type-name
2661 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002662 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002663 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002664 // This template-id does not refer to a type name, so we're
2665 // done with the type-specifiers.
2666 goto DoneWithDeclSpec;
2667 }
2668
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002669 // If we're in a context where the template-id could be a
2670 // constructor name or specialization, check whether this is a
2671 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002672 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002673 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002674 isConstructorDeclarator())
2675 goto DoneWithDeclSpec;
2676
Douglas Gregor39a8de12009-02-25 19:37:18 +00002677 // Turn the template-id annotation token into a type annotation
2678 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002679 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002680 continue;
2681 }
2682
Reid Spencer5f016e22007-07-11 17:01:13 +00002683 // GNU attributes support.
2684 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002685 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002686 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002687
2688 // Microsoft declspec support.
2689 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002690 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002691 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002692
Steve Naroff239f0732008-12-25 14:16:32 +00002693 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002694 case tok::kw___forceinline: {
Chad Rosier22aa6902012-12-21 22:24:43 +00002695 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002696 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002697 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002698 // FIXME: This does not work correctly if it is set to be a declspec
2699 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002700 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002701 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002702 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002703 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002704
2705 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002706 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002707 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002708 case tok::kw___cdecl:
2709 case tok::kw___stdcall:
2710 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002711 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002712 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002713 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002714 continue;
2715
Dawn Perchik52fc3142010-09-03 01:29:35 +00002716 // Borland single token adornments.
2717 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002718 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002719 continue;
2720
Peter Collingbournef315fa82011-02-14 01:42:53 +00002721 // OpenCL single token adornments.
2722 case tok::kw___kernel:
2723 ParseOpenCLAttributes(DS.getAttributes());
2724 continue;
2725
Reid Spencer5f016e22007-07-11 17:01:13 +00002726 // storage-class-specifier
2727 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002728 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2729 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002730 break;
2731 case tok::kw_extern:
Richard Smithec642442013-04-12 22:46:28 +00002732 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner1ab3b962008-11-18 07:48:38 +00002733 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002734 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2735 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002736 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002737 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002738 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2739 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002740 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002741 case tok::kw_static:
Richard Smithec642442013-04-12 22:46:28 +00002742 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner1ab3b962008-11-18 07:48:38 +00002743 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002744 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2745 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002746 break;
2747 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002748 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002749 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002750 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2751 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002752 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002753 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002754 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002755 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002756 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2757 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002758 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002759 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2760 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002761 break;
2762 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002763 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2764 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002765 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002766 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002767 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2768 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002769 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002770 case tok::kw___thread:
Richard Smithec642442013-04-12 22:46:28 +00002771 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2772 PrevSpec, DiagID);
2773 break;
2774 case tok::kw_thread_local:
2775 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2776 PrevSpec, DiagID);
2777 break;
2778 case tok::kw__Thread_local:
2779 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2780 Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002781 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002782
Reid Spencer5f016e22007-07-11 17:01:13 +00002783 // function-specifier
2784 case tok::kw_inline:
Chad Rosier22aa6902012-12-21 22:24:43 +00002785 isInvalid = DS.setFunctionSpecInline(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002786 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002787 case tok::kw_virtual:
Chad Rosier22aa6902012-12-21 22:24:43 +00002788 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002789 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002790 case tok::kw_explicit:
Chad Rosier22aa6902012-12-21 22:24:43 +00002791 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002792 break;
Richard Smithde03c152013-01-17 22:16:11 +00002793 case tok::kw__Noreturn:
2794 if (!getLangOpts().C11)
2795 Diag(Loc, diag::ext_c11_noreturn);
2796 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2797 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002798
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002799 // alignment-specifier
2800 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002801 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002802 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002803 ParseAlignmentSpecifier(DS.getAttributes());
2804 continue;
2805
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002806 // friend
2807 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002808 if (DSContext == DSC_class)
2809 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2810 else {
2811 PrevSpec = ""; // not actually used by the diagnostic
2812 DiagID = diag::err_friend_invalid_in_context;
2813 isInvalid = true;
2814 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002815 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002816
Douglas Gregor8d267c52011-09-09 02:06:17 +00002817 // Modules
2818 case tok::kw___module_private__:
2819 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2820 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002821
Sebastian Redl2ac67232009-11-05 15:47:02 +00002822 // constexpr
2823 case tok::kw_constexpr:
2824 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2825 break;
2826
Chris Lattner80d0c892009-01-21 19:48:37 +00002827 // type-specifier
2828 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002829 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2830 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002831 break;
2832 case tok::kw_long:
2833 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002834 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2835 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002836 else
John McCallfec54012009-08-03 20:12:06 +00002837 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2838 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002839 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002840 case tok::kw___int64:
2841 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2842 DiagID);
2843 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002844 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002845 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2846 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002847 break;
2848 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002849 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2850 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002851 break;
2852 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002853 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2854 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002855 break;
2856 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002857 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2858 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002859 break;
2860 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002861 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2862 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002863 break;
2864 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002865 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2866 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002867 break;
2868 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002869 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2870 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002871 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002872 case tok::kw___int128:
2873 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2874 DiagID);
2875 break;
2876 case tok::kw_half:
2877 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2878 DiagID);
2879 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002880 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002881 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2882 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002883 break;
2884 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002885 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2886 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002887 break;
2888 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002889 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2890 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002891 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002892 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002893 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2894 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002895 break;
2896 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002897 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2898 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002899 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002900 case tok::kw_bool:
2901 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002902 if (Tok.is(tok::kw_bool) &&
2903 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2904 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2905 PrevSpec = ""; // Not used by the diagnostic.
2906 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002907 // For better error recovery.
2908 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002909 isInvalid = true;
2910 } else {
2911 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2912 DiagID);
2913 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002914 break;
2915 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002916 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2917 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002918 break;
2919 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002920 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2921 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002922 break;
2923 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002924 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2925 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002926 break;
John Thompson82287d12010-02-05 00:12:22 +00002927 case tok::kw___vector:
2928 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2929 break;
2930 case tok::kw___pixel:
2931 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2932 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00002933 case tok::kw_image1d_t:
2934 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2935 PrevSpec, DiagID);
2936 break;
2937 case tok::kw_image1d_array_t:
2938 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2939 PrevSpec, DiagID);
2940 break;
2941 case tok::kw_image1d_buffer_t:
2942 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2943 PrevSpec, DiagID);
2944 break;
2945 case tok::kw_image2d_t:
2946 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2947 PrevSpec, DiagID);
2948 break;
2949 case tok::kw_image2d_array_t:
2950 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2951 PrevSpec, DiagID);
2952 break;
2953 case tok::kw_image3d_t:
2954 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2955 PrevSpec, DiagID);
2956 break;
Guy Benyei21f18c42013-02-07 10:55:47 +00002957 case tok::kw_sampler_t:
2958 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2959 PrevSpec, DiagID);
2960 break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00002961 case tok::kw_event_t:
2962 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2963 PrevSpec, DiagID);
2964 break;
John McCalla5fc4722011-04-09 22:50:59 +00002965 case tok::kw___unknown_anytype:
2966 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2967 PrevSpec, DiagID);
2968 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002969
2970 // class-specifier:
2971 case tok::kw_class:
2972 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00002973 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00002974 case tok::kw_union: {
2975 tok::TokenKind Kind = Tok.getKind();
2976 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00002977
2978 // These are attributes following class specifiers.
2979 // To produce better diagnostic, we parse them when
2980 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002981 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00002982 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002983 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002984
2985 // If there are attributes following class specifier,
2986 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002987 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00002988 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00002989 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002990 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002991 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002992 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002993
2994 // enum-specifier:
2995 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002996 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002997 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002998 continue;
2999
3000 // cv-qualifier:
3001 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003002 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003003 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003004 break;
3005 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003006 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003007 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003008 break;
3009 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003010 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003011 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003012 break;
3013
Douglas Gregord57959a2009-03-27 23:10:48 +00003014 // C++ typename-specifier:
3015 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00003016 if (TryAnnotateTypeOrScopeToken()) {
3017 DS.SetTypeSpecError();
3018 goto DoneWithDeclSpec;
3019 }
3020 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00003021 continue;
3022 break;
3023
Chris Lattner80d0c892009-01-21 19:48:37 +00003024 // GNU typeof support.
3025 case tok::kw_typeof:
3026 ParseTypeofSpecifier(DS);
3027 continue;
3028
David Blaikie42d6d0c2011-12-04 05:04:18 +00003029 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00003030 ParseDecltypeSpecifier(DS);
3031 continue;
3032
Sean Huntdb5d44b2011-05-19 05:37:45 +00003033 case tok::kw___underlying_type:
3034 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00003035 continue;
3036
3037 case tok::kw__Atomic:
Richard Smith4cf4a5e2013-03-28 01:55:44 +00003038 // C11 6.7.2.4/4:
3039 // If the _Atomic keyword is immediately followed by a left parenthesis,
3040 // it is interpreted as a type specifier (with a type name), not as a
3041 // type qualifier.
3042 if (NextToken().is(tok::l_paren)) {
3043 ParseAtomicSpecifier(DS);
3044 continue;
3045 }
3046 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3047 getLangOpts());
3048 break;
Sean Huntdb5d44b2011-05-19 05:37:45 +00003049
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003050 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00003051 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003052 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003053 goto DoneWithDeclSpec;
3054 case tok::kw___private:
3055 case tok::kw___global:
3056 case tok::kw___local:
3057 case tok::kw___constant:
3058 case tok::kw___read_only:
3059 case tok::kw___write_only:
3060 case tok::kw___read_write:
3061 ParseOpenCLQualifiers(DS);
3062 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00003063
Steve Naroffd3ded1f2008-06-05 00:02:44 +00003064 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00003065 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00003066 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3067 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00003068 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00003069 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00003070
Douglas Gregor46f936e2010-11-19 17:10:50 +00003071 if (!ParseObjCProtocolQualifiers(DS))
3072 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3073 << FixItHint::CreateInsertion(Loc, "id")
3074 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00003075
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00003076 // Need to support trailing type qualifiers (e.g. "id<p> const").
3077 // If a type specifier follows, it will be diagnosed elsewhere.
3078 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00003079 }
John McCallfec54012009-08-03 20:12:06 +00003080 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00003081 if (isInvalid) {
3082 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00003083 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00003084
Douglas Gregorae2fb142010-08-23 14:34:43 +00003085 if (DiagID == diag::ext_duplicate_declspec)
3086 Diag(Tok, DiagID)
3087 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3088 else
3089 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003090 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00003091
Chris Lattner81c018d2008-03-13 06:29:04 +00003092 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00003093 if (DiagID != diag::err_bool_redeclaration)
3094 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00003095
3096 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003097 }
3098}
Douglas Gregoradcac882008-12-01 23:54:00 +00003099
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003100/// ParseStructDeclaration - Parse a struct declaration without the terminating
3101/// semicolon.
3102///
Reid Spencer5f016e22007-07-11 17:01:13 +00003103/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003104/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00003105/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003106/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00003107/// struct-declarator-list:
3108/// struct-declarator
3109/// struct-declarator-list ',' struct-declarator
3110/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3111/// struct-declarator:
3112/// declarator
3113/// [GNU] declarator attributes[opt]
3114/// declarator[opt] ':' constant-expression
3115/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3116///
Chris Lattnere1359422008-04-10 06:46:29 +00003117void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003118ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003119
Chris Lattnerc46d1a12008-10-20 06:45:43 +00003120 if (Tok.is(tok::kw___extension__)) {
3121 // __extension__ silences extension warnings in the subexpression.
3122 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00003123 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00003124 return ParseStructDeclaration(DS, Fields);
3125 }
Mike Stump1eb44332009-09-09 15:08:12 +00003126
Steve Naroff28a7ca82007-08-20 22:28:22 +00003127 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00003128 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003129
Douglas Gregor4920f1f2009-01-12 22:49:06 +00003130 // If there are no declarators, this is a free-standing declaration
3131 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00003132 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003133 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3134 DS);
3135 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00003136 return;
3137 }
3138
3139 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00003140 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00003141 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003142 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003143 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00003144 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00003145
Bill Wendlingad017fa2012-12-20 19:22:21 +00003146 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00003147 if (!FirstDeclarator)
3148 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00003149
Steve Naroff28a7ca82007-08-20 22:28:22 +00003150 /// struct-declarator: declarator
3151 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003152 if (Tok.isNot(tok::colon)) {
3153 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3154 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00003155 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003156 }
Mike Stump1eb44332009-09-09 15:08:12 +00003157
Chris Lattner04d66662007-10-09 17:33:22 +00003158 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00003159 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00003160 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003161 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00003162 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00003163 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00003164 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00003165 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003166
Steve Naroff28a7ca82007-08-20 22:28:22 +00003167 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003168 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003169
John McCallbdd563e2009-11-03 02:38:08 +00003170 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00003171 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00003172
Steve Naroff28a7ca82007-08-20 22:28:22 +00003173 // If we don't have a comma, it is either the end of the list (a ';')
3174 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00003175 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003176 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00003177
Steve Naroff28a7ca82007-08-20 22:28:22 +00003178 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00003179 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003180
John McCallbdd563e2009-11-03 02:38:08 +00003181 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003182 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00003183}
3184
3185/// ParseStructUnionBody
3186/// struct-contents:
3187/// struct-declaration-list
3188/// [EXT] empty
3189/// [GNU] "struct-declaration-list" without terminatoring ';'
3190/// struct-declaration-list:
3191/// struct-declaration
3192/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003193/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003194///
Reid Spencer5f016e22007-07-11 17:01:13 +00003195void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003196 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003197 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3198 "parsing struct/union body");
Andy Gibbsf50f3f72013-04-03 09:31:19 +00003199 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump1eb44332009-09-09 15:08:12 +00003200
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003201 BalancedDelimiterTracker T(*this, tok::l_brace);
3202 if (T.consumeOpen())
3203 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003204
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003205 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003206 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003207
Andy Gibbsf50f3f72013-04-03 09:31:19 +00003208 // Empty structs are an extension in C (C99 6.7.2.1p7).
3209 if (Tok.is(tok::r_brace)) {
Richard Smithd7c56e12011-12-29 21:57:33 +00003210 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3211 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3212 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003213
Chris Lattner5f9e2722011-07-23 10:55:15 +00003214 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003215
Reid Spencer5f016e22007-07-11 17:01:13 +00003216 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003217 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003218 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003219
Reid Spencer5f016e22007-07-11 17:01:13 +00003220 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003221 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003222 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003223 continue;
3224 }
Chris Lattnere1359422008-04-10 06:46:29 +00003225
Andy Gibbs74b9fa12013-04-03 09:46:04 +00003226 // Parse _Static_assert declaration.
3227 if (Tok.is(tok::kw__Static_assert)) {
3228 SourceLocation DeclEnd;
3229 ParseStaticAssertDeclaration(DeclEnd);
3230 continue;
3231 }
3232
Argyrios Kyrtzidisbd957452013-04-18 01:42:35 +00003233 if (Tok.is(tok::annot_pragma_pack)) {
3234 HandlePragmaPack();
3235 continue;
3236 }
3237
3238 if (Tok.is(tok::annot_pragma_align)) {
3239 HandlePragmaAlign();
3240 continue;
3241 }
3242
John McCallbdd563e2009-11-03 02:38:08 +00003243 if (!Tok.is(tok::at)) {
3244 struct CFieldCallback : FieldCallback {
3245 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003246 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003247 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003248
John McCalld226f652010-08-21 09:40:31 +00003249 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003250 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003251 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3252
Eli Friedmandcdff462012-08-08 23:53:27 +00003253 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003254 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003255 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003256 FD.D.getDeclSpec().getSourceRange().getBegin(),
3257 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003258 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003259 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003260 }
John McCallbdd563e2009-11-03 02:38:08 +00003261 } Callback(*this, TagDecl, FieldDecls);
3262
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003263 // Parse all the comma separated declarators.
3264 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003265 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003266 } else { // Handle @defs
3267 ConsumeToken();
3268 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3269 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003270 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003271 continue;
3272 }
3273 ConsumeToken();
3274 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3275 if (!Tok.is(tok::identifier)) {
3276 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003277 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003278 continue;
3279 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003280 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003281 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003282 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003283 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3284 ConsumeToken();
3285 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003286 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003287
Chris Lattner04d66662007-10-09 17:33:22 +00003288 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003289 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003290 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003291 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003292 break;
3293 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003294 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3295 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003296 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003297 // If we stopped at a ';', eat it.
3298 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003299 }
3300 }
Mike Stump1eb44332009-09-09 15:08:12 +00003301
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003302 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003303
John McCall0b7e6782011-03-24 11:26:52 +00003304 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003305 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003306 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003307
Douglas Gregor23c94db2010-07-02 17:43:08 +00003308 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003309 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003310 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003311 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003312 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003313 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3314 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003315}
3316
Reid Spencer5f016e22007-07-11 17:01:13 +00003317/// ParseEnumSpecifier
3318/// enum-specifier: [C99 6.7.2.2]
3319/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003320///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003321/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3322/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003323/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3324/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003325/// 'enum' identifier
3326/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003327///
Richard Smith1af83c42012-03-23 03:33:32 +00003328/// [C++11] enum-head '{' enumerator-list[opt] '}'
3329/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003330///
Richard Smith1af83c42012-03-23 03:33:32 +00003331/// enum-head: [C++11]
3332/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3333/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3334/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003335///
Richard Smith1af83c42012-03-23 03:33:32 +00003336/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003337/// 'enum'
3338/// 'enum' 'class'
3339/// 'enum' 'struct'
3340///
Richard Smith1af83c42012-03-23 03:33:32 +00003341/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003342/// ':' type-specifier-seq
3343///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003344/// [C++] elaborated-type-specifier:
3345/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3346///
Chris Lattner4c97d762009-04-12 21:49:30 +00003347void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003348 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003349 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003350 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003351 if (Tok.is(tok::code_completion)) {
3352 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003353 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003354 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003355 }
John McCall57c13002011-07-06 05:58:41 +00003356
Sean Hunt2edf0a22012-06-23 05:07:58 +00003357 // If attributes exist after tag, parse them.
3358 ParsedAttributesWithRange attrs(AttrFactory);
3359 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003360 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003361
3362 // If declspecs exist after tag, parse them.
3363 while (Tok.is(tok::kw___declspec))
3364 ParseMicrosoftDeclSpec(attrs);
3365
Richard Smithbdad7a22012-01-10 01:33:14 +00003366 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003367 bool IsScopedUsingClassTag = false;
3368
John McCall1e12b3d2012-06-23 22:30:04 +00003369 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieued5a2922013-04-23 02:47:36 +00003370 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3371 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3372 : diag::ext_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003373 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003374 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003375
Bill Wendlingad017fa2012-12-20 19:22:21 +00003376 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003377 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003378 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003379
3380 // They are allowed afterwards, though.
3381 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003382 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003383 while (Tok.is(tok::kw___declspec))
3384 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003385 }
Richard Smith1af83c42012-03-23 03:33:32 +00003386
John McCall13489672012-05-07 06:16:58 +00003387 // C++11 [temp.explicit]p12:
3388 // The usual access controls do not apply to names used to specify
3389 // explicit instantiations.
3390 // We extend this to also cover explicit specializations. Note that
3391 // we don't suppress if this turns out to be an elaborated type
3392 // specifier.
3393 bool shouldDelayDiagsInTag =
3394 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3395 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3396 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003397
Richard Smith7796eb52012-03-12 08:56:40 +00003398 // Enum definitions should not be parsed in a trailing-return-type.
3399 bool AllowDeclaration = DSC != DSC_trailing;
3400
3401 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003402 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003403 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003404
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003405 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003406 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003407 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3408 // if a fixed underlying type is allowed.
3409 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003410
3411 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith725fe0e2013-04-01 21:43:41 +00003412 /*EnteringContext=*/true))
John McCall9ba61662010-02-26 08:45:28 +00003413 return;
3414
3415 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003416 Diag(Tok, diag::err_expected_ident);
3417 if (Tok.isNot(tok::l_brace)) {
3418 // Has no name and is not a definition.
3419 // Skip the rest of this declarator, up until the comma or semicolon.
3420 SkipUntil(tok::comma, true);
3421 return;
3422 }
3423 }
3424 }
Mike Stump1eb44332009-09-09 15:08:12 +00003425
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003426 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003427 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003428 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003429 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003430
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003431 // Skip the rest of this declarator, up until the comma or semicolon.
3432 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003433 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003434 }
Mike Stump1eb44332009-09-09 15:08:12 +00003435
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003436 // If an identifier is present, consume and remember it.
3437 IdentifierInfo *Name = 0;
3438 SourceLocation NameLoc;
3439 if (Tok.is(tok::identifier)) {
3440 Name = Tok.getIdentifierInfo();
3441 NameLoc = ConsumeToken();
3442 }
Mike Stump1eb44332009-09-09 15:08:12 +00003443
Richard Smithbdad7a22012-01-10 01:33:14 +00003444 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003445 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3446 // declaration of a scoped enumeration.
3447 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003448 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003449 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003450 }
3451
John McCall13489672012-05-07 06:16:58 +00003452 // Okay, end the suppression area. We'll decide whether to emit the
3453 // diagnostics in a second.
3454 if (shouldDelayDiagsInTag)
3455 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003456
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003457 TypeResult BaseType;
3458
Douglas Gregora61b3e72010-12-01 17:42:47 +00003459 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003460 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003461 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003462 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003463 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003464 // If we're in class scope, this can either be an enum declaration with
3465 // an underlying type, or a declaration of a bitfield member. We try to
3466 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003467 // (integer literal, sizeof); if it's still ambiguous, we then consider
3468 // anything that's a simple-type-specifier followed by '(' as an
3469 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003470 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003471 EnterExpressionEvaluationContext Unevaluated(Actions,
3472 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003473 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003474 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003475 // bit-field. This is the common case.
3476 if (TPR == TPResult::True())
3477 PossibleBitfield = true;
3478 // If the next token starts a type-specifier-seq, it may be either a
3479 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003480 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003481 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003482 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003483 GetLookAheadToken(2).getKind() == tok::semi) {
3484 // Consume the ':'.
3485 ConsumeToken();
3486 } else {
3487 // We have the start of a type-specifier-seq, so we have to perform
3488 // tentative parsing to determine whether we have an expression or a
3489 // type.
3490 TentativeParsingAction TPA(*this);
3491
3492 // Consume the ':'.
3493 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003494
3495 // If we see a type specifier followed by an open-brace, we have an
3496 // ambiguity between an underlying type and a C++11 braced
3497 // function-style cast. Resolve this by always treating it as an
3498 // underlying type.
3499 // FIXME: The standard is not entirely clear on how to disambiguate in
3500 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003501 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003502 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003503 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003504 // We'll parse this as a bitfield later.
3505 PossibleBitfield = true;
3506 TPA.Revert();
3507 } else {
3508 // We have a type-specifier-seq.
3509 TPA.Commit();
3510 }
3511 }
3512 } else {
3513 // Consume the ':'.
3514 ConsumeToken();
3515 }
3516
3517 if (!PossibleBitfield) {
3518 SourceRange Range;
3519 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003520
Richard Smith80ad52f2013-01-02 11:42:31 +00003521 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003522 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003523 } else if (!getLangOpts().ObjC2) {
3524 if (getLangOpts().CPlusPlus)
3525 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3526 else
3527 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3528 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003529 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003530 }
3531
Richard Smithbdad7a22012-01-10 01:33:14 +00003532 // There are four options here. If we have 'friend enum foo;' then this is a
3533 // friend declaration, and cannot have an accompanying definition. If we have
3534 // 'enum foo;', then this is a forward declaration. If we have
3535 // 'enum foo {...' then this is a definition. Otherwise we have something
3536 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003537 //
3538 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3539 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3540 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3541 //
John McCallf312b1e2010-08-26 23:41:50 +00003542 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003543 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003544 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003545 } else if (Tok.is(tok::l_brace)) {
3546 if (DS.isFriendSpecified()) {
3547 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3548 << SourceRange(DS.getFriendSpecLoc());
3549 ConsumeBrace();
3550 SkipUntil(tok::r_brace);
3551 TUK = Sema::TUK_Friend;
3552 } else {
3553 TUK = Sema::TUK_Definition;
3554 }
Richard Smithc9f35172012-06-25 21:37:02 +00003555 } else if (DSC != DSC_type_specifier &&
3556 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003557 (Tok.isAtStartOfLine() &&
3558 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003559 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3560 if (Tok.isNot(tok::semi)) {
3561 // A semicolon was missing after this declaration. Diagnose and recover.
3562 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3563 "enum");
3564 PP.EnterToken(Tok);
3565 Tok.setKind(tok::semi);
3566 }
John McCall13489672012-05-07 06:16:58 +00003567 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003568 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003569 }
3570
3571 // If this is an elaborated type specifier, and we delayed
3572 // diagnostics before, just merge them into the current pool.
3573 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3574 diagsFromTag.redelay();
3575 }
Richard Smith1af83c42012-03-23 03:33:32 +00003576
3577 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003578 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003579 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003580 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003581 // Skip the rest of this declarator, up until the comma or semicolon.
3582 Diag(Tok, diag::err_enum_template);
3583 SkipUntil(tok::comma, true);
3584 return;
3585 }
3586
3587 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3588 // Enumerations can't be explicitly instantiated.
3589 DS.SetTypeSpecError();
3590 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3591 return;
3592 }
3593
3594 assert(TemplateInfo.TemplateParams && "no template parameters");
3595 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3596 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003597 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003598
Sean Hunt2edf0a22012-06-23 05:07:58 +00003599 if (TUK == Sema::TUK_Reference)
3600 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003601
Douglas Gregorb9075602011-02-22 02:55:24 +00003602 if (!Name && TUK != Sema::TUK_Definition) {
3603 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003604
Douglas Gregorb9075602011-02-22 02:55:24 +00003605 // Skip the rest of this declarator, up until the comma or semicolon.
3606 SkipUntil(tok::comma, true);
3607 return;
3608 }
Richard Smith1af83c42012-03-23 03:33:32 +00003609
Douglas Gregor402abb52009-05-28 23:31:59 +00003610 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003611 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003612 const char *PrevSpec = 0;
3613 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003614 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003615 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003616 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003617 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003618 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003619
Douglas Gregor48c89f42010-04-24 16:38:41 +00003620 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003621 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003622 // dependent tag.
3623 if (!Name) {
3624 DS.SetTypeSpecError();
3625 Diag(Tok, diag::err_expected_type_name_after_typename);
3626 return;
3627 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003628
Douglas Gregor23c94db2010-07-02 17:43:08 +00003629 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003630 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003631 NameLoc);
3632 if (Type.isInvalid()) {
3633 DS.SetTypeSpecError();
3634 return;
3635 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003636
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003637 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3638 NameLoc.isValid() ? NameLoc : StartLoc,
3639 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003640 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003641
Douglas Gregor48c89f42010-04-24 16:38:41 +00003642 return;
3643 }
Mike Stump1eb44332009-09-09 15:08:12 +00003644
John McCalld226f652010-08-21 09:40:31 +00003645 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003646 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003647 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003648 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003649 ConsumeBrace();
3650 SkipUntil(tok::r_brace);
3651 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003652
Douglas Gregor48c89f42010-04-24 16:38:41 +00003653 DS.SetTypeSpecError();
3654 return;
3655 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003656
Richard Smithc9f35172012-06-25 21:37:02 +00003657 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003658 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003659
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003660 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3661 NameLoc.isValid() ? NameLoc : StartLoc,
3662 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003663 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003664}
3665
3666/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3667/// enumerator-list:
3668/// enumerator
3669/// enumerator-list ',' enumerator
3670/// enumerator:
3671/// enumeration-constant
3672/// enumeration-constant '=' constant-expression
3673/// enumeration-constant:
3674/// identifier
3675///
John McCalld226f652010-08-21 09:40:31 +00003676void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003677 // Enter the scope of the enum body and start the definition.
3678 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003679 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003680
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003681 BalancedDelimiterTracker T(*this, tok::l_brace);
3682 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003683
Chris Lattner7946dd32007-08-27 17:24:30 +00003684 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003685 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003686 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003687
Chris Lattner5f9e2722011-07-23 10:55:15 +00003688 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003689
John McCalld226f652010-08-21 09:40:31 +00003690 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003691
Reid Spencer5f016e22007-07-11 17:01:13 +00003692 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003693 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003694 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3695 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003696
John McCall5b629aa2010-10-22 23:36:17 +00003697 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003698 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003699 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003700 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003701 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003702
Reid Spencer5f016e22007-07-11 17:01:13 +00003703 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003704 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003705 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003706
Chris Lattner04d66662007-10-09 17:33:22 +00003707 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003708 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003709 AssignedVal = ParseConstantExpression();
3710 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003711 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003712 }
Mike Stump1eb44332009-09-09 15:08:12 +00003713
Reid Spencer5f016e22007-07-11 17:01:13 +00003714 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003715 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3716 LastEnumConstDecl,
3717 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003718 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003719 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003720 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003721
Reid Spencer5f016e22007-07-11 17:01:13 +00003722 EnumConstantDecls.push_back(EnumConstDecl);
3723 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003724
Douglas Gregor751f6922010-09-07 14:51:08 +00003725 if (Tok.is(tok::identifier)) {
3726 // We're missing a comma between enumerators.
3727 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003728 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003729 << FixItHint::CreateInsertion(Loc, ", ");
3730 continue;
3731 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003732
Chris Lattner04d66662007-10-09 17:33:22 +00003733 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003734 break;
3735 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003736
Richard Smith7fe62082011-10-15 05:09:34 +00003737 if (Tok.isNot(tok::identifier)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003738 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003739 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3740 diag::ext_enumerator_list_comma_cxx :
3741 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003742 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003743 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003744 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3745 << FixItHint::CreateRemoval(CommaLoc);
3746 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003747 }
Mike Stump1eb44332009-09-09 15:08:12 +00003748
Reid Spencer5f016e22007-07-11 17:01:13 +00003749 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003750 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003751
Reid Spencer5f016e22007-07-11 17:01:13 +00003752 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003753 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003754 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003755
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003756 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3757 EnumDecl, EnumConstantDecls.data(),
3758 EnumConstantDecls.size(), getCurScope(),
3759 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003760
Douglas Gregor72de6672009-01-08 20:45:30 +00003761 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003762 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3763 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003764
3765 // The next token must be valid after an enum definition. If not, a ';'
3766 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003767 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3768 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003769 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3770 // Push this token back into the preprocessor and change our current token
3771 // to ';' so that the rest of the code recovers as though there were an
3772 // ';' after the definition.
3773 PP.EnterToken(Tok);
3774 Tok.setKind(tok::semi);
3775 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003776}
3777
3778/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003779/// start of a type-qualifier-list.
3780bool Parser::isTypeQualifier() const {
3781 switch (Tok.getKind()) {
3782 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003783
3784 // type-qualifier only in OpenCL
3785 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003786 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003787
Steve Naroff5f8aa692008-02-11 23:15:56 +00003788 // type-qualifier
3789 case tok::kw_const:
3790 case tok::kw_volatile:
3791 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003792 case tok::kw___private:
3793 case tok::kw___local:
3794 case tok::kw___global:
3795 case tok::kw___constant:
3796 case tok::kw___read_only:
3797 case tok::kw___read_write:
3798 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003799 return true;
3800 }
3801}
3802
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003803/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3804/// is definitely a type-specifier. Return false if it isn't part of a type
3805/// specifier or if we're not sure.
3806bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3807 switch (Tok.getKind()) {
3808 default: return false;
3809 // type-specifiers
3810 case tok::kw_short:
3811 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003812 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003813 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003814 case tok::kw_signed:
3815 case tok::kw_unsigned:
3816 case tok::kw__Complex:
3817 case tok::kw__Imaginary:
3818 case tok::kw_void:
3819 case tok::kw_char:
3820 case tok::kw_wchar_t:
3821 case tok::kw_char16_t:
3822 case tok::kw_char32_t:
3823 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003824 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003825 case tok::kw_float:
3826 case tok::kw_double:
3827 case tok::kw_bool:
3828 case tok::kw__Bool:
3829 case tok::kw__Decimal32:
3830 case tok::kw__Decimal64:
3831 case tok::kw__Decimal128:
3832 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003833
Guy Benyeib13621d2012-12-18 14:38:23 +00003834 // OpenCL specific types:
3835 case tok::kw_image1d_t:
3836 case tok::kw_image1d_array_t:
3837 case tok::kw_image1d_buffer_t:
3838 case tok::kw_image2d_t:
3839 case tok::kw_image2d_array_t:
3840 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003841 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003842 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003843
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003844 // struct-or-union-specifier (C99) or class-specifier (C++)
3845 case tok::kw_class:
3846 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003847 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003848 case tok::kw_union:
3849 // enum-specifier
3850 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003851
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003852 // typedef-name
3853 case tok::annot_typename:
3854 return true;
3855 }
3856}
3857
Steve Naroff5f8aa692008-02-11 23:15:56 +00003858/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003859/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003860bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003861 switch (Tok.getKind()) {
3862 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003863
Chris Lattner166a8fc2009-01-04 23:41:41 +00003864 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003865 if (TryAltiVecVectorToken())
3866 return true;
3867 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003868 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003869 // Annotate typenames and C++ scope specifiers. If we get one, just
3870 // recurse to handle whatever we get.
3871 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003872 return true;
3873 if (Tok.is(tok::identifier))
3874 return false;
3875 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003876
Chris Lattner166a8fc2009-01-04 23:41:41 +00003877 case tok::coloncolon: // ::foo::bar
3878 if (NextToken().is(tok::kw_new) || // ::new
3879 NextToken().is(tok::kw_delete)) // ::delete
3880 return false;
3881
Chris Lattner166a8fc2009-01-04 23:41:41 +00003882 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003883 return true;
3884 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003885
Reid Spencer5f016e22007-07-11 17:01:13 +00003886 // GNU attributes support.
3887 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003888 // GNU typeof support.
3889 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003890
Reid Spencer5f016e22007-07-11 17:01:13 +00003891 // type-specifiers
3892 case tok::kw_short:
3893 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003894 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003895 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003896 case tok::kw_signed:
3897 case tok::kw_unsigned:
3898 case tok::kw__Complex:
3899 case tok::kw__Imaginary:
3900 case tok::kw_void:
3901 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003902 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003903 case tok::kw_char16_t:
3904 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003905 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003906 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003907 case tok::kw_float:
3908 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003909 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003910 case tok::kw__Bool:
3911 case tok::kw__Decimal32:
3912 case tok::kw__Decimal64:
3913 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003914 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003915
Guy Benyeib13621d2012-12-18 14:38:23 +00003916 // OpenCL specific types:
3917 case tok::kw_image1d_t:
3918 case tok::kw_image1d_array_t:
3919 case tok::kw_image1d_buffer_t:
3920 case tok::kw_image2d_t:
3921 case tok::kw_image2d_array_t:
3922 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003923 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003924 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003925
Chris Lattner99dc9142008-04-13 18:59:07 +00003926 // struct-or-union-specifier (C99) or class-specifier (C++)
3927 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003928 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003929 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003930 case tok::kw_union:
3931 // enum-specifier
3932 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003933
Reid Spencer5f016e22007-07-11 17:01:13 +00003934 // type-qualifier
3935 case tok::kw_const:
3936 case tok::kw_volatile:
3937 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003938
John McCallb8a8de32012-11-14 00:49:39 +00003939 // Debugger support.
3940 case tok::kw___unknown_anytype:
3941
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003942 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003943 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003944 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003945
Chris Lattner7c186be2008-10-20 00:25:30 +00003946 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3947 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003948 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003949
Steve Naroff239f0732008-12-25 14:16:32 +00003950 case tok::kw___cdecl:
3951 case tok::kw___stdcall:
3952 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003953 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003954 case tok::kw___w64:
3955 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003956 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003957 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003958 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003959
3960 case tok::kw___private:
3961 case tok::kw___local:
3962 case tok::kw___global:
3963 case tok::kw___constant:
3964 case tok::kw___read_only:
3965 case tok::kw___read_write:
3966 case tok::kw___write_only:
3967
Eli Friedman290eeb02009-06-08 23:27:34 +00003968 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003969
3970 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003971 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003972
Richard Smith4cf4a5e2013-03-28 01:55:44 +00003973 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00003974 case tok::kw__Atomic:
3975 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003976 }
3977}
3978
3979/// isDeclarationSpecifier() - Return true if the current token is part of a
3980/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003981///
3982/// \param DisambiguatingWithExpression True to indicate that the purpose of
3983/// this check is to disambiguate between an expression and a declaration.
3984bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003985 switch (Tok.getKind()) {
3986 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003987
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003988 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003989 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003990
Chris Lattner166a8fc2009-01-04 23:41:41 +00003991 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003992 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003993 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003994 return false;
John Thompson82287d12010-02-05 00:12:22 +00003995 if (TryAltiVecVectorToken())
3996 return true;
3997 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003998 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003999 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00004000 // Annotate typenames and C++ scope specifiers. If we get one, just
4001 // recurse to handle whatever we get.
4002 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00004003 return true;
4004 if (Tok.is(tok::identifier))
4005 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00004006
Douglas Gregor9497a732010-09-16 01:51:54 +00004007 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00004008 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00004009 // expression is permitted, then this is probably a class message send
4010 // missing the initial '['. In this case, we won't consider this to be
4011 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00004012 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00004013 isStartOfObjCClassMessageMissingOpenBracket())
4014 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00004015
John McCall9ba61662010-02-26 08:45:28 +00004016 return isDeclarationSpecifier();
4017
Chris Lattner166a8fc2009-01-04 23:41:41 +00004018 case tok::coloncolon: // ::foo::bar
4019 if (NextToken().is(tok::kw_new) || // ::new
4020 NextToken().is(tok::kw_delete)) // ::delete
4021 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004022
Chris Lattner166a8fc2009-01-04 23:41:41 +00004023 // Annotate typenames and C++ scope specifiers. If we get one, just
4024 // recurse to handle whatever we get.
4025 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00004026 return true;
4027 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004028
Reid Spencer5f016e22007-07-11 17:01:13 +00004029 // storage-class-specifier
4030 case tok::kw_typedef:
4031 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00004032 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00004033 case tok::kw_static:
4034 case tok::kw_auto:
4035 case tok::kw_register:
4036 case tok::kw___thread:
Richard Smithec642442013-04-12 22:46:28 +00004037 case tok::kw_thread_local:
4038 case tok::kw__Thread_local:
Mike Stump1eb44332009-09-09 15:08:12 +00004039
Douglas Gregor8d267c52011-09-09 02:06:17 +00004040 // Modules
4041 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00004042
John McCallb8a8de32012-11-14 00:49:39 +00004043 // Debugger support
4044 case tok::kw___unknown_anytype:
4045
Reid Spencer5f016e22007-07-11 17:01:13 +00004046 // type-specifiers
4047 case tok::kw_short:
4048 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00004049 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00004050 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00004051 case tok::kw_signed:
4052 case tok::kw_unsigned:
4053 case tok::kw__Complex:
4054 case tok::kw__Imaginary:
4055 case tok::kw_void:
4056 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00004057 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00004058 case tok::kw_char16_t:
4059 case tok::kw_char32_t:
4060
Reid Spencer5f016e22007-07-11 17:01:13 +00004061 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004062 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00004063 case tok::kw_float:
4064 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00004065 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00004066 case tok::kw__Bool:
4067 case tok::kw__Decimal32:
4068 case tok::kw__Decimal64:
4069 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00004070 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00004071
Guy Benyeib13621d2012-12-18 14:38:23 +00004072 // OpenCL specific types:
4073 case tok::kw_image1d_t:
4074 case tok::kw_image1d_array_t:
4075 case tok::kw_image1d_buffer_t:
4076 case tok::kw_image2d_t:
4077 case tok::kw_image2d_array_t:
4078 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00004079 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00004080 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00004081
Chris Lattner99dc9142008-04-13 18:59:07 +00004082 // struct-or-union-specifier (C99) or class-specifier (C++)
4083 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00004084 case tok::kw_struct:
4085 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00004086 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00004087 // enum-specifier
4088 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00004089
Reid Spencer5f016e22007-07-11 17:01:13 +00004090 // type-qualifier
4091 case tok::kw_const:
4092 case tok::kw_volatile:
4093 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00004094
Reid Spencer5f016e22007-07-11 17:01:13 +00004095 // function-specifier
4096 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00004097 case tok::kw_virtual:
4098 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00004099 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00004100
Richard Smith4cd81c52013-01-29 09:02:09 +00004101 // alignment-specifier
4102 case tok::kw__Alignas:
4103
Richard Smith53aec2a2012-10-25 00:00:53 +00004104 // friend keyword.
4105 case tok::kw_friend:
4106
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00004107 // static_assert-declaration
4108 case tok::kw__Static_assert:
4109
Chris Lattner1ef08762007-08-09 17:01:07 +00004110 // GNU typeof support.
4111 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00004112
Chris Lattner1ef08762007-08-09 17:01:07 +00004113 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00004114 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00004115
Richard Smith53aec2a2012-10-25 00:00:53 +00004116 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00004117 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00004118 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00004119
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004120 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00004121 case tok::kw__Atomic:
4122 return true;
4123
Chris Lattnerf3948c42008-07-26 03:38:44 +00004124 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4125 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00004126 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00004127
Douglas Gregord9d75e52011-04-27 05:41:15 +00004128 // typedef-name
4129 case tok::annot_typename:
4130 return !DisambiguatingWithExpression ||
4131 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00004132
Steve Naroff47f52092009-01-06 19:34:12 +00004133 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00004134 case tok::kw___cdecl:
4135 case tok::kw___stdcall:
4136 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004137 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00004138 case tok::kw___w64:
4139 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004140 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00004141 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004142 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004143 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004144
4145 case tok::kw___private:
4146 case tok::kw___local:
4147 case tok::kw___global:
4148 case tok::kw___constant:
4149 case tok::kw___read_only:
4150 case tok::kw___read_write:
4151 case tok::kw___write_only:
4152
Eli Friedman290eeb02009-06-08 23:27:34 +00004153 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00004154 }
4155}
4156
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004157bool Parser::isConstructorDeclarator() {
4158 TentativeParsingAction TPA(*this);
4159
4160 // Parse the C++ scope specifier.
4161 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00004162 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004163 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00004164 TPA.Revert();
4165 return false;
4166 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004167
4168 // Parse the constructor name.
4169 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4170 // We already know that we have a constructor name; just consume
4171 // the token.
4172 ConsumeToken();
4173 } else {
4174 TPA.Revert();
4175 return false;
4176 }
4177
Richard Smith22592862012-03-27 23:05:05 +00004178 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004179 if (Tok.isNot(tok::l_paren)) {
4180 TPA.Revert();
4181 return false;
4182 }
4183 ConsumeParen();
4184
Richard Smith22592862012-03-27 23:05:05 +00004185 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4186 // that we have a constructor.
4187 if (Tok.is(tok::r_paren) ||
4188 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004189 TPA.Revert();
4190 return true;
4191 }
4192
4193 // If we need to, enter the specified scope.
4194 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00004195 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004196 DeclScopeObj.EnterDeclaratorScope();
4197
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004198 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00004199 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004200 MaybeParseMicrosoftAttributes(Attrs);
4201
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004202 // Check whether the next token(s) are part of a declaration
4203 // specifier, in which case we have the start of a parameter and,
4204 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00004205 bool IsConstructor = false;
4206 if (isDeclarationSpecifier())
4207 IsConstructor = true;
4208 else if (Tok.is(tok::identifier) ||
4209 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4210 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4211 // This might be a parenthesized member name, but is more likely to
4212 // be a constructor declaration with an invalid argument type. Keep
4213 // looking.
4214 if (Tok.is(tok::annot_cxxscope))
4215 ConsumeToken();
4216 ConsumeToken();
4217
4218 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004219 // which must have one of the following syntactic forms (see the
4220 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004221 switch (Tok.getKind()) {
4222 case tok::l_paren:
4223 // C(X ( int));
4224 case tok::l_square:
4225 // C(X [ 5]);
4226 // C(X [ [attribute]]);
4227 case tok::coloncolon:
4228 // C(X :: Y);
4229 // C(X :: *p);
4230 case tok::r_paren:
4231 // C(X )
4232 // Assume this isn't a constructor, rather than assuming it's a
4233 // constructor with an unnamed parameter of an ill-formed type.
4234 break;
4235
4236 default:
4237 IsConstructor = true;
4238 break;
4239 }
4240 }
4241
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004242 TPA.Revert();
4243 return IsConstructor;
4244}
Reid Spencer5f016e22007-07-11 17:01:13 +00004245
4246/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004247/// type-qualifier-list: [C99 6.7.5]
4248/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004249/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004250/// [ only if VendorAttributesAllowed=true ]
4251/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004252/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004253/// [ only if VendorAttributesAllowed=true ]
4254/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004255/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004256/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004257///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004258void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4259 bool VendorAttributesAllowed,
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004260 bool CXX11AttributesAllowed,
4261 bool AtomicAllowed) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004262 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004263 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004264 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004265 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004266 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004267 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004268
4269 SourceLocation EndLoc;
4270
Reid Spencer5f016e22007-07-11 17:01:13 +00004271 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004272 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004273 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004274 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004275 SourceLocation Loc = Tok.getLocation();
4276
4277 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004278 case tok::code_completion:
4279 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004280 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004281
Reid Spencer5f016e22007-07-11 17:01:13 +00004282 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004283 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004284 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004285 break;
4286 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004287 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004288 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004289 break;
4290 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004291 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004292 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004293 break;
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004294 case tok::kw__Atomic:
4295 if (!AtomicAllowed)
4296 goto DoneWithTypeQuals;
4297 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4298 getLangOpts());
4299 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004300
4301 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004302 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004303 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004304 goto DoneWithTypeQuals;
4305 case tok::kw___private:
4306 case tok::kw___global:
4307 case tok::kw___local:
4308 case tok::kw___constant:
4309 case tok::kw___read_only:
4310 case tok::kw___write_only:
4311 case tok::kw___read_write:
4312 ParseOpenCLQualifiers(DS);
4313 break;
4314
Eli Friedman290eeb02009-06-08 23:27:34 +00004315 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004316 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004317 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004318 case tok::kw___cdecl:
4319 case tok::kw___stdcall:
4320 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004321 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004322 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004323 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004324 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004325 continue;
4326 }
4327 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004328 case tok::kw___pascal:
4329 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004330 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004331 continue;
4332 }
4333 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004334 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004335 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004336 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004337 continue; // do *not* consume the next token!
4338 }
4339 // otherwise, FALL THROUGH!
4340 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004341 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004342 // If this is not a type-qualifier token, we're done reading type
4343 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004344 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004345 if (EndLoc.isValid())
4346 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004347 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004348 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004349
Reid Spencer5f016e22007-07-11 17:01:13 +00004350 // If the specifier combination wasn't legal, issue a diagnostic.
4351 if (isInvalid) {
4352 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004353 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004354 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004355 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004356 }
4357}
4358
4359
4360/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4361///
4362void Parser::ParseDeclarator(Declarator &D) {
4363 /// This implements the 'declarator' production in the C grammar, then checks
4364 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004365 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004366}
4367
Richard Smith9988f282012-03-29 01:16:42 +00004368static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4369 if (Kind == tok::star || Kind == tok::caret)
4370 return true;
4371
4372 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4373 if (!Lang.CPlusPlus)
4374 return false;
4375
4376 return Kind == tok::amp || Kind == tok::ampamp;
4377}
4378
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004379/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4380/// is parsed by the function passed to it. Pass null, and the direct-declarator
4381/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004382/// ptr-operator production.
4383///
Richard Smith0706df42011-10-19 21:33:05 +00004384/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004385/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4386/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004387///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004388/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4389/// [C] pointer[opt] direct-declarator
4390/// [C++] direct-declarator
4391/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004392///
4393/// pointer: [C99 6.7.5]
4394/// '*' type-qualifier-list[opt]
4395/// '*' type-qualifier-list[opt] pointer
4396///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004397/// ptr-operator:
4398/// '*' cv-qualifier-seq[opt]
4399/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004400/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004401/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004402/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004403/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004404void Parser::ParseDeclaratorInternal(Declarator &D,
4405 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004406 if (Diags.hasAllExtensionsSilenced())
4407 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004408
Sebastian Redlf30208a2009-01-24 21:16:55 +00004409 // C++ member pointers start with a '::' or a nested-name.
4410 // Member pointers get special handling, since there's no place for the
4411 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004412 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004413 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4414 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004415 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4416 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004417 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004418 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004419
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004420 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004421 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004422 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004423 if (D.mayHaveIdentifier())
4424 D.getCXXScopeSpec() = SS;
4425 else
4426 AnnotateScopeToken(SS, true);
4427
Sebastian Redlf30208a2009-01-24 21:16:55 +00004428 if (DirectDeclParser)
4429 (this->*DirectDeclParser)(D);
4430 return;
4431 }
4432
4433 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004434 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004435 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004436 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004437 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004438
4439 // Recurse to parse whatever is left.
4440 ParseDeclaratorInternal(D, DirectDeclParser);
4441
4442 // Sema will have to catch (syntactically invalid) pointers into global
4443 // scope. It has to catch pointers into namespace scope anyway.
4444 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004445 Loc),
4446 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004447 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004448 return;
4449 }
4450 }
4451
4452 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004453 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004454 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004455 if (DirectDeclParser)
4456 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004457 return;
4458 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004459
Sebastian Redl05532f22009-03-15 22:02:01 +00004460 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4461 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004462 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004463 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004464
Chris Lattner9af55002009-03-27 04:18:06 +00004465 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004466 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004467 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004468
Richard Smith6ee326a2012-04-10 01:32:12 +00004469 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004470 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004471 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004472
Reid Spencer5f016e22007-07-11 17:01:13 +00004473 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004474 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004475 if (Kind == tok::star)
4476 // Remember that we parsed a pointer type, and remember the type-quals.
4477 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004478 DS.getConstSpecLoc(),
4479 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004480 DS.getRestrictSpecLoc()),
4481 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004482 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004483 else
4484 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004485 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004486 Loc),
4487 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004488 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004489 } else {
4490 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004491 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004492
Sebastian Redl743de1f2009-03-23 00:00:23 +00004493 // Complain about rvalue references in C++03, but then go on and build
4494 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004495 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004496 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004497 diag::warn_cxx98_compat_rvalue_reference :
4498 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004499
Richard Smith6ee326a2012-04-10 01:32:12 +00004500 // GNU-style and C++11 attributes are allowed here, as is restrict.
4501 ParseTypeQualifierListOpt(DS);
4502 D.ExtendWithDeclSpec(DS);
4503
Reid Spencer5f016e22007-07-11 17:01:13 +00004504 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4505 // cv-qualifiers are introduced through the use of a typedef or of a
4506 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004507 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4508 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4509 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004510 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004511 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4512 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004513 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004514 // 'restrict' is permitted as an extension.
4515 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4516 Diag(DS.getAtomicSpecLoc(),
4517 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Reid Spencer5f016e22007-07-11 17:01:13 +00004518 }
4519
4520 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004521 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004522
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004523 if (D.getNumTypeObjects() > 0) {
4524 // C++ [dcl.ref]p4: There shall be no references to references.
4525 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4526 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004527 if (const IdentifierInfo *II = D.getIdentifier())
4528 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4529 << II;
4530 else
4531 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4532 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004533
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004534 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004535 // can go ahead and build the (technically ill-formed)
4536 // declarator: reference collapsing will take care of it.
4537 }
4538 }
4539
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004540 // Remember that we parsed a reference type.
Chris Lattner76549142008-02-21 01:32:26 +00004541 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004542 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004543 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004544 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004545 }
4546}
4547
Richard Smith9988f282012-03-29 01:16:42 +00004548static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4549 SourceLocation EllipsisLoc) {
4550 if (EllipsisLoc.isValid()) {
4551 FixItHint Insertion;
4552 if (!D.getEllipsisLoc().isValid()) {
4553 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4554 D.setEllipsisLoc(EllipsisLoc);
4555 }
4556 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4557 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4558 }
4559}
4560
Reid Spencer5f016e22007-07-11 17:01:13 +00004561/// ParseDirectDeclarator
4562/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004563/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004564/// '(' declarator ')'
4565/// [GNU] '(' attributes declarator ')'
4566/// [C90] direct-declarator '[' constant-expression[opt] ']'
4567/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4568/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4569/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4570/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004571/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4572/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004573/// direct-declarator '(' parameter-type-list ')'
4574/// direct-declarator '(' identifier-list[opt] ')'
4575/// [GNU] direct-declarator '(' parameter-forward-declarations
4576/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004577/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4578/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004579/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4580/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4581/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004582/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004583/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004584///
4585/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004586/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004587/// '::'[opt] nested-name-specifier[opt] type-name
4588///
4589/// id-expression: [C++ 5.1]
4590/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004591/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004592///
4593/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004594/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004595/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004596/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004597/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004598/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004599///
Richard Smith5d8388c2012-03-27 01:42:32 +00004600/// Note, any additional constructs added here may need corresponding changes
4601/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004602void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004603 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004604
David Blaikie4e4d0842012-03-11 07:00:24 +00004605 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004606 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004607 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004608 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4609 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004610 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004611 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004612 }
4613
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004614 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004615 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004616 // Change the declaration context for name lookup, until this function
4617 // is exited (and the declarator has been parsed).
4618 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004619 }
4620
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004621 // C++0x [dcl.fct]p14:
4622 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004623 // of a parameter-declaration-clause without a preceding comma. In
4624 // this case, the ellipsis is parsed as part of the
4625 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004626 // parameter pack that has not been expanded; otherwise, it is parsed
4627 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004628 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004629 !((D.getContext() == Declarator::PrototypeContext ||
4630 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004631 NextToken().is(tok::r_paren) &&
Richard Smith30f2a742013-02-20 20:19:27 +00004632 !D.hasGroupingParens() &&
Richard Smith9988f282012-03-29 01:16:42 +00004633 !Actions.containsUnexpandedParameterPacks(D))) {
4634 SourceLocation EllipsisLoc = ConsumeToken();
4635 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4636 // The ellipsis was put in the wrong place. Recover, and explain to
4637 // the user what they should have done.
4638 ParseDeclarator(D);
4639 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4640 return;
4641 } else
4642 D.setEllipsisLoc(EllipsisLoc);
4643
4644 // The ellipsis can't be followed by a parenthesized declarator. We
4645 // check for that in ParseParenDeclarator, after we have disambiguated
4646 // the l_paren token.
4647 }
4648
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004649 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4650 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4651 // We found something that indicates the start of an unqualified-id.
4652 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004653 bool AllowConstructorName;
4654 if (D.getDeclSpec().hasTypeSpecifier())
4655 AllowConstructorName = false;
4656 else if (D.getCXXScopeSpec().isSet())
4657 AllowConstructorName =
4658 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00004659 D.getContext() == Declarator::MemberContext);
John McCallba9d8532010-04-13 06:39:49 +00004660 else
4661 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4662
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004663 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004664 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4665 /*EnteringContext=*/true,
4666 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004667 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004668 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004669 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004670 D.getName()) ||
4671 // Once we're past the identifier, if the scope was bad, mark the
4672 // whole declarator bad.
4673 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004674 D.SetIdentifier(0, Tok.getLocation());
4675 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004676 } else {
4677 // Parsed the unqualified-id; update range information and move along.
4678 if (D.getSourceRange().getBegin().isInvalid())
4679 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4680 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004681 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004682 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004683 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004684 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004685 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004686 "There's a C++-specific check for tok::identifier above");
4687 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4688 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4689 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004690 goto PastIdentifier;
4691 }
Richard Smith9988f282012-03-29 01:16:42 +00004692
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004693 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004694 // direct-declarator: '(' declarator ')'
4695 // direct-declarator: '(' attributes declarator ')'
4696 // Example: 'char (*X)' or 'int (*XX)(void)'
4697 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004698
4699 // If the declarator was parenthesized, we entered the declarator
4700 // scope when parsing the parenthesized declarator, then exited
4701 // the scope already. Re-enter the scope, if we need to.
4702 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004703 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004704 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004705 if (!D.isInvalidType() &&
4706 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004707 // Change the declaration context for name lookup, until this function
4708 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004709 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004710 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004711 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004712 // This could be something simple like "int" (in which case the declarator
4713 // portion is empty), if an abstract-declarator is allowed.
4714 D.SetIdentifier(0, Tok.getLocation());
Richard Smith30f2a742013-02-20 20:19:27 +00004715
4716 // The grammar for abstract-pack-declarator does not allow grouping parens.
4717 // FIXME: Revisit this once core issue 1488 is resolved.
4718 if (D.hasEllipsis() && D.hasGroupingParens())
4719 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4720 diag::ext_abstract_pack_declarator_parens);
Reid Spencer5f016e22007-07-11 17:01:13 +00004721 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004722 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004723 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004724 if (D.getContext() == Declarator::MemberContext)
4725 Diag(Tok, diag::err_expected_member_name_or_semi)
4726 << D.getDeclSpec().getSourceRange();
Richard Trieudb55c04c2013-01-26 02:31:38 +00004727 else if (getLangOpts().CPlusPlus) {
4728 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4729 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4730 else
4731 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4732 } else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004733 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004734 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004735 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004736 }
Mike Stump1eb44332009-09-09 15:08:12 +00004737
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004738 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004739 assert(D.isPastIdentifier() &&
4740 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004741
Richard Smith6ee326a2012-04-10 01:32:12 +00004742 // Don't parse attributes unless we have parsed an unparenthesized name.
4743 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004744 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004745
Reid Spencer5f016e22007-07-11 17:01:13 +00004746 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004747 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004748 // Enter function-declaration scope, limiting any declarators to the
4749 // function prototype scope, including parameter declarators.
4750 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004751 Scope::FunctionPrototypeScope|Scope::DeclScope|
4752 (D.isFunctionDeclaratorAFunctionDeclaration()
4753 ? Scope::FunctionDeclarationScope : 0));
4754
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004755 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4756 // In such a case, check if we actually have a function declarator; if it
4757 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004758 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004759 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4760 // The name of the declarator, if any, is tentatively declared within
4761 // a possible direct initializer.
4762 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4763 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4764 TentativelyDeclaredIdentifiers.pop_back();
4765 if (!IsFunctionDecl)
4766 break;
4767 }
John McCall0b7e6782011-03-24 11:26:52 +00004768 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004769 BalancedDelimiterTracker T(*this, tok::l_paren);
4770 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004771 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004772 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004773 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004774 ParseBracketDeclarator(D);
4775 } else {
4776 break;
4777 }
4778 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004779}
Reid Spencer5f016e22007-07-11 17:01:13 +00004780
Chris Lattneref4715c2008-04-06 05:45:57 +00004781/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4782/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004783/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004784/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4785///
4786/// direct-declarator:
4787/// '(' declarator ')'
4788/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004789/// direct-declarator '(' parameter-type-list ')'
4790/// direct-declarator '(' identifier-list[opt] ')'
4791/// [GNU] direct-declarator '(' parameter-forward-declarations
4792/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004793///
4794void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004795 BalancedDelimiterTracker T(*this, tok::l_paren);
4796 T.consumeOpen();
4797
Chris Lattneref4715c2008-04-06 05:45:57 +00004798 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004799
Chris Lattner7399ee02008-10-20 02:05:46 +00004800 // Eat any attributes before we look at whether this is a grouping or function
4801 // declarator paren. If this is a grouping paren, the attribute applies to
4802 // the type being built up, for example:
4803 // int (__attribute__(()) *x)(long y)
4804 // If this ends up not being a grouping paren, the attribute applies to the
4805 // first argument, for example:
4806 // int (__attribute__(()) int x)
4807 // In either case, we need to eat any attributes to be able to determine what
4808 // sort of paren this is.
4809 //
John McCall0b7e6782011-03-24 11:26:52 +00004810 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004811 bool RequiresArg = false;
4812 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004813 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004814
Chris Lattner7399ee02008-10-20 02:05:46 +00004815 // We require that the argument list (if this is a non-grouping paren) be
4816 // present even if the attribute list was empty.
4817 RequiresArg = true;
4818 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004819
Steve Naroff239f0732008-12-25 14:16:32 +00004820 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004821 ParseMicrosoftTypeAttributes(attrs);
4822
Dawn Perchik52fc3142010-09-03 01:29:35 +00004823 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004824 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004825 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004826
Chris Lattneref4715c2008-04-06 05:45:57 +00004827 // If we haven't past the identifier yet (or where the identifier would be
4828 // stored, if this is an abstract declarator), then this is probably just
4829 // grouping parens. However, if this could be an abstract-declarator, then
4830 // this could also be the start of function arguments (consider 'void()').
4831 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004832
Chris Lattneref4715c2008-04-06 05:45:57 +00004833 if (!D.mayOmitIdentifier()) {
4834 // If this can't be an abstract-declarator, this *must* be a grouping
4835 // paren, because we haven't seen the identifier yet.
4836 isGrouping = true;
4837 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004838 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4839 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004840 isDeclarationSpecifier() || // 'int(int)' is a function.
4841 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004842 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4843 // considered to be a type, not a K&R identifier-list.
4844 isGrouping = false;
4845 } else {
4846 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4847 isGrouping = true;
4848 }
Mike Stump1eb44332009-09-09 15:08:12 +00004849
Chris Lattneref4715c2008-04-06 05:45:57 +00004850 // If this is a grouping paren, handle:
4851 // direct-declarator: '(' declarator ')'
4852 // direct-declarator: '(' attributes declarator ')'
4853 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004854 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4855 D.setEllipsisLoc(SourceLocation());
4856
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004857 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004858 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004859 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004860 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004861 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004862 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004863 T.getCloseLocation()),
4864 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004865
4866 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004867
4868 // An ellipsis cannot be placed outside parentheses.
4869 if (EllipsisLoc.isValid())
4870 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4871
Chris Lattneref4715c2008-04-06 05:45:57 +00004872 return;
4873 }
Mike Stump1eb44332009-09-09 15:08:12 +00004874
Chris Lattneref4715c2008-04-06 05:45:57 +00004875 // Okay, if this wasn't a grouping paren, it must be the start of a function
4876 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004877 // identifier (and remember where it would have been), then call into
4878 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004879 D.SetIdentifier(0, Tok.getLocation());
4880
David Blaikie42d6d0c2011-12-04 05:04:18 +00004881 // Enter function-declaration scope, limiting any declarators to the
4882 // function prototype scope, including parameter declarators.
4883 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004884 Scope::FunctionPrototypeScope | Scope::DeclScope |
4885 (D.isFunctionDeclaratorAFunctionDeclaration()
4886 ? Scope::FunctionDeclarationScope : 0));
Richard Smithb9c62612012-07-30 21:30:52 +00004887 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004888 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004889}
4890
4891/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4892/// declarator D up to a paren, which indicates that we are parsing function
4893/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004894///
Richard Smith6ee326a2012-04-10 01:32:12 +00004895/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4896/// immediately after the open paren - they should be considered to be the
4897/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004898///
Richard Smith6ee326a2012-04-10 01:32:12 +00004899/// If RequiresArg is true, then the first argument of the function is required
4900/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004901///
Richard Smith6ee326a2012-04-10 01:32:12 +00004902/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4903/// (C++11) ref-qualifier[opt], exception-specification[opt],
4904/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4905///
4906/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004907/// dynamic-exception-specification
4908/// noexcept-specification
4909///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004910void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004911 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004912 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004913 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004914 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004915 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004916 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004917 // lparen is already consumed!
4918 assert(D.isPastIdentifier() && "Should not call before identifier!");
4919
4920 // This should be true when the function has typed arguments.
4921 // Otherwise, it is treated as a K&R-style function.
4922 bool HasProto = false;
4923 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004924 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004925 // Remember where we see an ellipsis, if any.
4926 SourceLocation EllipsisLoc;
4927
4928 DeclSpec DS(AttrFactory);
4929 bool RefQualifierIsLValueRef = true;
4930 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004931 SourceLocation ConstQualifierLoc;
4932 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004933 ExceptionSpecificationType ESpecType = EST_None;
4934 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004935 SmallVector<ParsedType, 2> DynamicExceptions;
4936 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004937 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004938 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004939 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004940
James Molloy16f1f712012-02-29 10:24:19 +00004941 Actions.ActOnStartFunctionDeclarator();
4942
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004943 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4944 EndLoc is the end location for the function declarator.
4945 They differ for trailing return types. */
4946 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004947 SourceLocation LParenLoc, RParenLoc;
4948 LParenLoc = Tracker.getOpenLocation();
4949 StartLoc = LParenLoc;
4950
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004951 if (isFunctionDeclaratorIdentifierList()) {
4952 if (RequiresArg)
4953 Diag(Tok, diag::err_argument_required_after_attribute);
4954
4955 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4956
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004957 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004958 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004959 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004960 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004961 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004962 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004963 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004964 else if (RequiresArg)
4965 Diag(Tok, diag::err_argument_required_after_attribute);
4966
David Blaikie4e4d0842012-03-11 07:00:24 +00004967 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004968
4969 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004970 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004971 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004972 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004973 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004974
David Blaikie4e4d0842012-03-11 07:00:24 +00004975 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004976 // FIXME: Accept these components in any order, and produce fixits to
4977 // correct the order if the user gets it wrong. Ideally we should deal
4978 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004979
4980 // Parse cv-qualifier-seq[opt].
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004981 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
4982 /*CXX11AttributesAllowed*/ false,
4983 /*AtomicAllowed*/ false);
Richard Smith6ee326a2012-04-10 01:32:12 +00004984 if (!DS.getSourceRange().getEnd().isInvalid()) {
4985 EndLoc = DS.getSourceRange().getEnd();
4986 ConstQualifierLoc = DS.getConstSpecLoc();
4987 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4988 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004989
4990 // Parse ref-qualifier[opt].
4991 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004992 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004993 diag::warn_cxx98_compat_ref_qualifier :
4994 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004995
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004996 RefQualifierIsLValueRef = Tok.is(tok::amp);
4997 RefQualifierLoc = ConsumeToken();
4998 EndLoc = RefQualifierLoc;
4999 }
5000
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005001 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00005002 // If a declaration declares a member function or member function
5003 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005004 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00005005 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005006 // declarator.
Richard Smithd9227792013-03-15 00:41:52 +00005007 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosier8decdee2012-06-26 22:30:43 +00005008 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00005009 getLangOpts().CPlusPlus11 &&
Richard Smithd9227792013-03-15 00:41:52 +00005010 (D.getContext() == Declarator::MemberContext
5011 ? !D.getDeclSpec().isFriendSpecified()
5012 : D.getContext() == Declarator::FileContext &&
5013 D.getCXXScopeSpec().isValid() &&
5014 Actions.CurContext->isRecord());
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005015 Sema::CXXThisScopeRAII ThisScope(Actions,
5016 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00005017 DS.getTypeQualifiers() |
Richard Smith84046262013-04-21 01:08:50 +00005018 (D.getDeclSpec().isConstexprSpecified() &&
5019 !getLangOpts().CPlusPlus1y
Richard Smith7b19cb12013-01-14 01:55:13 +00005020 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005021 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00005022
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005023 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00005024 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00005025 DynamicExceptions,
5026 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00005027 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005028 if (ESpecType != EST_None)
5029 EndLoc = ESpecRange.getEnd();
5030
Richard Smith6ee326a2012-04-10 01:32:12 +00005031 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5032 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005033 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00005034
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005035 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005036 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00005037 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00005038 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005039 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5040 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005041 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00005042 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00005043 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005044 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005045 }
5046 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005047 }
5048
5049 // Remember that we parsed a function type, and remember the attributes.
5050 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005051 IsAmbiguous,
5052 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005053 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005054 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005055 DS.getTypeQualifiers(),
5056 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00005057 RefQualifierLoc, ConstQualifierLoc,
5058 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00005059 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005060 ESpecType, ESpecRange.getBegin(),
5061 DynamicExceptions.data(),
5062 DynamicExceptionRanges.data(),
5063 DynamicExceptions.size(),
5064 NoexceptExpr.isUsable() ?
5065 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005066 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005067 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00005068 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00005069
5070 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005071}
5072
5073/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5074/// identifier list form for a K&R-style function: void foo(a,b,c)
5075///
5076/// Note that identifier-lists are only allowed for normal declarators, not for
5077/// abstract-declarators.
5078bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00005079 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005080 && Tok.is(tok::identifier)
5081 && !TryAltiVecVectorToken()
5082 // K&R identifier lists can't have typedefs as identifiers, per C99
5083 // 6.7.5.3p11.
5084 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5085 // Identifier lists follow a really simple grammar: the identifiers can
5086 // be followed *only* by a ", identifier" or ")". However, K&R
5087 // identifier lists are really rare in the brave new modern world, and
5088 // it is very common for someone to typo a type in a non-K&R style
5089 // list. If we are presented with something like: "void foo(intptr x,
5090 // float y)", we don't want to start parsing the function declarator as
5091 // though it is a K&R style declarator just because intptr is an
5092 // invalid type.
5093 //
5094 // To handle this, we check to see if the token after the first
5095 // identifier is a "," or ")". Only then do we parse it as an
5096 // identifier list.
5097 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5098}
5099
5100/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5101/// we found a K&R-style identifier list instead of a typed parameter list.
5102///
5103/// After returning, ParamInfo will hold the parsed parameters.
5104///
5105/// identifier-list: [C99 6.7.5]
5106/// identifier
5107/// identifier-list ',' identifier
5108///
5109void Parser::ParseFunctionDeclaratorIdentifierList(
5110 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005111 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005112 // If there was no identifier specified for the declarator, either we are in
5113 // an abstract-declarator, or we are in a parameter declarator which was found
5114 // to be abstract. In abstract-declarators, identifier lists are not valid:
5115 // diagnose this.
5116 if (!D.getIdentifier())
5117 Diag(Tok, diag::ext_ident_list_in_param);
5118
5119 // Maintain an efficient lookup of params we have seen so far.
5120 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5121
5122 while (1) {
5123 // If this isn't an identifier, report the error and skip until ')'.
5124 if (Tok.isNot(tok::identifier)) {
5125 Diag(Tok, diag::err_expected_ident);
5126 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
5127 // Forget we parsed anything.
5128 ParamInfo.clear();
5129 return;
5130 }
5131
5132 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5133
5134 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5135 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5136 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5137
5138 // Verify that the argument identifier has not already been mentioned.
5139 if (!ParamsSoFar.insert(ParmII)) {
5140 Diag(Tok, diag::err_param_redefinition) << ParmII;
5141 } else {
5142 // Remember this identifier in ParamInfo.
5143 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5144 Tok.getLocation(),
5145 0));
5146 }
5147
5148 // Eat the identifier.
5149 ConsumeToken();
5150
5151 // The list continues if we see a comma.
5152 if (Tok.isNot(tok::comma))
5153 break;
5154 ConsumeToken();
5155 }
5156}
5157
5158/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5159/// after the opening parenthesis. This function will not parse a K&R-style
5160/// identifier list.
5161///
Richard Smith6ce48a72012-04-11 04:01:28 +00005162/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5163/// caller parsed those arguments immediately after the open paren - they should
5164/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005165///
5166/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5167/// be the location of the ellipsis, if any was parsed.
5168///
Reid Spencer5f016e22007-07-11 17:01:13 +00005169/// parameter-type-list: [C99 6.7.5]
5170/// parameter-list
5171/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00005172/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00005173///
5174/// parameter-list: [C99 6.7.5]
5175/// parameter-declaration
5176/// parameter-list ',' parameter-declaration
5177///
5178/// parameter-declaration: [C99 6.7.5]
5179/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00005180/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00005181/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00005182/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00005183/// declaration-specifiers abstract-declarator[opt]
5184/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00005185/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00005186/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00005187/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00005188///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005189void Parser::ParseParameterDeclarationClause(
5190 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00005191 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005192 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005193 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00005194
Chris Lattnerf97409f2008-04-06 06:57:35 +00005195 while (1) {
5196 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00005197 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5198 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00005199 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00005200 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00005201 }
Mike Stump1eb44332009-09-09 15:08:12 +00005202
Chris Lattnerf97409f2008-04-06 06:57:35 +00005203 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00005204 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00005205 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005206
Richard Smith6ce48a72012-04-11 04:01:28 +00005207 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005208 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00005209
John McCall7f040a92010-12-24 02:08:15 +00005210 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00005211 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00005212
5213 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00005214
5215 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00005216 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005217 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00005218 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5219 // too much hassle.
5220 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00005221
Chris Lattnere64c5492009-02-27 18:38:20 +00005222 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00005223
Chris Lattnerf97409f2008-04-06 06:57:35 +00005224 // Parse the declarator. This is "PrototypeContext", because we must
5225 // accept either 'declarator' or 'abstract-declarator' here.
5226 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5227 ParseDeclarator(ParmDecl);
5228
5229 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00005230 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00005231
Chris Lattnerf97409f2008-04-06 06:57:35 +00005232 // Remember this parsed parameter in ParamInfo.
5233 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00005234
Douglas Gregor72b505b2008-12-16 21:30:33 +00005235 // DefArgToks is used when the parsing of default arguments needs
5236 // to be delayed.
5237 CachedTokens *DefArgToks = 0;
5238
Chris Lattnerf97409f2008-04-06 06:57:35 +00005239 // If no parameter was specified, verify that *something* was specified,
5240 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00005241 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5242 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005243 // Completely missing, emit error.
5244 Diag(DSStart, diag::err_missing_param);
5245 } else {
5246 // Otherwise, we have something. Add it and let semantic analysis try
5247 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005248
Chris Lattnerf97409f2008-04-06 06:57:35 +00005249 // Inform the actions module about the parameter declarator, so it gets
5250 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00005251 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00005252
5253 // Parse the default argument, if any. We parse the default
5254 // arguments in all dialects; the semantic analysis in
5255 // ActOnParamDefaultArgument will reject the default argument in
5256 // C.
5257 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005258 SourceLocation EqualLoc = Tok.getLocation();
5259
Chris Lattner04421082008-04-08 04:40:51 +00005260 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005261 if (D.getContext() == Declarator::MemberContext) {
5262 // If we're inside a class definition, cache the tokens
5263 // corresponding to the default argument. We'll actually parse
5264 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005265 // FIXME: Can we use a smart pointer for Toks?
5266 DefArgToks = new CachedTokens;
5267
Mike Stump1eb44332009-09-09 15:08:12 +00005268 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00005269 /*StopAtSemi=*/true,
5270 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005271 delete DefArgToks;
5272 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005273 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005274 } else {
5275 // Mark the end of the default argument so that we know when to
5276 // stop when we parse it later on.
5277 Token DefArgEnd;
5278 DefArgEnd.startToken();
5279 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5280 DefArgEnd.setLocation(Tok.getLocation());
5281 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005282 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005283 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005284 }
Chris Lattner04421082008-04-08 04:40:51 +00005285 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005286 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005287 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005288
Chad Rosier8decdee2012-06-26 22:30:43 +00005289 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005290 // used.
5291 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005292 Sema::PotentiallyEvaluatedIfUsed,
5293 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005294
Sebastian Redl84407ba2012-03-14 15:54:00 +00005295 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005296 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005297 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005298 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005299 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005300 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005301 if (DefArgResult.isInvalid()) {
5302 Actions.ActOnParamDefaultArgumentError(Param);
5303 SkipUntil(tok::comma, tok::r_paren, true, true);
5304 } else {
5305 // Inform the actions module about the default argument
5306 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005307 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005308 }
Chris Lattner04421082008-04-08 04:40:51 +00005309 }
5310 }
Mike Stump1eb44332009-09-09 15:08:12 +00005311
5312 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5313 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00005314 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005315 }
5316
5317 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005318 if (Tok.isNot(tok::comma)) {
5319 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005320 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005321
David Blaikie4e4d0842012-03-11 07:00:24 +00005322 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005323 // We have ellipsis without a preceding ',', which is ill-formed
5324 // in C. Complain and provide the fix.
5325 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005326 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005327 }
5328 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005329
Douglas Gregored5d6512009-09-22 21:41:40 +00005330 break;
5331 }
Mike Stump1eb44332009-09-09 15:08:12 +00005332
Chris Lattnerf97409f2008-04-06 06:57:35 +00005333 // Consume the comma.
5334 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005335 }
Mike Stump1eb44332009-09-09 15:08:12 +00005336
Chris Lattner66d28652008-04-06 06:34:08 +00005337}
Chris Lattneref4715c2008-04-06 05:45:57 +00005338
Reid Spencer5f016e22007-07-11 17:01:13 +00005339/// [C90] direct-declarator '[' constant-expression[opt] ']'
5340/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5341/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5342/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5343/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005344/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5345/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005346void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005347 if (CheckProhibitedCXX11Attribute())
5348 return;
5349
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005350 BalancedDelimiterTracker T(*this, tok::l_square);
5351 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005352
Chris Lattner378c7e42008-12-18 07:27:21 +00005353 // C array syntax has many features, but by-far the most common is [] and [4].
5354 // This code does a fast path to handle some of the most obvious cases.
5355 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005356 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005357 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005358 MaybeParseCXX11Attributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005359
Chris Lattner378c7e42008-12-18 07:27:21 +00005360 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005361 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005362 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005363 T.getOpenLocation(),
5364 T.getCloseLocation()),
5365 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005366 return;
5367 } else if (Tok.getKind() == tok::numeric_constant &&
5368 GetLookAheadToken(1).is(tok::r_square)) {
5369 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005370 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005371 ConsumeToken();
5372
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005373 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005374 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005375 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005376
Chris Lattner378c7e42008-12-18 07:27:21 +00005377 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005378 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005379 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005380 T.getOpenLocation(),
5381 T.getCloseLocation()),
5382 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005383 return;
5384 }
Mike Stump1eb44332009-09-09 15:08:12 +00005385
Reid Spencer5f016e22007-07-11 17:01:13 +00005386 // If valid, this location is the position where we read the 'static' keyword.
5387 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005388 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005389 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005390
Reid Spencer5f016e22007-07-11 17:01:13 +00005391 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005392 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005393 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005394 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005395
Reid Spencer5f016e22007-07-11 17:01:13 +00005396 // If we haven't already read 'static', check to see if there is one after the
5397 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005398 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005399 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005400
Reid Spencer5f016e22007-07-11 17:01:13 +00005401 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5402 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005403 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005404
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005405 // Handle the case where we have '[*]' as the array size. However, a leading
5406 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005407 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005408 // infrequent, use of lookahead is not costly here.
5409 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005410 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005411
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005412 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005413 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005414 StaticLoc = SourceLocation(); // Drop the static.
5415 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005416 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005417 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005418 // Note, in C89, this production uses the constant-expr production instead
5419 // of assignment-expr. The only difference is that assignment-expr allows
5420 // things like '=' and '*='. Sema rejects these in C89 mode because they
5421 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005422
Douglas Gregore0762c92009-06-19 23:52:42 +00005423 // Parse the constant-expression or assignment-expression now (depending
5424 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005425 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005426 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005427 } else {
5428 EnterExpressionEvaluationContext Unevaluated(Actions,
5429 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005430 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005431 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005432 }
Mike Stump1eb44332009-09-09 15:08:12 +00005433
Reid Spencer5f016e22007-07-11 17:01:13 +00005434 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005435 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005436 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005437 // If the expression was invalid, skip it.
5438 SkipUntil(tok::r_square);
5439 return;
5440 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005441
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005442 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005443
John McCall0b7e6782011-03-24 11:26:52 +00005444 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005445 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005446
Chris Lattner378c7e42008-12-18 07:27:21 +00005447 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005448 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005449 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005450 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005451 T.getOpenLocation(),
5452 T.getCloseLocation()),
5453 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005454}
5455
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005456/// [GNU] typeof-specifier:
5457/// typeof ( expressions )
5458/// typeof ( type-name )
5459/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005460///
5461void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005462 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005463 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005464 SourceLocation StartLoc = ConsumeToken();
5465
John McCallcfb708c2010-01-13 20:03:27 +00005466 const bool hasParens = Tok.is(tok::l_paren);
5467
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005468 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5469 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005470
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005471 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005472 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005473 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005474 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5475 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005476 if (hasParens)
5477 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005478
5479 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005480 // FIXME: Not accurate, the range gets one token more than it should.
5481 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005482 else
5483 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005484
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005485 if (isCastExpr) {
5486 if (!CastTy) {
5487 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005488 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005489 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005490
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005491 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005492 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005493 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5494 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005495 DiagID, CastTy))
5496 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005497 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005498 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005499
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005500 // If we get here, the operand to the typeof was an expresion.
5501 if (Operand.isInvalid()) {
5502 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005503 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005504 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005505
Eli Friedman71b8fb52012-01-21 01:01:51 +00005506 // We might need to transform the operand if it is potentially evaluated.
5507 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5508 if (Operand.isInvalid()) {
5509 DS.SetTypeSpecError();
5510 return;
5511 }
5512
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005513 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005514 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005515 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5516 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005517 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005518 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005519}
Chris Lattner1b492422010-02-28 18:33:55 +00005520
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005521/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005522/// _Atomic ( type-name )
5523///
5524void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005525 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5526 "Not an atomic specifier");
Eli Friedmanb001de72011-10-06 23:00:33 +00005527
5528 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005529 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005530 if (T.consumeOpen())
Eli Friedmanb001de72011-10-06 23:00:33 +00005531 return;
Eli Friedmanb001de72011-10-06 23:00:33 +00005532
5533 TypeResult Result = ParseTypeName();
5534 if (Result.isInvalid()) {
5535 SkipUntil(tok::r_paren);
5536 return;
5537 }
5538
5539 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005540 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005541
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005542 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005543 return;
5544
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005545 DS.setTypeofParensRange(T.getRange());
5546 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005547
5548 const char *PrevSpec = 0;
5549 unsigned DiagID;
5550 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5551 DiagID, Result.release()))
5552 Diag(StartLoc, DiagID) << PrevSpec;
5553}
5554
Chris Lattner1b492422010-02-28 18:33:55 +00005555
5556/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5557/// from TryAltiVecVectorToken.
5558bool Parser::TryAltiVecVectorTokenOutOfLine() {
5559 Token Next = NextToken();
5560 switch (Next.getKind()) {
5561 default: return false;
5562 case tok::kw_short:
5563 case tok::kw_long:
5564 case tok::kw_signed:
5565 case tok::kw_unsigned:
5566 case tok::kw_void:
5567 case tok::kw_char:
5568 case tok::kw_int:
5569 case tok::kw_float:
5570 case tok::kw_double:
5571 case tok::kw_bool:
5572 case tok::kw___pixel:
5573 Tok.setKind(tok::kw___vector);
5574 return true;
5575 case tok::identifier:
5576 if (Next.getIdentifierInfo() == Ident_pixel) {
5577 Tok.setKind(tok::kw___vector);
5578 return true;
5579 }
5580 return false;
5581 }
5582}
5583
5584bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5585 const char *&PrevSpec, unsigned &DiagID,
5586 bool &isInvalid) {
5587 if (Tok.getIdentifierInfo() == Ident_vector) {
5588 Token Next = NextToken();
5589 switch (Next.getKind()) {
5590 case tok::kw_short:
5591 case tok::kw_long:
5592 case tok::kw_signed:
5593 case tok::kw_unsigned:
5594 case tok::kw_void:
5595 case tok::kw_char:
5596 case tok::kw_int:
5597 case tok::kw_float:
5598 case tok::kw_double:
5599 case tok::kw_bool:
5600 case tok::kw___pixel:
5601 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5602 return true;
5603 case tok::identifier:
5604 if (Next.getIdentifierInfo() == Ident_pixel) {
5605 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5606 return true;
5607 }
5608 break;
5609 default:
5610 break;
5611 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005612 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005613 DS.isTypeAltiVecVector()) {
5614 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5615 return true;
5616 }
5617 return false;
5618}