blob: b71cced3536b30e43b704e3aaf4cb91cd4e297c7 [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"
Larisse Voufo7c64ef02013-06-21 00:08:46 +000016#include "clang/AST/DeclTemplate.h"
Benjamin Kramer9852f582012-12-01 16:35:25 +000017#include "clang/Basic/AddressSpaces.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000018#include "clang/Basic/CharInfo.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000019#include "clang/Basic/OpenCL.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000021#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000022#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000024#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000026#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000027#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// C99 6.7: Declarations.
32//===----------------------------------------------------------------------===//
33
34/// ParseTypeName
35/// type-name: [C99 6.7.6]
36/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000037///
38/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000039TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000040 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000041 AccessSpecifier AS,
Richard Smith6b3d3e52013-02-20 19:22:51 +000042 Decl **OwnedType,
43 ParsedAttributes *Attrs) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000044 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000045 if (DSC == DSC_normal)
46 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000047
Reid Spencer5f016e22007-07-11 17:01:13 +000048 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000049 DeclSpec DS(AttrFactory);
Richard Smith6b3d3e52013-02-20 19:22:51 +000050 if (Attrs)
51 DS.addAttributes(Attrs->getList());
Richard Smith7796eb52012-03-12 08:56:40 +000052 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000053 if (OwnedType)
54 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000055
Reid Spencer5f016e22007-07-11 17:01:13 +000056 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000057 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000058 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000059 if (Range)
60 *Range = DeclaratorInfo.getSourceRange();
61
Chris Lattnereaaebc72009-04-25 08:06:05 +000062 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000063 return true;
64
Douglas Gregor23c94db2010-07-02 17:43:08 +000065 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000066}
67
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000068
69/// isAttributeLateParsed - Return true if the attribute has arguments that
70/// require late parsing.
71static bool isAttributeLateParsed(const IdentifierInfo &II) {
72 return llvm::StringSwitch<bool>(II.getName())
73#include "clang/Parse/AttrLateParsed.inc"
74 .Default(false);
75}
76
Sean Huntbbd37c62009-11-21 08:43:09 +000077/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000078///
79/// [GNU] attributes:
80/// attribute
81/// attributes attribute
82///
83/// [GNU] attribute:
84/// '__attribute__' '(' '(' attribute-list ')' ')'
85///
86/// [GNU] attribute-list:
87/// attrib
88/// attribute_list ',' attrib
89///
90/// [GNU] attrib:
91/// empty
92/// attrib-name
93/// attrib-name '(' identifier ')'
94/// attrib-name '(' identifier ',' nonempty-expr-list ')'
95/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
96///
97/// [GNU] attrib-name:
98/// identifier
99/// typespec
100/// typequal
101/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +0000102///
Richard Smith4c6c4112013-09-03 18:57:36 +0000103/// Whether an attribute takes an 'identifier' is determined by the
104/// attrib-name. GCC's behavior here is not worth imitating:
Reid Spencer5f016e22007-07-11 17:01:13 +0000105///
Richard Smith4c6c4112013-09-03 18:57:36 +0000106/// * In C mode, if the attribute argument list starts with an identifier
107/// followed by a ',' or an ')', and the identifier doesn't resolve to
108/// a type, it is parsed as an identifier. If the attribute actually
109/// wanted an expression, it's out of luck (but it turns out that no
110/// attributes work that way, because C constant expressions are very
111/// limited).
112/// * In C++ mode, if the attribute argument list starts with an identifier,
113/// and the attribute *wants* an identifier, it is parsed as an identifier.
114/// At block scope, any additional tokens between the identifier and the
115/// ',' or ')' are ignored, otherwise they produce a parse error.
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000116///
Richard Smith4c6c4112013-09-03 18:57:36 +0000117/// We follow the C++ model, but don't allow junk after the identifier.
John McCall7f040a92010-12-24 02:08:15 +0000118void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000119 SourceLocation *endLoc,
120 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000121 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Chris Lattner04d66662007-10-09 17:33:22 +0000123 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 ConsumeToken();
125 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
126 "attribute")) {
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 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
131 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000132 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 }
134 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000135 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
136 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000137 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
139 ConsumeToken();
140 continue;
141 }
142 // we have an identifier or declaration specifier (const, int, etc.)
143 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
144 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000146 if (Tok.is(tok::l_paren)) {
147 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000148 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000149 LateParsedAttribute *LA =
150 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
151 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000152
Bill Wendlingad017fa2012-12-20 19:22:21 +0000153 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000154 // with other late-parsed declarations.
DeLesley Hutchins161db022012-11-02 21:44:32 +0000155 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000156 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000158 // consume everything up to and including the matching right parens
159 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000161 Token Eof;
162 Eof.startToken();
163 Eof.setLocation(Tok.getLocation());
164 LA->Toks.push_back(Eof);
165 } else {
Michael Han6880f492012-10-03 01:56:22 +0000166 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000167 0, SourceLocation(), AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 }
169 } else {
Aaron Ballman624421f2013-08-31 01:11:41 +0000170 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
171 AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 }
173 }
174 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000176 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000177 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
178 SkipUntil(tok::r_paren, false);
179 }
John McCall7f040a92010-12-24 02:08:15 +0000180 if (endLoc)
181 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000183}
184
Douglas Gregor92eb7d82013-05-02 23:25:32 +0000185/// \brief Determine whether the given attribute has all expression arguments.
186static bool attributeHasExprArgs(const IdentifierInfo &II) {
187 return llvm::StringSwitch<bool>(II.getName())
188#include "clang/Parse/AttrExprArgs.inc"
189 .Default(false);
190}
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000191
Richard Smith8edabd92013-09-03 18:01:40 +0000192IdentifierLoc *Parser::ParseIdentifierLoc() {
193 assert(Tok.is(tok::identifier) && "expected an identifier");
194 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
195 Tok.getLocation(),
196 Tok.getIdentifierInfo());
197 ConsumeToken();
198 return IL;
199}
200
Michael Han6880f492012-10-03 01:56:22 +0000201/// Parse the arguments to a parameterized GNU attribute or
202/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000203void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
204 SourceLocation AttrNameLoc,
205 ParsedAttributes &Attrs,
Michael Han6880f492012-10-03 01:56:22 +0000206 SourceLocation *EndLoc,
207 IdentifierInfo *ScopeName,
208 SourceLocation ScopeLoc,
209 AttributeList::Syntax Syntax) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000210
211 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
212
213 // Availability attributes have their own grammar.
214 if (AttrName->isStr("availability")) {
215 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
216 return;
217 }
218 // Thread safety attributes fit into the FIXME case above, so we
219 // just parse the arguments as a list of expressions
220 if (IsThreadSafetyAttribute(AttrName->getName())) {
221 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
222 return;
223 }
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000224 // Type safety attributes have their own grammar.
225 if (AttrName->isStr("type_tag_for_datatype")) {
226 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
227 return;
228 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000229
230 ConsumeParen(); // ignore the left paren loc for now
231
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000232 bool BuiltinType = false;
Aaron Ballman624421f2013-08-31 01:11:41 +0000233 ArgsVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000234
Joey Gouly37453b92013-03-08 09:42:32 +0000235 TypeResult T;
236 SourceRange TypeRange;
237 bool TypeParsed = false;
238
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000239 switch (Tok.getKind()) {
240 case tok::kw_char:
241 case tok::kw_wchar_t:
242 case tok::kw_char16_t:
243 case tok::kw_char32_t:
244 case tok::kw_bool:
245 case tok::kw_short:
246 case tok::kw_int:
247 case tok::kw_long:
248 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000249 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000250 case tok::kw_signed:
251 case tok::kw_unsigned:
252 case tok::kw_float:
253 case tok::kw_double:
254 case tok::kw_void:
255 case tok::kw_typeof:
256 // __attribute__(( vec_type_hint(char) ))
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000257 BuiltinType = true;
Joey Gouly37453b92013-03-08 09:42:32 +0000258 T = ParseTypeName(&TypeRange);
259 TypeParsed = true;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000260 break;
261
Aaron Ballman624421f2013-08-31 01:11:41 +0000262 case tok::identifier: {
Joey Gouly37453b92013-03-08 09:42:32 +0000263 if (AttrName->isStr("vec_type_hint")) {
264 T = ParseTypeName(&TypeRange);
265 TypeParsed = true;
266 break;
267 }
Richard Smith4c6c4112013-09-03 18:57:36 +0000268 // If this attribute doesn't want an 'identifier' argument, then this
269 // argument should be parsed as an expression.
Douglas Gregor92eb7d82013-05-02 23:25:32 +0000270 if (attributeHasExprArgs(*AttrName))
271 break;
Aaron Ballman624421f2013-08-31 01:11:41 +0000272
Richard Smith8edabd92013-09-03 18:01:40 +0000273 ArgExprs.push_back(ParseIdentifierLoc());
Aaron Ballman624421f2013-08-31 01:11:41 +0000274 } break;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000275
276 default:
277 break;
278 }
279
Joey Gouly37453b92013-03-08 09:42:32 +0000280 bool isInvalid = false;
281 bool isParmType = false;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000282
Joey Gouly37453b92013-03-08 09:42:32 +0000283 if (!BuiltinType && !AttrName->isStr("vec_type_hint") &&
Aaron Ballman624421f2013-08-31 01:11:41 +0000284 (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000285 // Eat the comma.
Aaron Ballman624421f2013-08-31 01:11:41 +0000286 if (!ArgExprs.empty())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000287 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000288
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000289 // Parse the non-empty comma-separated list of expressions.
290 while (1) {
291 ExprResult ArgExpr(ParseAssignmentExpression());
292 if (ArgExpr.isInvalid()) {
293 SkipUntil(tok::r_paren);
294 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000295 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000296 ArgExprs.push_back(ArgExpr.release());
297 if (Tok.isNot(tok::comma))
298 break;
299 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000300 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000301 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000302 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
303 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
304 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000305 while (Tok.is(tok::identifier)) {
306 ConsumeToken();
307 if (Tok.is(tok::greater))
308 break;
309 if (Tok.is(tok::comma)) {
310 ConsumeToken();
311 continue;
312 }
313 }
314 if (Tok.isNot(tok::greater))
315 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000316 SkipUntil(tok::r_paren, false, true); // skip until ')'
317 }
Joey Gouly37453b92013-03-08 09:42:32 +0000318 } else if (AttrName->isStr("vec_type_hint")) {
319 if (T.get() && !T.isInvalid())
320 isParmType = true;
321 else {
322 if (Tok.is(tok::identifier))
323 ConsumeToken();
324 if (TypeParsed)
325 isInvalid = true;
326 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000327 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000328
329 SourceLocation RParen = Tok.getLocation();
Joey Gouly37453b92013-03-08 09:42:32 +0000330 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen) &&
331 !isInvalid) {
Michael Han45bed132012-10-04 16:42:52 +0000332 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Joey Gouly37453b92013-03-08 09:42:32 +0000333 if (isParmType) {
Joey Gouly37453b92013-03-08 09:42:32 +0000334 Attrs.addNewTypeAttr(AttrName, SourceRange(AttrLoc, RParen), ScopeName,
Aaron Ballman624421f2013-08-31 01:11:41 +0000335 ScopeLoc, T.get(), Syntax);
Joey Gouly37453b92013-03-08 09:42:32 +0000336 } else {
337 AttributeList *attr = Attrs.addNew(
Aaron Ballman624421f2013-08-31 01:11:41 +0000338 AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
339 ArgExprs.data(), ArgExprs.size(), Syntax);
Joey Gouly37453b92013-03-08 09:42:32 +0000340 if (BuiltinType &&
341 attr->getKind() == AttributeList::AT_IBOutletCollection)
342 Diag(Tok, diag::err_iboutletcollection_builtintype);
343 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000344 }
345}
346
Chad Rosier8decdee2012-06-26 22:30:43 +0000347/// \brief Parses a single argument for a declspec, including the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000348/// surrounding parens.
Chad Rosier8decdee2012-06-26 22:30:43 +0000349void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000350 SourceLocation AttrNameLoc,
351 ParsedAttributes &Attrs)
352{
353 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000354 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000355 AttrName->getNameStart(), tok::r_paren))
356 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000357
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000358 ExprResult ArgExpr(ParseConstantExpression());
359 if (ArgExpr.isInvalid()) {
360 T.skipToEnd();
361 return;
362 }
Aaron Ballman624421f2013-08-31 01:11:41 +0000363 ArgsUnion ExprList = ArgExpr.take();
364 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
365 AttributeList::AS_Declspec);
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000366
367 T.consumeClose();
368}
369
Chad Rosier8decdee2012-06-26 22:30:43 +0000370/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000371/// arguments.
372bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
373 return llvm::StringSwitch<bool>(Ident->getName())
374 .Case("dllimport", true)
375 .Case("dllexport", true)
376 .Case("noreturn", true)
377 .Case("nothrow", true)
378 .Case("noinline", true)
379 .Case("naked", true)
380 .Case("appdomain", true)
381 .Case("process", true)
382 .Case("jitintrinsic", true)
383 .Case("noalias", true)
384 .Case("restrict", true)
385 .Case("novtable", true)
386 .Case("selectany", true)
387 .Case("thread", true)
Aaron Ballman3ce0de62013-05-04 16:58:37 +0000388 .Case("safebuffers", true )
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000389 .Default(false);
390}
391
Chad Rosier8decdee2012-06-26 22:30:43 +0000392/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000393/// parameters). Will return false if we properly handled the declspec, or
394/// true if it is an unknown declspec.
Chad Rosier8decdee2012-06-26 22:30:43 +0000395void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000396 SourceLocation Loc,
397 ParsedAttributes &Attrs) {
398 // Try to handle the easy case first -- these declspecs all take a single
399 // parameter as their argument.
400 if (llvm::StringSwitch<bool>(Ident->getName())
401 .Case("uuid", true)
402 .Case("align", true)
403 .Case("allocate", true)
404 .Default(false)) {
405 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
406 } else if (Ident->getName() == "deprecated") {
Chad Rosier8decdee2012-06-26 22:30:43 +0000407 // The deprecated declspec has an optional single argument, so we will
408 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000409 // not.
410 if (Tok.getKind() == tok::l_paren)
411 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
412 else
Aaron Ballman624421f2013-08-31 01:11:41 +0000413 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000414 } else if (Ident->getName() == "property") {
415 // The property declspec is more complex in that it can take one or two
Chad Rosier8decdee2012-06-26 22:30:43 +0000416 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000417 // must be named get or put.
John McCall76da55d2013-04-16 07:28:30 +0000418 if (Tok.isNot(tok::l_paren)) {
419 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
420 << Ident->getNameStart();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000421 return;
John McCall76da55d2013-04-16 07:28:30 +0000422 }
423 BalancedDelimiterTracker T(*this, tok::l_paren);
424 T.expectAndConsume(diag::err_expected_lparen_after,
425 Ident->getNameStart(), tok::r_paren);
426
427 enum AccessorKind {
428 AK_Invalid = -1,
429 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
430 };
431 IdentifierInfo *AccessorNames[] = { 0, 0 };
432 bool HasInvalidAccessor = false;
433
434 // Parse the accessor specifications.
435 while (true) {
436 // Stop if this doesn't look like an accessor spec.
437 if (!Tok.is(tok::identifier)) {
438 // If the user wrote a completely empty list, use a special diagnostic.
439 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
440 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
441 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
442 break;
443 }
444
445 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
446 break;
447 }
448
449 AccessorKind Kind;
450 SourceLocation KindLoc = Tok.getLocation();
451 StringRef KindStr = Tok.getIdentifierInfo()->getName();
452 if (KindStr == "get") {
453 Kind = AK_Get;
454 } else if (KindStr == "put") {
455 Kind = AK_Put;
456
457 // Recover from the common mistake of using 'set' instead of 'put'.
458 } else if (KindStr == "set") {
459 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
460 << FixItHint::CreateReplacement(KindLoc, "put");
461 Kind = AK_Put;
462
463 // Handle the mistake of forgetting the accessor kind by skipping
464 // this accessor.
465 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
466 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
467 ConsumeToken();
468 HasInvalidAccessor = true;
469 goto next_property_accessor;
470
471 // Otherwise, complain about the unknown accessor kind.
472 } else {
473 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
474 HasInvalidAccessor = true;
475 Kind = AK_Invalid;
476
477 // Try to keep parsing unless it doesn't look like an accessor spec.
478 if (!NextToken().is(tok::equal)) break;
479 }
480
481 // Consume the identifier.
482 ConsumeToken();
483
484 // Consume the '='.
485 if (Tok.is(tok::equal)) {
486 ConsumeToken();
487 } else {
488 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
489 << KindStr;
490 break;
491 }
492
493 // Expect the method name.
494 if (!Tok.is(tok::identifier)) {
495 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
496 break;
497 }
498
499 if (Kind == AK_Invalid) {
500 // Just drop invalid accessors.
501 } else if (AccessorNames[Kind] != NULL) {
502 // Complain about the repeated accessor, ignore it, and keep parsing.
503 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
504 } else {
505 AccessorNames[Kind] = Tok.getIdentifierInfo();
506 }
507 ConsumeToken();
508
509 next_property_accessor:
510 // Keep processing accessors until we run out.
511 if (Tok.is(tok::comma)) {
512 ConsumeAnyToken();
513 continue;
514
515 // If we run into the ')', stop without consuming it.
516 } else if (Tok.is(tok::r_paren)) {
517 break;
518 } else {
519 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
520 break;
521 }
522 }
523
524 // Only add the property attribute if it was well-formed.
525 if (!HasInvalidAccessor) {
Aaron Ballman624421f2013-08-31 01:11:41 +0000526 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall76da55d2013-04-16 07:28:30 +0000527 AccessorNames[AK_Get], AccessorNames[AK_Put],
528 AttributeList::AS_Declspec);
529 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000530 T.skipToEnd();
531 } else {
532 // We don't recognize this as a valid declspec, but instead of creating the
533 // attribute and allowing sema to warn about it, we will warn here instead.
534 // This is because some attributes have multiple spellings, but we need to
535 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosier8decdee2012-06-26 22:30:43 +0000536 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000537 // both locations.
538 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
539
540 // If there's an open paren, we should eat the open and close parens under
541 // the assumption that this unknown declspec has parameters.
542 BalancedDelimiterTracker T(*this, tok::l_paren);
543 if (!T.consumeOpen())
544 T.skipToEnd();
545 }
546}
547
Eli Friedmana23b4852009-06-08 07:21:15 +0000548/// [MS] decl-specifier:
549/// __declspec ( extended-decl-modifier-seq )
550///
551/// [MS] extended-decl-modifier-seq:
552/// extended-decl-modifier[opt]
553/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000554void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000555 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000556
Steve Narofff59e17e2008-12-24 20:59:21 +0000557 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000558 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000559 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000560 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000561 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000562
Chad Rosier8decdee2012-06-26 22:30:43 +0000563 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000564 // you can specify multiple attributes per declspec.
565 while (Tok.getKind() != tok::r_paren) {
566 // We expect either a well-known identifier or a generic string. Anything
567 // else is a malformed declspec.
568 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosier8decdee2012-06-26 22:30:43 +0000569 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000570 Tok.getKind() != tok::kw_restrict) {
571 Diag(Tok, diag::err_ms_declspec_type);
572 T.skipToEnd();
573 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000574 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000575
576 IdentifierInfo *AttrName;
577 SourceLocation AttrNameLoc;
578 if (IsString) {
579 SmallString<8> StrBuffer;
580 bool Invalid = false;
581 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
582 if (Invalid) {
583 T.skipToEnd();
584 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000585 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000586 AttrName = PP.getIdentifierInfo(Str);
587 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000588 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000589 AttrName = Tok.getIdentifierInfo();
590 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000591 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000592
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000593 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosier8decdee2012-06-26 22:30:43 +0000594 // If we have a generic string, we will allow it because there is no
595 // documented list of allowable string declspecs, but we know they exist
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000596 // (for instance, SAL declspecs in older versions of MSVC).
597 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000598 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000599 // arguments and can be turned into an attribute directly.
Aaron Ballman624421f2013-08-31 01:11:41 +0000600 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
601 AttributeList::AS_Declspec);
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000602 else
603 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000604 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000605 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000606}
607
John McCall7f040a92010-12-24 02:08:15 +0000608void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000609 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000610 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000611 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000612 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballmanaa9df092013-05-22 23:25:32 +0000613 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
614 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000615 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
616 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman624421f2013-08-31 01:11:41 +0000617 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
618 AttributeList::AS_Keyword);
Eli Friedman290eeb02009-06-08 23:27:34 +0000619 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000620}
621
John McCall7f040a92010-12-24 02:08:15 +0000622void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000623 // Treat these like attributes
624 while (Tok.is(tok::kw___pascal)) {
625 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
626 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman624421f2013-08-31 01:11:41 +0000627 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
628 AttributeList::AS_Keyword);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000629 }
John McCall7f040a92010-12-24 02:08:15 +0000630}
631
Peter Collingbournef315fa82011-02-14 01:42:53 +0000632void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
633 // Treat these like attributes
634 while (Tok.is(tok::kw___kernel)) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000635 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbournef315fa82011-02-14 01:42:53 +0000636 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman624421f2013-08-31 01:11:41 +0000637 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
638 AttributeList::AS_Keyword);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000639 }
640}
641
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000642void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000643 // FIXME: The mapping from attribute spelling to semantics should be
644 // performed in Sema, not here.
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000645 SourceLocation Loc = Tok.getLocation();
646 switch(Tok.getKind()) {
647 // OpenCL qualifiers:
648 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000649 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000650 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000651 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000652 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000653 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000654
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000655 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000656 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000657 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000658 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000659 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000660
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000661 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000662 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000663 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000664 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000665 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000666
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000667 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000668 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000669 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000670 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000671 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000672
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000673 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000674 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000675 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000676 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000677 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000678
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000679 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000680 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000681 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000682 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000683 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000684
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000685 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000686 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000687 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000688 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000689 break;
690 default: break;
691 }
692}
693
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000694/// \brief Parse a version number.
695///
696/// version:
697/// simple-integer
698/// simple-integer ',' simple-integer
699/// simple-integer ',' simple-integer ',' simple-integer
700VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
701 Range = Tok.getLocation();
702
703 if (!Tok.is(tok::numeric_constant)) {
704 Diag(Tok, diag::err_expected_version);
705 SkipUntil(tok::comma, tok::r_paren, true, true, true);
706 return VersionTuple();
707 }
708
709 // Parse the major (and possibly minor and subminor) versions, which
710 // are stored in the numeric constant. We utilize a quirk of the
711 // lexer, which is that it handles something like 1.2.3 as a single
712 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000713 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000714 Buffer.resize(Tok.getLength()+1);
715 const char *ThisTokBegin = &Buffer[0];
716
717 // Get the spelling of the token, which eliminates trigraphs, etc.
718 bool Invalid = false;
719 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
720 if (Invalid)
721 return VersionTuple();
722
723 // Parse the major version.
724 unsigned AfterMajor = 0;
725 unsigned Major = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000726 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000727 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
728 ++AfterMajor;
729 }
730
731 if (AfterMajor == 0) {
732 Diag(Tok, diag::err_expected_version);
733 SkipUntil(tok::comma, tok::r_paren, true, true, true);
734 return VersionTuple();
735 }
736
737 if (AfterMajor == ActualLength) {
738 ConsumeToken();
739
740 // We only had a single version component.
741 if (Major == 0) {
742 Diag(Tok, diag::err_zero_version);
743 return VersionTuple();
744 }
745
746 return VersionTuple(Major);
747 }
748
749 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
750 Diag(Tok, diag::err_expected_version);
751 SkipUntil(tok::comma, tok::r_paren, true, true, true);
752 return VersionTuple();
753 }
754
755 // Parse the minor version.
756 unsigned AfterMinor = AfterMajor + 1;
757 unsigned Minor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000758 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000759 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
760 ++AfterMinor;
761 }
762
763 if (AfterMinor == ActualLength) {
764 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000765
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000766 // We had major.minor.
767 if (Major == 0 && Minor == 0) {
768 Diag(Tok, diag::err_zero_version);
769 return VersionTuple();
770 }
771
Chad Rosier8decdee2012-06-26 22:30:43 +0000772 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000773 }
774
775 // If what follows is not a '.', we have a problem.
776 if (ThisTokBegin[AfterMinor] != '.') {
777 Diag(Tok, diag::err_expected_version);
778 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosier8decdee2012-06-26 22:30:43 +0000779 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000780 }
781
782 // Parse the subminor version.
783 unsigned AfterSubminor = AfterMinor + 1;
784 unsigned Subminor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000785 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000786 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
787 ++AfterSubminor;
788 }
789
790 if (AfterSubminor != ActualLength) {
791 Diag(Tok, diag::err_expected_version);
792 SkipUntil(tok::comma, tok::r_paren, true, true, true);
793 return VersionTuple();
794 }
795 ConsumeToken();
796 return VersionTuple(Major, Minor, Subminor);
797}
798
799/// \brief Parse the contents of the "availability" attribute.
800///
801/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000802/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000803///
804/// platform:
805/// identifier
806///
807/// version-arg-list:
808/// version-arg
809/// version-arg ',' version-arg-list
810///
811/// version-arg:
812/// 'introduced' '=' version
813/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000814/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000815/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000816/// opt-message:
817/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000818void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
819 SourceLocation AvailabilityLoc,
820 ParsedAttributes &attrs,
821 SourceLocation *endLoc) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000822 enum { Introduced, Deprecated, Obsoleted, Unknown };
823 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000824 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000825
826 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000827 BalancedDelimiterTracker T(*this, tok::l_paren);
828 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000829 Diag(Tok, diag::err_expected_lparen);
830 return;
831 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000832
833 // Parse the platform name,
834 if (Tok.isNot(tok::identifier)) {
835 Diag(Tok, diag::err_availability_expected_platform);
836 SkipUntil(tok::r_paren);
837 return;
838 }
Richard Smith8edabd92013-09-03 18:01:40 +0000839 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000840
841 // Parse the ',' following the platform name.
842 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
843 return;
844
845 // If we haven't grabbed the pointers for the identifiers
846 // "introduced", "deprecated", and "obsoleted", do so now.
847 if (!Ident_introduced) {
848 Ident_introduced = PP.getIdentifierInfo("introduced");
849 Ident_deprecated = PP.getIdentifierInfo("deprecated");
850 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000851 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000852 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000853 }
854
855 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000856 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000857 do {
858 if (Tok.isNot(tok::identifier)) {
859 Diag(Tok, diag::err_availability_expected_change);
860 SkipUntil(tok::r_paren);
861 return;
862 }
863 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
864 SourceLocation KeywordLoc = ConsumeToken();
865
Douglas Gregorb53e4172011-03-26 03:35:55 +0000866 if (Keyword == Ident_unavailable) {
867 if (UnavailableLoc.isValid()) {
868 Diag(KeywordLoc, diag::err_availability_redundant)
869 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000870 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000871 UnavailableLoc = KeywordLoc;
872
873 if (Tok.isNot(tok::comma))
874 break;
875
876 ConsumeToken();
877 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000878 }
879
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000880 if (Tok.isNot(tok::equal)) {
881 Diag(Tok, diag::err_expected_equal_after)
882 << Keyword;
883 SkipUntil(tok::r_paren);
884 return;
885 }
886 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000887 if (Keyword == Ident_message) {
Benjamin Kramerc5617142013-09-13 17:31:48 +0000888 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbs97f84612012-11-17 19:16:52 +0000889 Diag(Tok, diag::err_expected_string_literal)
890 << /*Source='availability attribute'*/2;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000891 SkipUntil(tok::r_paren);
892 return;
893 }
894 MessageExpr = ParseStringLiteralExpression();
895 break;
896 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000897
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000898 SourceRange VersionRange;
899 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000900
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000901 if (Version.empty()) {
902 SkipUntil(tok::r_paren);
903 return;
904 }
905
906 unsigned Index;
907 if (Keyword == Ident_introduced)
908 Index = Introduced;
909 else if (Keyword == Ident_deprecated)
910 Index = Deprecated;
911 else if (Keyword == Ident_obsoleted)
912 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000913 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000914 Index = Unknown;
915
916 if (Index < Unknown) {
917 if (!Changes[Index].KeywordLoc.isInvalid()) {
918 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000919 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000920 << SourceRange(Changes[Index].KeywordLoc,
921 Changes[Index].VersionRange.getEnd());
922 }
923
924 Changes[Index].KeywordLoc = KeywordLoc;
925 Changes[Index].Version = Version;
926 Changes[Index].VersionRange = VersionRange;
927 } else {
928 Diag(KeywordLoc, diag::err_availability_unknown_change)
929 << Keyword << VersionRange;
930 }
931
932 if (Tok.isNot(tok::comma))
933 break;
934
935 ConsumeToken();
936 } while (true);
937
938 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000939 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000940 return;
941
942 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000943 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000944
Douglas Gregorb53e4172011-03-26 03:35:55 +0000945 // The 'unavailable' availability cannot be combined with any other
946 // availability changes. Make sure that hasn't happened.
947 if (UnavailableLoc.isValid()) {
948 bool Complained = false;
949 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
950 if (Changes[Index].KeywordLoc.isValid()) {
951 if (!Complained) {
952 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
953 << SourceRange(Changes[Index].KeywordLoc,
954 Changes[Index].VersionRange.getEnd());
955 Complained = true;
956 }
957
958 // Clear out the availability.
959 Changes[Index] = AvailabilityChange();
960 }
961 }
962 }
963
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000964 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000965 attrs.addNew(&Availability,
966 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000967 0, AvailabilityLoc,
Aaron Ballman624421f2013-08-31 01:11:41 +0000968 Platform,
John McCall0b7e6782011-03-24 11:26:52 +0000969 Changes[Introduced],
970 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000971 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000972 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000973 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000974}
975
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000976
Bill Wendlingad017fa2012-12-20 19:22:21 +0000977// Late Parsed Attributes:
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000978// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
979
980void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
981
982void Parser::LateParsedClass::ParseLexedAttributes() {
983 Self->ParseLexedAttributes(*Class);
984}
985
986void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000987 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000988}
989
990/// Wrapper class which calls ParseLexedAttribute, after setting up the
991/// scope appropriately.
992void Parser::ParseLexedAttributes(ParsingClass &Class) {
993 // Deal with templates
994 // FIXME: Test cases to make sure this does the right thing for templates.
995 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
996 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
997 HasTemplateScope);
998 if (HasTemplateScope)
999 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
1000
Douglas Gregorcefc3af2012-04-16 07:05:22 +00001001 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001002 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +00001003 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001004 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1005 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1006
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +00001007 // Enter the scope of nested classes
1008 if (!AlreadyHasClassScope)
1009 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1010 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +00001011 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +00001012 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1013 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1014 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001015 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001016
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +00001017 if (!AlreadyHasClassScope)
1018 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1019 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001020}
1021
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001022
1023/// \brief Parse all attributes in LAs, and attach them to Decl D.
1024void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1025 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +00001026 assert(LAs.parseSoon() &&
1027 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001028 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +00001029 if (D)
1030 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001031 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +00001032 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001033 }
1034 LAs.clear();
1035}
1036
1037
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001038/// \brief Finish parsing an attribute for which parsing was delayed.
1039/// This will be called at the end of parsing a class declaration
1040/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +00001041/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001042/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001043void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1044 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001045 // Save the current token position.
1046 SourceLocation OrigLoc = Tok.getLocation();
1047
1048 // Append the current token at the end of the new token stream so that it
1049 // doesn't get lost.
1050 LA.Toks.push_back(Tok);
1051 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1052 // Consume the previously pushed token.
Argyrios Kyrtzidisab2d09b2013-03-27 23:58:17 +00001053 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001054
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001055 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smithcd8ab512013-01-17 01:30:42 +00001056 // FIXME: Do not warn on C++11 attributes, once we start supporting
1057 // them here.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001058 Diag(Tok, diag::warn_attribute_on_function_definition)
1059 << LA.AttrName.getName();
1060 }
1061
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001062 ParsedAttributes Attrs(AttrFactory);
1063 SourceLocation endLoc;
1064
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001065 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001066 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001067 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1068 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +00001069
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001070 // Allow 'this' within late-parsed attributes.
Richard Smithcafeb942013-06-07 02:33:37 +00001071 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1072 ND && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001073
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001074 if (LA.Decls.size() == 1) {
1075 // If the Decl is templatized, add template parameters to scope.
1076 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1077 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1078 if (HasTemplateScope)
1079 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001080
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001081 // If the Decl is on a function, add function parameters to the scope.
1082 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1083 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1084 if (HasFunScope)
1085 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001086
Michael Han6880f492012-10-03 01:56:22 +00001087 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +00001088 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001089
1090 if (HasFunScope) {
1091 Actions.ActOnExitFunctionContext();
1092 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1093 }
1094 if (HasTemplateScope) {
1095 TempScope.Exit();
1096 }
1097 } else {
1098 // If there are multiple decls, then the decl cannot be within the
1099 // function scope.
Michael Han6880f492012-10-03 01:56:22 +00001100 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +00001101 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001102 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +00001103 } else {
1104 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +00001105 }
1106
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001107 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1108 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1109 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001110
1111 if (Tok.getLocation() != OrigLoc) {
1112 // Due to a parsing error, we either went over the cached tokens or
1113 // there are still cached tokens left, so we skip the leftover tokens.
1114 // Since this is an uncommon situation that should be avoided, use the
1115 // expensive isBeforeInTranslationUnit call.
1116 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1117 OrigLoc))
1118 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001119 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001120 }
1121}
1122
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001123/// \brief Wrapper around a case statement checking if AttrName is
1124/// one of the thread safety attributes
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001125bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001126 return llvm::StringSwitch<bool>(AttrName)
1127 .Case("guarded_by", true)
1128 .Case("guarded_var", true)
1129 .Case("pt_guarded_by", true)
1130 .Case("pt_guarded_var", true)
1131 .Case("lockable", true)
1132 .Case("scoped_lockable", true)
1133 .Case("no_thread_safety_analysis", true)
1134 .Case("acquired_after", true)
1135 .Case("acquired_before", true)
1136 .Case("exclusive_lock_function", true)
1137 .Case("shared_lock_function", true)
1138 .Case("exclusive_trylock_function", true)
1139 .Case("shared_trylock_function", true)
1140 .Case("unlock_function", true)
1141 .Case("lock_returned", true)
1142 .Case("locks_excluded", true)
1143 .Case("exclusive_locks_required", true)
1144 .Case("shared_locks_required", true)
1145 .Default(false);
1146}
1147
1148/// \brief Parse the contents of thread safety attributes. These
1149/// should always be parsed as an expression list.
1150///
1151/// We need to special case the parsing due to the fact that if the first token
1152/// of the first argument is an identifier, the main parse loop will store
1153/// that token as a "parameter" and the rest of
1154/// the arguments will be added to a list of "arguments". However,
1155/// subsequent tokens in the first argument are lost. We instead parse each
1156/// argument as an expression and add all arguments to the list of "arguments".
1157/// In future, we will take advantage of this special case to also
1158/// deal with some argument scoping issues here (for example, referring to a
1159/// function parameter in the attribute on that function).
1160void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1161 SourceLocation AttrNameLoc,
1162 ParsedAttributes &Attrs,
1163 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001164 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001165
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001166 BalancedDelimiterTracker T(*this, tok::l_paren);
1167 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001168
Aaron Ballman624421f2013-08-31 01:11:41 +00001169 ArgsVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001170 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001171
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001172 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001173 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinsed4330b2013-02-07 19:01:07 +00001174 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001175 ExprResult ArgExpr(ParseAssignmentExpression());
1176 if (ArgExpr.isInvalid()) {
1177 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001178 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001179 break;
1180 } else {
1181 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001182 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001183 if (Tok.isNot(tok::comma))
1184 break;
1185 ConsumeToken(); // Eat the comma, move to the next argument
1186 }
1187 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001188 if (ArgExprsOk && !T.consumeClose()) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001189 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(),
1190 ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001191 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001192 if (EndLoc)
1193 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001194}
1195
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001196void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1197 SourceLocation AttrNameLoc,
1198 ParsedAttributes &Attrs,
1199 SourceLocation *EndLoc) {
1200 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1201
1202 BalancedDelimiterTracker T(*this, tok::l_paren);
1203 T.consumeOpen();
1204
1205 if (Tok.isNot(tok::identifier)) {
1206 Diag(Tok, diag::err_expected_ident);
1207 T.skipToEnd();
1208 return;
1209 }
Richard Smith8edabd92013-09-03 18:01:40 +00001210 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001211
1212 if (Tok.isNot(tok::comma)) {
1213 Diag(Tok, diag::err_expected_comma);
1214 T.skipToEnd();
1215 return;
1216 }
1217 ConsumeToken();
1218
1219 SourceRange MatchingCTypeRange;
1220 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1221 if (MatchingCType.isInvalid()) {
1222 T.skipToEnd();
1223 return;
1224 }
1225
1226 bool LayoutCompatible = false;
1227 bool MustBeNull = false;
1228 while (Tok.is(tok::comma)) {
1229 ConsumeToken();
1230 if (Tok.isNot(tok::identifier)) {
1231 Diag(Tok, diag::err_expected_ident);
1232 T.skipToEnd();
1233 return;
1234 }
1235 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1236 if (Flag->isStr("layout_compatible"))
1237 LayoutCompatible = true;
1238 else if (Flag->isStr("must_be_null"))
1239 MustBeNull = true;
1240 else {
1241 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1242 T.skipToEnd();
1243 return;
1244 }
1245 ConsumeToken(); // consume flag
1246 }
1247
1248 if (!T.consumeClose()) {
1249 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman624421f2013-08-31 01:11:41 +00001250 ArgumentKind, MatchingCType.release(),
1251 LayoutCompatible, MustBeNull,
1252 AttributeList::AS_GNU);
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001253 }
1254
1255 if (EndLoc)
1256 *EndLoc = T.getCloseLocation();
1257}
1258
Richard Smith6ee326a2012-04-10 01:32:12 +00001259/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1260/// of a C++11 attribute-specifier in a location where an attribute is not
1261/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1262/// situation.
1263///
1264/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1265/// this doesn't appear to actually be an attribute-specifier, and the caller
1266/// should try to parse it.
1267bool Parser::DiagnoseProhibitedCXX11Attribute() {
1268 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1269
1270 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1271 case CAK_NotAttributeSpecifier:
1272 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1273 return false;
1274
1275 case CAK_InvalidAttributeSpecifier:
1276 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1277 return false;
1278
1279 case CAK_AttributeSpecifier:
1280 // Parse and discard the attributes.
1281 SourceLocation BeginLoc = ConsumeBracket();
1282 ConsumeBracket();
1283 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1284 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1285 SourceLocation EndLoc = ConsumeBracket();
1286 Diag(BeginLoc, diag::err_attributes_not_allowed)
1287 << SourceRange(BeginLoc, EndLoc);
1288 return true;
1289 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001290 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001291}
1292
Richard Smith975d52c2013-02-20 01:17:14 +00001293/// \brief We have found the opening square brackets of a C++11
1294/// attribute-specifier in a location where an attribute is not permitted, but
1295/// we know where the attributes ought to be written. Parse them anyway, and
1296/// provide a fixit moving them to the right place.
Richard Smith05321402013-02-19 23:47:15 +00001297void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1298 SourceLocation CorrectLocation) {
1299 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1300 Tok.is(tok::kw_alignas));
1301
1302 // Consume the attributes.
1303 SourceLocation Loc = Tok.getLocation();
1304 ParseCXX11Attributes(Attrs);
1305 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1306
1307 Diag(Loc, diag::err_attributes_not_allowed)
1308 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1309 << FixItHint::CreateRemoval(AttrRange);
1310}
1311
John McCall7f040a92010-12-24 02:08:15 +00001312void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1313 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1314 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001315}
1316
Michael Hanf64231e2012-11-06 19:34:54 +00001317void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1318 AttributeList *AttrList = attrs.getList();
1319 while (AttrList) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001320 if (AttrList->isCXX11Attribute()) {
Richard Smithd03de6a2013-01-29 10:02:16 +00001321 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Hanf64231e2012-11-06 19:34:54 +00001322 << AttrList->getName();
1323 AttrList->setInvalid();
1324 }
1325 AttrList = AttrList->getNext();
1326 }
1327}
1328
Reid Spencer5f016e22007-07-11 17:01:13 +00001329/// ParseDeclaration - Parse a full 'declaration', which consists of
1330/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001331/// 'Context' should be a Declarator::TheContext value. This returns the
1332/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001333///
1334/// declaration: [C99 6.7]
1335/// block-declaration ->
1336/// simple-declaration
1337/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001338/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001339/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001340/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001341/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001342/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001343/// others... [FIXME]
1344///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001345Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1346 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001347 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001348 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001349 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001350 // Must temporarily exit the objective-c container scope for
1351 // parsing c none objective-c decls.
1352 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001353
John McCalld226f652010-08-21 09:40:31 +00001354 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001355 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001356 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001357 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001358 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001359 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001360 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001361 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001362 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001363 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001364 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001365 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001366 SourceLocation InlineLoc = ConsumeToken();
1367 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1368 break;
1369 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001370 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001371 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001372 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001373 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001374 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001375 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001376 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001377 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001378 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001379 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001380 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001381 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001382 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001383 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001384 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001385 default:
John McCall7f040a92010-12-24 02:08:15 +00001386 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001387 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001388
Chris Lattner682bf922009-03-29 16:50:03 +00001389 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001390 // single decl, convert it now. Alias declarations can also declare a type;
1391 // include that too if it is present.
1392 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001393}
1394
1395/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1396/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001397/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1398/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001399///[C90/C++]init-declarator-list ';' [TODO]
1400/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001401///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001402/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001403/// attribute-specifier-seq[opt] type-specifier-seq declarator
1404///
Chris Lattnercd147752009-03-29 17:27:48 +00001405/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001406/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001407///
1408/// If FRI is non-null, we might be parsing a for-range-declaration instead
1409/// of a simple-declaration. If we find that we are, we also parse the
1410/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001411Parser::DeclGroupPtrTy
1412Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1413 SourceLocation &DeclEnd,
Richard Smith68ea3ae2013-02-22 09:06:26 +00001414 ParsedAttributesWithRange &Attrs,
Sean Hunt2edf0a22012-06-23 05:07:58 +00001415 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001416 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001417 ParsingDeclSpec DS(*this);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001418
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001419 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001420 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001421
Reid Spencer5f016e22007-07-11 17:01:13 +00001422 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1423 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001424 if (Tok.is(tok::semi)) {
Richard Smith68ea3ae2013-02-22 09:06:26 +00001425 ProhibitAttributes(Attrs);
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001426 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001427 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001428 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001429 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001430 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001431 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001432 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001433
Richard Smith68ea3ae2013-02-22 09:06:26 +00001434 DS.takeAttributesFrom(Attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00001435 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001436}
Mike Stump1eb44332009-09-09 15:08:12 +00001437
Richard Smith0706df42011-10-19 21:33:05 +00001438/// Returns true if this might be the start of a declarator, or a common typo
1439/// for a declarator.
1440bool Parser::MightBeDeclarator(unsigned Context) {
1441 switch (Tok.getKind()) {
1442 case tok::annot_cxxscope:
1443 case tok::annot_template_id:
1444 case tok::caret:
1445 case tok::code_completion:
1446 case tok::coloncolon:
1447 case tok::ellipsis:
1448 case tok::kw___attribute:
1449 case tok::kw_operator:
1450 case tok::l_paren:
1451 case tok::star:
1452 return true;
1453
1454 case tok::amp:
1455 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001456 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001457
Richard Smith1c94c162012-01-09 22:31:44 +00001458 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith80ad52f2013-01-02 11:42:31 +00001459 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smith1c94c162012-01-09 22:31:44 +00001460 NextToken().is(tok::l_square);
1461
1462 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001463 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001464
Richard Smith0706df42011-10-19 21:33:05 +00001465 case tok::identifier:
1466 switch (NextToken().getKind()) {
1467 case tok::code_completion:
1468 case tok::coloncolon:
1469 case tok::comma:
1470 case tok::equal:
1471 case tok::equalequal: // Might be a typo for '='.
1472 case tok::kw_alignas:
1473 case tok::kw_asm:
1474 case tok::kw___attribute:
1475 case tok::l_brace:
1476 case tok::l_paren:
1477 case tok::l_square:
1478 case tok::less:
1479 case tok::r_brace:
1480 case tok::r_paren:
1481 case tok::r_square:
1482 case tok::semi:
1483 return true;
1484
1485 case tok::colon:
1486 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001487 // and in block scope it's probably a label. Inside a class definition,
1488 // this is a bit-field.
1489 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001490 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001491
1492 case tok::identifier: // Possible virt-specifier.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001493 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001494
1495 default:
1496 return false;
1497 }
1498
1499 default:
1500 return false;
1501 }
1502}
1503
Richard Smith994d73f2012-04-11 20:59:20 +00001504/// Skip until we reach something which seems like a sensible place to pick
1505/// up parsing after a malformed declaration. This will sometimes stop sooner
1506/// than SkipUntil(tok::r_brace) would, but will never stop later.
1507void Parser::SkipMalformedDecl() {
1508 while (true) {
1509 switch (Tok.getKind()) {
1510 case tok::l_brace:
1511 // Skip until matching }, then stop. We've probably skipped over
1512 // a malformed class or function definition or similar.
1513 ConsumeBrace();
1514 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1515 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1516 // This declaration isn't over yet. Keep skipping.
1517 continue;
1518 }
1519 if (Tok.is(tok::semi))
1520 ConsumeToken();
1521 return;
1522
1523 case tok::l_square:
1524 ConsumeBracket();
1525 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1526 continue;
1527
1528 case tok::l_paren:
1529 ConsumeParen();
1530 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1531 continue;
1532
1533 case tok::r_brace:
1534 return;
1535
1536 case tok::semi:
1537 ConsumeToken();
1538 return;
1539
1540 case tok::kw_inline:
1541 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001542 // a good place to pick back up parsing, except in an Objective-C
1543 // @interface context.
1544 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1545 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001546 return;
1547 break;
1548
1549 case tok::kw_namespace:
1550 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001551 // place to pick back up parsing, except in an Objective-C
1552 // @interface context.
1553 if (Tok.isAtStartOfLine() &&
1554 (!ParsingInObjCContainer || CurParsedObjCImpl))
1555 return;
1556 break;
1557
1558 case tok::at:
1559 // @end is very much like } in Objective-C contexts.
1560 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1561 ParsingInObjCContainer)
1562 return;
1563 break;
1564
1565 case tok::minus:
1566 case tok::plus:
1567 // - and + probably start new method declarations in Objective-C contexts.
1568 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001569 return;
1570 break;
1571
1572 case tok::eof:
1573 return;
1574
1575 default:
1576 break;
1577 }
1578
1579 ConsumeAnyToken();
1580 }
1581}
1582
John McCalld8ac0572009-11-03 19:26:08 +00001583/// ParseDeclGroup - Having concluded that this is either a function
1584/// definition or a group of object declarations, actually parse the
1585/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001586Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1587 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001588 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001589 SourceLocation *DeclEnd,
1590 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001591 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001592 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001593 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001594
John McCalld8ac0572009-11-03 19:26:08 +00001595 // Bail out if the first declarator didn't seem well-formed.
1596 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001597 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001598 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001599 }
Mike Stump1eb44332009-09-09 15:08:12 +00001600
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001601 // Save late-parsed attributes for now; they need to be parsed in the
1602 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001603 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1604 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001605 if (D.isFunctionDeclarator())
1606 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1607
Chris Lattnerc82daef2010-07-11 22:24:20 +00001608 // Check to see if we have a function *definition* which must have a body.
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001609 if (D.isFunctionDeclarator() &&
Chris Lattnerc82daef2010-07-11 22:24:20 +00001610 // Look at the next token to make sure that this isn't a function
1611 // declaration. We have to check this because __attribute__ might be the
1612 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001613 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001614
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001615 if (AllowFunctionDefinitions) {
1616 if (isStartOfFunctionDefinition(D)) {
1617 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1618 Diag(Tok, diag::err_function_declared_typedef);
John McCalld8ac0572009-11-03 19:26:08 +00001619
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001620 // Recover by treating the 'typedef' as spurious.
1621 DS.ClearStorageClassSpecs();
1622 }
1623
1624 Decl *TheDecl =
1625 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1626 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld8ac0572009-11-03 19:26:08 +00001627 }
1628
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001629 if (isDeclarationSpecifier()) {
1630 // If there is an invalid declaration specifier right after the function
1631 // prototype, then we must be in a missing semicolon case where this isn't
1632 // actually a body. Just fall through into the code that handles it as a
1633 // prototype, and let the top-level code handle the erroneous declspec
1634 // where it would otherwise expect a comma or semicolon.
1635 } else {
1636 Diag(Tok, diag::err_expected_fn_body);
1637 SkipUntil(tok::semi);
1638 return DeclGroupPtrTy();
1639 }
John McCalld8ac0572009-11-03 19:26:08 +00001640 } else {
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001641 if (Tok.is(tok::l_brace)) {
1642 Diag(Tok, diag::err_function_definition_not_allowed);
1643 SkipUntil(tok::r_brace, true, true);
1644 }
John McCalld8ac0572009-11-03 19:26:08 +00001645 }
1646 }
1647
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001648 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001649 return DeclGroupPtrTy();
1650
1651 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1652 // must parse and analyze the for-range-initializer before the declaration is
1653 // analyzed.
Douglas Gregor12849d02013-04-08 20:52:24 +00001654 //
1655 // Handle the Objective-C for-in loop variable similarly, although we
1656 // don't need to parse the container in advance.
1657 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1658 bool IsForRangeLoop = false;
1659 if (Tok.is(tok::colon)) {
1660 IsForRangeLoop = true;
1661 FRI->ColonLoc = ConsumeToken();
1662 if (Tok.is(tok::l_brace))
1663 FRI->RangeExpr = ParseBraceInitializer();
1664 else
1665 FRI->RangeExpr = ParseExpression();
1666 }
1667
Richard Smithad762fc2011-04-14 22:09:26 +00001668 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor12849d02013-04-08 20:52:24 +00001669 if (IsForRangeLoop)
1670 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001671 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001672 D.complete(ThisDecl);
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001673 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001674 }
1675
Chris Lattner5f9e2722011-07-23 10:55:15 +00001676 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001677 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001678 if (LateParsedAttrs.size() > 0)
1679 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001680 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001681 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001682 DeclsInGroup.push_back(FirstDecl);
1683
Richard Smith0706df42011-10-19 21:33:05 +00001684 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001685
John McCalld8ac0572009-11-03 19:26:08 +00001686 // If we don't have a comma, it is either the end of the list (a ';') or an
1687 // error, bail out.
1688 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001689 SourceLocation CommaLoc = ConsumeToken();
1690
1691 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1692 // This comma was followed by a line-break and something which can't be
1693 // the start of a declarator. The comma was probably a typo for a
1694 // semicolon.
1695 Diag(CommaLoc, diag::err_expected_semi_declaration)
1696 << FixItHint::CreateReplacement(CommaLoc, ";");
1697 ExpectSemi = false;
1698 break;
1699 }
John McCalld8ac0572009-11-03 19:26:08 +00001700
1701 // Parse the next declarator.
1702 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001703 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001704
1705 // Accept attributes in an init-declarator. In the first declarator in a
1706 // declaration, these would be part of the declspec. In subsequent
1707 // declarators, they become part of the declarator itself, so that they
1708 // don't apply to declarators after *this* one. Examples:
1709 // short __attribute__((common)) var; -> declspec
1710 // short var __attribute__((common)); -> declarator
1711 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001712 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001713
1714 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001715 if (!D.isInvalidType()) {
1716 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1717 D.complete(ThisDecl);
1718 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001719 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001720 }
John McCalld8ac0572009-11-03 19:26:08 +00001721 }
1722
1723 if (DeclEnd)
1724 *DeclEnd = Tok.getLocation();
1725
Richard Smith0706df42011-10-19 21:33:05 +00001726 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001727 ExpectAndConsumeSemi(Context == Declarator::FileContext
1728 ? diag::err_invalid_token_after_toplevel_declarator
1729 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001730 // Okay, there was no semicolon and one was expected. If we see a
1731 // declaration specifier, just assume it was missing and continue parsing.
1732 // Otherwise things are very confused and we skip to recover.
1733 if (!isDeclarationSpecifier()) {
1734 SkipUntil(tok::r_brace, true, true);
1735 if (Tok.is(tok::semi))
1736 ConsumeToken();
1737 }
John McCalld8ac0572009-11-03 19:26:08 +00001738 }
1739
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001740 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +00001741}
1742
Richard Smithad762fc2011-04-14 22:09:26 +00001743/// Parse an optional simple-asm-expr and attributes, and attach them to a
1744/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001745bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001746 // If a simple-asm-expr is present, parse it.
1747 if (Tok.is(tok::kw_asm)) {
1748 SourceLocation Loc;
1749 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1750 if (AsmLabel.isInvalid()) {
1751 SkipUntil(tok::semi, true, true);
1752 return true;
1753 }
1754
1755 D.setAsmLabel(AsmLabel.release());
1756 D.SetRangeEnd(Loc);
1757 }
1758
1759 MaybeParseGNUAttributes(D);
1760 return false;
1761}
1762
Douglas Gregor1426e532009-05-12 21:31:51 +00001763/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1764/// declarator'. This method parses the remainder of the declaration
1765/// (including any attributes or initializer, among other things) and
1766/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001767///
Reid Spencer5f016e22007-07-11 17:01:13 +00001768/// init-declarator: [C99 6.7]
1769/// declarator
1770/// declarator '=' initializer
1771/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1772/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001773/// [C++] declarator initializer[opt]
1774///
1775/// [C++] initializer:
1776/// [C++] '=' initializer-clause
1777/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001778/// [C++0x] '=' 'default' [TODO]
1779/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001780/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001781///
1782/// According to the standard grammar, =default and =delete are function
1783/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001784///
John McCalld226f652010-08-21 09:40:31 +00001785Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001786 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001787 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001788 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Richard Smithad762fc2011-04-14 22:09:26 +00001790 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1791}
Mike Stump1eb44332009-09-09 15:08:12 +00001792
Richard Smithad762fc2011-04-14 22:09:26 +00001793Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1794 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001795 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001796 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001797 switch (TemplateInfo.Kind) {
1798 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001799 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001800 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001801
Douglas Gregord5a423b2009-09-25 18:43:00 +00001802 case ParsedTemplateInfo::Template:
Larisse Voufoef4579c2013-08-06 01:03:05 +00001803 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001804 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001805 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001806 D);
Larisse Voufo25218132013-08-06 07:33:00 +00001807 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufoef4579c2013-08-06 01:03:05 +00001808 // Re-direct this decl to refer to the templated decl so that we can
1809 // initialize it.
1810 ThisDecl = VT->getTemplatedDecl();
1811 break;
1812 }
1813 case ParsedTemplateInfo::ExplicitInstantiation: {
1814 if (Tok.is(tok::semi)) {
1815 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1816 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1817 if (ThisRes.isInvalid()) {
1818 SkipUntil(tok::semi, true, true);
1819 return 0;
1820 }
1821 ThisDecl = ThisRes.get();
1822 } else {
1823 // FIXME: This check should be for a variable template instantiation only.
1824
1825 // Check that this is a valid instantiation
1826 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1827 // If the declarator-id is not a template-id, issue a diagnostic and
1828 // recover by ignoring the 'template' keyword.
1829 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1830 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1831 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1832 } else {
1833 SourceLocation LAngleLoc =
1834 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1835 Diag(D.getIdentifierLoc(),
1836 diag::err_explicit_instantiation_with_definition)
1837 << SourceRange(TemplateInfo.TemplateLoc)
1838 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1839
1840 // Recover as if it were an explicit specialization.
1841 TemplateParameterLists FakedParamLists;
1842 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1843 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1844 LAngleLoc));
1845
1846 ThisDecl =
1847 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1848 }
1849 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00001850 break;
1851 }
1852 }
Mike Stump1eb44332009-09-09 15:08:12 +00001853
Richard Smitha2c36462013-04-26 16:15:35 +00001854 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith34b41d92011-02-20 03:19:35 +00001855
Douglas Gregor1426e532009-05-12 21:31:51 +00001856 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001857 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001858 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001859 ConsumeToken();
Larisse Voufoef4579c2013-08-06 01:03:05 +00001860
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001861 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001862 if (D.isFunctionDeclarator())
1863 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1864 << 1 /* delete */;
1865 else
1866 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001867 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001868 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001869 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1870 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001871 else
1872 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001873 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001874 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001875 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001876 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001877 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001878
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001879 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001880 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001881 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001882 cutOffParsing();
1883 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001884 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001885
John McCall60d7b3a2010-08-24 06:29:42 +00001886 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001887
David Blaikie4e4d0842012-03-11 07:00:24 +00001888 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001889 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001890 ExitScope();
1891 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001892
Douglas Gregor1426e532009-05-12 21:31:51 +00001893 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001894 SkipUntil(tok::comma, true, true);
1895 Actions.ActOnInitializerError(ThisDecl);
1896 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001897 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1898 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001899 }
1900 } else if (Tok.is(tok::l_paren)) {
1901 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001902 BalancedDelimiterTracker T(*this, tok::l_paren);
1903 T.consumeOpen();
1904
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001905 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001906 CommaLocsTy CommaLocs;
1907
David Blaikie4e4d0842012-03-11 07:00:24 +00001908 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001909 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001910 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001911 }
1912
Douglas Gregor1426e532009-05-12 21:31:51 +00001913 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001914 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001915 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001916
David Blaikie4e4d0842012-03-11 07:00:24 +00001917 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001918 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001919 ExitScope();
1920 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001921 } else {
1922 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001923 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001924
1925 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1926 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001927
David Blaikie4e4d0842012-03-11 07:00:24 +00001928 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001929 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001930 ExitScope();
1931 }
1932
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001933 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1934 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001935 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001936 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1937 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001938 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001939 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001940 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001941 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001942 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1943
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001944 if (D.getCXXScopeSpec().isSet()) {
1945 EnterScope(0);
1946 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1947 }
1948
1949 ExprResult Init(ParseBraceInitializer());
1950
1951 if (D.getCXXScopeSpec().isSet()) {
1952 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1953 ExitScope();
1954 }
1955
1956 if (Init.isInvalid()) {
1957 Actions.ActOnInitializerError(ThisDecl);
1958 } else
1959 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1960 /*DirectInit=*/true, TypeContainsAuto);
1961
Douglas Gregor1426e532009-05-12 21:31:51 +00001962 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001963 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001964 }
1965
Richard Smith483b9f32011-02-21 20:05:19 +00001966 Actions.FinalizeDeclaration(ThisDecl);
1967
Douglas Gregor1426e532009-05-12 21:31:51 +00001968 return ThisDecl;
1969}
1970
Reid Spencer5f016e22007-07-11 17:01:13 +00001971/// ParseSpecifierQualifierList
1972/// specifier-qualifier-list:
1973/// type-specifier specifier-qualifier-list[opt]
1974/// type-qualifier specifier-qualifier-list[opt]
1975/// [GNU] attributes specifier-qualifier-list[opt]
1976///
Richard Smith69730c12012-03-12 07:56:15 +00001977void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1978 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001979 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1980 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001981 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001982 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001983
Reid Spencer5f016e22007-07-11 17:01:13 +00001984 // Validate declspec for type-name.
1985 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001986 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1987 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001988 Diag(Tok, diag::err_expected_type);
1989 DS.SetTypeSpecError();
1990 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1991 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001992 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001993 if (!DS.hasTypeSpecifier())
1994 DS.SetTypeSpecError();
1995 }
Mike Stump1eb44332009-09-09 15:08:12 +00001996
Reid Spencer5f016e22007-07-11 17:01:13 +00001997 // Issue diagnostic and remove storage class if present.
1998 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1999 if (DS.getStorageClassSpecLoc().isValid())
2000 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2001 else
Richard Smithec642442013-04-12 22:46:28 +00002002 Diag(DS.getThreadStorageClassSpecLoc(),
2003 diag::err_typename_invalid_storageclass);
Reid Spencer5f016e22007-07-11 17:01:13 +00002004 DS.ClearStorageClassSpecs();
2005 }
Mike Stump1eb44332009-09-09 15:08:12 +00002006
Reid Spencer5f016e22007-07-11 17:01:13 +00002007 // Issue diagnostic and remove function specfier if present.
2008 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00002009 if (DS.isInlineSpecified())
2010 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2011 if (DS.isVirtualSpecified())
2012 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2013 if (DS.isExplicitSpecified())
2014 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00002015 DS.ClearFunctionSpecs();
2016 }
Richard Smith69730c12012-03-12 07:56:15 +00002017
2018 // Issue diagnostic and remove constexpr specfier if present.
2019 if (DS.isConstexprSpecified()) {
2020 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2021 DS.ClearConstexprSpec();
2022 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002023}
2024
Chris Lattnerc199ab32009-04-12 20:42:31 +00002025/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2026/// specified token is valid after the identifier in a declarator which
2027/// immediately follows the declspec. For example, these things are valid:
2028///
2029/// int x [ 4]; // direct-declarator
2030/// int x ( int y); // direct-declarator
2031/// int(int x ) // direct-declarator
2032/// int x ; // simple-declaration
2033/// int x = 17; // init-declarator-list
2034/// int x , y; // init-declarator-list
2035/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00002036/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00002037/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00002038///
2039/// This is not, because 'x' does not immediately follow the declspec (though
2040/// ')' happens to be valid anyway).
2041/// int (x)
2042///
2043static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2044 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2045 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00002046 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00002047}
2048
Chris Lattnere40c2952009-04-14 21:34:55 +00002049
2050/// ParseImplicitInt - This method is called when we have an non-typename
2051/// identifier in a declspec (which normally terminates the decl spec) when
2052/// the declspec has no type specifier. In this case, the declspec is either
2053/// malformed or is "implicit int" (in K&R and C89).
2054///
2055/// This method handles diagnosing this prettily and returns false if the
2056/// declspec is done being processed. If it recovers and thinks there may be
2057/// other pieces of declspec after it, it returns true.
2058///
Chris Lattnerf4382f52009-04-14 22:17:06 +00002059bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002060 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00002061 AccessSpecifier AS, DeclSpecContext DSC,
2062 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00002063 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002064
Chris Lattnere40c2952009-04-14 21:34:55 +00002065 SourceLocation Loc = Tok.getLocation();
2066 // If we see an identifier that is not a type name, we normally would
2067 // parse it as the identifer being declared. However, when a typename
2068 // is typo'd or the definition is not included, this will incorrectly
2069 // parse the typename as the identifier name and fall over misparsing
2070 // later parts of the diagnostic.
2071 //
2072 // As such, we try to do some look-ahead in cases where this would
2073 // otherwise be an "implicit-int" case to see if this is invalid. For
2074 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2075 // an identifier with implicit int, we'd get a parse error because the
2076 // next token is obviously invalid for a type. Parse these as a case
2077 // with an invalid type specifier.
2078 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00002079
Chris Lattnere40c2952009-04-14 21:34:55 +00002080 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00002081 // error, do lookahead to try to do better recovery. This never applies
2082 // within a type specifier. Outside of C++, we allow this even if the
2083 // language doesn't "officially" support implicit int -- we support
Richard Smith58eb3702013-04-30 22:43:51 +00002084 // implicit int as an extension in C99 and C11.
Richard Smitha971d242012-05-09 20:55:26 +00002085 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith58eb3702013-04-30 22:43:51 +00002086 !getLangOpts().CPlusPlus &&
Richard Smith69730c12012-03-12 07:56:15 +00002087 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00002088 // If this token is valid for implicit int, e.g. "static x = 4", then
2089 // we just avoid eating the identifier, so it will be parsed as the
2090 // identifier in the declarator.
2091 return false;
2092 }
Mike Stump1eb44332009-09-09 15:08:12 +00002093
Richard Smith827adaf2012-05-15 21:01:51 +00002094 if (getLangOpts().CPlusPlus &&
2095 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2096 // Don't require a type specifier if we have the 'auto' storage class
2097 // specifier in C++98 -- we'll promote it to a type specifier.
2098 return false;
2099 }
2100
Chris Lattnere40c2952009-04-14 21:34:55 +00002101 // Otherwise, if we don't consume this token, we are going to emit an
2102 // error anyway. Try to recover from various common problems. Check
2103 // to see if this was a reference to a tag name without a tag specified.
2104 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00002105 //
2106 // C++ doesn't need this, and isTagName doesn't take SS.
2107 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002108 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002109 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00002110
Douglas Gregor23c94db2010-07-02 17:43:08 +00002111 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00002112 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002113 case DeclSpec::TST_enum:
2114 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2115 case DeclSpec::TST_union:
2116 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2117 case DeclSpec::TST_struct:
2118 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00002119 case DeclSpec::TST_interface:
2120 TagName="__interface"; FixitTagName = "__interface ";
2121 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002122 case DeclSpec::TST_class:
2123 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00002124 }
Mike Stump1eb44332009-09-09 15:08:12 +00002125
Chris Lattnerf4382f52009-04-14 22:17:06 +00002126 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002127 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2128 LookupResult R(Actions, TokenName, SourceLocation(),
2129 Sema::LookupOrdinaryName);
2130
Chris Lattnerf4382f52009-04-14 22:17:06 +00002131 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002132 << TokenName << TagName << getLangOpts().CPlusPlus
2133 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2134
2135 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2136 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2137 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00002138 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002139 << TokenName << TagName;
2140 }
Mike Stump1eb44332009-09-09 15:08:12 +00002141
Chris Lattnerf4382f52009-04-14 22:17:06 +00002142 // Parse this as a tag as if the missing tag were present.
2143 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00002144 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00002145 else
Richard Smith69730c12012-03-12 07:56:15 +00002146 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00002147 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00002148 return true;
2149 }
Chris Lattnere40c2952009-04-14 21:34:55 +00002150 }
Mike Stump1eb44332009-09-09 15:08:12 +00002151
Richard Smith8f0a7e72012-05-15 21:29:55 +00002152 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00002153 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00002154 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2155 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00002156 // Look ahead to the next token to try to figure out what this declaration
2157 // was supposed to be.
2158 switch (NextToken().getKind()) {
2159 case tok::comma:
2160 case tok::equal:
2161 case tok::kw_asm:
2162 case tok::l_brace:
2163 case tok::l_square:
2164 case tok::semi:
2165 // This looks like a variable declaration. The type is probably missing.
2166 // We're done parsing decl-specifiers.
2167 return false;
2168
2169 case tok::l_paren: {
2170 // static x(4); // 'x' is not a type
2171 // x(int n); // 'x' is not a type
2172 // x (*p)[]; // 'x' is a type
2173 //
2174 // Since we're in an error case (or the rare 'implicit int in C++' MS
2175 // extension), we can afford to perform a tentative parse to determine
2176 // which case we're in.
2177 TentativeParsingAction PA(*this);
2178 ConsumeToken();
2179 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2180 PA.Revert();
2181 if (TPR == TPResult::False())
2182 return false;
2183 // The identifier is followed by a parenthesized declarator.
2184 // It's supposed to be a type.
2185 break;
2186 }
2187
2188 default:
2189 // This is probably supposed to be a type. This includes cases like:
2190 // int f(itn);
2191 // struct S { unsinged : 4; };
2192 break;
2193 }
2194 }
2195
Chad Rosier8decdee2012-06-26 22:30:43 +00002196 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00002197 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00002198 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002199 IdentifierInfo *II = Tok.getIdentifierInfo();
2200 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00002201 // The action emitted a diagnostic, so we don't have to.
2202 if (T) {
2203 // The action has suggested that the type T could be used. Set that as
2204 // the type in the declaration specifiers, consume the would-be type
2205 // name token, and we're done.
2206 const char *PrevSpec;
2207 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00002208 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00002209 DS.SetRangeEnd(Tok.getLocation());
2210 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002211 // There may be other declaration specifiers after this.
2212 return true;
2213 } else if (II != Tok.getIdentifierInfo()) {
2214 // If no type was suggested, the correction is to a keyword
2215 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002216 // There may be other declaration specifiers after this.
2217 return true;
2218 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002219
Douglas Gregora786fdb2009-10-13 23:27:22 +00002220 // Fall through; the action had no suggestion for us.
2221 } else {
2222 // The action did not emit a diagnostic, so emit one now.
2223 SourceRange R;
2224 if (SS) R = SS->getRange();
2225 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2226 }
Mike Stump1eb44332009-09-09 15:08:12 +00002227
Douglas Gregora786fdb2009-10-13 23:27:22 +00002228 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002229 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002230 DS.SetRangeEnd(Tok.getLocation());
2231 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002232
Chris Lattnere40c2952009-04-14 21:34:55 +00002233 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2234 // avoid rippling error messages on subsequent uses of the same type,
2235 // could be useful if #include was forgotten.
2236 return false;
2237}
2238
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002239/// \brief Determine the declaration specifier context from the declarator
2240/// context.
2241///
2242/// \param Context the declarator context, which is one of the
2243/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002244Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002245Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2246 if (Context == Declarator::MemberContext)
2247 return DSC_class;
2248 if (Context == Declarator::FileContext)
2249 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002250 if (Context == Declarator::TrailingReturnContext)
2251 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002252 return DSC_normal;
2253}
2254
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002255/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2256///
2257/// FIXME: Simply returns an alignof() expression if the argument is a
2258/// type. Ideally, the type should be propagated directly into Sema.
2259///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002260/// [C11] type-id
2261/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002262/// [C++0x] type-id ...[opt]
2263/// [C++0x] assignment-expression ...[opt]
2264ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2265 SourceLocation &EllipsisLoc) {
2266 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002267 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002268 SourceLocation TypeLoc = Tok.getLocation();
2269 ParsedType Ty = ParseTypeName().get();
2270 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002271 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2272 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002273 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002274 ER = ParseConstantExpression();
2275
Richard Smith80ad52f2013-01-02 11:42:31 +00002276 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002277 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002278
2279 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002280}
2281
2282/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2283/// attribute to Attrs.
2284///
2285/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002286/// [C11] '_Alignas' '(' type-id ')'
2287/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smith33f04a22013-01-29 01:48:07 +00002288/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2289/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002290void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smithf6565a92013-02-22 08:32:16 +00002291 SourceLocation *EndLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002292 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2293 "Not an alignment-specifier!");
2294
Richard Smith33f04a22013-01-29 01:48:07 +00002295 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2296 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002297
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002298 BalancedDelimiterTracker T(*this, tok::l_paren);
2299 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002300 return;
2301
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002302 SourceLocation EllipsisLoc;
2303 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002304 if (ArgExpr.isInvalid()) {
2305 SkipUntil(tok::r_paren);
2306 return;
2307 }
2308
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002309 T.consumeClose();
Richard Smithf6565a92013-02-22 08:32:16 +00002310 if (EndLoc)
2311 *EndLoc = T.getCloseLocation();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002312
Aaron Ballman624421f2013-08-31 01:11:41 +00002313 ArgsVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002314 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman624421f2013-08-31 01:11:41 +00002315 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2316 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002317}
2318
Reid Spencer5f016e22007-07-11 17:01:13 +00002319/// ParseDeclarationSpecifiers
2320/// declaration-specifiers: [C99 6.7]
2321/// storage-class-specifier declaration-specifiers[opt]
2322/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002323/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002324/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002325/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002326/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002327///
2328/// storage-class-specifier: [C99 6.7.1]
2329/// 'typedef'
2330/// 'extern'
2331/// 'static'
2332/// 'auto'
2333/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002334/// [C++] 'mutable'
Richard Smithec642442013-04-12 22:46:28 +00002335/// [C++11] 'thread_local'
2336/// [C11] '_Thread_local'
Reid Spencer5f016e22007-07-11 17:01:13 +00002337/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002338/// function-specifier: [C99 6.7.4]
2339/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002340/// [C++] 'virtual'
2341/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002342/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002343/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002344/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002345
Reid Spencer5f016e22007-07-11 17:01:13 +00002346///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002347void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002348 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002349 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002350 DeclSpecContext DSContext,
2351 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002352 if (DS.getSourceRange().isInvalid()) {
2353 DS.SetRangeStart(Tok.getLocation());
2354 DS.SetRangeEnd(Tok.getLocation());
2355 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002356
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002357 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002358 bool AttrsLastTime = false;
2359 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002360 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002361 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002362 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002363 unsigned DiagID = 0;
2364
Reid Spencer5f016e22007-07-11 17:01:13 +00002365 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002366
Reid Spencer5f016e22007-07-11 17:01:13 +00002367 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002368 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002369 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002370 if (!AttrsLastTime)
2371 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002372 else {
2373 // Reject C++11 attributes that appertain to decl specifiers as
2374 // we don't support any C++11 attributes that appertain to decl
2375 // specifiers. This also conforms to what g++ 4.8 is doing.
2376 ProhibitCXX11Attributes(attrs);
2377
Sean Hunt2edf0a22012-06-23 05:07:58 +00002378 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002379 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002380
Reid Spencer5f016e22007-07-11 17:01:13 +00002381 // If this is not a declaration specifier token, we're done reading decl
2382 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002383 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002384 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002385
Sean Hunt2edf0a22012-06-23 05:07:58 +00002386 case tok::l_square:
2387 case tok::kw_alignas:
Richard Smith672edb02013-02-22 09:15:49 +00002388 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Sean Hunt2edf0a22012-06-23 05:07:58 +00002389 goto DoneWithDeclSpec;
2390
2391 ProhibitAttributes(attrs);
2392 // FIXME: It would be good to recover by accepting the attributes,
2393 // but attempting to do that now would cause serious
2394 // madness in terms of diagnostics.
2395 attrs.clear();
2396 attrs.Range = SourceRange();
2397
2398 ParseCXX11Attributes(attrs);
2399 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002400 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002401
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002402 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002403 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002404 if (DS.hasTypeSpecifier()) {
2405 bool AllowNonIdentifiers
2406 = (getCurScope()->getFlags() & (Scope::ControlScope |
2407 Scope::BlockScope |
2408 Scope::TemplateParamScope |
2409 Scope::FunctionPrototypeScope |
2410 Scope::AtCatchScope)) == 0;
2411 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002412 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002413 (DSContext == DSC_class && DS.isFriendSpecified());
2414
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002415 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002416 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002417 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002418 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002419 }
2420
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002421 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2422 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2423 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002424 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002425 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002426 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002427 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002428 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002429 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002430
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002431 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002432 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002433 }
2434
Chris Lattner5e02c472009-01-05 00:07:25 +00002435 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002436 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman5a428202013-08-15 23:59:20 +00002437 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall9ba61662010-02-26 08:45:28 +00002438 if (!DS.hasTypeSpecifier())
2439 DS.SetTypeSpecError();
2440 goto DoneWithDeclSpec;
2441 }
John McCall2e0a7152010-03-01 18:20:46 +00002442 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2443 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002444 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002445
2446 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002447 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002448 goto DoneWithDeclSpec;
2449
John McCallaa87d332009-12-12 11:40:51 +00002450 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002451 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2452 Tok.getAnnotationRange(),
2453 SS);
John McCallaa87d332009-12-12 11:40:51 +00002454
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002455 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002456 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002457 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002458 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002459 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002460 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002461
2462 // C++ [class.qual]p2:
2463 // In a lookup in which the constructor is an acceptable lookup
2464 // result and the nested-name-specifier nominates a class C:
2465 //
2466 // - if the name specified after the
2467 // nested-name-specifier, when looked up in C, is the
2468 // injected-class-name of C (Clause 9), or
2469 //
2470 // - if the name specified after the nested-name-specifier
2471 // is the same as the identifier or the
2472 // simple-template-id's template-name in the last
2473 // component of the nested-name-specifier,
2474 //
2475 // the name is instead considered to name the constructor of
2476 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002477 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002478 // Thus, if the template-name is actually the constructor
2479 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002480 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002481 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002482 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCallba9d8532010-04-13 06:39:49 +00002483 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002484 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002485 if (isConstructorDeclarator()) {
2486 // The user meant this to be an out-of-line constructor
2487 // definition, but template arguments are not allowed
2488 // there. Just allow this as a constructor; we'll
2489 // complain about it later.
2490 goto DoneWithDeclSpec;
2491 }
2492
2493 // The user meant this to name a type, but it actually names
2494 // a constructor with some extraneous template
2495 // arguments. Complain, then parse it as a type as the user
2496 // intended.
2497 Diag(TemplateId->TemplateNameLoc,
2498 diag::err_out_of_line_template_id_names_constructor)
2499 << TemplateId->Name;
2500 }
2501
John McCallaa87d332009-12-12 11:40:51 +00002502 DS.getTypeSpecScope() = SS;
2503 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002504 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002505 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002506 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002507 continue;
2508 }
2509
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002510 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002511 DS.getTypeSpecScope() = SS;
2512 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002513 if (Tok.getAnnotationValue()) {
2514 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002515 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002516 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002517 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002518 if (isInvalid)
2519 break;
John McCallb3d87482010-08-24 05:47:05 +00002520 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002521 else
2522 DS.SetTypeSpecError();
2523 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2524 ConsumeToken(); // The typename
2525 }
2526
Douglas Gregor9135c722009-03-25 15:40:00 +00002527 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002528 goto DoneWithDeclSpec;
2529
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002530 // If we're in a context where the identifier could be a class name,
2531 // check whether this is a constructor declaration.
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002532 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002533 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002534 &SS)) {
2535 if (isConstructorDeclarator())
2536 goto DoneWithDeclSpec;
2537
2538 // As noted in C++ [class.qual]p2 (cited above), when the name
2539 // of the class is qualified in a context where it could name
2540 // a constructor, its a constructor name. However, we've
2541 // looked at the declarator, and the user probably meant this
2542 // to be a type. Complain that it isn't supposed to be treated
2543 // as a type, then proceed to parse it as a type.
2544 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2545 << Next.getIdentifierInfo();
2546 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002547
John McCallb3d87482010-08-24 05:47:05 +00002548 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2549 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002550 getCurScope(), &SS,
2551 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002552 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002553 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002554
Chris Lattnerf4382f52009-04-14 22:17:06 +00002555 // If the referenced identifier is not a type, then this declspec is
2556 // erroneous: We already checked about that it has no type specifier, and
2557 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002558 // typename.
David Blaikie7247c882013-05-15 07:37:26 +00002559 if (!TypeRep) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00002560 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002561 ParsedAttributesWithRange Attrs(AttrFactory);
2562 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2563 if (!Attrs.empty()) {
2564 AttrsLastTime = true;
2565 attrs.takeAllFrom(Attrs);
2566 }
2567 continue;
2568 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002569 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002570 }
Mike Stump1eb44332009-09-09 15:08:12 +00002571
John McCallaa87d332009-12-12 11:40:51 +00002572 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002573 ConsumeToken(); // The C++ scope.
2574
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002575 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002576 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002577 if (isInvalid)
2578 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002579
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002580 DS.SetRangeEnd(Tok.getLocation());
2581 ConsumeToken(); // The typename.
2582
2583 continue;
2584 }
Mike Stump1eb44332009-09-09 15:08:12 +00002585
Chris Lattner80d0c892009-01-21 19:48:37 +00002586 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002587 if (Tok.getAnnotationValue()) {
2588 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002589 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002590 DiagID, T);
2591 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002592 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002593
Chris Lattner5c5db552010-04-05 18:18:31 +00002594 if (isInvalid)
2595 break;
2596
Chris Lattner80d0c892009-01-21 19:48:37 +00002597 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2598 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002599
Chris Lattner80d0c892009-01-21 19:48:37 +00002600 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2601 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002602 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002603 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002604 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002605
Chris Lattner80d0c892009-01-21 19:48:37 +00002606 continue;
2607 }
Mike Stump1eb44332009-09-09 15:08:12 +00002608
Douglas Gregorbfad9152011-04-28 15:48:45 +00002609 case tok::kw___is_signed:
2610 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2611 // typically treats it as a trait. If we see __is_signed as it appears
2612 // in libstdc++, e.g.,
2613 //
2614 // static const bool __is_signed;
2615 //
2616 // then treat __is_signed as an identifier rather than as a keyword.
2617 if (DS.getTypeSpecType() == TST_bool &&
2618 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2619 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2620 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2621 Tok.setKind(tok::identifier);
2622 }
2623
2624 // We're done with the declaration-specifiers.
2625 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002626
Chris Lattner3bd934a2008-07-26 01:18:38 +00002627 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002628 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002629 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002630 // In C++, check to see if this is a scope specifier like foo::bar::, if
2631 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002632 if (getLangOpts().CPlusPlus) {
Eli Friedman5a428202013-08-15 23:59:20 +00002633 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall9ba61662010-02-26 08:45:28 +00002634 if (!DS.hasTypeSpecifier())
2635 DS.SetTypeSpecError();
2636 goto DoneWithDeclSpec;
2637 }
2638 if (!Tok.is(tok::identifier))
2639 continue;
2640 }
Mike Stump1eb44332009-09-09 15:08:12 +00002641
Chris Lattner3bd934a2008-07-26 01:18:38 +00002642 // This identifier can only be a typedef name if we haven't already seen
2643 // a type-specifier. Without this check we misparse:
2644 // typedef int X; struct Y { short X; }; as 'short int'.
2645 if (DS.hasTypeSpecifier())
2646 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002647
John Thompson82287d12010-02-05 00:12:22 +00002648 // Check for need to substitute AltiVec keyword tokens.
2649 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2650 break;
2651
Richard Smithf63eee72012-05-09 18:56:43 +00002652 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2653 // allow the use of a typedef name as a type specifier.
2654 if (DS.isTypeAltiVecVector())
2655 goto DoneWithDeclSpec;
2656
John McCallb3d87482010-08-24 05:47:05 +00002657 ParsedType TypeRep =
2658 Actions.getTypeName(*Tok.getIdentifierInfo(),
2659 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002660
Chris Lattnerc199ab32009-04-12 20:42:31 +00002661 // If this is not a typedef name, don't parse it as part of the declspec,
2662 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002663 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002664 ParsedAttributesWithRange Attrs(AttrFactory);
2665 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2666 if (!Attrs.empty()) {
2667 AttrsLastTime = true;
2668 attrs.takeAllFrom(Attrs);
2669 }
2670 continue;
2671 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002672 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002673 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002674
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002675 // If we're in a context where the identifier could be a class name,
2676 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002677 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002678 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002679 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002680 goto DoneWithDeclSpec;
2681
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002682 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002683 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002684 if (isInvalid)
2685 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002686
Chris Lattner3bd934a2008-07-26 01:18:38 +00002687 DS.SetRangeEnd(Tok.getLocation());
2688 ConsumeToken(); // The identifier
2689
2690 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2691 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002692 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002693 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002694 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002695
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002696 // Need to support trailing type qualifiers (e.g. "id<p> const").
2697 // If a type specifier follows, it will be diagnosed elsewhere.
2698 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002699 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002700
2701 // type-name
2702 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002703 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002704 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002705 // This template-id does not refer to a type name, so we're
2706 // done with the type-specifiers.
2707 goto DoneWithDeclSpec;
2708 }
2709
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002710 // If we're in a context where the template-id could be a
2711 // constructor name or specialization, check whether this is a
2712 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002713 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002714 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002715 isConstructorDeclarator())
2716 goto DoneWithDeclSpec;
2717
Douglas Gregor39a8de12009-02-25 19:37:18 +00002718 // Turn the template-id annotation token into a type annotation
2719 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002720 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002721 continue;
2722 }
2723
Reid Spencer5f016e22007-07-11 17:01:13 +00002724 // GNU attributes support.
2725 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002726 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002727 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002728
2729 // Microsoft declspec support.
2730 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002731 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002732 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002733
Steve Naroff239f0732008-12-25 14:16:32 +00002734 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002735 case tok::kw___forceinline: {
Chad Rosier22aa6902012-12-21 22:24:43 +00002736 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002737 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002738 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002739 // FIXME: This does not work correctly if it is set to be a declspec
2740 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman624421f2013-08-31 01:11:41 +00002741 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2742 AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002743 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002744 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002745
Aaron Ballmanaa9df092013-05-22 23:25:32 +00002746 case tok::kw___sptr:
2747 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00002748 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002749 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002750 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002751 case tok::kw___cdecl:
2752 case tok::kw___stdcall:
2753 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002754 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002755 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002756 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002757 continue;
2758
Dawn Perchik52fc3142010-09-03 01:29:35 +00002759 // Borland single token adornments.
2760 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002761 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002762 continue;
2763
Peter Collingbournef315fa82011-02-14 01:42:53 +00002764 // OpenCL single token adornments.
2765 case tok::kw___kernel:
2766 ParseOpenCLAttributes(DS.getAttributes());
2767 continue;
2768
Reid Spencer5f016e22007-07-11 17:01:13 +00002769 // storage-class-specifier
2770 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002771 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2772 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002773 break;
2774 case tok::kw_extern:
Richard Smithec642442013-04-12 22:46:28 +00002775 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner1ab3b962008-11-18 07:48:38 +00002776 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002777 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2778 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002779 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002780 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002781 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2782 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002783 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002784 case tok::kw_static:
Richard Smithec642442013-04-12 22:46:28 +00002785 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner1ab3b962008-11-18 07:48:38 +00002786 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002787 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2788 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002789 break;
2790 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002791 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002792 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002793 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2794 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002795 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002796 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002797 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002798 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002799 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2800 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002801 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002802 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2803 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002804 break;
2805 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002806 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2807 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002808 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002809 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002810 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2811 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002812 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002813 case tok::kw___thread:
Richard Smithec642442013-04-12 22:46:28 +00002814 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2815 PrevSpec, DiagID);
2816 break;
2817 case tok::kw_thread_local:
2818 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2819 PrevSpec, DiagID);
2820 break;
2821 case tok::kw__Thread_local:
2822 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2823 Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002824 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002825
Reid Spencer5f016e22007-07-11 17:01:13 +00002826 // function-specifier
2827 case tok::kw_inline:
Chad Rosier22aa6902012-12-21 22:24:43 +00002828 isInvalid = DS.setFunctionSpecInline(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002829 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002830 case tok::kw_virtual:
Chad Rosier22aa6902012-12-21 22:24:43 +00002831 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002832 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002833 case tok::kw_explicit:
Chad Rosier22aa6902012-12-21 22:24:43 +00002834 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002835 break;
Richard Smithde03c152013-01-17 22:16:11 +00002836 case tok::kw__Noreturn:
2837 if (!getLangOpts().C11)
2838 Diag(Loc, diag::ext_c11_noreturn);
2839 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2840 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002841
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002842 // alignment-specifier
2843 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002844 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002845 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002846 ParseAlignmentSpecifier(DS.getAttributes());
2847 continue;
2848
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002849 // friend
2850 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002851 if (DSContext == DSC_class)
2852 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2853 else {
2854 PrevSpec = ""; // not actually used by the diagnostic
2855 DiagID = diag::err_friend_invalid_in_context;
2856 isInvalid = true;
2857 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002858 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002859
Douglas Gregor8d267c52011-09-09 02:06:17 +00002860 // Modules
2861 case tok::kw___module_private__:
2862 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2863 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002864
Sebastian Redl2ac67232009-11-05 15:47:02 +00002865 // constexpr
2866 case tok::kw_constexpr:
2867 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2868 break;
2869
Chris Lattner80d0c892009-01-21 19:48:37 +00002870 // type-specifier
2871 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002872 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2873 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002874 break;
2875 case tok::kw_long:
2876 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002877 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2878 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002879 else
John McCallfec54012009-08-03 20:12:06 +00002880 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2881 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002882 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002883 case tok::kw___int64:
2884 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2885 DiagID);
2886 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002887 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002888 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2889 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002890 break;
2891 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002892 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2893 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002894 break;
2895 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002896 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2897 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002898 break;
2899 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002900 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2901 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002902 break;
2903 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002904 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2905 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002906 break;
2907 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002908 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2909 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002910 break;
2911 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002912 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2913 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002914 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002915 case tok::kw___int128:
2916 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2917 DiagID);
2918 break;
2919 case tok::kw_half:
2920 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2921 DiagID);
2922 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002923 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002924 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2925 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002926 break;
2927 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002928 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2929 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002930 break;
2931 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002932 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2933 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002934 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002935 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002936 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2937 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002938 break;
2939 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002940 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2941 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002942 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002943 case tok::kw_bool:
2944 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002945 if (Tok.is(tok::kw_bool) &&
2946 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2947 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2948 PrevSpec = ""; // Not used by the diagnostic.
2949 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002950 // For better error recovery.
2951 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002952 isInvalid = true;
2953 } else {
2954 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2955 DiagID);
2956 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002957 break;
2958 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002959 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2960 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002961 break;
2962 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002963 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2964 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002965 break;
2966 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002967 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2968 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002969 break;
John Thompson82287d12010-02-05 00:12:22 +00002970 case tok::kw___vector:
2971 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2972 break;
2973 case tok::kw___pixel:
2974 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2975 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00002976 case tok::kw_image1d_t:
2977 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2978 PrevSpec, DiagID);
2979 break;
2980 case tok::kw_image1d_array_t:
2981 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2982 PrevSpec, DiagID);
2983 break;
2984 case tok::kw_image1d_buffer_t:
2985 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2986 PrevSpec, DiagID);
2987 break;
2988 case tok::kw_image2d_t:
2989 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2990 PrevSpec, DiagID);
2991 break;
2992 case tok::kw_image2d_array_t:
2993 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2994 PrevSpec, DiagID);
2995 break;
2996 case tok::kw_image3d_t:
2997 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2998 PrevSpec, DiagID);
2999 break;
Guy Benyei21f18c42013-02-07 10:55:47 +00003000 case tok::kw_sampler_t:
3001 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
3002 PrevSpec, DiagID);
3003 break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00003004 case tok::kw_event_t:
3005 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
3006 PrevSpec, DiagID);
3007 break;
John McCalla5fc4722011-04-09 22:50:59 +00003008 case tok::kw___unknown_anytype:
3009 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3010 PrevSpec, DiagID);
3011 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00003012
3013 // class-specifier:
3014 case tok::kw_class:
3015 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003016 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00003017 case tok::kw_union: {
3018 tok::TokenKind Kind = Tok.getKind();
3019 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00003020
3021 // These are attributes following class specifiers.
3022 // To produce better diagnostic, we parse them when
3023 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00003024 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00003025 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00003026 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00003027
3028 // If there are attributes following class specifier,
3029 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00003030 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00003031 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00003032 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00003033 }
Chris Lattner80d0c892009-01-21 19:48:37 +00003034 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00003035 }
Chris Lattner80d0c892009-01-21 19:48:37 +00003036
3037 // enum-specifier:
3038 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00003039 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00003040 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00003041 continue;
3042
3043 // cv-qualifier:
3044 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003045 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003046 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003047 break;
3048 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003049 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003050 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003051 break;
3052 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003053 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003054 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003055 break;
3056
Douglas Gregord57959a2009-03-27 23:10:48 +00003057 // C++ typename-specifier:
3058 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00003059 if (TryAnnotateTypeOrScopeToken()) {
3060 DS.SetTypeSpecError();
3061 goto DoneWithDeclSpec;
3062 }
3063 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00003064 continue;
3065 break;
3066
Chris Lattner80d0c892009-01-21 19:48:37 +00003067 // GNU typeof support.
3068 case tok::kw_typeof:
3069 ParseTypeofSpecifier(DS);
3070 continue;
3071
David Blaikie42d6d0c2011-12-04 05:04:18 +00003072 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00003073 ParseDecltypeSpecifier(DS);
3074 continue;
3075
Sean Huntdb5d44b2011-05-19 05:37:45 +00003076 case tok::kw___underlying_type:
3077 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00003078 continue;
3079
3080 case tok::kw__Atomic:
Richard Smith4cf4a5e2013-03-28 01:55:44 +00003081 // C11 6.7.2.4/4:
3082 // If the _Atomic keyword is immediately followed by a left parenthesis,
3083 // it is interpreted as a type specifier (with a type name), not as a
3084 // type qualifier.
3085 if (NextToken().is(tok::l_paren)) {
3086 ParseAtomicSpecifier(DS);
3087 continue;
3088 }
3089 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3090 getLangOpts());
3091 break;
Sean Huntdb5d44b2011-05-19 05:37:45 +00003092
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003093 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00003094 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003095 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003096 goto DoneWithDeclSpec;
3097 case tok::kw___private:
3098 case tok::kw___global:
3099 case tok::kw___local:
3100 case tok::kw___constant:
3101 case tok::kw___read_only:
3102 case tok::kw___write_only:
3103 case tok::kw___read_write:
3104 ParseOpenCLQualifiers(DS);
3105 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00003106
Steve Naroffd3ded1f2008-06-05 00:02:44 +00003107 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00003108 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00003109 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3110 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00003111 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00003112 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00003113
Douglas Gregor46f936e2010-11-19 17:10:50 +00003114 if (!ParseObjCProtocolQualifiers(DS))
3115 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3116 << FixItHint::CreateInsertion(Loc, "id")
3117 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00003118
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00003119 // Need to support trailing type qualifiers (e.g. "id<p> const").
3120 // If a type specifier follows, it will be diagnosed elsewhere.
3121 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00003122 }
John McCallfec54012009-08-03 20:12:06 +00003123 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00003124 if (isInvalid) {
3125 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00003126 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00003127
Douglas Gregorae2fb142010-08-23 14:34:43 +00003128 if (DiagID == diag::ext_duplicate_declspec)
3129 Diag(Tok, DiagID)
3130 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3131 else
3132 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003133 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00003134
Chris Lattner81c018d2008-03-13 06:29:04 +00003135 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00003136 if (DiagID != diag::err_bool_redeclaration)
3137 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00003138
3139 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003140 }
3141}
Douglas Gregoradcac882008-12-01 23:54:00 +00003142
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003143/// ParseStructDeclaration - Parse a struct declaration without the terminating
3144/// semicolon.
3145///
Reid Spencer5f016e22007-07-11 17:01:13 +00003146/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003147/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00003148/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003149/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00003150/// struct-declarator-list:
3151/// struct-declarator
3152/// struct-declarator-list ',' struct-declarator
3153/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3154/// struct-declarator:
3155/// declarator
3156/// [GNU] declarator attributes[opt]
3157/// declarator[opt] ':' constant-expression
3158/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3159///
Chris Lattnere1359422008-04-10 06:46:29 +00003160void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003161ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003162
Chris Lattnerc46d1a12008-10-20 06:45:43 +00003163 if (Tok.is(tok::kw___extension__)) {
3164 // __extension__ silences extension warnings in the subexpression.
3165 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00003166 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00003167 return ParseStructDeclaration(DS, Fields);
3168 }
Mike Stump1eb44332009-09-09 15:08:12 +00003169
Steve Naroff28a7ca82007-08-20 22:28:22 +00003170 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00003171 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003172
Douglas Gregor4920f1f2009-01-12 22:49:06 +00003173 // If there are no declarators, this is a free-standing declaration
3174 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00003175 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003176 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3177 DS);
3178 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00003179 return;
3180 }
3181
3182 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00003183 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00003184 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003185 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003186 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00003187 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00003188
Bill Wendlingad017fa2012-12-20 19:22:21 +00003189 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00003190 if (!FirstDeclarator)
3191 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00003192
Steve Naroff28a7ca82007-08-20 22:28:22 +00003193 /// struct-declarator: declarator
3194 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003195 if (Tok.isNot(tok::colon)) {
3196 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3197 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00003198 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003199 }
Mike Stump1eb44332009-09-09 15:08:12 +00003200
Chris Lattner04d66662007-10-09 17:33:22 +00003201 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00003202 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00003203 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003204 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00003205 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00003206 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00003207 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00003208 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003209
Steve Naroff28a7ca82007-08-20 22:28:22 +00003210 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003211 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003212
John McCallbdd563e2009-11-03 02:38:08 +00003213 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00003214 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00003215
Steve Naroff28a7ca82007-08-20 22:28:22 +00003216 // If we don't have a comma, it is either the end of the list (a ';')
3217 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00003218 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003219 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00003220
Steve Naroff28a7ca82007-08-20 22:28:22 +00003221 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00003222 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003223
John McCallbdd563e2009-11-03 02:38:08 +00003224 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003225 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00003226}
3227
3228/// ParseStructUnionBody
3229/// struct-contents:
3230/// struct-declaration-list
3231/// [EXT] empty
3232/// [GNU] "struct-declaration-list" without terminatoring ';'
3233/// struct-declaration-list:
3234/// struct-declaration
3235/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003236/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003237///
Reid Spencer5f016e22007-07-11 17:01:13 +00003238void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003239 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003240 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3241 "parsing struct/union body");
Andy Gibbsf50f3f72013-04-03 09:31:19 +00003242 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump1eb44332009-09-09 15:08:12 +00003243
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003244 BalancedDelimiterTracker T(*this, tok::l_brace);
3245 if (T.consumeOpen())
3246 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003247
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003248 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003249 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003250
Chris Lattner5f9e2722011-07-23 10:55:15 +00003251 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003252
Reid Spencer5f016e22007-07-11 17:01:13 +00003253 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003254 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003255 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003256
Reid Spencer5f016e22007-07-11 17:01:13 +00003257 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003258 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003259 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003260 continue;
3261 }
Chris Lattnere1359422008-04-10 06:46:29 +00003262
Andy Gibbs74b9fa12013-04-03 09:46:04 +00003263 // Parse _Static_assert declaration.
3264 if (Tok.is(tok::kw__Static_assert)) {
3265 SourceLocation DeclEnd;
3266 ParseStaticAssertDeclaration(DeclEnd);
3267 continue;
3268 }
3269
Argyrios Kyrtzidisbd957452013-04-18 01:42:35 +00003270 if (Tok.is(tok::annot_pragma_pack)) {
3271 HandlePragmaPack();
3272 continue;
3273 }
3274
3275 if (Tok.is(tok::annot_pragma_align)) {
3276 HandlePragmaAlign();
3277 continue;
3278 }
3279
John McCallbdd563e2009-11-03 02:38:08 +00003280 if (!Tok.is(tok::at)) {
3281 struct CFieldCallback : FieldCallback {
3282 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003283 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003284 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003285
John McCalld226f652010-08-21 09:40:31 +00003286 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003287 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003288 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3289
Eli Friedmandcdff462012-08-08 23:53:27 +00003290 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003291 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003292 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003293 FD.D.getDeclSpec().getSourceRange().getBegin(),
3294 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003295 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003296 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003297 }
John McCallbdd563e2009-11-03 02:38:08 +00003298 } Callback(*this, TagDecl, FieldDecls);
3299
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003300 // Parse all the comma separated declarators.
3301 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003302 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003303 } else { // Handle @defs
3304 ConsumeToken();
3305 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3306 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003307 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003308 continue;
3309 }
3310 ConsumeToken();
3311 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3312 if (!Tok.is(tok::identifier)) {
3313 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003314 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003315 continue;
3316 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003317 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003318 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003319 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003320 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3321 ConsumeToken();
3322 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003323 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003324
Chris Lattner04d66662007-10-09 17:33:22 +00003325 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003326 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003327 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003328 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003329 break;
3330 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003331 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3332 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003333 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003334 // If we stopped at a ';', eat it.
3335 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003336 }
3337 }
Mike Stump1eb44332009-09-09 15:08:12 +00003338
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003339 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003340
John McCall0b7e6782011-03-24 11:26:52 +00003341 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003342 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003343 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003344
Douglas Gregor23c94db2010-07-02 17:43:08 +00003345 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003346 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003347 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003348 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003349 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003350 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3351 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003352}
3353
Reid Spencer5f016e22007-07-11 17:01:13 +00003354/// ParseEnumSpecifier
3355/// enum-specifier: [C99 6.7.2.2]
3356/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003357///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003358/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3359/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003360/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3361/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003362/// 'enum' identifier
3363/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003364///
Richard Smith1af83c42012-03-23 03:33:32 +00003365/// [C++11] enum-head '{' enumerator-list[opt] '}'
3366/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003367///
Richard Smith1af83c42012-03-23 03:33:32 +00003368/// enum-head: [C++11]
3369/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3370/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3371/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003372///
Richard Smith1af83c42012-03-23 03:33:32 +00003373/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003374/// 'enum'
3375/// 'enum' 'class'
3376/// 'enum' 'struct'
3377///
Richard Smith1af83c42012-03-23 03:33:32 +00003378/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003379/// ':' type-specifier-seq
3380///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003381/// [C++] elaborated-type-specifier:
3382/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3383///
Chris Lattner4c97d762009-04-12 21:49:30 +00003384void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003385 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003386 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003387 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003388 if (Tok.is(tok::code_completion)) {
3389 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003390 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003391 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003392 }
John McCall57c13002011-07-06 05:58:41 +00003393
Sean Hunt2edf0a22012-06-23 05:07:58 +00003394 // If attributes exist after tag, parse them.
3395 ParsedAttributesWithRange attrs(AttrFactory);
3396 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003397 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003398
3399 // If declspecs exist after tag, parse them.
3400 while (Tok.is(tok::kw___declspec))
3401 ParseMicrosoftDeclSpec(attrs);
3402
Richard Smithbdad7a22012-01-10 01:33:14 +00003403 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003404 bool IsScopedUsingClassTag = false;
3405
John McCall1e12b3d2012-06-23 22:30:04 +00003406 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieued5a2922013-04-23 02:47:36 +00003407 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3408 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3409 : diag::ext_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003410 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003411 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003412
Bill Wendlingad017fa2012-12-20 19:22:21 +00003413 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003414 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003415 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003416
3417 // They are allowed afterwards, though.
3418 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003419 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003420 while (Tok.is(tok::kw___declspec))
3421 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003422 }
Richard Smith1af83c42012-03-23 03:33:32 +00003423
John McCall13489672012-05-07 06:16:58 +00003424 // C++11 [temp.explicit]p12:
3425 // The usual access controls do not apply to names used to specify
3426 // explicit instantiations.
3427 // We extend this to also cover explicit specializations. Note that
3428 // we don't suppress if this turns out to be an elaborated type
3429 // specifier.
3430 bool shouldDelayDiagsInTag =
3431 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3432 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3433 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003434
Richard Smith7796eb52012-03-12 08:56:40 +00003435 // Enum definitions should not be parsed in a trailing-return-type.
3436 bool AllowDeclaration = DSC != DSC_trailing;
3437
3438 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003439 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003440 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003441
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003442 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003443 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003444 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3445 // if a fixed underlying type is allowed.
3446 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003447
3448 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith725fe0e2013-04-01 21:43:41 +00003449 /*EnteringContext=*/true))
John McCall9ba61662010-02-26 08:45:28 +00003450 return;
3451
3452 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003453 Diag(Tok, diag::err_expected_ident);
3454 if (Tok.isNot(tok::l_brace)) {
3455 // Has no name and is not a definition.
3456 // Skip the rest of this declarator, up until the comma or semicolon.
3457 SkipUntil(tok::comma, true);
3458 return;
3459 }
3460 }
3461 }
Mike Stump1eb44332009-09-09 15:08:12 +00003462
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003463 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003464 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003465 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003466 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003467
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003468 // Skip the rest of this declarator, up until the comma or semicolon.
3469 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003470 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003471 }
Mike Stump1eb44332009-09-09 15:08:12 +00003472
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003473 // If an identifier is present, consume and remember it.
3474 IdentifierInfo *Name = 0;
3475 SourceLocation NameLoc;
3476 if (Tok.is(tok::identifier)) {
3477 Name = Tok.getIdentifierInfo();
3478 NameLoc = ConsumeToken();
3479 }
Mike Stump1eb44332009-09-09 15:08:12 +00003480
Richard Smithbdad7a22012-01-10 01:33:14 +00003481 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003482 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3483 // declaration of a scoped enumeration.
3484 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003485 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003486 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003487 }
3488
John McCall13489672012-05-07 06:16:58 +00003489 // Okay, end the suppression area. We'll decide whether to emit the
3490 // diagnostics in a second.
3491 if (shouldDelayDiagsInTag)
3492 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003493
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003494 TypeResult BaseType;
3495
Douglas Gregora61b3e72010-12-01 17:42:47 +00003496 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003497 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003498 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003499 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003500 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003501 // If we're in class scope, this can either be an enum declaration with
3502 // an underlying type, or a declaration of a bitfield member. We try to
3503 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003504 // (integer literal, sizeof); if it's still ambiguous, we then consider
3505 // anything that's a simple-type-specifier followed by '(' as an
3506 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003507 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003508 EnterExpressionEvaluationContext Unevaluated(Actions,
3509 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003510 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003511 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003512 // bit-field. This is the common case.
3513 if (TPR == TPResult::True())
3514 PossibleBitfield = true;
3515 // If the next token starts a type-specifier-seq, it may be either a
3516 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003517 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003518 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003519 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003520 GetLookAheadToken(2).getKind() == tok::semi) {
3521 // Consume the ':'.
3522 ConsumeToken();
3523 } else {
3524 // We have the start of a type-specifier-seq, so we have to perform
3525 // tentative parsing to determine whether we have an expression or a
3526 // type.
3527 TentativeParsingAction TPA(*this);
3528
3529 // Consume the ':'.
3530 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003531
3532 // If we see a type specifier followed by an open-brace, we have an
3533 // ambiguity between an underlying type and a C++11 braced
3534 // function-style cast. Resolve this by always treating it as an
3535 // underlying type.
3536 // FIXME: The standard is not entirely clear on how to disambiguate in
3537 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003538 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003539 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003540 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003541 // We'll parse this as a bitfield later.
3542 PossibleBitfield = true;
3543 TPA.Revert();
3544 } else {
3545 // We have a type-specifier-seq.
3546 TPA.Commit();
3547 }
3548 }
3549 } else {
3550 // Consume the ':'.
3551 ConsumeToken();
3552 }
3553
3554 if (!PossibleBitfield) {
3555 SourceRange Range;
3556 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003557
Richard Smith80ad52f2013-01-02 11:42:31 +00003558 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003559 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003560 } else if (!getLangOpts().ObjC2) {
3561 if (getLangOpts().CPlusPlus)
3562 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3563 else
3564 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3565 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003566 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003567 }
3568
Richard Smithbdad7a22012-01-10 01:33:14 +00003569 // There are four options here. If we have 'friend enum foo;' then this is a
3570 // friend declaration, and cannot have an accompanying definition. If we have
3571 // 'enum foo;', then this is a forward declaration. If we have
3572 // 'enum foo {...' then this is a definition. Otherwise we have something
3573 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003574 //
3575 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3576 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3577 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3578 //
John McCallf312b1e2010-08-26 23:41:50 +00003579 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003580 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003581 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003582 } else if (Tok.is(tok::l_brace)) {
3583 if (DS.isFriendSpecified()) {
3584 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3585 << SourceRange(DS.getFriendSpecLoc());
3586 ConsumeBrace();
3587 SkipUntil(tok::r_brace);
3588 TUK = Sema::TUK_Friend;
3589 } else {
3590 TUK = Sema::TUK_Definition;
3591 }
Richard Smithc9f35172012-06-25 21:37:02 +00003592 } else if (DSC != DSC_type_specifier &&
3593 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003594 (Tok.isAtStartOfLine() &&
3595 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003596 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3597 if (Tok.isNot(tok::semi)) {
3598 // A semicolon was missing after this declaration. Diagnose and recover.
3599 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3600 "enum");
3601 PP.EnterToken(Tok);
3602 Tok.setKind(tok::semi);
3603 }
John McCall13489672012-05-07 06:16:58 +00003604 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003605 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003606 }
3607
3608 // If this is an elaborated type specifier, and we delayed
3609 // diagnostics before, just merge them into the current pool.
3610 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3611 diagsFromTag.redelay();
3612 }
Richard Smith1af83c42012-03-23 03:33:32 +00003613
3614 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003615 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003616 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003617 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003618 // Skip the rest of this declarator, up until the comma or semicolon.
3619 Diag(Tok, diag::err_enum_template);
3620 SkipUntil(tok::comma, true);
3621 return;
3622 }
3623
3624 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3625 // Enumerations can't be explicitly instantiated.
3626 DS.SetTypeSpecError();
3627 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3628 return;
3629 }
3630
3631 assert(TemplateInfo.TemplateParams && "no template parameters");
3632 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3633 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003634 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003635
Sean Hunt2edf0a22012-06-23 05:07:58 +00003636 if (TUK == Sema::TUK_Reference)
3637 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003638
Douglas Gregorb9075602011-02-22 02:55:24 +00003639 if (!Name && TUK != Sema::TUK_Definition) {
3640 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003641
Douglas Gregorb9075602011-02-22 02:55:24 +00003642 // Skip the rest of this declarator, up until the comma or semicolon.
3643 SkipUntil(tok::comma, true);
3644 return;
3645 }
Richard Smith1af83c42012-03-23 03:33:32 +00003646
Douglas Gregor402abb52009-05-28 23:31:59 +00003647 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003648 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003649 const char *PrevSpec = 0;
3650 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003651 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003652 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003653 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003654 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003655 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003656
Douglas Gregor48c89f42010-04-24 16:38:41 +00003657 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003658 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003659 // dependent tag.
3660 if (!Name) {
3661 DS.SetTypeSpecError();
3662 Diag(Tok, diag::err_expected_type_name_after_typename);
3663 return;
3664 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003665
Douglas Gregor23c94db2010-07-02 17:43:08 +00003666 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003667 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003668 NameLoc);
3669 if (Type.isInvalid()) {
3670 DS.SetTypeSpecError();
3671 return;
3672 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003673
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003674 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3675 NameLoc.isValid() ? NameLoc : StartLoc,
3676 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003677 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003678
Douglas Gregor48c89f42010-04-24 16:38:41 +00003679 return;
3680 }
Mike Stump1eb44332009-09-09 15:08:12 +00003681
John McCalld226f652010-08-21 09:40:31 +00003682 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003683 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003684 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003685 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003686 ConsumeBrace();
3687 SkipUntil(tok::r_brace);
3688 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003689
Douglas Gregor48c89f42010-04-24 16:38:41 +00003690 DS.SetTypeSpecError();
3691 return;
3692 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003693
Richard Smithc9f35172012-06-25 21:37:02 +00003694 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003695 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003696
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003697 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3698 NameLoc.isValid() ? NameLoc : StartLoc,
3699 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003700 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003701}
3702
3703/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3704/// enumerator-list:
3705/// enumerator
3706/// enumerator-list ',' enumerator
3707/// enumerator:
3708/// enumeration-constant
3709/// enumeration-constant '=' constant-expression
3710/// enumeration-constant:
3711/// identifier
3712///
John McCalld226f652010-08-21 09:40:31 +00003713void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003714 // Enter the scope of the enum body and start the definition.
3715 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003716 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003717
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003718 BalancedDelimiterTracker T(*this, tok::l_brace);
3719 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003720
Chris Lattner7946dd32007-08-27 17:24:30 +00003721 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003722 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003723 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003724
Chris Lattner5f9e2722011-07-23 10:55:15 +00003725 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003726
John McCalld226f652010-08-21 09:40:31 +00003727 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003728
Reid Spencer5f016e22007-07-11 17:01:13 +00003729 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003730 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003731 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3732 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003733
John McCall5b629aa2010-10-22 23:36:17 +00003734 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003735 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003736 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003737 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003738 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003739
Reid Spencer5f016e22007-07-11 17:01:13 +00003740 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003741 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003742 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003743
Chris Lattner04d66662007-10-09 17:33:22 +00003744 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003745 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003746 AssignedVal = ParseConstantExpression();
3747 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003748 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003749 }
Mike Stump1eb44332009-09-09 15:08:12 +00003750
Reid Spencer5f016e22007-07-11 17:01:13 +00003751 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003752 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3753 LastEnumConstDecl,
3754 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003755 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003756 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003757 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003758
Reid Spencer5f016e22007-07-11 17:01:13 +00003759 EnumConstantDecls.push_back(EnumConstDecl);
3760 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003761
Douglas Gregor751f6922010-09-07 14:51:08 +00003762 if (Tok.is(tok::identifier)) {
3763 // We're missing a comma between enumerators.
3764 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003765 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003766 << FixItHint::CreateInsertion(Loc, ", ");
3767 continue;
3768 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003769
Chris Lattner04d66662007-10-09 17:33:22 +00003770 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003771 break;
3772 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003773
Richard Smith7fe62082011-10-15 05:09:34 +00003774 if (Tok.isNot(tok::identifier)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003775 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003776 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3777 diag::ext_enumerator_list_comma_cxx :
3778 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003779 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003780 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003781 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3782 << FixItHint::CreateRemoval(CommaLoc);
3783 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003784 }
Mike Stump1eb44332009-09-09 15:08:12 +00003785
Reid Spencer5f016e22007-07-11 17:01:13 +00003786 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003787 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003788
Reid Spencer5f016e22007-07-11 17:01:13 +00003789 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003790 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003791 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003792
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003793 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenko9ff2b422013-04-27 20:23:52 +00003794 EnumDecl, EnumConstantDecls,
3795 getCurScope(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003796 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003797
Douglas Gregor72de6672009-01-08 20:45:30 +00003798 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003799 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3800 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003801
3802 // The next token must be valid after an enum definition. If not, a ';'
3803 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003804 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3805 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003806 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3807 // Push this token back into the preprocessor and change our current token
3808 // to ';' so that the rest of the code recovers as though there were an
3809 // ';' after the definition.
3810 PP.EnterToken(Tok);
3811 Tok.setKind(tok::semi);
3812 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003813}
3814
3815/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003816/// start of a type-qualifier-list.
3817bool Parser::isTypeQualifier() const {
3818 switch (Tok.getKind()) {
3819 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003820
3821 // type-qualifier only in OpenCL
3822 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003823 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003824
Steve Naroff5f8aa692008-02-11 23:15:56 +00003825 // type-qualifier
3826 case tok::kw_const:
3827 case tok::kw_volatile:
3828 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003829 case tok::kw___private:
3830 case tok::kw___local:
3831 case tok::kw___global:
3832 case tok::kw___constant:
3833 case tok::kw___read_only:
3834 case tok::kw___read_write:
3835 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003836 return true;
3837 }
3838}
3839
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003840/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3841/// is definitely a type-specifier. Return false if it isn't part of a type
3842/// specifier or if we're not sure.
3843bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3844 switch (Tok.getKind()) {
3845 default: return false;
3846 // type-specifiers
3847 case tok::kw_short:
3848 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003849 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003850 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003851 case tok::kw_signed:
3852 case tok::kw_unsigned:
3853 case tok::kw__Complex:
3854 case tok::kw__Imaginary:
3855 case tok::kw_void:
3856 case tok::kw_char:
3857 case tok::kw_wchar_t:
3858 case tok::kw_char16_t:
3859 case tok::kw_char32_t:
3860 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003861 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003862 case tok::kw_float:
3863 case tok::kw_double:
3864 case tok::kw_bool:
3865 case tok::kw__Bool:
3866 case tok::kw__Decimal32:
3867 case tok::kw__Decimal64:
3868 case tok::kw__Decimal128:
3869 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003870
Guy Benyeib13621d2012-12-18 14:38:23 +00003871 // OpenCL specific types:
3872 case tok::kw_image1d_t:
3873 case tok::kw_image1d_array_t:
3874 case tok::kw_image1d_buffer_t:
3875 case tok::kw_image2d_t:
3876 case tok::kw_image2d_array_t:
3877 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003878 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003879 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003880
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003881 // struct-or-union-specifier (C99) or class-specifier (C++)
3882 case tok::kw_class:
3883 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003884 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003885 case tok::kw_union:
3886 // enum-specifier
3887 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003888
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003889 // typedef-name
3890 case tok::annot_typename:
3891 return true;
3892 }
3893}
3894
Steve Naroff5f8aa692008-02-11 23:15:56 +00003895/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003896/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003897bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003898 switch (Tok.getKind()) {
3899 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003900
Chris Lattner166a8fc2009-01-04 23:41:41 +00003901 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003902 if (TryAltiVecVectorToken())
3903 return true;
3904 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003905 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003906 // Annotate typenames and C++ scope specifiers. If we get one, just
3907 // recurse to handle whatever we get.
3908 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003909 return true;
3910 if (Tok.is(tok::identifier))
3911 return false;
3912 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003913
Chris Lattner166a8fc2009-01-04 23:41:41 +00003914 case tok::coloncolon: // ::foo::bar
3915 if (NextToken().is(tok::kw_new) || // ::new
3916 NextToken().is(tok::kw_delete)) // ::delete
3917 return false;
3918
Chris Lattner166a8fc2009-01-04 23:41:41 +00003919 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003920 return true;
3921 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003922
Reid Spencer5f016e22007-07-11 17:01:13 +00003923 // GNU attributes support.
3924 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003925 // GNU typeof support.
3926 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003927
Reid Spencer5f016e22007-07-11 17:01:13 +00003928 // type-specifiers
3929 case tok::kw_short:
3930 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003931 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003932 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003933 case tok::kw_signed:
3934 case tok::kw_unsigned:
3935 case tok::kw__Complex:
3936 case tok::kw__Imaginary:
3937 case tok::kw_void:
3938 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003939 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003940 case tok::kw_char16_t:
3941 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003942 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003943 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003944 case tok::kw_float:
3945 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003946 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003947 case tok::kw__Bool:
3948 case tok::kw__Decimal32:
3949 case tok::kw__Decimal64:
3950 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003951 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003952
Guy Benyeib13621d2012-12-18 14:38:23 +00003953 // OpenCL specific types:
3954 case tok::kw_image1d_t:
3955 case tok::kw_image1d_array_t:
3956 case tok::kw_image1d_buffer_t:
3957 case tok::kw_image2d_t:
3958 case tok::kw_image2d_array_t:
3959 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003960 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003961 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003962
Chris Lattner99dc9142008-04-13 18:59:07 +00003963 // struct-or-union-specifier (C99) or class-specifier (C++)
3964 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003965 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003966 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003967 case tok::kw_union:
3968 // enum-specifier
3969 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003970
Reid Spencer5f016e22007-07-11 17:01:13 +00003971 // type-qualifier
3972 case tok::kw_const:
3973 case tok::kw_volatile:
3974 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003975
John McCallb8a8de32012-11-14 00:49:39 +00003976 // Debugger support.
3977 case tok::kw___unknown_anytype:
3978
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003979 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003980 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003981 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003982
Chris Lattner7c186be2008-10-20 00:25:30 +00003983 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3984 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003985 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003986
Steve Naroff239f0732008-12-25 14:16:32 +00003987 case tok::kw___cdecl:
3988 case tok::kw___stdcall:
3989 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003990 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003991 case tok::kw___w64:
3992 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003993 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003994 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003995 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003996
3997 case tok::kw___private:
3998 case tok::kw___local:
3999 case tok::kw___global:
4000 case tok::kw___constant:
4001 case tok::kw___read_only:
4002 case tok::kw___read_write:
4003 case tok::kw___write_only:
4004
Eli Friedman290eeb02009-06-08 23:27:34 +00004005 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004006
4007 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004008 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00004009
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004010 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00004011 case tok::kw__Atomic:
4012 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00004013 }
4014}
4015
4016/// isDeclarationSpecifier() - Return true if the current token is part of a
4017/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00004018///
4019/// \param DisambiguatingWithExpression True to indicate that the purpose of
4020/// this check is to disambiguate between an expression and a declaration.
4021bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004022 switch (Tok.getKind()) {
4023 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004024
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004025 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004026 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004027
Chris Lattner166a8fc2009-01-04 23:41:41 +00004028 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00004029 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00004030 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00004031 return false;
John Thompson82287d12010-02-05 00:12:22 +00004032 if (TryAltiVecVectorToken())
4033 return true;
4034 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00004035 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00004036 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00004037 // Annotate typenames and C++ scope specifiers. If we get one, just
4038 // recurse to handle whatever we get.
4039 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00004040 return true;
4041 if (Tok.is(tok::identifier))
4042 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00004043
Douglas Gregor9497a732010-09-16 01:51:54 +00004044 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00004045 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00004046 // expression is permitted, then this is probably a class message send
4047 // missing the initial '['. In this case, we won't consider this to be
4048 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00004049 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00004050 isStartOfObjCClassMessageMissingOpenBracket())
4051 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00004052
John McCall9ba61662010-02-26 08:45:28 +00004053 return isDeclarationSpecifier();
4054
Chris Lattner166a8fc2009-01-04 23:41:41 +00004055 case tok::coloncolon: // ::foo::bar
4056 if (NextToken().is(tok::kw_new) || // ::new
4057 NextToken().is(tok::kw_delete)) // ::delete
4058 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004059
Chris Lattner166a8fc2009-01-04 23:41:41 +00004060 // Annotate typenames and C++ scope specifiers. If we get one, just
4061 // recurse to handle whatever we get.
4062 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00004063 return true;
4064 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004065
Reid Spencer5f016e22007-07-11 17:01:13 +00004066 // storage-class-specifier
4067 case tok::kw_typedef:
4068 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00004069 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00004070 case tok::kw_static:
4071 case tok::kw_auto:
4072 case tok::kw_register:
4073 case tok::kw___thread:
Richard Smithec642442013-04-12 22:46:28 +00004074 case tok::kw_thread_local:
4075 case tok::kw__Thread_local:
Mike Stump1eb44332009-09-09 15:08:12 +00004076
Douglas Gregor8d267c52011-09-09 02:06:17 +00004077 // Modules
4078 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00004079
John McCallb8a8de32012-11-14 00:49:39 +00004080 // Debugger support
4081 case tok::kw___unknown_anytype:
4082
Reid Spencer5f016e22007-07-11 17:01:13 +00004083 // type-specifiers
4084 case tok::kw_short:
4085 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00004086 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00004087 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00004088 case tok::kw_signed:
4089 case tok::kw_unsigned:
4090 case tok::kw__Complex:
4091 case tok::kw__Imaginary:
4092 case tok::kw_void:
4093 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00004094 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00004095 case tok::kw_char16_t:
4096 case tok::kw_char32_t:
4097
Reid Spencer5f016e22007-07-11 17:01:13 +00004098 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004099 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00004100 case tok::kw_float:
4101 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00004102 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00004103 case tok::kw__Bool:
4104 case tok::kw__Decimal32:
4105 case tok::kw__Decimal64:
4106 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00004107 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00004108
Guy Benyeib13621d2012-12-18 14:38:23 +00004109 // OpenCL specific types:
4110 case tok::kw_image1d_t:
4111 case tok::kw_image1d_array_t:
4112 case tok::kw_image1d_buffer_t:
4113 case tok::kw_image2d_t:
4114 case tok::kw_image2d_array_t:
4115 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00004116 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00004117 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00004118
Chris Lattner99dc9142008-04-13 18:59:07 +00004119 // struct-or-union-specifier (C99) or class-specifier (C++)
4120 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00004121 case tok::kw_struct:
4122 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00004123 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00004124 // enum-specifier
4125 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00004126
Reid Spencer5f016e22007-07-11 17:01:13 +00004127 // type-qualifier
4128 case tok::kw_const:
4129 case tok::kw_volatile:
4130 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00004131
Reid Spencer5f016e22007-07-11 17:01:13 +00004132 // function-specifier
4133 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00004134 case tok::kw_virtual:
4135 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00004136 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00004137
Richard Smith4cd81c52013-01-29 09:02:09 +00004138 // alignment-specifier
4139 case tok::kw__Alignas:
4140
Richard Smith53aec2a2012-10-25 00:00:53 +00004141 // friend keyword.
4142 case tok::kw_friend:
4143
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00004144 // static_assert-declaration
4145 case tok::kw__Static_assert:
4146
Chris Lattner1ef08762007-08-09 17:01:07 +00004147 // GNU typeof support.
4148 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00004149
Chris Lattner1ef08762007-08-09 17:01:07 +00004150 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00004151 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00004152
Richard Smith53aec2a2012-10-25 00:00:53 +00004153 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00004154 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00004155 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00004156
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004157 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00004158 case tok::kw__Atomic:
4159 return true;
4160
Chris Lattnerf3948c42008-07-26 03:38:44 +00004161 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4162 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00004163 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00004164
Douglas Gregord9d75e52011-04-27 05:41:15 +00004165 // typedef-name
4166 case tok::annot_typename:
4167 return !DisambiguatingWithExpression ||
4168 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00004169
Steve Naroff47f52092009-01-06 19:34:12 +00004170 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00004171 case tok::kw___cdecl:
4172 case tok::kw___stdcall:
4173 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004174 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00004175 case tok::kw___w64:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004176 case tok::kw___sptr:
4177 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00004178 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004179 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00004180 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004181 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004182 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004183
4184 case tok::kw___private:
4185 case tok::kw___local:
4186 case tok::kw___global:
4187 case tok::kw___constant:
4188 case tok::kw___read_only:
4189 case tok::kw___read_write:
4190 case tok::kw___write_only:
4191
Eli Friedman290eeb02009-06-08 23:27:34 +00004192 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00004193 }
4194}
4195
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004196bool Parser::isConstructorDeclarator() {
4197 TentativeParsingAction TPA(*this);
4198
4199 // Parse the C++ scope specifier.
4200 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00004201 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004202 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00004203 TPA.Revert();
4204 return false;
4205 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004206
4207 // Parse the constructor name.
4208 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4209 // We already know that we have a constructor name; just consume
4210 // the token.
4211 ConsumeToken();
4212 } else {
4213 TPA.Revert();
4214 return false;
4215 }
4216
Richard Smith22592862012-03-27 23:05:05 +00004217 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004218 if (Tok.isNot(tok::l_paren)) {
4219 TPA.Revert();
4220 return false;
4221 }
4222 ConsumeParen();
4223
Richard Smith22592862012-03-27 23:05:05 +00004224 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4225 // that we have a constructor.
4226 if (Tok.is(tok::r_paren) ||
4227 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004228 TPA.Revert();
4229 return true;
4230 }
4231
Richard Smith9ec28912013-09-06 00:12:20 +00004232 // A C++11 attribute here signals that we have a constructor, and is an
4233 // attribute on the first constructor parameter.
4234 if (getLangOpts().CPlusPlus11 &&
4235 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4236 /*OuterMightBeMessageSend*/ true)) {
4237 TPA.Revert();
4238 return true;
4239 }
4240
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004241 // If we need to, enter the specified scope.
4242 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00004243 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004244 DeclScopeObj.EnterDeclaratorScope();
4245
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004246 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00004247 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004248 MaybeParseMicrosoftAttributes(Attrs);
4249
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004250 // Check whether the next token(s) are part of a declaration
4251 // specifier, in which case we have the start of a parameter and,
4252 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00004253 bool IsConstructor = false;
4254 if (isDeclarationSpecifier())
4255 IsConstructor = true;
4256 else if (Tok.is(tok::identifier) ||
4257 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4258 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4259 // This might be a parenthesized member name, but is more likely to
4260 // be a constructor declaration with an invalid argument type. Keep
4261 // looking.
4262 if (Tok.is(tok::annot_cxxscope))
4263 ConsumeToken();
4264 ConsumeToken();
4265
4266 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004267 // which must have one of the following syntactic forms (see the
4268 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004269 switch (Tok.getKind()) {
4270 case tok::l_paren:
4271 // C(X ( int));
4272 case tok::l_square:
4273 // C(X [ 5]);
4274 // C(X [ [attribute]]);
4275 case tok::coloncolon:
4276 // C(X :: Y);
4277 // C(X :: *p);
4278 case tok::r_paren:
4279 // C(X )
4280 // Assume this isn't a constructor, rather than assuming it's a
4281 // constructor with an unnamed parameter of an ill-formed type.
4282 break;
4283
4284 default:
4285 IsConstructor = true;
4286 break;
4287 }
4288 }
4289
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004290 TPA.Revert();
4291 return IsConstructor;
4292}
Reid Spencer5f016e22007-07-11 17:01:13 +00004293
4294/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004295/// type-qualifier-list: [C99 6.7.5]
4296/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004297/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004298/// [ only if VendorAttributesAllowed=true ]
4299/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004300/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004301/// [ only if VendorAttributesAllowed=true ]
4302/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004303/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004304/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004305///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004306void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4307 bool VendorAttributesAllowed,
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004308 bool CXX11AttributesAllowed,
4309 bool AtomicAllowed) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004310 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004311 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004312 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004313 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004314 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004315 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004316
4317 SourceLocation EndLoc;
4318
Reid Spencer5f016e22007-07-11 17:01:13 +00004319 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004320 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004321 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004322 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004323 SourceLocation Loc = Tok.getLocation();
4324
4325 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004326 case tok::code_completion:
4327 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004328 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004329
Reid Spencer5f016e22007-07-11 17:01:13 +00004330 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004331 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004332 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004333 break;
4334 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004335 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004336 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004337 break;
4338 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004339 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004340 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004341 break;
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004342 case tok::kw__Atomic:
4343 if (!AtomicAllowed)
4344 goto DoneWithTypeQuals;
4345 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4346 getLangOpts());
4347 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004348
4349 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004350 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004351 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004352 goto DoneWithTypeQuals;
4353 case tok::kw___private:
4354 case tok::kw___global:
4355 case tok::kw___local:
4356 case tok::kw___constant:
4357 case tok::kw___read_only:
4358 case tok::kw___write_only:
4359 case tok::kw___read_write:
4360 ParseOpenCLQualifiers(DS);
4361 break;
4362
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004363 case tok::kw___sptr:
4364 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00004365 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004366 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004367 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004368 case tok::kw___cdecl:
4369 case tok::kw___stdcall:
4370 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004371 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004372 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004373 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004374 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004375 continue;
4376 }
4377 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004378 case tok::kw___pascal:
4379 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004380 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004381 continue;
4382 }
4383 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004384 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004385 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004386 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004387 continue; // do *not* consume the next token!
4388 }
4389 // otherwise, FALL THROUGH!
4390 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004391 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004392 // If this is not a type-qualifier token, we're done reading type
4393 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004394 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004395 if (EndLoc.isValid())
4396 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004397 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004398 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004399
Reid Spencer5f016e22007-07-11 17:01:13 +00004400 // If the specifier combination wasn't legal, issue a diagnostic.
4401 if (isInvalid) {
4402 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004403 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004404 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004405 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004406 }
4407}
4408
4409
4410/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4411///
4412void Parser::ParseDeclarator(Declarator &D) {
4413 /// This implements the 'declarator' production in the C grammar, then checks
4414 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004415 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004416}
4417
Richard Smith9988f282012-03-29 01:16:42 +00004418static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4419 if (Kind == tok::star || Kind == tok::caret)
4420 return true;
4421
4422 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4423 if (!Lang.CPlusPlus)
4424 return false;
4425
4426 return Kind == tok::amp || Kind == tok::ampamp;
4427}
4428
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004429/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4430/// is parsed by the function passed to it. Pass null, and the direct-declarator
4431/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004432/// ptr-operator production.
4433///
Richard Smith0706df42011-10-19 21:33:05 +00004434/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004435/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4436/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004437///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004438/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4439/// [C] pointer[opt] direct-declarator
4440/// [C++] direct-declarator
4441/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004442///
4443/// pointer: [C99 6.7.5]
4444/// '*' type-qualifier-list[opt]
4445/// '*' type-qualifier-list[opt] pointer
4446///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004447/// ptr-operator:
4448/// '*' cv-qualifier-seq[opt]
4449/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004450/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004451/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004452/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004453/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004454void Parser::ParseDeclaratorInternal(Declarator &D,
4455 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004456 if (Diags.hasAllExtensionsSilenced())
4457 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004458
Sebastian Redlf30208a2009-01-24 21:16:55 +00004459 // C++ member pointers start with a '::' or a nested-name.
4460 // Member pointers get special handling, since there's no place for the
4461 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004462 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004463 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4464 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004465 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4466 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004467 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004468 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004469
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004470 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004471 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004472 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004473 if (D.mayHaveIdentifier())
4474 D.getCXXScopeSpec() = SS;
4475 else
4476 AnnotateScopeToken(SS, true);
4477
Sebastian Redlf30208a2009-01-24 21:16:55 +00004478 if (DirectDeclParser)
4479 (this->*DirectDeclParser)(D);
4480 return;
4481 }
4482
4483 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004484 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004485 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004486 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004487 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004488
4489 // Recurse to parse whatever is left.
4490 ParseDeclaratorInternal(D, DirectDeclParser);
4491
4492 // Sema will have to catch (syntactically invalid) pointers into global
4493 // scope. It has to catch pointers into namespace scope anyway.
4494 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004495 Loc),
4496 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004497 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004498 return;
4499 }
4500 }
4501
4502 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004503 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004504 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004505 if (DirectDeclParser)
4506 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004507 return;
4508 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004509
Sebastian Redl05532f22009-03-15 22:02:01 +00004510 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4511 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004512 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004513 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004514
Chris Lattner9af55002009-03-27 04:18:06 +00004515 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004516 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004517 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004518
Richard Smith6ee326a2012-04-10 01:32:12 +00004519 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004520 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004521 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004522
Reid Spencer5f016e22007-07-11 17:01:13 +00004523 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004524 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004525 if (Kind == tok::star)
4526 // Remember that we parsed a pointer type, and remember the type-quals.
4527 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004528 DS.getConstSpecLoc(),
4529 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004530 DS.getRestrictSpecLoc()),
4531 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004532 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004533 else
4534 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004535 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004536 Loc),
4537 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004538 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004539 } else {
4540 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004541 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004542
Sebastian Redl743de1f2009-03-23 00:00:23 +00004543 // Complain about rvalue references in C++03, but then go on and build
4544 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004545 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004546 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004547 diag::warn_cxx98_compat_rvalue_reference :
4548 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004549
Richard Smith6ee326a2012-04-10 01:32:12 +00004550 // GNU-style and C++11 attributes are allowed here, as is restrict.
4551 ParseTypeQualifierListOpt(DS);
4552 D.ExtendWithDeclSpec(DS);
4553
Reid Spencer5f016e22007-07-11 17:01:13 +00004554 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4555 // cv-qualifiers are introduced through the use of a typedef or of a
4556 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004557 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4558 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4559 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004560 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004561 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4562 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004563 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004564 // 'restrict' is permitted as an extension.
4565 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4566 Diag(DS.getAtomicSpecLoc(),
4567 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Reid Spencer5f016e22007-07-11 17:01:13 +00004568 }
4569
4570 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004571 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004572
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004573 if (D.getNumTypeObjects() > 0) {
4574 // C++ [dcl.ref]p4: There shall be no references to references.
4575 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4576 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004577 if (const IdentifierInfo *II = D.getIdentifier())
4578 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4579 << II;
4580 else
4581 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4582 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004583
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004584 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004585 // can go ahead and build the (technically ill-formed)
4586 // declarator: reference collapsing will take care of it.
4587 }
4588 }
4589
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004590 // Remember that we parsed a reference type.
Chris Lattner76549142008-02-21 01:32:26 +00004591 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004592 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004593 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004594 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004595 }
4596}
4597
Richard Smith9988f282012-03-29 01:16:42 +00004598static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4599 SourceLocation EllipsisLoc) {
4600 if (EllipsisLoc.isValid()) {
4601 FixItHint Insertion;
4602 if (!D.getEllipsisLoc().isValid()) {
4603 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4604 D.setEllipsisLoc(EllipsisLoc);
4605 }
4606 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4607 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4608 }
4609}
4610
Reid Spencer5f016e22007-07-11 17:01:13 +00004611/// ParseDirectDeclarator
4612/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004613/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004614/// '(' declarator ')'
4615/// [GNU] '(' attributes declarator ')'
4616/// [C90] direct-declarator '[' constant-expression[opt] ']'
4617/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4618/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4619/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4620/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004621/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4622/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004623/// direct-declarator '(' parameter-type-list ')'
4624/// direct-declarator '(' identifier-list[opt] ')'
4625/// [GNU] direct-declarator '(' parameter-forward-declarations
4626/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004627/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4628/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004629/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4630/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4631/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004632/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004633/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004634///
4635/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004636/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004637/// '::'[opt] nested-name-specifier[opt] type-name
4638///
4639/// id-expression: [C++ 5.1]
4640/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004641/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004642///
4643/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004644/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004645/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004646/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004647/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004648/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004649///
Richard Smith5d8388c2012-03-27 01:42:32 +00004650/// Note, any additional constructs added here may need corresponding changes
4651/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004652void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004653 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004654
David Blaikie4e4d0842012-03-11 07:00:24 +00004655 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004656 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004657 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004658 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4659 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004660 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004661 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004662 }
4663
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004664 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004665 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004666 // Change the declaration context for name lookup, until this function
4667 // is exited (and the declarator has been parsed).
4668 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004669 }
4670
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004671 // C++0x [dcl.fct]p14:
4672 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004673 // of a parameter-declaration-clause without a preceding comma. In
4674 // this case, the ellipsis is parsed as part of the
4675 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004676 // parameter pack that has not been expanded; otherwise, it is parsed
4677 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004678 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004679 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Valifad9e132013-09-26 19:54:12 +00004680 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004681 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004682 NextToken().is(tok::r_paren) &&
Richard Smith30f2a742013-02-20 20:19:27 +00004683 !D.hasGroupingParens() &&
Richard Smith9988f282012-03-29 01:16:42 +00004684 !Actions.containsUnexpandedParameterPacks(D))) {
4685 SourceLocation EllipsisLoc = ConsumeToken();
4686 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4687 // The ellipsis was put in the wrong place. Recover, and explain to
4688 // the user what they should have done.
4689 ParseDeclarator(D);
4690 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4691 return;
4692 } else
4693 D.setEllipsisLoc(EllipsisLoc);
4694
4695 // The ellipsis can't be followed by a parenthesized declarator. We
4696 // check for that in ParseParenDeclarator, after we have disambiguated
4697 // the l_paren token.
4698 }
4699
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004700 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4701 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4702 // We found something that indicates the start of an unqualified-id.
4703 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004704 bool AllowConstructorName;
4705 if (D.getDeclSpec().hasTypeSpecifier())
4706 AllowConstructorName = false;
4707 else if (D.getCXXScopeSpec().isSet())
4708 AllowConstructorName =
4709 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00004710 D.getContext() == Declarator::MemberContext);
John McCallba9d8532010-04-13 06:39:49 +00004711 else
4712 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4713
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004714 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004715 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4716 /*EnteringContext=*/true,
4717 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004718 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004719 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004720 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004721 D.getName()) ||
4722 // Once we're past the identifier, if the scope was bad, mark the
4723 // whole declarator bad.
4724 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004725 D.SetIdentifier(0, Tok.getLocation());
4726 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004727 } else {
4728 // Parsed the unqualified-id; update range information and move along.
4729 if (D.getSourceRange().getBegin().isInvalid())
4730 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4731 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004732 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004733 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004734 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004735 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004736 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004737 "There's a C++-specific check for tok::identifier above");
4738 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4739 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4740 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004741 goto PastIdentifier;
Richard Smitha38253c2013-07-11 05:10:21 +00004742 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
4743 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4744 << FixItHint::CreateRemoval(Tok.getLocation());
4745 D.SetIdentifier(0, Tok.getLocation());
4746 ConsumeToken();
4747 goto PastIdentifier;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004748 }
Richard Smith9988f282012-03-29 01:16:42 +00004749
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004750 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004751 // direct-declarator: '(' declarator ')'
4752 // direct-declarator: '(' attributes declarator ')'
4753 // Example: 'char (*X)' or 'int (*XX)(void)'
4754 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004755
4756 // If the declarator was parenthesized, we entered the declarator
4757 // scope when parsing the parenthesized declarator, then exited
4758 // the scope already. Re-enter the scope, if we need to.
4759 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004760 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004761 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004762 if (!D.isInvalidType() &&
4763 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004764 // Change the declaration context for name lookup, until this function
4765 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004766 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004767 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004768 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004769 // This could be something simple like "int" (in which case the declarator
4770 // portion is empty), if an abstract-declarator is allowed.
4771 D.SetIdentifier(0, Tok.getLocation());
Richard Smith30f2a742013-02-20 20:19:27 +00004772
4773 // The grammar for abstract-pack-declarator does not allow grouping parens.
4774 // FIXME: Revisit this once core issue 1488 is resolved.
4775 if (D.hasEllipsis() && D.hasGroupingParens())
4776 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4777 diag::ext_abstract_pack_declarator_parens);
Reid Spencer5f016e22007-07-11 17:01:13 +00004778 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004779 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004780 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004781 if (D.getContext() == Declarator::MemberContext)
4782 Diag(Tok, diag::err_expected_member_name_or_semi)
4783 << D.getDeclSpec().getSourceRange();
Richard Trieudb55c04c2013-01-26 02:31:38 +00004784 else if (getLangOpts().CPlusPlus) {
4785 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4786 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieuefb288c2013-09-05 02:31:33 +00004787 else {
4788 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4789 if (Tok.isAtStartOfLine() && Loc.isValid())
4790 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4791 << getLangOpts().CPlusPlus;
4792 else
4793 Diag(Tok, diag::err_expected_unqualified_id)
4794 << getLangOpts().CPlusPlus;
4795 }
Richard Trieudb55c04c2013-01-26 02:31:38 +00004796 } else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004797 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004798 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004799 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004800 }
Mike Stump1eb44332009-09-09 15:08:12 +00004801
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004802 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004803 assert(D.isPastIdentifier() &&
4804 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004805
Richard Smith6ee326a2012-04-10 01:32:12 +00004806 // Don't parse attributes unless we have parsed an unparenthesized name.
4807 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004808 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004809
Reid Spencer5f016e22007-07-11 17:01:13 +00004810 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004811 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004812 // Enter function-declaration scope, limiting any declarators to the
4813 // function prototype scope, including parameter declarators.
4814 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004815 Scope::FunctionPrototypeScope|Scope::DeclScope|
4816 (D.isFunctionDeclaratorAFunctionDeclaration()
4817 ? Scope::FunctionDeclarationScope : 0));
4818
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004819 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4820 // In such a case, check if we actually have a function declarator; if it
4821 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004822 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004823 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4824 // The name of the declarator, if any, is tentatively declared within
4825 // a possible direct initializer.
4826 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4827 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4828 TentativelyDeclaredIdentifiers.pop_back();
4829 if (!IsFunctionDecl)
4830 break;
4831 }
John McCall0b7e6782011-03-24 11:26:52 +00004832 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004833 BalancedDelimiterTracker T(*this, tok::l_paren);
4834 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004835 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004836 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004837 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004838 ParseBracketDeclarator(D);
4839 } else {
4840 break;
4841 }
4842 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004843}
Reid Spencer5f016e22007-07-11 17:01:13 +00004844
Chris Lattneref4715c2008-04-06 05:45:57 +00004845/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4846/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004847/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004848/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4849///
4850/// direct-declarator:
4851/// '(' declarator ')'
4852/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004853/// direct-declarator '(' parameter-type-list ')'
4854/// direct-declarator '(' identifier-list[opt] ')'
4855/// [GNU] direct-declarator '(' parameter-forward-declarations
4856/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004857///
4858void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004859 BalancedDelimiterTracker T(*this, tok::l_paren);
4860 T.consumeOpen();
4861
Chris Lattneref4715c2008-04-06 05:45:57 +00004862 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004863
Chris Lattner7399ee02008-10-20 02:05:46 +00004864 // Eat any attributes before we look at whether this is a grouping or function
4865 // declarator paren. If this is a grouping paren, the attribute applies to
4866 // the type being built up, for example:
4867 // int (__attribute__(()) *x)(long y)
4868 // If this ends up not being a grouping paren, the attribute applies to the
4869 // first argument, for example:
4870 // int (__attribute__(()) int x)
4871 // In either case, we need to eat any attributes to be able to determine what
4872 // sort of paren this is.
4873 //
John McCall0b7e6782011-03-24 11:26:52 +00004874 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004875 bool RequiresArg = false;
4876 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004877 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004878
Chris Lattner7399ee02008-10-20 02:05:46 +00004879 // We require that the argument list (if this is a non-grouping paren) be
4880 // present even if the attribute list was empty.
4881 RequiresArg = true;
4882 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004883
Steve Naroff239f0732008-12-25 14:16:32 +00004884 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004885 ParseMicrosoftTypeAttributes(attrs);
4886
Dawn Perchik52fc3142010-09-03 01:29:35 +00004887 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004888 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004889 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004890
Chris Lattneref4715c2008-04-06 05:45:57 +00004891 // If we haven't past the identifier yet (or where the identifier would be
4892 // stored, if this is an abstract declarator), then this is probably just
4893 // grouping parens. However, if this could be an abstract-declarator, then
4894 // this could also be the start of function arguments (consider 'void()').
4895 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004896
Chris Lattneref4715c2008-04-06 05:45:57 +00004897 if (!D.mayOmitIdentifier()) {
4898 // If this can't be an abstract-declarator, this *must* be a grouping
4899 // paren, because we haven't seen the identifier yet.
4900 isGrouping = true;
4901 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004902 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4903 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004904 isDeclarationSpecifier() || // 'int(int)' is a function.
4905 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004906 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4907 // considered to be a type, not a K&R identifier-list.
4908 isGrouping = false;
4909 } else {
4910 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4911 isGrouping = true;
4912 }
Mike Stump1eb44332009-09-09 15:08:12 +00004913
Chris Lattneref4715c2008-04-06 05:45:57 +00004914 // If this is a grouping paren, handle:
4915 // direct-declarator: '(' declarator ')'
4916 // direct-declarator: '(' attributes declarator ')'
4917 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004918 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4919 D.setEllipsisLoc(SourceLocation());
4920
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004921 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004922 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004923 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004924 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004925 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004926 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004927 T.getCloseLocation()),
4928 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004929
4930 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004931
4932 // An ellipsis cannot be placed outside parentheses.
4933 if (EllipsisLoc.isValid())
4934 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4935
Chris Lattneref4715c2008-04-06 05:45:57 +00004936 return;
4937 }
Mike Stump1eb44332009-09-09 15:08:12 +00004938
Chris Lattneref4715c2008-04-06 05:45:57 +00004939 // Okay, if this wasn't a grouping paren, it must be the start of a function
4940 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004941 // identifier (and remember where it would have been), then call into
4942 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004943 D.SetIdentifier(0, Tok.getLocation());
4944
David Blaikie42d6d0c2011-12-04 05:04:18 +00004945 // Enter function-declaration scope, limiting any declarators to the
4946 // function prototype scope, including parameter declarators.
4947 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004948 Scope::FunctionPrototypeScope | Scope::DeclScope |
4949 (D.isFunctionDeclaratorAFunctionDeclaration()
4950 ? Scope::FunctionDeclarationScope : 0));
Richard Smithb9c62612012-07-30 21:30:52 +00004951 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004952 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004953}
4954
4955/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4956/// declarator D up to a paren, which indicates that we are parsing function
4957/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004958///
Richard Smith6ee326a2012-04-10 01:32:12 +00004959/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4960/// immediately after the open paren - they should be considered to be the
4961/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004962///
Richard Smith6ee326a2012-04-10 01:32:12 +00004963/// If RequiresArg is true, then the first argument of the function is required
4964/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004965///
Richard Smith6ee326a2012-04-10 01:32:12 +00004966/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4967/// (C++11) ref-qualifier[opt], exception-specification[opt],
4968/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4969///
4970/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004971/// dynamic-exception-specification
4972/// noexcept-specification
4973///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004974void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004975 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004976 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004977 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004978 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004979 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004980 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004981 // lparen is already consumed!
4982 assert(D.isPastIdentifier() && "Should not call before identifier!");
4983
4984 // This should be true when the function has typed arguments.
4985 // Otherwise, it is treated as a K&R-style function.
4986 bool HasProto = false;
4987 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004988 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004989 // Remember where we see an ellipsis, if any.
4990 SourceLocation EllipsisLoc;
4991
4992 DeclSpec DS(AttrFactory);
4993 bool RefQualifierIsLValueRef = true;
4994 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004995 SourceLocation ConstQualifierLoc;
4996 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004997 ExceptionSpecificationType ESpecType = EST_None;
4998 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004999 SmallVector<ParsedType, 2> DynamicExceptions;
5000 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005001 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00005002 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00005003 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00005004
James Molloy16f1f712012-02-29 10:24:19 +00005005 Actions.ActOnStartFunctionDeclarator();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005006 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5007 EndLoc is the end location for the function declarator.
5008 They differ for trailing return types. */
5009 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005010 SourceLocation LParenLoc, RParenLoc;
5011 LParenLoc = Tracker.getOpenLocation();
5012 StartLoc = LParenLoc;
5013
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005014 if (isFunctionDeclaratorIdentifierList()) {
5015 if (RequiresArg)
5016 Diag(Tok, diag::err_argument_required_after_attribute);
5017
5018 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5019
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005020 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005021 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005022 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005023 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005024 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005025 if (Tok.isNot(tok::r_paren))
Faisal Valifad9e132013-09-26 19:54:12 +00005026 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5027 EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005028 else if (RequiresArg)
5029 Diag(Tok, diag::err_argument_required_after_attribute);
5030
David Blaikie4e4d0842012-03-11 07:00:24 +00005031 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005032
5033 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005034 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005035 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005036 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005037 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005038
David Blaikie4e4d0842012-03-11 07:00:24 +00005039 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005040 // FIXME: Accept these components in any order, and produce fixits to
5041 // correct the order if the user gets it wrong. Ideally we should deal
5042 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005043
5044 // Parse cv-qualifier-seq[opt].
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005045 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5046 /*CXX11AttributesAllowed*/ false,
5047 /*AtomicAllowed*/ false);
Richard Smith6ee326a2012-04-10 01:32:12 +00005048 if (!DS.getSourceRange().getEnd().isInvalid()) {
5049 EndLoc = DS.getSourceRange().getEnd();
5050 ConstQualifierLoc = DS.getConstSpecLoc();
5051 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5052 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005053
5054 // Parse ref-qualifier[opt].
5055 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00005056 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00005057 diag::warn_cxx98_compat_ref_qualifier :
5058 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00005059
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005060 RefQualifierIsLValueRef = Tok.is(tok::amp);
5061 RefQualifierLoc = ConsumeToken();
5062 EndLoc = RefQualifierLoc;
5063 }
5064
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005065 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00005066 // If a declaration declares a member function or member function
5067 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005068 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00005069 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005070 // declarator.
Richard Smithd9227792013-03-15 00:41:52 +00005071 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosier8decdee2012-06-26 22:30:43 +00005072 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00005073 getLangOpts().CPlusPlus11 &&
Richard Smithd9227792013-03-15 00:41:52 +00005074 (D.getContext() == Declarator::MemberContext
5075 ? !D.getDeclSpec().isFriendSpecified()
5076 : D.getContext() == Declarator::FileContext &&
5077 D.getCXXScopeSpec().isValid() &&
5078 Actions.CurContext->isRecord());
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005079 Sema::CXXThisScopeRAII ThisScope(Actions,
5080 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00005081 DS.getTypeQualifiers() |
Richard Smith84046262013-04-21 01:08:50 +00005082 (D.getDeclSpec().isConstexprSpecified() &&
5083 !getLangOpts().CPlusPlus1y
Richard Smith7b19cb12013-01-14 01:55:13 +00005084 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005085 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00005086
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005087 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00005088 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00005089 DynamicExceptions,
5090 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00005091 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005092 if (ESpecType != EST_None)
5093 EndLoc = ESpecRange.getEnd();
5094
Richard Smith6ee326a2012-04-10 01:32:12 +00005095 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5096 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005097 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00005098
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005099 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005100 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00005101 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00005102 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005103 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5104 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005105 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00005106 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00005107 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005108 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005109 }
5110 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005111 }
5112
5113 // Remember that we parsed a function type, and remember the attributes.
5114 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005115 IsAmbiguous,
5116 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005117 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005118 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005119 DS.getTypeQualifiers(),
5120 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00005121 RefQualifierLoc, ConstQualifierLoc,
5122 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00005123 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005124 ESpecType, ESpecRange.getBegin(),
5125 DynamicExceptions.data(),
5126 DynamicExceptionRanges.data(),
5127 DynamicExceptions.size(),
5128 NoexceptExpr.isUsable() ?
5129 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005130 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005131 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00005132 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00005133
5134 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005135}
5136
5137/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5138/// identifier list form for a K&R-style function: void foo(a,b,c)
5139///
5140/// Note that identifier-lists are only allowed for normal declarators, not for
5141/// abstract-declarators.
5142bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00005143 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005144 && Tok.is(tok::identifier)
5145 && !TryAltiVecVectorToken()
5146 // K&R identifier lists can't have typedefs as identifiers, per C99
5147 // 6.7.5.3p11.
5148 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5149 // Identifier lists follow a really simple grammar: the identifiers can
5150 // be followed *only* by a ", identifier" or ")". However, K&R
5151 // identifier lists are really rare in the brave new modern world, and
5152 // it is very common for someone to typo a type in a non-K&R style
5153 // list. If we are presented with something like: "void foo(intptr x,
5154 // float y)", we don't want to start parsing the function declarator as
5155 // though it is a K&R style declarator just because intptr is an
5156 // invalid type.
5157 //
5158 // To handle this, we check to see if the token after the first
5159 // identifier is a "," or ")". Only then do we parse it as an
5160 // identifier list.
5161 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5162}
5163
5164/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5165/// we found a K&R-style identifier list instead of a typed parameter list.
5166///
5167/// After returning, ParamInfo will hold the parsed parameters.
5168///
5169/// identifier-list: [C99 6.7.5]
5170/// identifier
5171/// identifier-list ',' identifier
5172///
5173void Parser::ParseFunctionDeclaratorIdentifierList(
5174 Declarator &D,
Craig Topper6b9240e2013-07-05 19:34:19 +00005175 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005176 // If there was no identifier specified for the declarator, either we are in
5177 // an abstract-declarator, or we are in a parameter declarator which was found
5178 // to be abstract. In abstract-declarators, identifier lists are not valid:
5179 // diagnose this.
5180 if (!D.getIdentifier())
5181 Diag(Tok, diag::ext_ident_list_in_param);
5182
5183 // Maintain an efficient lookup of params we have seen so far.
5184 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5185
5186 while (1) {
5187 // If this isn't an identifier, report the error and skip until ')'.
5188 if (Tok.isNot(tok::identifier)) {
5189 Diag(Tok, diag::err_expected_ident);
5190 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
5191 // Forget we parsed anything.
5192 ParamInfo.clear();
5193 return;
5194 }
5195
5196 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5197
5198 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5199 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5200 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5201
5202 // Verify that the argument identifier has not already been mentioned.
5203 if (!ParamsSoFar.insert(ParmII)) {
5204 Diag(Tok, diag::err_param_redefinition) << ParmII;
5205 } else {
5206 // Remember this identifier in ParamInfo.
5207 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5208 Tok.getLocation(),
5209 0));
5210 }
5211
5212 // Eat the identifier.
5213 ConsumeToken();
5214
5215 // The list continues if we see a comma.
5216 if (Tok.isNot(tok::comma))
5217 break;
5218 ConsumeToken();
5219 }
5220}
5221
5222/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5223/// after the opening parenthesis. This function will not parse a K&R-style
5224/// identifier list.
5225///
Richard Smith6ce48a72012-04-11 04:01:28 +00005226/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5227/// caller parsed those arguments immediately after the open paren - they should
5228/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005229///
5230/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5231/// be the location of the ellipsis, if any was parsed.
5232///
Reid Spencer5f016e22007-07-11 17:01:13 +00005233/// parameter-type-list: [C99 6.7.5]
5234/// parameter-list
5235/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00005236/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00005237///
5238/// parameter-list: [C99 6.7.5]
5239/// parameter-declaration
5240/// parameter-list ',' parameter-declaration
5241///
5242/// parameter-declaration: [C99 6.7.5]
5243/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00005244/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00005245/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00005246/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00005247/// declaration-specifiers abstract-declarator[opt]
5248/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00005249/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00005250/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00005251/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00005252///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005253void Parser::ParseParameterDeclarationClause(
5254 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00005255 ParsedAttributes &FirstArgAttrs,
Craig Topper6b9240e2013-07-05 19:34:19 +00005256 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005257 SourceLocation &EllipsisLoc) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005258 while (1) {
5259 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00005260 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5261 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00005262 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00005263 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00005264 }
Mike Stump1eb44332009-09-09 15:08:12 +00005265
Chris Lattnerf97409f2008-04-06 06:57:35 +00005266 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00005267 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00005268 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005269
Richard Smith6ce48a72012-04-11 04:01:28 +00005270 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005271 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00005272
John McCall7f040a92010-12-24 02:08:15 +00005273 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00005274 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00005275
5276 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00005277
5278 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00005279 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005280 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00005281 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5282 // too much hassle.
5283 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00005284
Chris Lattnere64c5492009-02-27 18:38:20 +00005285 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00005286
Faisal Valifad9e132013-09-26 19:54:12 +00005287
5288 // Parse the declarator. This is "PrototypeContext" or
5289 // "LambdaExprParameterContext", because we must accept either
5290 // 'declarator' or 'abstract-declarator' here.
5291 Declarator ParmDeclarator(DS,
5292 D.getContext() == Declarator::LambdaExprContext ?
5293 Declarator::LambdaExprParameterContext :
5294 Declarator::PrototypeContext);
5295 ParseDeclarator(ParmDeclarator);
Chris Lattnerf97409f2008-04-06 06:57:35 +00005296
5297 // Parse GNU attributes, if present.
Faisal Valifad9e132013-09-26 19:54:12 +00005298 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump1eb44332009-09-09 15:08:12 +00005299
Chris Lattnerf97409f2008-04-06 06:57:35 +00005300 // Remember this parsed parameter in ParamInfo.
Faisal Valifad9e132013-09-26 19:54:12 +00005301 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00005302
Douglas Gregor72b505b2008-12-16 21:30:33 +00005303 // DefArgToks is used when the parsing of default arguments needs
5304 // to be delayed.
5305 CachedTokens *DefArgToks = 0;
5306
Chris Lattnerf97409f2008-04-06 06:57:35 +00005307 // If no parameter was specified, verify that *something* was specified,
5308 // otherwise we have a missing type and identifier.
Faisal Valifad9e132013-09-26 19:54:12 +00005309 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5310 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005311 // Completely missing, emit error.
5312 Diag(DSStart, diag::err_missing_param);
5313 } else {
5314 // Otherwise, we have something. Add it and let semantic analysis try
5315 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005316
Chris Lattnerf97409f2008-04-06 06:57:35 +00005317 // Inform the actions module about the parameter declarator, so it gets
5318 // added to the current scope.
Faisal Valifad9e132013-09-26 19:54:12 +00005319 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5320 ParmDeclarator);
Chris Lattner04421082008-04-08 04:40:51 +00005321 // Parse the default argument, if any. We parse the default
5322 // arguments in all dialects; the semantic analysis in
5323 // ActOnParamDefaultArgument will reject the default argument in
5324 // C.
5325 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005326 SourceLocation EqualLoc = Tok.getLocation();
5327
Chris Lattner04421082008-04-08 04:40:51 +00005328 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005329 if (D.getContext() == Declarator::MemberContext) {
5330 // If we're inside a class definition, cache the tokens
5331 // corresponding to the default argument. We'll actually parse
5332 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005333 // FIXME: Can we use a smart pointer for Toks?
5334 DefArgToks = new CachedTokens;
5335
Richard Smith9bd3cdc2013-09-12 23:28:08 +00005336 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005337 delete DefArgToks;
5338 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005339 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005340 } else {
5341 // Mark the end of the default argument so that we know when to
5342 // stop when we parse it later on.
5343 Token DefArgEnd;
5344 DefArgEnd.startToken();
5345 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5346 DefArgEnd.setLocation(Tok.getLocation());
5347 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005348 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005349 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005350 }
Chris Lattner04421082008-04-08 04:40:51 +00005351 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005352 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005353 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005354
Chad Rosier8decdee2012-06-26 22:30:43 +00005355 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005356 // used.
5357 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005358 Sema::PotentiallyEvaluatedIfUsed,
5359 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005360
Sebastian Redl84407ba2012-03-14 15:54:00 +00005361 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005362 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005363 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005364 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005365 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005366 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005367 if (DefArgResult.isInvalid()) {
5368 Actions.ActOnParamDefaultArgumentError(Param);
5369 SkipUntil(tok::comma, tok::r_paren, true, true);
5370 } else {
5371 // Inform the actions module about the default argument
5372 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005373 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005374 }
Chris Lattner04421082008-04-08 04:40:51 +00005375 }
5376 }
Mike Stump1eb44332009-09-09 15:08:12 +00005377
5378 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Valifad9e132013-09-26 19:54:12 +00005379 ParmDeclarator.getIdentifierLoc(),
5380 Param, DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005381 }
5382
5383 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005384 if (Tok.isNot(tok::comma)) {
5385 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005386 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005387
David Blaikie4e4d0842012-03-11 07:00:24 +00005388 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005389 // We have ellipsis without a preceding ',', which is ill-formed
5390 // in C. Complain and provide the fix.
5391 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005392 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005393 }
5394 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005395
Douglas Gregored5d6512009-09-22 21:41:40 +00005396 break;
5397 }
Mike Stump1eb44332009-09-09 15:08:12 +00005398
Chris Lattnerf97409f2008-04-06 06:57:35 +00005399 // Consume the comma.
5400 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005401 }
Mike Stump1eb44332009-09-09 15:08:12 +00005402
Chris Lattner66d28652008-04-06 06:34:08 +00005403}
Chris Lattneref4715c2008-04-06 05:45:57 +00005404
Reid Spencer5f016e22007-07-11 17:01:13 +00005405/// [C90] direct-declarator '[' constant-expression[opt] ']'
5406/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5407/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5408/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5409/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005410/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5411/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005412void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005413 if (CheckProhibitedCXX11Attribute())
5414 return;
5415
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005416 BalancedDelimiterTracker T(*this, tok::l_square);
5417 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005418
Chris Lattner378c7e42008-12-18 07:27:21 +00005419 // C array syntax has many features, but by-far the most common is [] and [4].
5420 // This code does a fast path to handle some of the most obvious cases.
5421 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005422 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005423 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005424 MaybeParseCXX11Attributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005425
Chris Lattner378c7e42008-12-18 07:27:21 +00005426 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005427 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005428 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005429 T.getOpenLocation(),
5430 T.getCloseLocation()),
5431 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005432 return;
5433 } else if (Tok.getKind() == tok::numeric_constant &&
5434 GetLookAheadToken(1).is(tok::r_square)) {
5435 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005436 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005437 ConsumeToken();
5438
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005439 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005440 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005441 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005442
Chris Lattner378c7e42008-12-18 07:27:21 +00005443 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005444 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005445 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005446 T.getOpenLocation(),
5447 T.getCloseLocation()),
5448 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005449 return;
5450 }
Mike Stump1eb44332009-09-09 15:08:12 +00005451
Reid Spencer5f016e22007-07-11 17:01:13 +00005452 // If valid, this location is the position where we read the 'static' keyword.
5453 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005454 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005455 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005456
Reid Spencer5f016e22007-07-11 17:01:13 +00005457 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005458 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005459 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005460 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005461
Reid Spencer5f016e22007-07-11 17:01:13 +00005462 // If we haven't already read 'static', check to see if there is one after the
5463 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005464 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005465 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005466
Reid Spencer5f016e22007-07-11 17:01:13 +00005467 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5468 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005469 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005470
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005471 // Handle the case where we have '[*]' as the array size. However, a leading
5472 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005473 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005474 // infrequent, use of lookahead is not costly here.
5475 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005476 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005477
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005478 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005479 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005480 StaticLoc = SourceLocation(); // Drop the static.
5481 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005482 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005483 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005484 // Note, in C89, this production uses the constant-expr production instead
5485 // of assignment-expr. The only difference is that assignment-expr allows
5486 // things like '=' and '*='. Sema rejects these in C89 mode because they
5487 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005488
Douglas Gregore0762c92009-06-19 23:52:42 +00005489 // Parse the constant-expression or assignment-expression now (depending
5490 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005491 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005492 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005493 } else {
5494 EnterExpressionEvaluationContext Unevaluated(Actions,
5495 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005496 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005497 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005498 }
Mike Stump1eb44332009-09-09 15:08:12 +00005499
Reid Spencer5f016e22007-07-11 17:01:13 +00005500 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005501 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005502 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005503 // If the expression was invalid, skip it.
5504 SkipUntil(tok::r_square);
5505 return;
5506 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005507
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005508 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005509
John McCall0b7e6782011-03-24 11:26:52 +00005510 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005511 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005512
Chris Lattner378c7e42008-12-18 07:27:21 +00005513 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005514 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005515 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005516 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005517 T.getOpenLocation(),
5518 T.getCloseLocation()),
5519 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005520}
5521
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005522/// [GNU] typeof-specifier:
5523/// typeof ( expressions )
5524/// typeof ( type-name )
5525/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005526///
5527void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005528 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005529 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005530 SourceLocation StartLoc = ConsumeToken();
5531
John McCallcfb708c2010-01-13 20:03:27 +00005532 const bool hasParens = Tok.is(tok::l_paren);
5533
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005534 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5535 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005536
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005537 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005538 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005539 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005540 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5541 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005542 if (hasParens)
5543 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005544
5545 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005546 // FIXME: Not accurate, the range gets one token more than it should.
5547 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005548 else
5549 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005550
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005551 if (isCastExpr) {
5552 if (!CastTy) {
5553 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005554 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005555 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005556
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005557 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005558 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005559 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5560 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005561 DiagID, CastTy))
5562 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005563 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005564 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005565
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005566 // If we get here, the operand to the typeof was an expresion.
5567 if (Operand.isInvalid()) {
5568 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005569 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005570 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005571
Eli Friedman71b8fb52012-01-21 01:01:51 +00005572 // We might need to transform the operand if it is potentially evaluated.
5573 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5574 if (Operand.isInvalid()) {
5575 DS.SetTypeSpecError();
5576 return;
5577 }
5578
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005579 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005580 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005581 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5582 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005583 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005584 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005585}
Chris Lattner1b492422010-02-28 18:33:55 +00005586
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005587/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005588/// _Atomic ( type-name )
5589///
5590void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005591 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5592 "Not an atomic specifier");
Eli Friedmanb001de72011-10-06 23:00:33 +00005593
5594 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005595 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005596 if (T.consumeOpen())
Eli Friedmanb001de72011-10-06 23:00:33 +00005597 return;
Eli Friedmanb001de72011-10-06 23:00:33 +00005598
5599 TypeResult Result = ParseTypeName();
5600 if (Result.isInvalid()) {
5601 SkipUntil(tok::r_paren);
5602 return;
5603 }
5604
5605 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005606 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005607
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005608 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005609 return;
5610
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005611 DS.setTypeofParensRange(T.getRange());
5612 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005613
5614 const char *PrevSpec = 0;
5615 unsigned DiagID;
5616 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5617 DiagID, Result.release()))
5618 Diag(StartLoc, DiagID) << PrevSpec;
5619}
5620
Chris Lattner1b492422010-02-28 18:33:55 +00005621
5622/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5623/// from TryAltiVecVectorToken.
5624bool Parser::TryAltiVecVectorTokenOutOfLine() {
5625 Token Next = NextToken();
5626 switch (Next.getKind()) {
5627 default: return false;
5628 case tok::kw_short:
5629 case tok::kw_long:
5630 case tok::kw_signed:
5631 case tok::kw_unsigned:
5632 case tok::kw_void:
5633 case tok::kw_char:
5634 case tok::kw_int:
5635 case tok::kw_float:
5636 case tok::kw_double:
5637 case tok::kw_bool:
5638 case tok::kw___pixel:
5639 Tok.setKind(tok::kw___vector);
5640 return true;
5641 case tok::identifier:
5642 if (Next.getIdentifierInfo() == Ident_pixel) {
5643 Tok.setKind(tok::kw___vector);
5644 return true;
5645 }
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005646 if (Next.getIdentifierInfo() == Ident_bool) {
5647 Tok.setKind(tok::kw___vector);
5648 return true;
5649 }
Chris Lattner1b492422010-02-28 18:33:55 +00005650 return false;
5651 }
5652}
5653
5654bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5655 const char *&PrevSpec, unsigned &DiagID,
5656 bool &isInvalid) {
5657 if (Tok.getIdentifierInfo() == Ident_vector) {
5658 Token Next = NextToken();
5659 switch (Next.getKind()) {
5660 case tok::kw_short:
5661 case tok::kw_long:
5662 case tok::kw_signed:
5663 case tok::kw_unsigned:
5664 case tok::kw_void:
5665 case tok::kw_char:
5666 case tok::kw_int:
5667 case tok::kw_float:
5668 case tok::kw_double:
5669 case tok::kw_bool:
5670 case tok::kw___pixel:
5671 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5672 return true;
5673 case tok::identifier:
5674 if (Next.getIdentifierInfo() == Ident_pixel) {
5675 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5676 return true;
5677 }
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005678 if (Next.getIdentifierInfo() == Ident_bool) {
5679 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5680 return true;
5681 }
Chris Lattner1b492422010-02-28 18:33:55 +00005682 break;
5683 default:
5684 break;
5685 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005686 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005687 DS.isTypeAltiVecVector()) {
5688 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5689 return true;
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005690 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5691 DS.isTypeAltiVecVector()) {
5692 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5693 return true;
Chris Lattner1b492422010-02-28 18:33:55 +00005694 }
5695 return false;
5696}