blob: 2e95a317fd9c43c78fec482eb09f32fe6d536038 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000017#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000018#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000024#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// C99 6.7: Declarations.
29//===----------------------------------------------------------------------===//
30
31/// ParseTypeName
32/// type-name: [C99 6.7.6]
33/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000034///
35/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000036TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000037 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000038 AccessSpecifier AS,
39 Decl **OwnedType) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000040 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000041 if (DSC == DSC_normal)
42 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000043
Reid Spencer5f016e22007-07-11 17:01:13 +000044 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000045 DeclSpec DS(AttrFactory);
Richard Smith7796eb52012-03-12 08:56:40 +000046 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000047 if (OwnedType)
48 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000049
Reid Spencer5f016e22007-07-11 17:01:13 +000050 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000051 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000052 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000053 if (Range)
54 *Range = DeclaratorInfo.getSourceRange();
55
Chris Lattnereaaebc72009-04-25 08:06:05 +000056 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000057 return true;
58
Douglas Gregor23c94db2010-07-02 17:43:08 +000059 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000060}
61
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000062
63/// isAttributeLateParsed - Return true if the attribute has arguments that
64/// require late parsing.
65static bool isAttributeLateParsed(const IdentifierInfo &II) {
66 return llvm::StringSwitch<bool>(II.getName())
67#include "clang/Parse/AttrLateParsed.inc"
68 .Default(false);
69}
70
71
Sean Huntbbd37c62009-11-21 08:43:09 +000072/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000073///
74/// [GNU] attributes:
75/// attribute
76/// attributes attribute
77///
78/// [GNU] attribute:
79/// '__attribute__' '(' '(' attribute-list ')' ')'
80///
81/// [GNU] attribute-list:
82/// attrib
83/// attribute_list ',' attrib
84///
85/// [GNU] attrib:
86/// empty
87/// attrib-name
88/// attrib-name '(' identifier ')'
89/// attrib-name '(' identifier ',' nonempty-expr-list ')'
90/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
91///
92/// [GNU] attrib-name:
93/// identifier
94/// typespec
95/// typequal
96/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000097///
Reid Spencer5f016e22007-07-11 17:01:13 +000098/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000099/// token lookahead. Comment from gcc: "If they start with an identifier
100/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +0000101/// start with that identifier; otherwise they are an expression list."
102///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000103/// GCC does not require the ',' between attribs in an attribute-list.
104///
Reid Spencer5f016e22007-07-11 17:01:13 +0000105/// At the moment, I am not doing 2 token lookahead. I am also unaware of
106/// any attributes that don't work (based on my limited testing). Most
107/// attributes are very simple in practice. Until we find a bug, I don't see
108/// a pressing need to implement the 2 token lookahead.
109
John McCall7f040a92010-12-24 02:08:15 +0000110void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000111 SourceLocation *endLoc,
112 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000113 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattner04d66662007-10-09 17:33:22 +0000115 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 ConsumeToken();
117 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
118 "attribute")) {
119 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000120 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 }
122 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
123 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000124 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 }
126 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000127 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
128 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000129 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
131 ConsumeToken();
132 continue;
133 }
134 // we have an identifier or declaration specifier (const, int, etc.)
135 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
136 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000138 if (Tok.is(tok::l_paren)) {
139 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000140 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000141 LateParsedAttribute *LA =
142 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
143 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000144
145 // Attributes in a class are parsed at the end of the class, along
146 // with other late-parsed declarations.
147 if (!ClassStack.empty())
148 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000150 // consume everything up to and including the matching right parens
151 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000153 Token Eof;
154 Eof.startToken();
155 Eof.setLocation(Tok.getLocation());
156 LA->Toks.push_back(Eof);
157 } else {
158 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 }
160 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000161 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000162 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 }
164 }
165 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000167 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000168 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
169 SkipUntil(tok::r_paren, false);
170 }
John McCall7f040a92010-12-24 02:08:15 +0000171 if (endLoc)
172 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000174}
175
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000176
177/// Parse the arguments to a parameterized GNU attribute
178void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
179 SourceLocation AttrNameLoc,
180 ParsedAttributes &Attrs,
181 SourceLocation *EndLoc) {
182
183 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
184
185 // Availability attributes have their own grammar.
186 if (AttrName->isStr("availability")) {
187 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
188 return;
189 }
190 // Thread safety attributes fit into the FIXME case above, so we
191 // just parse the arguments as a list of expressions
192 if (IsThreadSafetyAttribute(AttrName->getName())) {
193 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
194 return;
195 }
196
197 ConsumeParen(); // ignore the left paren loc for now
198
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000199 IdentifierInfo *ParmName = 0;
200 SourceLocation ParmLoc;
201 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000202
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000203 switch (Tok.getKind()) {
204 case tok::kw_char:
205 case tok::kw_wchar_t:
206 case tok::kw_char16_t:
207 case tok::kw_char32_t:
208 case tok::kw_bool:
209 case tok::kw_short:
210 case tok::kw_int:
211 case tok::kw_long:
212 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000213 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000214 case tok::kw_signed:
215 case tok::kw_unsigned:
216 case tok::kw_float:
217 case tok::kw_double:
218 case tok::kw_void:
219 case tok::kw_typeof:
220 // __attribute__(( vec_type_hint(char) ))
221 // FIXME: Don't just discard the builtin type token.
222 ConsumeToken();
223 BuiltinType = true;
224 break;
225
226 case tok::identifier:
227 ParmName = Tok.getIdentifierInfo();
228 ParmLoc = ConsumeToken();
229 break;
230
231 default:
232 break;
233 }
234
235 ExprVector ArgExprs(Actions);
236
237 if (!BuiltinType &&
238 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
239 // Eat the comma.
240 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000241 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000242
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000243 // Parse the non-empty comma-separated list of expressions.
244 while (1) {
245 ExprResult ArgExpr(ParseAssignmentExpression());
246 if (ArgExpr.isInvalid()) {
247 SkipUntil(tok::r_paren);
248 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000249 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000250 ArgExprs.push_back(ArgExpr.release());
251 if (Tok.isNot(tok::comma))
252 break;
253 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000254 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000255 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000256 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
257 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
258 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000259 while (Tok.is(tok::identifier)) {
260 ConsumeToken();
261 if (Tok.is(tok::greater))
262 break;
263 if (Tok.is(tok::comma)) {
264 ConsumeToken();
265 continue;
266 }
267 }
268 if (Tok.isNot(tok::greater))
269 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000270 SkipUntil(tok::r_paren, false, true); // skip until ')'
271 }
272 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000273
274 SourceLocation RParen = Tok.getLocation();
275 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
276 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000277 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000278 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size(),
279 AttributeList::AS_GNU);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000280 if (BuiltinType && attr->getKind() == AttributeList::AT_iboutletcollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000281 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000282 }
283}
284
285
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000286/// ParseMicrosoftDeclSpec - Parse an __declspec construct
287///
Eli Friedmana23b4852009-06-08 07:21:15 +0000288/// [MS] decl-specifier:
289/// __declspec ( extended-decl-modifier-seq )
290///
291/// [MS] extended-decl-modifier-seq:
292/// extended-decl-modifier[opt]
293/// extended-decl-modifier extended-decl-modifier-seq
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000294
295void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000296 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000297
Steve Narofff59e17e2008-12-24 20:59:21 +0000298 ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000299 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
300 "declspec")) {
301 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000302 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000303 }
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000304
305 while (Tok.getIdentifierInfo()) {
306 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
307 SourceLocation AttrNameLoc = ConsumeToken();
308
309 // FIXME: Remove this when we have proper __declspec(property()) support.
310 // Just skip everything inside property().
311 if (AttrName->getName() == "property") {
312 ConsumeParen();
313 SkipUntil(tok::r_paren);
314 }
315 if (Tok.is(tok::l_paren)) {
316 ConsumeParen();
317 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
318 // correctly.
319 ExprResult ArgExpr(ParseAssignmentExpression());
320 if (!ArgExpr.isInvalid()) {
321 Expr *ExprList = ArgExpr.take();
322 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
323 SourceLocation(), &ExprList, 1,
324 AttributeList::AS_Declspec);
325 }
326 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
327 SkipUntil(tok::r_paren, false);
328 } else {
329 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
330 0, SourceLocation(), 0, 0, AttributeList::AS_Declspec);
331 }
332 }
333 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
334 SkipUntil(tok::r_paren, false);
335 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000336}
337
John McCall7f040a92010-12-24 02:08:15 +0000338void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000339 // Treat these like attributes
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000340 // FIXME: Allow Sema to distinguish between these and real attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +0000341 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000342 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000343 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000344 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000345 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000346 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
347 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000348 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000349 SourceLocation(), 0, 0, AttributeList::AS_Declspec);
Eli Friedman290eeb02009-06-08 23:27:34 +0000350 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000351}
352
John McCall7f040a92010-12-24 02:08:15 +0000353void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000354 // Treat these like attributes
355 while (Tok.is(tok::kw___pascal)) {
356 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
357 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000358 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000359 SourceLocation(), 0, 0, AttributeList::AS_Declspec);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000360 }
John McCall7f040a92010-12-24 02:08:15 +0000361}
362
Peter Collingbournef315fa82011-02-14 01:42:53 +0000363void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
364 // Treat these like attributes
365 while (Tok.is(tok::kw___kernel)) {
366 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000367 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
368 AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000369 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000370 }
371}
372
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000373void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
374 SourceLocation Loc = Tok.getLocation();
375 switch(Tok.getKind()) {
376 // OpenCL qualifiers:
377 case tok::kw___private:
378 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000379 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000380 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000381 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000382 break;
383
384 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000385 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000386 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000387 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000388 break;
389
390 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000391 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000392 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000393 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000394 break;
395
396 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000397 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000398 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000399 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000400 break;
401
402 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000403 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000404 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000405 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000406 break;
407
408 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000409 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000410 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000411 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000412 break;
413
414 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000415 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000416 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000417 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000418 break;
419 default: break;
420 }
421}
422
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000423/// \brief Parse a version number.
424///
425/// version:
426/// simple-integer
427/// simple-integer ',' simple-integer
428/// simple-integer ',' simple-integer ',' simple-integer
429VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
430 Range = Tok.getLocation();
431
432 if (!Tok.is(tok::numeric_constant)) {
433 Diag(Tok, diag::err_expected_version);
434 SkipUntil(tok::comma, tok::r_paren, true, true, true);
435 return VersionTuple();
436 }
437
438 // Parse the major (and possibly minor and subminor) versions, which
439 // are stored in the numeric constant. We utilize a quirk of the
440 // lexer, which is that it handles something like 1.2.3 as a single
441 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000442 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000443 Buffer.resize(Tok.getLength()+1);
444 const char *ThisTokBegin = &Buffer[0];
445
446 // Get the spelling of the token, which eliminates trigraphs, etc.
447 bool Invalid = false;
448 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
449 if (Invalid)
450 return VersionTuple();
451
452 // Parse the major version.
453 unsigned AfterMajor = 0;
454 unsigned Major = 0;
455 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
456 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
457 ++AfterMajor;
458 }
459
460 if (AfterMajor == 0) {
461 Diag(Tok, diag::err_expected_version);
462 SkipUntil(tok::comma, tok::r_paren, true, true, true);
463 return VersionTuple();
464 }
465
466 if (AfterMajor == ActualLength) {
467 ConsumeToken();
468
469 // We only had a single version component.
470 if (Major == 0) {
471 Diag(Tok, diag::err_zero_version);
472 return VersionTuple();
473 }
474
475 return VersionTuple(Major);
476 }
477
478 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
479 Diag(Tok, diag::err_expected_version);
480 SkipUntil(tok::comma, tok::r_paren, true, true, true);
481 return VersionTuple();
482 }
483
484 // Parse the minor version.
485 unsigned AfterMinor = AfterMajor + 1;
486 unsigned Minor = 0;
487 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
488 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
489 ++AfterMinor;
490 }
491
492 if (AfterMinor == ActualLength) {
493 ConsumeToken();
494
495 // We had major.minor.
496 if (Major == 0 && Minor == 0) {
497 Diag(Tok, diag::err_zero_version);
498 return VersionTuple();
499 }
500
501 return VersionTuple(Major, Minor);
502 }
503
504 // If what follows is not a '.', we have a problem.
505 if (ThisTokBegin[AfterMinor] != '.') {
506 Diag(Tok, diag::err_expected_version);
507 SkipUntil(tok::comma, tok::r_paren, true, true, true);
508 return VersionTuple();
509 }
510
511 // Parse the subminor version.
512 unsigned AfterSubminor = AfterMinor + 1;
513 unsigned Subminor = 0;
514 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
515 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
516 ++AfterSubminor;
517 }
518
519 if (AfterSubminor != ActualLength) {
520 Diag(Tok, diag::err_expected_version);
521 SkipUntil(tok::comma, tok::r_paren, true, true, true);
522 return VersionTuple();
523 }
524 ConsumeToken();
525 return VersionTuple(Major, Minor, Subminor);
526}
527
528/// \brief Parse the contents of the "availability" attribute.
529///
530/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000531/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000532///
533/// platform:
534/// identifier
535///
536/// version-arg-list:
537/// version-arg
538/// version-arg ',' version-arg-list
539///
540/// version-arg:
541/// 'introduced' '=' version
542/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000543/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000544/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000545/// opt-message:
546/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000547void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
548 SourceLocation AvailabilityLoc,
549 ParsedAttributes &attrs,
550 SourceLocation *endLoc) {
551 SourceLocation PlatformLoc;
552 IdentifierInfo *Platform = 0;
553
554 enum { Introduced, Deprecated, Obsoleted, Unknown };
555 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000556 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000557
558 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000559 BalancedDelimiterTracker T(*this, tok::l_paren);
560 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000561 Diag(Tok, diag::err_expected_lparen);
562 return;
563 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000564
565 // Parse the platform name,
566 if (Tok.isNot(tok::identifier)) {
567 Diag(Tok, diag::err_availability_expected_platform);
568 SkipUntil(tok::r_paren);
569 return;
570 }
571 Platform = Tok.getIdentifierInfo();
572 PlatformLoc = ConsumeToken();
573
574 // Parse the ',' following the platform name.
575 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
576 return;
577
578 // If we haven't grabbed the pointers for the identifiers
579 // "introduced", "deprecated", and "obsoleted", do so now.
580 if (!Ident_introduced) {
581 Ident_introduced = PP.getIdentifierInfo("introduced");
582 Ident_deprecated = PP.getIdentifierInfo("deprecated");
583 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000584 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000585 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000586 }
587
588 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000589 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000590 do {
591 if (Tok.isNot(tok::identifier)) {
592 Diag(Tok, diag::err_availability_expected_change);
593 SkipUntil(tok::r_paren);
594 return;
595 }
596 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
597 SourceLocation KeywordLoc = ConsumeToken();
598
Douglas Gregorb53e4172011-03-26 03:35:55 +0000599 if (Keyword == Ident_unavailable) {
600 if (UnavailableLoc.isValid()) {
601 Diag(KeywordLoc, diag::err_availability_redundant)
602 << Keyword << SourceRange(UnavailableLoc);
603 }
604 UnavailableLoc = KeywordLoc;
605
606 if (Tok.isNot(tok::comma))
607 break;
608
609 ConsumeToken();
610 continue;
611 }
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000612
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000613 if (Tok.isNot(tok::equal)) {
614 Diag(Tok, diag::err_expected_equal_after)
615 << Keyword;
616 SkipUntil(tok::r_paren);
617 return;
618 }
619 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000620 if (Keyword == Ident_message) {
621 if (!isTokenStringLiteral()) {
622 Diag(Tok, diag::err_expected_string_literal);
623 SkipUntil(tok::r_paren);
624 return;
625 }
626 MessageExpr = ParseStringLiteralExpression();
627 break;
628 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000629
630 SourceRange VersionRange;
631 VersionTuple Version = ParseVersionTuple(VersionRange);
632
633 if (Version.empty()) {
634 SkipUntil(tok::r_paren);
635 return;
636 }
637
638 unsigned Index;
639 if (Keyword == Ident_introduced)
640 Index = Introduced;
641 else if (Keyword == Ident_deprecated)
642 Index = Deprecated;
643 else if (Keyword == Ident_obsoleted)
644 Index = Obsoleted;
645 else
646 Index = Unknown;
647
648 if (Index < Unknown) {
649 if (!Changes[Index].KeywordLoc.isInvalid()) {
650 Diag(KeywordLoc, diag::err_availability_redundant)
651 << Keyword
652 << SourceRange(Changes[Index].KeywordLoc,
653 Changes[Index].VersionRange.getEnd());
654 }
655
656 Changes[Index].KeywordLoc = KeywordLoc;
657 Changes[Index].Version = Version;
658 Changes[Index].VersionRange = VersionRange;
659 } else {
660 Diag(KeywordLoc, diag::err_availability_unknown_change)
661 << Keyword << VersionRange;
662 }
663
664 if (Tok.isNot(tok::comma))
665 break;
666
667 ConsumeToken();
668 } while (true);
669
670 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000671 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000672 return;
673
674 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000675 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000676
Douglas Gregorb53e4172011-03-26 03:35:55 +0000677 // The 'unavailable' availability cannot be combined with any other
678 // availability changes. Make sure that hasn't happened.
679 if (UnavailableLoc.isValid()) {
680 bool Complained = false;
681 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
682 if (Changes[Index].KeywordLoc.isValid()) {
683 if (!Complained) {
684 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
685 << SourceRange(Changes[Index].KeywordLoc,
686 Changes[Index].VersionRange.getEnd());
687 Complained = true;
688 }
689
690 // Clear out the availability.
691 Changes[Index] = AvailabilityChange();
692 }
693 }
694 }
695
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000696 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000697 attrs.addNew(&Availability,
698 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000699 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000700 Platform, PlatformLoc,
701 Changes[Introduced],
702 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000703 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000704 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000705 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000706}
707
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000708
709// Late Parsed Attributes:
710// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
711
712void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
713
714void Parser::LateParsedClass::ParseLexedAttributes() {
715 Self->ParseLexedAttributes(*Class);
716}
717
718void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000719 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000720}
721
722/// Wrapper class which calls ParseLexedAttribute, after setting up the
723/// scope appropriately.
724void Parser::ParseLexedAttributes(ParsingClass &Class) {
725 // Deal with templates
726 // FIXME: Test cases to make sure this does the right thing for templates.
727 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
728 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
729 HasTemplateScope);
730 if (HasTemplateScope)
731 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
732
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000733 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000734 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000735 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000736 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
737 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
738
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000739 // Enter the scope of nested classes
740 if (!AlreadyHasClassScope)
741 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
742 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000743 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000744 // Allow 'this' within late-parsed attributes.
745 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
746 /*TypeQuals=*/0);
747
748 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
749 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
750 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000751 }
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000752
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000753 if (!AlreadyHasClassScope)
754 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
755 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000756}
757
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000758
759/// \brief Parse all attributes in LAs, and attach them to Decl D.
760void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
761 bool EnterScope, bool OnDefinition) {
762 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000763 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000764 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000765 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000766 }
767 LAs.clear();
768}
769
770
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000771/// \brief Finish parsing an attribute for which parsing was delayed.
772/// This will be called at the end of parsing a class declaration
773/// for each LateParsedAttribute. We consume the saved tokens and
774/// create an attribute with the arguments filled in. We add this
775/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000776void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
777 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000778 // Save the current token position.
779 SourceLocation OrigLoc = Tok.getLocation();
780
781 // Append the current token at the end of the new token stream so that it
782 // doesn't get lost.
783 LA.Toks.push_back(Tok);
784 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
785 // Consume the previously pushed token.
786 ConsumeAnyToken();
787
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000788 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
789 Diag(Tok, diag::warn_attribute_on_function_definition)
790 << LA.AttrName.getName();
791 }
792
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000793 ParsedAttributes Attrs(AttrFactory);
794 SourceLocation endLoc;
795
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000796 if (LA.Decls.size() == 1) {
797 Decl *D = LA.Decls[0];
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000798
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000799 // If the Decl is templatized, add template parameters to scope.
800 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
801 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
802 if (HasTemplateScope)
803 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000804
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000805 // If the Decl is on a function, add function parameters to the scope.
806 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
807 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
808 if (HasFunctionScope)
809 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
810
811 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
812
813 if (HasFunctionScope) {
814 Actions.ActOnExitFunctionContext();
815 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
816 }
817 if (HasTemplateScope) {
818 TempScope.Exit();
819 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000820 } else if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000821 // If there are multiple decls, then the decl cannot be within the
822 // function scope.
823 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000824 } else {
825 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000826 }
827
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000828 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
829 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
830 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000831
832 if (Tok.getLocation() != OrigLoc) {
833 // Due to a parsing error, we either went over the cached tokens or
834 // there are still cached tokens left, so we skip the leftover tokens.
835 // Since this is an uncommon situation that should be avoided, use the
836 // expensive isBeforeInTranslationUnit call.
837 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
838 OrigLoc))
839 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000840 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000841 }
842}
843
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000844/// \brief Wrapper around a case statement checking if AttrName is
845/// one of the thread safety attributes
846bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
847 return llvm::StringSwitch<bool>(AttrName)
848 .Case("guarded_by", true)
849 .Case("guarded_var", true)
850 .Case("pt_guarded_by", true)
851 .Case("pt_guarded_var", true)
852 .Case("lockable", true)
853 .Case("scoped_lockable", true)
854 .Case("no_thread_safety_analysis", true)
855 .Case("acquired_after", true)
856 .Case("acquired_before", true)
857 .Case("exclusive_lock_function", true)
858 .Case("shared_lock_function", true)
859 .Case("exclusive_trylock_function", true)
860 .Case("shared_trylock_function", true)
861 .Case("unlock_function", true)
862 .Case("lock_returned", true)
863 .Case("locks_excluded", true)
864 .Case("exclusive_locks_required", true)
865 .Case("shared_locks_required", true)
866 .Default(false);
867}
868
869/// \brief Parse the contents of thread safety attributes. These
870/// should always be parsed as an expression list.
871///
872/// We need to special case the parsing due to the fact that if the first token
873/// of the first argument is an identifier, the main parse loop will store
874/// that token as a "parameter" and the rest of
875/// the arguments will be added to a list of "arguments". However,
876/// subsequent tokens in the first argument are lost. We instead parse each
877/// argument as an expression and add all arguments to the list of "arguments".
878/// In future, we will take advantage of this special case to also
879/// deal with some argument scoping issues here (for example, referring to a
880/// function parameter in the attribute on that function).
881void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
882 SourceLocation AttrNameLoc,
883 ParsedAttributes &Attrs,
884 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000885 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000886
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000887 BalancedDelimiterTracker T(*this, tok::l_paren);
888 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000889
890 ExprVector ArgExprs(Actions);
891 bool ArgExprsOk = true;
892
893 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000894 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000895 ExprResult ArgExpr(ParseAssignmentExpression());
896 if (ArgExpr.isInvalid()) {
897 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000898 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000899 break;
900 } else {
901 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000902 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000903 if (Tok.isNot(tok::comma))
904 break;
905 ConsumeToken(); // Eat the comma, move to the next argument
906 }
907 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000908 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000909 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000910 ArgExprs.take(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000911 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000912 if (EndLoc)
913 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000914}
915
Richard Smith6ee326a2012-04-10 01:32:12 +0000916/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
917/// of a C++11 attribute-specifier in a location where an attribute is not
918/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
919/// situation.
920///
921/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
922/// this doesn't appear to actually be an attribute-specifier, and the caller
923/// should try to parse it.
924bool Parser::DiagnoseProhibitedCXX11Attribute() {
925 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
926
927 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
928 case CAK_NotAttributeSpecifier:
929 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
930 return false;
931
932 case CAK_InvalidAttributeSpecifier:
933 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
934 return false;
935
936 case CAK_AttributeSpecifier:
937 // Parse and discard the attributes.
938 SourceLocation BeginLoc = ConsumeBracket();
939 ConsumeBracket();
940 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
941 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
942 SourceLocation EndLoc = ConsumeBracket();
943 Diag(BeginLoc, diag::err_attributes_not_allowed)
944 << SourceRange(BeginLoc, EndLoc);
945 return true;
946 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +0000947 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +0000948}
949
John McCall7f040a92010-12-24 02:08:15 +0000950void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
951 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
952 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000953}
954
Reid Spencer5f016e22007-07-11 17:01:13 +0000955/// ParseDeclaration - Parse a full 'declaration', which consists of
956/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000957/// 'Context' should be a Declarator::TheContext value. This returns the
958/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000959///
960/// declaration: [C99 6.7]
961/// block-declaration ->
962/// simple-declaration
963/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000964/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000965/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000966/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000967/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +0000968/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000969/// others... [FIXME]
970///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000971Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
972 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000973 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000974 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000975 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000976 // Must temporarily exit the objective-c container scope for
977 // parsing c none objective-c decls.
978 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000979
John McCalld226f652010-08-21 09:40:31 +0000980 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000981 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000982 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000983 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000984 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000985 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000986 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000987 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000988 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000989 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +0000990 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000991 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000992 SourceLocation InlineLoc = ConsumeToken();
993 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
994 break;
995 }
John McCall7f040a92010-12-24 02:08:15 +0000996 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000997 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000998 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000999 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001000 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001001 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001002 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001003 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001004 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001005 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001006 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001007 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001008 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001009 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001010 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001011 default:
John McCall7f040a92010-12-24 02:08:15 +00001012 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001013 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001014
Chris Lattner682bf922009-03-29 16:50:03 +00001015 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001016 // single decl, convert it now. Alias declarations can also declare a type;
1017 // include that too if it is present.
1018 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001019}
1020
1021/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1022/// declaration-specifiers init-declarator-list[opt] ';'
1023///[C90/C++]init-declarator-list ';' [TODO]
1024/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001025///
Richard Smithad762fc2011-04-14 22:09:26 +00001026/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
1027/// attribute-specifier-seq[opt] type-specifier-seq declarator
1028///
Chris Lattnercd147752009-03-29 17:27:48 +00001029/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001030/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001031///
1032/// If FRI is non-null, we might be parsing a for-range-declaration instead
1033/// of a simple-declaration. If we find that we are, we also parse the
1034/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001035Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
1036 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001037 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001038 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +00001039 bool RequireSemi,
1040 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001042 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001043 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001044
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001045 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001046 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001047
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1049 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001050 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001051 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001052 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001053 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001054 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001055 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001056 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001058
1059 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001060}
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Richard Smith0706df42011-10-19 21:33:05 +00001062/// Returns true if this might be the start of a declarator, or a common typo
1063/// for a declarator.
1064bool Parser::MightBeDeclarator(unsigned Context) {
1065 switch (Tok.getKind()) {
1066 case tok::annot_cxxscope:
1067 case tok::annot_template_id:
1068 case tok::caret:
1069 case tok::code_completion:
1070 case tok::coloncolon:
1071 case tok::ellipsis:
1072 case tok::kw___attribute:
1073 case tok::kw_operator:
1074 case tok::l_paren:
1075 case tok::star:
1076 return true;
1077
1078 case tok::amp:
1079 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001080 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001081
Richard Smith1c94c162012-01-09 22:31:44 +00001082 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001083 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001084 NextToken().is(tok::l_square);
1085
1086 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001087 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001088
Richard Smith0706df42011-10-19 21:33:05 +00001089 case tok::identifier:
1090 switch (NextToken().getKind()) {
1091 case tok::code_completion:
1092 case tok::coloncolon:
1093 case tok::comma:
1094 case tok::equal:
1095 case tok::equalequal: // Might be a typo for '='.
1096 case tok::kw_alignas:
1097 case tok::kw_asm:
1098 case tok::kw___attribute:
1099 case tok::l_brace:
1100 case tok::l_paren:
1101 case tok::l_square:
1102 case tok::less:
1103 case tok::r_brace:
1104 case tok::r_paren:
1105 case tok::r_square:
1106 case tok::semi:
1107 return true;
1108
1109 case tok::colon:
1110 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001111 // and in block scope it's probably a label. Inside a class definition,
1112 // this is a bit-field.
1113 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001114 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001115
1116 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001117 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001118
1119 default:
1120 return false;
1121 }
1122
1123 default:
1124 return false;
1125 }
1126}
1127
Richard Smith994d73f2012-04-11 20:59:20 +00001128/// Skip until we reach something which seems like a sensible place to pick
1129/// up parsing after a malformed declaration. This will sometimes stop sooner
1130/// than SkipUntil(tok::r_brace) would, but will never stop later.
1131void Parser::SkipMalformedDecl() {
1132 while (true) {
1133 switch (Tok.getKind()) {
1134 case tok::l_brace:
1135 // Skip until matching }, then stop. We've probably skipped over
1136 // a malformed class or function definition or similar.
1137 ConsumeBrace();
1138 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1139 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1140 // This declaration isn't over yet. Keep skipping.
1141 continue;
1142 }
1143 if (Tok.is(tok::semi))
1144 ConsumeToken();
1145 return;
1146
1147 case tok::l_square:
1148 ConsumeBracket();
1149 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1150 continue;
1151
1152 case tok::l_paren:
1153 ConsumeParen();
1154 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1155 continue;
1156
1157 case tok::r_brace:
1158 return;
1159
1160 case tok::semi:
1161 ConsumeToken();
1162 return;
1163
1164 case tok::kw_inline:
1165 // 'inline namespace' at the start of a line is almost certainly
1166 // a good place to pick back up parsing.
1167 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace))
1168 return;
1169 break;
1170
1171 case tok::kw_namespace:
1172 // 'namespace' at the start of a line is almost certainly a good
1173 // place to pick back up parsing.
1174 if (Tok.isAtStartOfLine())
1175 return;
1176 break;
1177
1178 case tok::eof:
1179 return;
1180
1181 default:
1182 break;
1183 }
1184
1185 ConsumeAnyToken();
1186 }
1187}
1188
John McCalld8ac0572009-11-03 19:26:08 +00001189/// ParseDeclGroup - Having concluded that this is either a function
1190/// definition or a group of object declarations, actually parse the
1191/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001192Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1193 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001194 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001195 SourceLocation *DeclEnd,
1196 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001197 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001198 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001199 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001200
John McCalld8ac0572009-11-03 19:26:08 +00001201 // Bail out if the first declarator didn't seem well-formed.
1202 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001203 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001204 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001205 }
Mike Stump1eb44332009-09-09 15:08:12 +00001206
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001207 // Save late-parsed attributes for now; they need to be parsed in the
1208 // appropriate function scope after the function Decl has been constructed.
1209 LateParsedAttrList LateParsedAttrs;
1210 if (D.isFunctionDeclarator())
1211 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1212
Chris Lattnerc82daef2010-07-11 22:24:20 +00001213 // Check to see if we have a function *definition* which must have a body.
1214 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1215 // Look at the next token to make sure that this isn't a function
1216 // declaration. We have to check this because __attribute__ might be the
1217 // start of a function definition in GCC-extended K&R C.
1218 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001219
Chris Lattner004659a2010-07-11 22:42:07 +00001220 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001221 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1222 Diag(Tok, diag::err_function_declared_typedef);
1223
1224 // Recover by treating the 'typedef' as spurious.
1225 DS.ClearStorageClassSpecs();
1226 }
1227
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001228 Decl *TheDecl =
1229 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001230 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001231 }
1232
1233 if (isDeclarationSpecifier()) {
1234 // If there is an invalid declaration specifier right after the function
1235 // prototype, then we must be in a missing semicolon case where this isn't
1236 // actually a body. Just fall through into the code that handles it as a
1237 // prototype, and let the top-level code handle the erroneous declspec
1238 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001239 } else {
1240 Diag(Tok, diag::err_expected_fn_body);
1241 SkipUntil(tok::semi);
1242 return DeclGroupPtrTy();
1243 }
1244 }
1245
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001246 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001247 return DeclGroupPtrTy();
1248
1249 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1250 // must parse and analyze the for-range-initializer before the declaration is
1251 // analyzed.
1252 if (FRI && Tok.is(tok::colon)) {
1253 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001254 if (Tok.is(tok::l_brace))
1255 FRI->RangeExpr = ParseBraceInitializer();
1256 else
1257 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001258 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1259 Actions.ActOnCXXForRangeDecl(ThisDecl);
1260 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001261 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001262 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1263 }
1264
Chris Lattner5f9e2722011-07-23 10:55:15 +00001265 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001266 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001267 if (LateParsedAttrs.size() > 0)
1268 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001269 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001270 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001271 DeclsInGroup.push_back(FirstDecl);
1272
Richard Smith0706df42011-10-19 21:33:05 +00001273 bool ExpectSemi = Context != Declarator::ForContext;
1274
John McCalld8ac0572009-11-03 19:26:08 +00001275 // If we don't have a comma, it is either the end of the list (a ';') or an
1276 // error, bail out.
1277 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001278 SourceLocation CommaLoc = ConsumeToken();
1279
1280 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1281 // This comma was followed by a line-break and something which can't be
1282 // the start of a declarator. The comma was probably a typo for a
1283 // semicolon.
1284 Diag(CommaLoc, diag::err_expected_semi_declaration)
1285 << FixItHint::CreateReplacement(CommaLoc, ";");
1286 ExpectSemi = false;
1287 break;
1288 }
John McCalld8ac0572009-11-03 19:26:08 +00001289
1290 // Parse the next declarator.
1291 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001292 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001293
1294 // Accept attributes in an init-declarator. In the first declarator in a
1295 // declaration, these would be part of the declspec. In subsequent
1296 // declarators, they become part of the declarator itself, so that they
1297 // don't apply to declarators after *this* one. Examples:
1298 // short __attribute__((common)) var; -> declspec
1299 // short var __attribute__((common)); -> declarator
1300 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001301 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001302
1303 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001304 if (!D.isInvalidType()) {
1305 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1306 D.complete(ThisDecl);
1307 if (ThisDecl)
1308 DeclsInGroup.push_back(ThisDecl);
1309 }
John McCalld8ac0572009-11-03 19:26:08 +00001310 }
1311
1312 if (DeclEnd)
1313 *DeclEnd = Tok.getLocation();
1314
Richard Smith0706df42011-10-19 21:33:05 +00001315 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001316 ExpectAndConsumeSemi(Context == Declarator::FileContext
1317 ? diag::err_invalid_token_after_toplevel_declarator
1318 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001319 // Okay, there was no semicolon and one was expected. If we see a
1320 // declaration specifier, just assume it was missing and continue parsing.
1321 // Otherwise things are very confused and we skip to recover.
1322 if (!isDeclarationSpecifier()) {
1323 SkipUntil(tok::r_brace, true, true);
1324 if (Tok.is(tok::semi))
1325 ConsumeToken();
1326 }
John McCalld8ac0572009-11-03 19:26:08 +00001327 }
1328
Douglas Gregor23c94db2010-07-02 17:43:08 +00001329 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001330 DeclsInGroup.data(),
1331 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001332}
1333
Richard Smithad762fc2011-04-14 22:09:26 +00001334/// Parse an optional simple-asm-expr and attributes, and attach them to a
1335/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001336bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001337 // If a simple-asm-expr is present, parse it.
1338 if (Tok.is(tok::kw_asm)) {
1339 SourceLocation Loc;
1340 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1341 if (AsmLabel.isInvalid()) {
1342 SkipUntil(tok::semi, true, true);
1343 return true;
1344 }
1345
1346 D.setAsmLabel(AsmLabel.release());
1347 D.SetRangeEnd(Loc);
1348 }
1349
1350 MaybeParseGNUAttributes(D);
1351 return false;
1352}
1353
Douglas Gregor1426e532009-05-12 21:31:51 +00001354/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1355/// declarator'. This method parses the remainder of the declaration
1356/// (including any attributes or initializer, among other things) and
1357/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001358///
Reid Spencer5f016e22007-07-11 17:01:13 +00001359/// init-declarator: [C99 6.7]
1360/// declarator
1361/// declarator '=' initializer
1362/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1363/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001364/// [C++] declarator initializer[opt]
1365///
1366/// [C++] initializer:
1367/// [C++] '=' initializer-clause
1368/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001369/// [C++0x] '=' 'default' [TODO]
1370/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001371/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001372///
1373/// According to the standard grammar, =default and =delete are function
1374/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001375///
John McCalld226f652010-08-21 09:40:31 +00001376Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001377 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001378 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001379 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Richard Smithad762fc2011-04-14 22:09:26 +00001381 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1382}
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Richard Smithad762fc2011-04-14 22:09:26 +00001384Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1385 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001386 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001387 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001388 switch (TemplateInfo.Kind) {
1389 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001390 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001391 break;
1392
1393 case ParsedTemplateInfo::Template:
1394 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001395 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001396 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001397 TemplateInfo.TemplateParams->data(),
1398 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001399 D);
1400 break;
1401
1402 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001403 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001404 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001405 TemplateInfo.ExternLoc,
1406 TemplateInfo.TemplateLoc,
1407 D);
1408 if (ThisRes.isInvalid()) {
1409 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001410 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001411 }
1412
1413 ThisDecl = ThisRes.get();
1414 break;
1415 }
1416 }
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Richard Smith34b41d92011-02-20 03:19:35 +00001418 bool TypeContainsAuto =
1419 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1420
Douglas Gregor1426e532009-05-12 21:31:51 +00001421 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001422 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001423 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001424 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001425 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001426 if (D.isFunctionDeclarator())
1427 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1428 << 1 /* delete */;
1429 else
1430 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001431 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001432 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001433 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1434 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001435 else
1436 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001437 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001438 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001439 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001440 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001441 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001442
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001443 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001444 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001445 cutOffParsing();
1446 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001447 }
1448
John McCall60d7b3a2010-08-24 06:29:42 +00001449 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001450
David Blaikie4e4d0842012-03-11 07:00:24 +00001451 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001452 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001453 ExitScope();
1454 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001455
Douglas Gregor1426e532009-05-12 21:31:51 +00001456 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001457 SkipUntil(tok::comma, true, true);
1458 Actions.ActOnInitializerError(ThisDecl);
1459 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001460 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1461 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001462 }
1463 } else if (Tok.is(tok::l_paren)) {
1464 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001465 BalancedDelimiterTracker T(*this, tok::l_paren);
1466 T.consumeOpen();
1467
Douglas Gregor1426e532009-05-12 21:31:51 +00001468 ExprVector Exprs(Actions);
1469 CommaLocsTy CommaLocs;
1470
David Blaikie4e4d0842012-03-11 07:00:24 +00001471 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001472 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001473 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001474 }
1475
Douglas Gregor1426e532009-05-12 21:31:51 +00001476 if (ParseExpressionList(Exprs, CommaLocs)) {
1477 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001478
David Blaikie4e4d0842012-03-11 07:00:24 +00001479 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001480 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001481 ExitScope();
1482 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001483 } else {
1484 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001485 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001486
1487 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1488 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001489
David Blaikie4e4d0842012-03-11 07:00:24 +00001490 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001491 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001492 ExitScope();
1493 }
1494
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001495 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1496 T.getCloseLocation(),
1497 move_arg(Exprs));
1498 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1499 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001500 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001501 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001502 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001503 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1504
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001505 if (D.getCXXScopeSpec().isSet()) {
1506 EnterScope(0);
1507 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1508 }
1509
1510 ExprResult Init(ParseBraceInitializer());
1511
1512 if (D.getCXXScopeSpec().isSet()) {
1513 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1514 ExitScope();
1515 }
1516
1517 if (Init.isInvalid()) {
1518 Actions.ActOnInitializerError(ThisDecl);
1519 } else
1520 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1521 /*DirectInit=*/true, TypeContainsAuto);
1522
Douglas Gregor1426e532009-05-12 21:31:51 +00001523 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001524 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001525 }
1526
Richard Smith483b9f32011-02-21 20:05:19 +00001527 Actions.FinalizeDeclaration(ThisDecl);
1528
Douglas Gregor1426e532009-05-12 21:31:51 +00001529 return ThisDecl;
1530}
1531
Reid Spencer5f016e22007-07-11 17:01:13 +00001532/// ParseSpecifierQualifierList
1533/// specifier-qualifier-list:
1534/// type-specifier specifier-qualifier-list[opt]
1535/// type-qualifier specifier-qualifier-list[opt]
1536/// [GNU] attributes specifier-qualifier-list[opt]
1537///
Richard Smith69730c12012-03-12 07:56:15 +00001538void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1539 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001540 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1541 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001542 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001543 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Reid Spencer5f016e22007-07-11 17:01:13 +00001545 // Validate declspec for type-name.
1546 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001547 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1548 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001549 Diag(Tok, diag::err_expected_type);
1550 DS.SetTypeSpecError();
1551 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1552 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001553 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001554 if (!DS.hasTypeSpecifier())
1555 DS.SetTypeSpecError();
1556 }
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Reid Spencer5f016e22007-07-11 17:01:13 +00001558 // Issue diagnostic and remove storage class if present.
1559 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1560 if (DS.getStorageClassSpecLoc().isValid())
1561 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1562 else
1563 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1564 DS.ClearStorageClassSpecs();
1565 }
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Reid Spencer5f016e22007-07-11 17:01:13 +00001567 // Issue diagnostic and remove function specfier if present.
1568 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001569 if (DS.isInlineSpecified())
1570 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1571 if (DS.isVirtualSpecified())
1572 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1573 if (DS.isExplicitSpecified())
1574 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001575 DS.ClearFunctionSpecs();
1576 }
Richard Smith69730c12012-03-12 07:56:15 +00001577
1578 // Issue diagnostic and remove constexpr specfier if present.
1579 if (DS.isConstexprSpecified()) {
1580 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1581 DS.ClearConstexprSpec();
1582 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001583}
1584
Chris Lattnerc199ab32009-04-12 20:42:31 +00001585/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1586/// specified token is valid after the identifier in a declarator which
1587/// immediately follows the declspec. For example, these things are valid:
1588///
1589/// int x [ 4]; // direct-declarator
1590/// int x ( int y); // direct-declarator
1591/// int(int x ) // direct-declarator
1592/// int x ; // simple-declaration
1593/// int x = 17; // init-declarator-list
1594/// int x , y; // init-declarator-list
1595/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001596/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001597/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001598///
1599/// This is not, because 'x' does not immediately follow the declspec (though
1600/// ')' happens to be valid anyway).
1601/// int (x)
1602///
1603static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1604 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1605 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001606 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001607}
1608
Chris Lattnere40c2952009-04-14 21:34:55 +00001609
1610/// ParseImplicitInt - This method is called when we have an non-typename
1611/// identifier in a declspec (which normally terminates the decl spec) when
1612/// the declspec has no type specifier. In this case, the declspec is either
1613/// malformed or is "implicit int" (in K&R and C89).
1614///
1615/// This method handles diagnosing this prettily and returns false if the
1616/// declspec is done being processed. If it recovers and thinks there may be
1617/// other pieces of declspec after it, it returns true.
1618///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001619bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001620 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001621 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001622 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Chris Lattnere40c2952009-04-14 21:34:55 +00001624 SourceLocation Loc = Tok.getLocation();
1625 // If we see an identifier that is not a type name, we normally would
1626 // parse it as the identifer being declared. However, when a typename
1627 // is typo'd or the definition is not included, this will incorrectly
1628 // parse the typename as the identifier name and fall over misparsing
1629 // later parts of the diagnostic.
1630 //
1631 // As such, we try to do some look-ahead in cases where this would
1632 // otherwise be an "implicit-int" case to see if this is invalid. For
1633 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1634 // an identifier with implicit int, we'd get a parse error because the
1635 // next token is obviously invalid for a type. Parse these as a case
1636 // with an invalid type specifier.
1637 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Chris Lattnere40c2952009-04-14 21:34:55 +00001639 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001640 // error, do lookahead to try to do better recovery. This never applies
1641 // within a type specifier. Outside of C++, we allow this even if the
1642 // language doesn't "officially" support implicit int -- we support
1643 // implicit int as an extension in C99 and C11. Allegedly, MS also
1644 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001645 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001646 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001647 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001648 // If this token is valid for implicit int, e.g. "static x = 4", then
1649 // we just avoid eating the identifier, so it will be parsed as the
1650 // identifier in the declarator.
1651 return false;
1652 }
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Richard Smith827adaf2012-05-15 21:01:51 +00001654 if (getLangOpts().CPlusPlus &&
1655 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1656 // Don't require a type specifier if we have the 'auto' storage class
1657 // specifier in C++98 -- we'll promote it to a type specifier.
1658 return false;
1659 }
1660
Chris Lattnere40c2952009-04-14 21:34:55 +00001661 // Otherwise, if we don't consume this token, we are going to emit an
1662 // error anyway. Try to recover from various common problems. Check
1663 // to see if this was a reference to a tag name without a tag specified.
1664 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001665 //
1666 // C++ doesn't need this, and isTagName doesn't take SS.
1667 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001668 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001669 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Douglas Gregor23c94db2010-07-02 17:43:08 +00001671 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001672 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001673 case DeclSpec::TST_enum:
1674 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1675 case DeclSpec::TST_union:
1676 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1677 case DeclSpec::TST_struct:
1678 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1679 case DeclSpec::TST_class:
1680 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001681 }
Mike Stump1eb44332009-09-09 15:08:12 +00001682
Chris Lattnerf4382f52009-04-14 22:17:06 +00001683 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001684 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1685 LookupResult R(Actions, TokenName, SourceLocation(),
1686 Sema::LookupOrdinaryName);
1687
Chris Lattnerf4382f52009-04-14 22:17:06 +00001688 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001689 << TokenName << TagName << getLangOpts().CPlusPlus
1690 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1691
1692 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1693 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1694 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001695 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001696 << TokenName << TagName;
1697 }
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Chris Lattnerf4382f52009-04-14 22:17:06 +00001699 // Parse this as a tag as if the missing tag were present.
1700 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001701 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001702 else
Richard Smith69730c12012-03-12 07:56:15 +00001703 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1704 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001705 return true;
1706 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001707 }
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Richard Smith8f0a7e72012-05-15 21:29:55 +00001709 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001710 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001711 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1712 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001713 // Look ahead to the next token to try to figure out what this declaration
1714 // was supposed to be.
1715 switch (NextToken().getKind()) {
1716 case tok::comma:
1717 case tok::equal:
1718 case tok::kw_asm:
1719 case tok::l_brace:
1720 case tok::l_square:
1721 case tok::semi:
1722 // This looks like a variable declaration. The type is probably missing.
1723 // We're done parsing decl-specifiers.
1724 return false;
1725
1726 case tok::l_paren: {
1727 // static x(4); // 'x' is not a type
1728 // x(int n); // 'x' is not a type
1729 // x (*p)[]; // 'x' is a type
1730 //
1731 // Since we're in an error case (or the rare 'implicit int in C++' MS
1732 // extension), we can afford to perform a tentative parse to determine
1733 // which case we're in.
1734 TentativeParsingAction PA(*this);
1735 ConsumeToken();
1736 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1737 PA.Revert();
1738 if (TPR == TPResult::False())
1739 return false;
1740 // The identifier is followed by a parenthesized declarator.
1741 // It's supposed to be a type.
1742 break;
1743 }
1744
1745 default:
1746 // This is probably supposed to be a type. This includes cases like:
1747 // int f(itn);
1748 // struct S { unsinged : 4; };
1749 break;
1750 }
1751 }
1752
Douglas Gregora786fdb2009-10-13 23:27:22 +00001753 // This is almost certainly an invalid type name. Let the action emit a
1754 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001755 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001756 IdentifierInfo *II = Tok.getIdentifierInfo();
1757 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001758 // The action emitted a diagnostic, so we don't have to.
1759 if (T) {
1760 // The action has suggested that the type T could be used. Set that as
1761 // the type in the declaration specifiers, consume the would-be type
1762 // name token, and we're done.
1763 const char *PrevSpec;
1764 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001765 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001766 DS.SetRangeEnd(Tok.getLocation());
1767 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001768 // There may be other declaration specifiers after this.
1769 return true;
1770 } else if (II != Tok.getIdentifierInfo()) {
1771 // If no type was suggested, the correction is to a keyword
1772 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00001773 // There may be other declaration specifiers after this.
1774 return true;
1775 }
1776
1777 // Fall through; the action had no suggestion for us.
1778 } else {
1779 // The action did not emit a diagnostic, so emit one now.
1780 SourceRange R;
1781 if (SS) R = SS->getRange();
1782 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1783 }
Mike Stump1eb44332009-09-09 15:08:12 +00001784
Douglas Gregora786fdb2009-10-13 23:27:22 +00001785 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001786 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001787 DS.SetRangeEnd(Tok.getLocation());
1788 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Chris Lattnere40c2952009-04-14 21:34:55 +00001790 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1791 // avoid rippling error messages on subsequent uses of the same type,
1792 // could be useful if #include was forgotten.
1793 return false;
1794}
1795
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001796/// \brief Determine the declaration specifier context from the declarator
1797/// context.
1798///
1799/// \param Context the declarator context, which is one of the
1800/// Declarator::TheContext enumerator values.
1801Parser::DeclSpecContext
1802Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1803 if (Context == Declarator::MemberContext)
1804 return DSC_class;
1805 if (Context == Declarator::FileContext)
1806 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001807 if (Context == Declarator::TrailingReturnContext)
1808 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001809 return DSC_normal;
1810}
1811
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001812/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1813///
1814/// FIXME: Simply returns an alignof() expression if the argument is a
1815/// type. Ideally, the type should be propagated directly into Sema.
1816///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001817/// [C11] type-id
1818/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001819/// [C++0x] type-id ...[opt]
1820/// [C++0x] assignment-expression ...[opt]
1821ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1822 SourceLocation &EllipsisLoc) {
1823 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001824 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001825 SourceLocation TypeLoc = Tok.getLocation();
1826 ParsedType Ty = ParseTypeName().get();
1827 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001828 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1829 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001830 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001831 ER = ParseConstantExpression();
1832
David Blaikie4e4d0842012-03-11 07:00:24 +00001833 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001834 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001835
1836 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001837}
1838
1839/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1840/// attribute to Attrs.
1841///
1842/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001843/// [C11] '_Alignas' '(' type-id ')'
1844/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001845/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1846/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001847void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1848 SourceLocation *endLoc) {
1849 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1850 "Not an alignment-specifier!");
1851
1852 SourceLocation KWLoc = Tok.getLocation();
1853 ConsumeToken();
1854
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001855 BalancedDelimiterTracker T(*this, tok::l_paren);
1856 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001857 return;
1858
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001859 SourceLocation EllipsisLoc;
1860 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001861 if (ArgExpr.isInvalid()) {
1862 SkipUntil(tok::r_paren);
1863 return;
1864 }
1865
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001866 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001867 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001868 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001869
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001870 // FIXME: Handle pack-expansions here.
1871 if (EllipsisLoc.isValid()) {
1872 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1873 return;
1874 }
1875
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001876 ExprVector ArgExprs(Actions);
1877 ArgExprs.push_back(ArgExpr.release());
1878 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +00001879 0, T.getOpenLocation(), ArgExprs.take(), 1,
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +00001880 AttributeList::AS_CXX11);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001881}
1882
Reid Spencer5f016e22007-07-11 17:01:13 +00001883/// ParseDeclarationSpecifiers
1884/// declaration-specifiers: [C99 6.7]
1885/// storage-class-specifier declaration-specifiers[opt]
1886/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001887/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001888/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001889/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001890/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001891///
1892/// storage-class-specifier: [C99 6.7.1]
1893/// 'typedef'
1894/// 'extern'
1895/// 'static'
1896/// 'auto'
1897/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001898/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001899/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001900/// function-specifier: [C99 6.7.4]
1901/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001902/// [C++] 'virtual'
1903/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001904/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001905/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001906/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001907
Reid Spencer5f016e22007-07-11 17:01:13 +00001908///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001909void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001910 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001911 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001912 DeclSpecContext DSContext,
1913 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001914 if (DS.getSourceRange().isInvalid()) {
1915 DS.SetRangeStart(Tok.getLocation());
1916 DS.SetRangeEnd(Tok.getLocation());
1917 }
1918
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001919 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001920 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001921 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001922 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001923 unsigned DiagID = 0;
1924
Reid Spencer5f016e22007-07-11 17:01:13 +00001925 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001926
Reid Spencer5f016e22007-07-11 17:01:13 +00001927 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001928 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001929 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001930 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1931 MaybeParseCXX0XAttributes(DS.getAttributes());
1932
Reid Spencer5f016e22007-07-11 17:01:13 +00001933 // If this is not a declaration specifier token, we're done reading decl
1934 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001935 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001936 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001937
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001938 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001939 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001940 if (DS.hasTypeSpecifier()) {
1941 bool AllowNonIdentifiers
1942 = (getCurScope()->getFlags() & (Scope::ControlScope |
1943 Scope::BlockScope |
1944 Scope::TemplateParamScope |
1945 Scope::FunctionPrototypeScope |
1946 Scope::AtCatchScope)) == 0;
1947 bool AllowNestedNameSpecifiers
1948 = DSContext == DSC_top_level ||
1949 (DSContext == DSC_class && DS.isFriendSpecified());
1950
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001951 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1952 AllowNonIdentifiers,
1953 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001954 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001955 }
1956
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001957 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1958 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1959 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001960 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1961 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001962 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001963 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001964 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001965 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001966
1967 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001968 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001969 }
1970
Chris Lattner5e02c472009-01-05 00:07:25 +00001971 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001972 // C++ scope specifier. Annotate and loop, or bail out on error.
1973 if (TryAnnotateCXXScopeToken(true)) {
1974 if (!DS.hasTypeSpecifier())
1975 DS.SetTypeSpecError();
1976 goto DoneWithDeclSpec;
1977 }
John McCall2e0a7152010-03-01 18:20:46 +00001978 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1979 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001980 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001981
1982 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00001983 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001984 goto DoneWithDeclSpec;
1985
John McCallaa87d332009-12-12 11:40:51 +00001986 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001987 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1988 Tok.getAnnotationRange(),
1989 SS);
John McCallaa87d332009-12-12 11:40:51 +00001990
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001991 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001992 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001993 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001994 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001995 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001996 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001997
1998 // C++ [class.qual]p2:
1999 // In a lookup in which the constructor is an acceptable lookup
2000 // result and the nested-name-specifier nominates a class C:
2001 //
2002 // - if the name specified after the
2003 // nested-name-specifier, when looked up in C, is the
2004 // injected-class-name of C (Clause 9), or
2005 //
2006 // - if the name specified after the nested-name-specifier
2007 // is the same as the identifier or the
2008 // simple-template-id's template-name in the last
2009 // component of the nested-name-specifier,
2010 //
2011 // the name is instead considered to name the constructor of
2012 // class C.
2013 //
2014 // Thus, if the template-name is actually the constructor
2015 // name, then the code is ill-formed; this interpretation is
2016 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002017 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002018 if ((DSContext == DSC_top_level ||
2019 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2020 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002021 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002022 if (isConstructorDeclarator()) {
2023 // The user meant this to be an out-of-line constructor
2024 // definition, but template arguments are not allowed
2025 // there. Just allow this as a constructor; we'll
2026 // complain about it later.
2027 goto DoneWithDeclSpec;
2028 }
2029
2030 // The user meant this to name a type, but it actually names
2031 // a constructor with some extraneous template
2032 // arguments. Complain, then parse it as a type as the user
2033 // intended.
2034 Diag(TemplateId->TemplateNameLoc,
2035 diag::err_out_of_line_template_id_names_constructor)
2036 << TemplateId->Name;
2037 }
2038
John McCallaa87d332009-12-12 11:40:51 +00002039 DS.getTypeSpecScope() = SS;
2040 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002041 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002042 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002043 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002044 continue;
2045 }
2046
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002047 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002048 DS.getTypeSpecScope() = SS;
2049 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002050 if (Tok.getAnnotationValue()) {
2051 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002052 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2053 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002054 PrevSpec, DiagID, T);
2055 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002056 else
2057 DS.SetTypeSpecError();
2058 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2059 ConsumeToken(); // The typename
2060 }
2061
Douglas Gregor9135c722009-03-25 15:40:00 +00002062 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002063 goto DoneWithDeclSpec;
2064
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002065 // If we're in a context where the identifier could be a class name,
2066 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002067 if ((DSContext == DSC_top_level ||
2068 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002069 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002070 &SS)) {
2071 if (isConstructorDeclarator())
2072 goto DoneWithDeclSpec;
2073
2074 // As noted in C++ [class.qual]p2 (cited above), when the name
2075 // of the class is qualified in a context where it could name
2076 // a constructor, its a constructor name. However, we've
2077 // looked at the declarator, and the user probably meant this
2078 // to be a type. Complain that it isn't supposed to be treated
2079 // as a type, then proceed to parse it as a type.
2080 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2081 << Next.getIdentifierInfo();
2082 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002083
John McCallb3d87482010-08-24 05:47:05 +00002084 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2085 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002086 getCurScope(), &SS,
2087 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002088 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002089 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002090
Chris Lattnerf4382f52009-04-14 22:17:06 +00002091 // If the referenced identifier is not a type, then this declspec is
2092 // erroneous: We already checked about that it has no type specifier, and
2093 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002094 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002095 if (TypeRep == 0) {
2096 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002097 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002098 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002099 }
Mike Stump1eb44332009-09-09 15:08:12 +00002100
John McCallaa87d332009-12-12 11:40:51 +00002101 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002102 ConsumeToken(); // The C++ scope.
2103
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002104 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002105 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002106 if (isInvalid)
2107 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002108
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002109 DS.SetRangeEnd(Tok.getLocation());
2110 ConsumeToken(); // The typename.
2111
2112 continue;
2113 }
Mike Stump1eb44332009-09-09 15:08:12 +00002114
Chris Lattner80d0c892009-01-21 19:48:37 +00002115 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002116 if (Tok.getAnnotationValue()) {
2117 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002118 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002119 DiagID, T);
2120 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002121 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00002122
2123 if (isInvalid)
2124 break;
2125
Chris Lattner80d0c892009-01-21 19:48:37 +00002126 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2127 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002128
Chris Lattner80d0c892009-01-21 19:48:37 +00002129 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2130 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002131 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002132 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002133 ParseObjCProtocolQualifiers(DS);
2134
Chris Lattner80d0c892009-01-21 19:48:37 +00002135 continue;
2136 }
Mike Stump1eb44332009-09-09 15:08:12 +00002137
Douglas Gregorbfad9152011-04-28 15:48:45 +00002138 case tok::kw___is_signed:
2139 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2140 // typically treats it as a trait. If we see __is_signed as it appears
2141 // in libstdc++, e.g.,
2142 //
2143 // static const bool __is_signed;
2144 //
2145 // then treat __is_signed as an identifier rather than as a keyword.
2146 if (DS.getTypeSpecType() == TST_bool &&
2147 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2148 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2149 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2150 Tok.setKind(tok::identifier);
2151 }
2152
2153 // We're done with the declaration-specifiers.
2154 goto DoneWithDeclSpec;
2155
Chris Lattner3bd934a2008-07-26 01:18:38 +00002156 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002157 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002158 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002159 // In C++, check to see if this is a scope specifier like foo::bar::, if
2160 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002161 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002162 if (TryAnnotateCXXScopeToken(true)) {
2163 if (!DS.hasTypeSpecifier())
2164 DS.SetTypeSpecError();
2165 goto DoneWithDeclSpec;
2166 }
2167 if (!Tok.is(tok::identifier))
2168 continue;
2169 }
Mike Stump1eb44332009-09-09 15:08:12 +00002170
Chris Lattner3bd934a2008-07-26 01:18:38 +00002171 // This identifier can only be a typedef name if we haven't already seen
2172 // a type-specifier. Without this check we misparse:
2173 // typedef int X; struct Y { short X; }; as 'short int'.
2174 if (DS.hasTypeSpecifier())
2175 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002176
John Thompson82287d12010-02-05 00:12:22 +00002177 // Check for need to substitute AltiVec keyword tokens.
2178 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2179 break;
2180
Richard Smithf63eee72012-05-09 18:56:43 +00002181 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2182 // allow the use of a typedef name as a type specifier.
2183 if (DS.isTypeAltiVecVector())
2184 goto DoneWithDeclSpec;
2185
John McCallb3d87482010-08-24 05:47:05 +00002186 ParsedType TypeRep =
2187 Actions.getTypeName(*Tok.getIdentifierInfo(),
2188 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002189
Chris Lattnerc199ab32009-04-12 20:42:31 +00002190 // If this is not a typedef name, don't parse it as part of the declspec,
2191 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002192 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002193 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002194 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002195 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002196
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002197 // If we're in a context where the identifier could be a class name,
2198 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002199 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002200 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002201 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002202 goto DoneWithDeclSpec;
2203
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002204 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002205 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002206 if (isInvalid)
2207 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002208
Chris Lattner3bd934a2008-07-26 01:18:38 +00002209 DS.SetRangeEnd(Tok.getLocation());
2210 ConsumeToken(); // The identifier
2211
2212 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2213 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002214 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002215 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002216 ParseObjCProtocolQualifiers(DS);
2217
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002218 // Need to support trailing type qualifiers (e.g. "id<p> const").
2219 // If a type specifier follows, it will be diagnosed elsewhere.
2220 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002221 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002222
2223 // type-name
2224 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002225 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002226 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002227 // This template-id does not refer to a type name, so we're
2228 // done with the type-specifiers.
2229 goto DoneWithDeclSpec;
2230 }
2231
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002232 // If we're in a context where the template-id could be a
2233 // constructor name or specialization, check whether this is a
2234 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002235 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002236 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002237 isConstructorDeclarator())
2238 goto DoneWithDeclSpec;
2239
Douglas Gregor39a8de12009-02-25 19:37:18 +00002240 // Turn the template-id annotation token into a type annotation
2241 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002242 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002243 continue;
2244 }
2245
Reid Spencer5f016e22007-07-11 17:01:13 +00002246 // GNU attributes support.
2247 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002248 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002249 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002250
2251 // Microsoft declspec support.
2252 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002253 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002254 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002255
Steve Naroff239f0732008-12-25 14:16:32 +00002256 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002257 case tok::kw___forceinline: {
2258 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2259 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
2260 SourceLocation AttrNameLoc = ConsumeToken();
Sean Hunt93f95f22012-06-18 16:13:52 +00002261 // FIXME: This does not work correctly if it is set to be a declspec
2262 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002263 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002264 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002265 continue;
2266 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002267
2268 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002269 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002270 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002271 case tok::kw___cdecl:
2272 case tok::kw___stdcall:
2273 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002274 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002275 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002276 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002277 continue;
2278
Dawn Perchik52fc3142010-09-03 01:29:35 +00002279 // Borland single token adornments.
2280 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002281 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002282 continue;
2283
Peter Collingbournef315fa82011-02-14 01:42:53 +00002284 // OpenCL single token adornments.
2285 case tok::kw___kernel:
2286 ParseOpenCLAttributes(DS.getAttributes());
2287 continue;
2288
Reid Spencer5f016e22007-07-11 17:01:13 +00002289 // storage-class-specifier
2290 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002291 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2292 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002293 break;
2294 case tok::kw_extern:
2295 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002296 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002297 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2298 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002299 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002300 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002301 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2302 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002303 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002304 case tok::kw_static:
2305 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002306 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002307 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2308 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002309 break;
2310 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002311 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002312 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002313 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2314 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002315 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002316 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002317 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002318 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002319 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2320 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002321 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002322 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2323 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002324 break;
2325 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002326 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2327 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002328 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002329 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002330 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2331 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002332 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002333 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002334 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002335 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002336
Reid Spencer5f016e22007-07-11 17:01:13 +00002337 // function-specifier
2338 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002339 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002340 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002341 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002342 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002343 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002344 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002345 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002346 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002347
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002348 // alignment-specifier
2349 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002350 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002351 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002352 ParseAlignmentSpecifier(DS.getAttributes());
2353 continue;
2354
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002355 // friend
2356 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002357 if (DSContext == DSC_class)
2358 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2359 else {
2360 PrevSpec = ""; // not actually used by the diagnostic
2361 DiagID = diag::err_friend_invalid_in_context;
2362 isInvalid = true;
2363 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002364 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002365
Douglas Gregor8d267c52011-09-09 02:06:17 +00002366 // Modules
2367 case tok::kw___module_private__:
2368 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2369 break;
2370
Sebastian Redl2ac67232009-11-05 15:47:02 +00002371 // constexpr
2372 case tok::kw_constexpr:
2373 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2374 break;
2375
Chris Lattner80d0c892009-01-21 19:48:37 +00002376 // type-specifier
2377 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002378 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2379 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002380 break;
2381 case tok::kw_long:
2382 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002383 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2384 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002385 else
John McCallfec54012009-08-03 20:12:06 +00002386 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2387 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002388 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002389 case tok::kw___int64:
2390 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2391 DiagID);
2392 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002393 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002394 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2395 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002396 break;
2397 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002398 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2399 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002400 break;
2401 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002402 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2403 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002404 break;
2405 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002406 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2407 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002408 break;
2409 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002410 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2411 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002412 break;
2413 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002414 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2415 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002416 break;
2417 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002418 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2419 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002420 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002421 case tok::kw___int128:
2422 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2423 DiagID);
2424 break;
2425 case tok::kw_half:
2426 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2427 DiagID);
2428 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002429 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002430 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2431 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002432 break;
2433 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002434 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2435 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002436 break;
2437 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002438 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2439 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002440 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002441 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002442 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2443 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002444 break;
2445 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002446 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2447 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002448 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002449 case tok::kw_bool:
2450 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002451 if (Tok.is(tok::kw_bool) &&
2452 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2453 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2454 PrevSpec = ""; // Not used by the diagnostic.
2455 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002456 // For better error recovery.
2457 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002458 isInvalid = true;
2459 } else {
2460 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2461 DiagID);
2462 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002463 break;
2464 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002465 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2466 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002467 break;
2468 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002469 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2470 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002471 break;
2472 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002473 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2474 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002475 break;
John Thompson82287d12010-02-05 00:12:22 +00002476 case tok::kw___vector:
2477 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2478 break;
2479 case tok::kw___pixel:
2480 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2481 break;
John McCalla5fc4722011-04-09 22:50:59 +00002482 case tok::kw___unknown_anytype:
2483 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2484 PrevSpec, DiagID);
2485 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002486
2487 // class-specifier:
2488 case tok::kw_class:
2489 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002490 case tok::kw_union: {
2491 tok::TokenKind Kind = Tok.getKind();
2492 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002493 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2494 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002495 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002496 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002497
2498 // enum-specifier:
2499 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002500 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002501 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002502 continue;
2503
2504 // cv-qualifier:
2505 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002506 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002507 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002508 break;
2509 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002510 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002511 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002512 break;
2513 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002514 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002515 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002516 break;
2517
Douglas Gregord57959a2009-03-27 23:10:48 +00002518 // C++ typename-specifier:
2519 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002520 if (TryAnnotateTypeOrScopeToken()) {
2521 DS.SetTypeSpecError();
2522 goto DoneWithDeclSpec;
2523 }
2524 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002525 continue;
2526 break;
2527
Chris Lattner80d0c892009-01-21 19:48:37 +00002528 // GNU typeof support.
2529 case tok::kw_typeof:
2530 ParseTypeofSpecifier(DS);
2531 continue;
2532
David Blaikie42d6d0c2011-12-04 05:04:18 +00002533 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002534 ParseDecltypeSpecifier(DS);
2535 continue;
2536
Sean Huntdb5d44b2011-05-19 05:37:45 +00002537 case tok::kw___underlying_type:
2538 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002539 continue;
2540
2541 case tok::kw__Atomic:
2542 ParseAtomicSpecifier(DS);
2543 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002544
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002545 // OpenCL qualifiers:
2546 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002547 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002548 goto DoneWithDeclSpec;
2549 case tok::kw___private:
2550 case tok::kw___global:
2551 case tok::kw___local:
2552 case tok::kw___constant:
2553 case tok::kw___read_only:
2554 case tok::kw___write_only:
2555 case tok::kw___read_write:
2556 ParseOpenCLQualifiers(DS);
2557 break;
2558
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002559 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002560 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002561 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2562 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002563 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002564 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002565
Douglas Gregor46f936e2010-11-19 17:10:50 +00002566 if (!ParseObjCProtocolQualifiers(DS))
2567 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2568 << FixItHint::CreateInsertion(Loc, "id")
2569 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002570
2571 // Need to support trailing type qualifiers (e.g. "id<p> const").
2572 // If a type specifier follows, it will be diagnosed elsewhere.
2573 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002574 }
John McCallfec54012009-08-03 20:12:06 +00002575 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002576 if (isInvalid) {
2577 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002578 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002579
2580 if (DiagID == diag::ext_duplicate_declspec)
2581 Diag(Tok, DiagID)
2582 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2583 else
2584 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002585 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002586
Chris Lattner81c018d2008-03-13 06:29:04 +00002587 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002588 if (DiagID != diag::err_bool_redeclaration)
2589 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002590 }
2591}
Douglas Gregoradcac882008-12-01 23:54:00 +00002592
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002593/// ParseStructDeclaration - Parse a struct declaration without the terminating
2594/// semicolon.
2595///
Reid Spencer5f016e22007-07-11 17:01:13 +00002596/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002597/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002598/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002599/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002600/// struct-declarator-list:
2601/// struct-declarator
2602/// struct-declarator-list ',' struct-declarator
2603/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2604/// struct-declarator:
2605/// declarator
2606/// [GNU] declarator attributes[opt]
2607/// declarator[opt] ':' constant-expression
2608/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2609///
Chris Lattnere1359422008-04-10 06:46:29 +00002610void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002611ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002612
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002613 if (Tok.is(tok::kw___extension__)) {
2614 // __extension__ silences extension warnings in the subexpression.
2615 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002616 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002617 return ParseStructDeclaration(DS, Fields);
2618 }
Mike Stump1eb44332009-09-09 15:08:12 +00002619
Steve Naroff28a7ca82007-08-20 22:28:22 +00002620 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002621 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002622
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002623 // If there are no declarators, this is a free-standing declaration
2624 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002625 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002626 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002627 return;
2628 }
2629
2630 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002631 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002632 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002633 while (1) {
John McCall92576642012-05-07 06:16:41 +00002634 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCallbdd563e2009-11-03 02:38:08 +00002635 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002636 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002637
2638 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002639 if (!FirstDeclarator)
2640 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002641
Steve Naroff28a7ca82007-08-20 22:28:22 +00002642 /// struct-declarator: declarator
2643 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002644 if (Tok.isNot(tok::colon)) {
2645 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2646 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002647 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002648 }
Mike Stump1eb44332009-09-09 15:08:12 +00002649
Chris Lattner04d66662007-10-09 17:33:22 +00002650 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002651 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002652 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002653 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002654 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002655 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002656 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002657 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002658
Steve Naroff28a7ca82007-08-20 22:28:22 +00002659 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002660 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002661
John McCallbdd563e2009-11-03 02:38:08 +00002662 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002663 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002664 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002665
Steve Naroff28a7ca82007-08-20 22:28:22 +00002666 // If we don't have a comma, it is either the end of the list (a ';')
2667 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002668 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002669 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002670
Steve Naroff28a7ca82007-08-20 22:28:22 +00002671 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002672 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002673
John McCallbdd563e2009-11-03 02:38:08 +00002674 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002675 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002676}
2677
2678/// ParseStructUnionBody
2679/// struct-contents:
2680/// struct-declaration-list
2681/// [EXT] empty
2682/// [GNU] "struct-declaration-list" without terminatoring ';'
2683/// struct-declaration-list:
2684/// struct-declaration
2685/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002686/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002687///
Reid Spencer5f016e22007-07-11 17:01:13 +00002688void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002689 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002690 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2691 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002692
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002693 BalancedDelimiterTracker T(*this, tok::l_brace);
2694 if (T.consumeOpen())
2695 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002696
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002697 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002698 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002699
Reid Spencer5f016e22007-07-11 17:01:13 +00002700 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2701 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002702 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002703 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2704 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2705 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002706
Chris Lattner5f9e2722011-07-23 10:55:15 +00002707 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002708
Reid Spencer5f016e22007-07-11 17:01:13 +00002709 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002710 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002711 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002712
Reid Spencer5f016e22007-07-11 17:01:13 +00002713 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002714 if (Tok.is(tok::semi)) {
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002715 ConsumeExtraSemi(InsideStruct,
2716 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Reid Spencer5f016e22007-07-11 17:01:13 +00002717 continue;
2718 }
Chris Lattnere1359422008-04-10 06:46:29 +00002719
2720 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002721 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002722
John McCallbdd563e2009-11-03 02:38:08 +00002723 if (!Tok.is(tok::at)) {
2724 struct CFieldCallback : FieldCallback {
2725 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002726 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002727 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002728
John McCalld226f652010-08-21 09:40:31 +00002729 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002730 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002731 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2732
John McCalld226f652010-08-21 09:40:31 +00002733 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002734 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002735 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002736 FD.D.getDeclSpec().getSourceRange().getBegin(),
2737 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002738 FieldDecls.push_back(Field);
2739 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002740 }
John McCallbdd563e2009-11-03 02:38:08 +00002741 } Callback(*this, TagDecl, FieldDecls);
2742
2743 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002744 } else { // Handle @defs
2745 ConsumeToken();
2746 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2747 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002748 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002749 continue;
2750 }
2751 ConsumeToken();
2752 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2753 if (!Tok.is(tok::identifier)) {
2754 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002755 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002756 continue;
2757 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002758 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002759 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002760 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002761 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2762 ConsumeToken();
2763 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002764 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002765
Chris Lattner04d66662007-10-09 17:33:22 +00002766 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002767 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002768 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002769 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002770 break;
2771 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002772 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2773 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002774 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002775 // If we stopped at a ';', eat it.
2776 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002777 }
2778 }
Mike Stump1eb44332009-09-09 15:08:12 +00002779
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002780 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002781
John McCall0b7e6782011-03-24 11:26:52 +00002782 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002783 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002784 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002785
Douglas Gregor23c94db2010-07-02 17:43:08 +00002786 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002787 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002788 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002789 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002790 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002791 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2792 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002793}
2794
Reid Spencer5f016e22007-07-11 17:01:13 +00002795/// ParseEnumSpecifier
2796/// enum-specifier: [C99 6.7.2.2]
2797/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002798///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002799/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2800/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002801/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2802/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002803/// 'enum' identifier
2804/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002805///
Richard Smith1af83c42012-03-23 03:33:32 +00002806/// [C++11] enum-head '{' enumerator-list[opt] '}'
2807/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002808///
Richard Smith1af83c42012-03-23 03:33:32 +00002809/// enum-head: [C++11]
2810/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2811/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2812/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002813///
Richard Smith1af83c42012-03-23 03:33:32 +00002814/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002815/// 'enum'
2816/// 'enum' 'class'
2817/// 'enum' 'struct'
2818///
Richard Smith1af83c42012-03-23 03:33:32 +00002819/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002820/// ':' type-specifier-seq
2821///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002822/// [C++] elaborated-type-specifier:
2823/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2824///
Chris Lattner4c97d762009-04-12 21:49:30 +00002825void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002826 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002827 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002828 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002829 if (Tok.is(tok::code_completion)) {
2830 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002831 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002832 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002833 }
John McCall57c13002011-07-06 05:58:41 +00002834
Richard Smithbdad7a22012-01-10 01:33:14 +00002835 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002836 bool IsScopedUsingClassTag = false;
2837
David Blaikie4e4d0842012-03-11 07:00:24 +00002838 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002839 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002840 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002841 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002842 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002843 }
Richard Smith1af83c42012-03-23 03:33:32 +00002844
John McCall13489672012-05-07 06:16:58 +00002845 // C++11 [temp.explicit]p12:
2846 // The usual access controls do not apply to names used to specify
2847 // explicit instantiations.
2848 // We extend this to also cover explicit specializations. Note that
2849 // we don't suppress if this turns out to be an elaborated type
2850 // specifier.
2851 bool shouldDelayDiagsInTag =
2852 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2853 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2854 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00002855
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002856 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002857 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002858 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002859
Aaron Ballman6454a022012-03-01 04:09:28 +00002860 // If declspecs exist after tag, parse them.
2861 while (Tok.is(tok::kw___declspec))
2862 ParseMicrosoftDeclSpec(attrs);
2863
Richard Smith7796eb52012-03-12 08:56:40 +00002864 // Enum definitions should not be parsed in a trailing-return-type.
2865 bool AllowDeclaration = DSC != DSC_trailing;
2866
2867 bool AllowFixedUnderlyingType = AllowDeclaration &&
2868 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2869 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002870
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002871 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002872 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002873 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2874 // if a fixed underlying type is allowed.
2875 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2876
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002877 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2878 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002879 return;
2880
2881 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002882 Diag(Tok, diag::err_expected_ident);
2883 if (Tok.isNot(tok::l_brace)) {
2884 // Has no name and is not a definition.
2885 // Skip the rest of this declarator, up until the comma or semicolon.
2886 SkipUntil(tok::comma, true);
2887 return;
2888 }
2889 }
2890 }
Mike Stump1eb44332009-09-09 15:08:12 +00002891
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002892 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002893 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002894 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002895 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002896
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002897 // Skip the rest of this declarator, up until the comma or semicolon.
2898 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002899 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002900 }
Mike Stump1eb44332009-09-09 15:08:12 +00002901
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002902 // If an identifier is present, consume and remember it.
2903 IdentifierInfo *Name = 0;
2904 SourceLocation NameLoc;
2905 if (Tok.is(tok::identifier)) {
2906 Name = Tok.getIdentifierInfo();
2907 NameLoc = ConsumeToken();
2908 }
Mike Stump1eb44332009-09-09 15:08:12 +00002909
Richard Smithbdad7a22012-01-10 01:33:14 +00002910 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002911 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2912 // declaration of a scoped enumeration.
2913 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002914 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002915 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002916 }
2917
John McCall13489672012-05-07 06:16:58 +00002918 // Okay, end the suppression area. We'll decide whether to emit the
2919 // diagnostics in a second.
2920 if (shouldDelayDiagsInTag)
2921 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00002922
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002923 TypeResult BaseType;
2924
Douglas Gregora61b3e72010-12-01 17:42:47 +00002925 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002926 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002927 bool PossibleBitfield = false;
2928 if (getCurScope()->getFlags() & Scope::ClassScope) {
2929 // If we're in class scope, this can either be an enum declaration with
2930 // an underlying type, or a declaration of a bitfield member. We try to
2931 // use a simple disambiguation scheme first to catch the common cases
2932 // (integer literal, sizeof); if it's still ambiguous, we then consider
2933 // anything that's a simple-type-specifier followed by '(' as an
2934 // expression. This suffices because function types are not valid
2935 // underlying types anyway.
2936 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2937 // If the next token starts an expression, we know we're parsing a
2938 // bit-field. This is the common case.
2939 if (TPR == TPResult::True())
2940 PossibleBitfield = true;
2941 // If the next token starts a type-specifier-seq, it may be either a
2942 // a fixed underlying type or the start of a function-style cast in C++;
2943 // lookahead one more token to see if it's obvious that we have a
2944 // fixed underlying type.
2945 else if (TPR == TPResult::False() &&
2946 GetLookAheadToken(2).getKind() == tok::semi) {
2947 // Consume the ':'.
2948 ConsumeToken();
2949 } else {
2950 // We have the start of a type-specifier-seq, so we have to perform
2951 // tentative parsing to determine whether we have an expression or a
2952 // type.
2953 TentativeParsingAction TPA(*this);
2954
2955 // Consume the ':'.
2956 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002957
2958 // If we see a type specifier followed by an open-brace, we have an
2959 // ambiguity between an underlying type and a C++11 braced
2960 // function-style cast. Resolve this by always treating it as an
2961 // underlying type.
2962 // FIXME: The standard is not entirely clear on how to disambiguate in
2963 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002964 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002965 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002966 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002967 // We'll parse this as a bitfield later.
2968 PossibleBitfield = true;
2969 TPA.Revert();
2970 } else {
2971 // We have a type-specifier-seq.
2972 TPA.Commit();
2973 }
2974 }
2975 } else {
2976 // Consume the ':'.
2977 ConsumeToken();
2978 }
2979
2980 if (!PossibleBitfield) {
2981 SourceRange Range;
2982 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002983
David Blaikie4e4d0842012-03-11 07:00:24 +00002984 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002985 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2986 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002987 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002988 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002989 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002990 }
2991
Richard Smithbdad7a22012-01-10 01:33:14 +00002992 // There are four options here. If we have 'friend enum foo;' then this is a
2993 // friend declaration, and cannot have an accompanying definition. If we have
2994 // 'enum foo;', then this is a forward declaration. If we have
2995 // 'enum foo {...' then this is a definition. Otherwise we have something
2996 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002997 //
2998 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2999 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3000 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3001 //
John McCallf312b1e2010-08-26 23:41:50 +00003002 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003003 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003004 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003005 } else if (Tok.is(tok::l_brace)) {
3006 if (DS.isFriendSpecified()) {
3007 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3008 << SourceRange(DS.getFriendSpecLoc());
3009 ConsumeBrace();
3010 SkipUntil(tok::r_brace);
3011 TUK = Sema::TUK_Friend;
3012 } else {
3013 TUK = Sema::TUK_Definition;
3014 }
3015 } else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) {
3016 TUK = (DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration);
3017 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003018 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003019 }
3020
3021 // If this is an elaborated type specifier, and we delayed
3022 // diagnostics before, just merge them into the current pool.
3023 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3024 diagsFromTag.redelay();
3025 }
Richard Smith1af83c42012-03-23 03:33:32 +00003026
3027 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003028 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003029 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003030 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3031 // Skip the rest of this declarator, up until the comma or semicolon.
3032 Diag(Tok, diag::err_enum_template);
3033 SkipUntil(tok::comma, true);
3034 return;
3035 }
3036
3037 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3038 // Enumerations can't be explicitly instantiated.
3039 DS.SetTypeSpecError();
3040 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3041 return;
3042 }
3043
3044 assert(TemplateInfo.TemplateParams && "no template parameters");
3045 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3046 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003047 }
Richard Smith1af83c42012-03-23 03:33:32 +00003048
Douglas Gregorb9075602011-02-22 02:55:24 +00003049 if (!Name && TUK != Sema::TUK_Definition) {
3050 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003051
Douglas Gregorb9075602011-02-22 02:55:24 +00003052 // Skip the rest of this declarator, up until the comma or semicolon.
3053 SkipUntil(tok::comma, true);
3054 return;
3055 }
Richard Smith1af83c42012-03-23 03:33:32 +00003056
Douglas Gregor402abb52009-05-28 23:31:59 +00003057 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003058 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003059 const char *PrevSpec = 0;
3060 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003061 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003062 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003063 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003064 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003065 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003066
Douglas Gregor48c89f42010-04-24 16:38:41 +00003067 if (IsDependent) {
3068 // This enum has a dependent nested-name-specifier. Handle it as a
3069 // dependent tag.
3070 if (!Name) {
3071 DS.SetTypeSpecError();
3072 Diag(Tok, diag::err_expected_type_name_after_typename);
3073 return;
3074 }
3075
Douglas Gregor23c94db2010-07-02 17:43:08 +00003076 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003077 TUK, SS, Name, StartLoc,
3078 NameLoc);
3079 if (Type.isInvalid()) {
3080 DS.SetTypeSpecError();
3081 return;
3082 }
3083
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003084 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3085 NameLoc.isValid() ? NameLoc : StartLoc,
3086 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003087 Diag(StartLoc, DiagID) << PrevSpec;
3088
3089 return;
3090 }
Mike Stump1eb44332009-09-09 15:08:12 +00003091
John McCalld226f652010-08-21 09:40:31 +00003092 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003093 // The action failed to produce an enumeration tag. If this is a
3094 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003095 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003096 ConsumeBrace();
3097 SkipUntil(tok::r_brace);
3098 }
3099
3100 DS.SetTypeSpecError();
3101 return;
3102 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003103
Richard Smith7796eb52012-03-12 08:56:40 +00003104 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
John McCall13489672012-05-07 06:16:58 +00003105 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00003106 }
Mike Stump1eb44332009-09-09 15:08:12 +00003107
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003108 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3109 NameLoc.isValid() ? NameLoc : StartLoc,
3110 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003111 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003112}
3113
3114/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3115/// enumerator-list:
3116/// enumerator
3117/// enumerator-list ',' enumerator
3118/// enumerator:
3119/// enumeration-constant
3120/// enumeration-constant '=' constant-expression
3121/// enumeration-constant:
3122/// identifier
3123///
John McCalld226f652010-08-21 09:40:31 +00003124void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003125 // Enter the scope of the enum body and start the definition.
3126 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003127 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003128
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003129 BalancedDelimiterTracker T(*this, tok::l_brace);
3130 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003131
Chris Lattner7946dd32007-08-27 17:24:30 +00003132 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003133 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003134 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003135
Chris Lattner5f9e2722011-07-23 10:55:15 +00003136 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003137
John McCalld226f652010-08-21 09:40:31 +00003138 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003139
Reid Spencer5f016e22007-07-11 17:01:13 +00003140 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003141 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003142 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3143 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003144
John McCall5b629aa2010-10-22 23:36:17 +00003145 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003146 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003147 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003148
Reid Spencer5f016e22007-07-11 17:01:13 +00003149 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003150 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003151 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003152
Chris Lattner04d66662007-10-09 17:33:22 +00003153 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003154 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003155 AssignedVal = ParseConstantExpression();
3156 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003157 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003158 }
Mike Stump1eb44332009-09-09 15:08:12 +00003159
Reid Spencer5f016e22007-07-11 17:01:13 +00003160 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003161 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3162 LastEnumConstDecl,
3163 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003164 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003165 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003166 PD.complete(EnumConstDecl);
3167
Reid Spencer5f016e22007-07-11 17:01:13 +00003168 EnumConstantDecls.push_back(EnumConstDecl);
3169 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003170
Douglas Gregor751f6922010-09-07 14:51:08 +00003171 if (Tok.is(tok::identifier)) {
3172 // We're missing a comma between enumerators.
3173 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3174 Diag(Loc, diag::err_enumerator_list_missing_comma)
3175 << FixItHint::CreateInsertion(Loc, ", ");
3176 continue;
3177 }
3178
Chris Lattner04d66662007-10-09 17:33:22 +00003179 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003180 break;
3181 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003182
Richard Smith7fe62082011-10-15 05:09:34 +00003183 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003184 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003185 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003186 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003187 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003188 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003189 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3190 << FixItHint::CreateRemoval(CommaLoc);
3191 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003192 }
Mike Stump1eb44332009-09-09 15:08:12 +00003193
Reid Spencer5f016e22007-07-11 17:01:13 +00003194 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003195 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003196
Reid Spencer5f016e22007-07-11 17:01:13 +00003197 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003198 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003199 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003200
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003201 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3202 EnumDecl, EnumConstantDecls.data(),
3203 EnumConstantDecls.size(), getCurScope(),
3204 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003205
Douglas Gregor72de6672009-01-08 20:45:30 +00003206 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003207 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3208 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003209}
3210
3211/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003212/// start of a type-qualifier-list.
3213bool Parser::isTypeQualifier() const {
3214 switch (Tok.getKind()) {
3215 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003216
3217 // type-qualifier only in OpenCL
3218 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003219 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003220
Steve Naroff5f8aa692008-02-11 23:15:56 +00003221 // type-qualifier
3222 case tok::kw_const:
3223 case tok::kw_volatile:
3224 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003225 case tok::kw___private:
3226 case tok::kw___local:
3227 case tok::kw___global:
3228 case tok::kw___constant:
3229 case tok::kw___read_only:
3230 case tok::kw___read_write:
3231 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003232 return true;
3233 }
3234}
3235
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003236/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3237/// is definitely a type-specifier. Return false if it isn't part of a type
3238/// specifier or if we're not sure.
3239bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3240 switch (Tok.getKind()) {
3241 default: return false;
3242 // type-specifiers
3243 case tok::kw_short:
3244 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003245 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003246 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003247 case tok::kw_signed:
3248 case tok::kw_unsigned:
3249 case tok::kw__Complex:
3250 case tok::kw__Imaginary:
3251 case tok::kw_void:
3252 case tok::kw_char:
3253 case tok::kw_wchar_t:
3254 case tok::kw_char16_t:
3255 case tok::kw_char32_t:
3256 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003257 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003258 case tok::kw_float:
3259 case tok::kw_double:
3260 case tok::kw_bool:
3261 case tok::kw__Bool:
3262 case tok::kw__Decimal32:
3263 case tok::kw__Decimal64:
3264 case tok::kw__Decimal128:
3265 case tok::kw___vector:
3266
3267 // struct-or-union-specifier (C99) or class-specifier (C++)
3268 case tok::kw_class:
3269 case tok::kw_struct:
3270 case tok::kw_union:
3271 // enum-specifier
3272 case tok::kw_enum:
3273
3274 // typedef-name
3275 case tok::annot_typename:
3276 return true;
3277 }
3278}
3279
Steve Naroff5f8aa692008-02-11 23:15:56 +00003280/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003281/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003282bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003283 switch (Tok.getKind()) {
3284 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003285
Chris Lattner166a8fc2009-01-04 23:41:41 +00003286 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003287 if (TryAltiVecVectorToken())
3288 return true;
3289 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003290 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003291 // Annotate typenames and C++ scope specifiers. If we get one, just
3292 // recurse to handle whatever we get.
3293 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003294 return true;
3295 if (Tok.is(tok::identifier))
3296 return false;
3297 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003298
Chris Lattner166a8fc2009-01-04 23:41:41 +00003299 case tok::coloncolon: // ::foo::bar
3300 if (NextToken().is(tok::kw_new) || // ::new
3301 NextToken().is(tok::kw_delete)) // ::delete
3302 return false;
3303
Chris Lattner166a8fc2009-01-04 23:41:41 +00003304 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003305 return true;
3306 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003307
Reid Spencer5f016e22007-07-11 17:01:13 +00003308 // GNU attributes support.
3309 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003310 // GNU typeof support.
3311 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003312
Reid Spencer5f016e22007-07-11 17:01:13 +00003313 // type-specifiers
3314 case tok::kw_short:
3315 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003316 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003317 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003318 case tok::kw_signed:
3319 case tok::kw_unsigned:
3320 case tok::kw__Complex:
3321 case tok::kw__Imaginary:
3322 case tok::kw_void:
3323 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003324 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003325 case tok::kw_char16_t:
3326 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003327 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003328 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003329 case tok::kw_float:
3330 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003331 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003332 case tok::kw__Bool:
3333 case tok::kw__Decimal32:
3334 case tok::kw__Decimal64:
3335 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003336 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003337
Chris Lattner99dc9142008-04-13 18:59:07 +00003338 // struct-or-union-specifier (C99) or class-specifier (C++)
3339 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003340 case tok::kw_struct:
3341 case tok::kw_union:
3342 // enum-specifier
3343 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003344
Reid Spencer5f016e22007-07-11 17:01:13 +00003345 // type-qualifier
3346 case tok::kw_const:
3347 case tok::kw_volatile:
3348 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003349
3350 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003351 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003352 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003353
Chris Lattner7c186be2008-10-20 00:25:30 +00003354 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3355 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003356 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003357
Steve Naroff239f0732008-12-25 14:16:32 +00003358 case tok::kw___cdecl:
3359 case tok::kw___stdcall:
3360 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003361 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003362 case tok::kw___w64:
3363 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003364 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003365 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003366 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003367
3368 case tok::kw___private:
3369 case tok::kw___local:
3370 case tok::kw___global:
3371 case tok::kw___constant:
3372 case tok::kw___read_only:
3373 case tok::kw___read_write:
3374 case tok::kw___write_only:
3375
Eli Friedman290eeb02009-06-08 23:27:34 +00003376 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003377
3378 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003379 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003380
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003381 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003382 case tok::kw__Atomic:
3383 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003384 }
3385}
3386
3387/// isDeclarationSpecifier() - Return true if the current token is part of a
3388/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003389///
3390/// \param DisambiguatingWithExpression True to indicate that the purpose of
3391/// this check is to disambiguate between an expression and a declaration.
3392bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003393 switch (Tok.getKind()) {
3394 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003395
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003396 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003397 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003398
Chris Lattner166a8fc2009-01-04 23:41:41 +00003399 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003400 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003401 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003402 return false;
John Thompson82287d12010-02-05 00:12:22 +00003403 if (TryAltiVecVectorToken())
3404 return true;
3405 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003406 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003407 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003408 // Annotate typenames and C++ scope specifiers. If we get one, just
3409 // recurse to handle whatever we get.
3410 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003411 return true;
3412 if (Tok.is(tok::identifier))
3413 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003414
3415 // If we're in Objective-C and we have an Objective-C class type followed
3416 // by an identifier and then either ':' or ']', in a place where an
3417 // expression is permitted, then this is probably a class message send
3418 // missing the initial '['. In this case, we won't consider this to be
3419 // the start of a declaration.
3420 if (DisambiguatingWithExpression &&
3421 isStartOfObjCClassMessageMissingOpenBracket())
3422 return false;
3423
John McCall9ba61662010-02-26 08:45:28 +00003424 return isDeclarationSpecifier();
3425
Chris Lattner166a8fc2009-01-04 23:41:41 +00003426 case tok::coloncolon: // ::foo::bar
3427 if (NextToken().is(tok::kw_new) || // ::new
3428 NextToken().is(tok::kw_delete)) // ::delete
3429 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003430
Chris Lattner166a8fc2009-01-04 23:41:41 +00003431 // Annotate typenames and C++ scope specifiers. If we get one, just
3432 // recurse to handle whatever we get.
3433 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003434 return true;
3435 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003436
Reid Spencer5f016e22007-07-11 17:01:13 +00003437 // storage-class-specifier
3438 case tok::kw_typedef:
3439 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003440 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003441 case tok::kw_static:
3442 case tok::kw_auto:
3443 case tok::kw_register:
3444 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003445
Douglas Gregor8d267c52011-09-09 02:06:17 +00003446 // Modules
3447 case tok::kw___module_private__:
3448
Reid Spencer5f016e22007-07-11 17:01:13 +00003449 // type-specifiers
3450 case tok::kw_short:
3451 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003452 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003453 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003454 case tok::kw_signed:
3455 case tok::kw_unsigned:
3456 case tok::kw__Complex:
3457 case tok::kw__Imaginary:
3458 case tok::kw_void:
3459 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003460 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003461 case tok::kw_char16_t:
3462 case tok::kw_char32_t:
3463
Reid Spencer5f016e22007-07-11 17:01:13 +00003464 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003465 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003466 case tok::kw_float:
3467 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003468 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003469 case tok::kw__Bool:
3470 case tok::kw__Decimal32:
3471 case tok::kw__Decimal64:
3472 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003473 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003474
Chris Lattner99dc9142008-04-13 18:59:07 +00003475 // struct-or-union-specifier (C99) or class-specifier (C++)
3476 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003477 case tok::kw_struct:
3478 case tok::kw_union:
3479 // enum-specifier
3480 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003481
Reid Spencer5f016e22007-07-11 17:01:13 +00003482 // type-qualifier
3483 case tok::kw_const:
3484 case tok::kw_volatile:
3485 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003486
Reid Spencer5f016e22007-07-11 17:01:13 +00003487 // function-specifier
3488 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003489 case tok::kw_virtual:
3490 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003491
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003492 // static_assert-declaration
3493 case tok::kw__Static_assert:
3494
Chris Lattner1ef08762007-08-09 17:01:07 +00003495 // GNU typeof support.
3496 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003497
Chris Lattner1ef08762007-08-09 17:01:07 +00003498 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003499 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003500 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003501
Francois Pichete3d49b42011-06-19 08:02:06 +00003502 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003503 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003504 return true;
3505
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003506 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003507 case tok::kw__Atomic:
3508 return true;
3509
Chris Lattnerf3948c42008-07-26 03:38:44 +00003510 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3511 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003512 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003513
Douglas Gregord9d75e52011-04-27 05:41:15 +00003514 // typedef-name
3515 case tok::annot_typename:
3516 return !DisambiguatingWithExpression ||
3517 !isStartOfObjCClassMessageMissingOpenBracket();
3518
Steve Naroff47f52092009-01-06 19:34:12 +00003519 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003520 case tok::kw___cdecl:
3521 case tok::kw___stdcall:
3522 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003523 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003524 case tok::kw___w64:
3525 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003526 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003527 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003528 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003529 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003530
3531 case tok::kw___private:
3532 case tok::kw___local:
3533 case tok::kw___global:
3534 case tok::kw___constant:
3535 case tok::kw___read_only:
3536 case tok::kw___read_write:
3537 case tok::kw___write_only:
3538
Eli Friedman290eeb02009-06-08 23:27:34 +00003539 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003540 }
3541}
3542
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003543bool Parser::isConstructorDeclarator() {
3544 TentativeParsingAction TPA(*this);
3545
3546 // Parse the C++ scope specifier.
3547 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003548 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3549 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003550 TPA.Revert();
3551 return false;
3552 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003553
3554 // Parse the constructor name.
3555 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3556 // We already know that we have a constructor name; just consume
3557 // the token.
3558 ConsumeToken();
3559 } else {
3560 TPA.Revert();
3561 return false;
3562 }
3563
Richard Smith22592862012-03-27 23:05:05 +00003564 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003565 if (Tok.isNot(tok::l_paren)) {
3566 TPA.Revert();
3567 return false;
3568 }
3569 ConsumeParen();
3570
Richard Smith22592862012-03-27 23:05:05 +00003571 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3572 // that we have a constructor.
3573 if (Tok.is(tok::r_paren) ||
3574 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003575 TPA.Revert();
3576 return true;
3577 }
3578
3579 // If we need to, enter the specified scope.
3580 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003581 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003582 DeclScopeObj.EnterDeclaratorScope();
3583
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003584 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003585 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003586 MaybeParseMicrosoftAttributes(Attrs);
3587
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003588 // Check whether the next token(s) are part of a declaration
3589 // specifier, in which case we have the start of a parameter and,
3590 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003591 bool IsConstructor = false;
3592 if (isDeclarationSpecifier())
3593 IsConstructor = true;
3594 else if (Tok.is(tok::identifier) ||
3595 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3596 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3597 // This might be a parenthesized member name, but is more likely to
3598 // be a constructor declaration with an invalid argument type. Keep
3599 // looking.
3600 if (Tok.is(tok::annot_cxxscope))
3601 ConsumeToken();
3602 ConsumeToken();
3603
3604 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003605 // which must have one of the following syntactic forms (see the
3606 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003607 switch (Tok.getKind()) {
3608 case tok::l_paren:
3609 // C(X ( int));
3610 case tok::l_square:
3611 // C(X [ 5]);
3612 // C(X [ [attribute]]);
3613 case tok::coloncolon:
3614 // C(X :: Y);
3615 // C(X :: *p);
3616 case tok::r_paren:
3617 // C(X )
3618 // Assume this isn't a constructor, rather than assuming it's a
3619 // constructor with an unnamed parameter of an ill-formed type.
3620 break;
3621
3622 default:
3623 IsConstructor = true;
3624 break;
3625 }
3626 }
3627
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003628 TPA.Revert();
3629 return IsConstructor;
3630}
Reid Spencer5f016e22007-07-11 17:01:13 +00003631
3632/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003633/// type-qualifier-list: [C99 6.7.5]
3634/// type-qualifier
3635/// [vendor] attributes
3636/// [ only if VendorAttributesAllowed=true ]
3637/// type-qualifier-list type-qualifier
3638/// [vendor] type-qualifier-list attributes
3639/// [ only if VendorAttributesAllowed=true ]
3640/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3641/// [ only if CXX0XAttributesAllowed=true ]
3642/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003643///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003644void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3645 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003646 bool CXX11AttributesAllowed) {
3647 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003648 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003649 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003650 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003651 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003652 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003653
3654 SourceLocation EndLoc;
3655
Reid Spencer5f016e22007-07-11 17:01:13 +00003656 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003657 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003658 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003659 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003660 SourceLocation Loc = Tok.getLocation();
3661
3662 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003663 case tok::code_completion:
3664 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003665 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003666
Reid Spencer5f016e22007-07-11 17:01:13 +00003667 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003668 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003669 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003670 break;
3671 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003672 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003673 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003674 break;
3675 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003676 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003677 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003678 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003679
3680 // OpenCL qualifiers:
3681 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003682 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003683 goto DoneWithTypeQuals;
3684 case tok::kw___private:
3685 case tok::kw___global:
3686 case tok::kw___local:
3687 case tok::kw___constant:
3688 case tok::kw___read_only:
3689 case tok::kw___write_only:
3690 case tok::kw___read_write:
3691 ParseOpenCLQualifiers(DS);
3692 break;
3693
Eli Friedman290eeb02009-06-08 23:27:34 +00003694 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003695 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003696 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003697 case tok::kw___cdecl:
3698 case tok::kw___stdcall:
3699 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003700 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003701 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003702 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003703 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003704 continue;
3705 }
3706 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003707 case tok::kw___pascal:
3708 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003709 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003710 continue;
3711 }
3712 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003713 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003714 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003715 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003716 continue; // do *not* consume the next token!
3717 }
3718 // otherwise, FALL THROUGH!
3719 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003720 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003721 // If this is not a type-qualifier token, we're done reading type
3722 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003723 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003724 if (EndLoc.isValid())
3725 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003726 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003727 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003728
Reid Spencer5f016e22007-07-11 17:01:13 +00003729 // If the specifier combination wasn't legal, issue a diagnostic.
3730 if (isInvalid) {
3731 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003732 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003733 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003734 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003735 }
3736}
3737
3738
3739/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3740///
3741void Parser::ParseDeclarator(Declarator &D) {
3742 /// This implements the 'declarator' production in the C grammar, then checks
3743 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003744 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003745}
3746
Richard Smith9988f282012-03-29 01:16:42 +00003747static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3748 if (Kind == tok::star || Kind == tok::caret)
3749 return true;
3750
3751 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3752 if (!Lang.CPlusPlus)
3753 return false;
3754
3755 return Kind == tok::amp || Kind == tok::ampamp;
3756}
3757
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003758/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3759/// is parsed by the function passed to it. Pass null, and the direct-declarator
3760/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003761/// ptr-operator production.
3762///
Richard Smith0706df42011-10-19 21:33:05 +00003763/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003764/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3765/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003766///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003767/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3768/// [C] pointer[opt] direct-declarator
3769/// [C++] direct-declarator
3770/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003771///
3772/// pointer: [C99 6.7.5]
3773/// '*' type-qualifier-list[opt]
3774/// '*' type-qualifier-list[opt] pointer
3775///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003776/// ptr-operator:
3777/// '*' cv-qualifier-seq[opt]
3778/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003779/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003780/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003781/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003782/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003783void Parser::ParseDeclaratorInternal(Declarator &D,
3784 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003785 if (Diags.hasAllExtensionsSilenced())
3786 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003787
Sebastian Redlf30208a2009-01-24 21:16:55 +00003788 // C++ member pointers start with a '::' or a nested-name.
3789 // Member pointers get special handling, since there's no place for the
3790 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003791 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003792 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3793 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003794 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3795 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003796 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003797 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003798
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003799 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003800 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003801 // The scope spec really belongs to the direct-declarator.
3802 D.getCXXScopeSpec() = SS;
3803 if (DirectDeclParser)
3804 (this->*DirectDeclParser)(D);
3805 return;
3806 }
3807
3808 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003809 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003810 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003811 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003812 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003813
3814 // Recurse to parse whatever is left.
3815 ParseDeclaratorInternal(D, DirectDeclParser);
3816
3817 // Sema will have to catch (syntactically invalid) pointers into global
3818 // scope. It has to catch pointers into namespace scope anyway.
3819 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003820 Loc),
3821 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003822 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003823 return;
3824 }
3825 }
3826
3827 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003828 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003829 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003830 if (DirectDeclParser)
3831 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003832 return;
3833 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003834
Sebastian Redl05532f22009-03-15 22:02:01 +00003835 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3836 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003837 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003838 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003839
Chris Lattner9af55002009-03-27 04:18:06 +00003840 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003841 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003842 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003843
Richard Smith6ee326a2012-04-10 01:32:12 +00003844 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00003845 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003846 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003847
Reid Spencer5f016e22007-07-11 17:01:13 +00003848 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003849 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003850 if (Kind == tok::star)
3851 // Remember that we parsed a pointer type, and remember the type-quals.
3852 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003853 DS.getConstSpecLoc(),
3854 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003855 DS.getRestrictSpecLoc()),
3856 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003857 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003858 else
3859 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003860 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003861 Loc),
3862 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003863 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003864 } else {
3865 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003866 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003867
Sebastian Redl743de1f2009-03-23 00:00:23 +00003868 // Complain about rvalue references in C++03, but then go on and build
3869 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003870 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003871 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003872 diag::warn_cxx98_compat_rvalue_reference :
3873 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003874
Richard Smith6ee326a2012-04-10 01:32:12 +00003875 // GNU-style and C++11 attributes are allowed here, as is restrict.
3876 ParseTypeQualifierListOpt(DS);
3877 D.ExtendWithDeclSpec(DS);
3878
Reid Spencer5f016e22007-07-11 17:01:13 +00003879 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3880 // cv-qualifiers are introduced through the use of a typedef or of a
3881 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00003882 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3883 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3884 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003885 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003886 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3887 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003888 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003889 }
3890
3891 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003892 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003893
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003894 if (D.getNumTypeObjects() > 0) {
3895 // C++ [dcl.ref]p4: There shall be no references to references.
3896 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3897 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003898 if (const IdentifierInfo *II = D.getIdentifier())
3899 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3900 << II;
3901 else
3902 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3903 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003904
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003905 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003906 // can go ahead and build the (technically ill-formed)
3907 // declarator: reference collapsing will take care of it.
3908 }
3909 }
3910
Reid Spencer5f016e22007-07-11 17:01:13 +00003911 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003912 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003913 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003914 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003915 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003916 }
3917}
3918
Richard Smith9988f282012-03-29 01:16:42 +00003919static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
3920 SourceLocation EllipsisLoc) {
3921 if (EllipsisLoc.isValid()) {
3922 FixItHint Insertion;
3923 if (!D.getEllipsisLoc().isValid()) {
3924 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
3925 D.setEllipsisLoc(EllipsisLoc);
3926 }
3927 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
3928 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
3929 }
3930}
3931
Reid Spencer5f016e22007-07-11 17:01:13 +00003932/// ParseDirectDeclarator
3933/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003934/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003935/// '(' declarator ')'
3936/// [GNU] '(' attributes declarator ')'
3937/// [C90] direct-declarator '[' constant-expression[opt] ']'
3938/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3939/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3940/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3941/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00003942/// [C++11] direct-declarator '[' constant-expression[opt] ']'
3943/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00003944/// direct-declarator '(' parameter-type-list ')'
3945/// direct-declarator '(' identifier-list[opt] ')'
3946/// [GNU] direct-declarator '(' parameter-forward-declarations
3947/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003948/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3949/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00003950/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
3951/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
3952/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003953/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00003954/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003955///
3956/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003957/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003958/// '::'[opt] nested-name-specifier[opt] type-name
3959///
3960/// id-expression: [C++ 5.1]
3961/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003962/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003963///
3964/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003965/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003966/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003967/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003968/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003969/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003970///
Richard Smith5d8388c2012-03-27 01:42:32 +00003971/// Note, any additional constructs added here may need corresponding changes
3972/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00003973void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003974 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003975
David Blaikie4e4d0842012-03-11 07:00:24 +00003976 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003977 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003978 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003979 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3980 D.getContext() == Declarator::MemberContext;
3981 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3982 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003983 }
3984
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003985 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003986 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003987 // Change the declaration context for name lookup, until this function
3988 // is exited (and the declarator has been parsed).
3989 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003990 }
3991
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003992 // C++0x [dcl.fct]p14:
3993 // There is a syntactic ambiguity when an ellipsis occurs at the end
3994 // of a parameter-declaration-clause without a preceding comma. In
3995 // this case, the ellipsis is parsed as part of the
3996 // abstract-declarator if the type of the parameter names a template
3997 // parameter pack that has not been expanded; otherwise, it is parsed
3998 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00003999 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004000 !((D.getContext() == Declarator::PrototypeContext ||
4001 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004002 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00004003 !Actions.containsUnexpandedParameterPacks(D))) {
4004 SourceLocation EllipsisLoc = ConsumeToken();
4005 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4006 // The ellipsis was put in the wrong place. Recover, and explain to
4007 // the user what they should have done.
4008 ParseDeclarator(D);
4009 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4010 return;
4011 } else
4012 D.setEllipsisLoc(EllipsisLoc);
4013
4014 // The ellipsis can't be followed by a parenthesized declarator. We
4015 // check for that in ParseParenDeclarator, after we have disambiguated
4016 // the l_paren token.
4017 }
4018
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004019 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4020 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4021 // We found something that indicates the start of an unqualified-id.
4022 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004023 bool AllowConstructorName;
4024 if (D.getDeclSpec().hasTypeSpecifier())
4025 AllowConstructorName = false;
4026 else if (D.getCXXScopeSpec().isSet())
4027 AllowConstructorName =
4028 (D.getContext() == Declarator::FileContext ||
4029 (D.getContext() == Declarator::MemberContext &&
4030 D.getDeclSpec().isFriendSpecified()));
4031 else
4032 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4033
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004034 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004035 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4036 /*EnteringContext=*/true,
4037 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004038 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004039 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004040 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004041 D.getName()) ||
4042 // Once we're past the identifier, if the scope was bad, mark the
4043 // whole declarator bad.
4044 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004045 D.SetIdentifier(0, Tok.getLocation());
4046 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004047 } else {
4048 // Parsed the unqualified-id; update range information and move along.
4049 if (D.getSourceRange().getBegin().isInvalid())
4050 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4051 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004052 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004053 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004054 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004055 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004056 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004057 "There's a C++-specific check for tok::identifier above");
4058 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4059 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4060 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004061 goto PastIdentifier;
4062 }
Richard Smith9988f282012-03-29 01:16:42 +00004063
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004064 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004065 // direct-declarator: '(' declarator ')'
4066 // direct-declarator: '(' attributes declarator ')'
4067 // Example: 'char (*X)' or 'int (*XX)(void)'
4068 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004069
4070 // If the declarator was parenthesized, we entered the declarator
4071 // scope when parsing the parenthesized declarator, then exited
4072 // the scope already. Re-enter the scope, if we need to.
4073 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004074 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004075 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004076 if (!D.isInvalidType() &&
4077 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004078 // Change the declaration context for name lookup, until this function
4079 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004080 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004081 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004082 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004083 // This could be something simple like "int" (in which case the declarator
4084 // portion is empty), if an abstract-declarator is allowed.
4085 D.SetIdentifier(0, Tok.getLocation());
4086 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00004087 if (D.getContext() == Declarator::MemberContext)
4088 Diag(Tok, diag::err_expected_member_name_or_semi)
4089 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004090 else if (getLangOpts().CPlusPlus)
4091 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004092 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004093 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004094 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004095 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004096 }
Mike Stump1eb44332009-09-09 15:08:12 +00004097
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004098 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004099 assert(D.isPastIdentifier() &&
4100 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004101
Richard Smith6ee326a2012-04-10 01:32:12 +00004102 // Don't parse attributes unless we have parsed an unparenthesized name.
4103 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004104 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004105
Reid Spencer5f016e22007-07-11 17:01:13 +00004106 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004107 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004108 // Enter function-declaration scope, limiting any declarators to the
4109 // function prototype scope, including parameter declarators.
4110 ParseScope PrototypeScope(this,
4111 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004112 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4113 // In such a case, check if we actually have a function declarator; if it
4114 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00004115 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00004116 // When not in file scope, warn for ambiguous function declarators, just
4117 // in case the author intended it as a variable definition.
4118 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4119 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4120 break;
4121 }
John McCall0b7e6782011-03-24 11:26:52 +00004122 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004123 BalancedDelimiterTracker T(*this, tok::l_paren);
4124 T.consumeOpen();
4125 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004126 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004127 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004128 ParseBracketDeclarator(D);
4129 } else {
4130 break;
4131 }
4132 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004133}
Reid Spencer5f016e22007-07-11 17:01:13 +00004134
Chris Lattneref4715c2008-04-06 05:45:57 +00004135/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4136/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004137/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004138/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4139///
4140/// direct-declarator:
4141/// '(' declarator ')'
4142/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004143/// direct-declarator '(' parameter-type-list ')'
4144/// direct-declarator '(' identifier-list[opt] ')'
4145/// [GNU] direct-declarator '(' parameter-forward-declarations
4146/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004147///
4148void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004149 BalancedDelimiterTracker T(*this, tok::l_paren);
4150 T.consumeOpen();
4151
Chris Lattneref4715c2008-04-06 05:45:57 +00004152 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004153
Chris Lattner7399ee02008-10-20 02:05:46 +00004154 // Eat any attributes before we look at whether this is a grouping or function
4155 // declarator paren. If this is a grouping paren, the attribute applies to
4156 // the type being built up, for example:
4157 // int (__attribute__(()) *x)(long y)
4158 // If this ends up not being a grouping paren, the attribute applies to the
4159 // first argument, for example:
4160 // int (__attribute__(()) int x)
4161 // In either case, we need to eat any attributes to be able to determine what
4162 // sort of paren this is.
4163 //
John McCall0b7e6782011-03-24 11:26:52 +00004164 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004165 bool RequiresArg = false;
4166 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004167 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004168
Chris Lattner7399ee02008-10-20 02:05:46 +00004169 // We require that the argument list (if this is a non-grouping paren) be
4170 // present even if the attribute list was empty.
4171 RequiresArg = true;
4172 }
Steve Naroff239f0732008-12-25 14:16:32 +00004173 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004174 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004175 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004176 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004177 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004178 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004179 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004180 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004181 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004182 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004183
Chris Lattneref4715c2008-04-06 05:45:57 +00004184 // If we haven't past the identifier yet (or where the identifier would be
4185 // stored, if this is an abstract declarator), then this is probably just
4186 // grouping parens. However, if this could be an abstract-declarator, then
4187 // this could also be the start of function arguments (consider 'void()').
4188 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004189
Chris Lattneref4715c2008-04-06 05:45:57 +00004190 if (!D.mayOmitIdentifier()) {
4191 // If this can't be an abstract-declarator, this *must* be a grouping
4192 // paren, because we haven't seen the identifier yet.
4193 isGrouping = true;
4194 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004195 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4196 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004197 isDeclarationSpecifier() || // 'int(int)' is a function.
4198 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004199 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4200 // considered to be a type, not a K&R identifier-list.
4201 isGrouping = false;
4202 } else {
4203 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4204 isGrouping = true;
4205 }
Mike Stump1eb44332009-09-09 15:08:12 +00004206
Chris Lattneref4715c2008-04-06 05:45:57 +00004207 // If this is a grouping paren, handle:
4208 // direct-declarator: '(' declarator ')'
4209 // direct-declarator: '(' attributes declarator ')'
4210 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004211 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4212 D.setEllipsisLoc(SourceLocation());
4213
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004214 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004215 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004216 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004217 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004218 T.consumeClose();
4219 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4220 T.getCloseLocation()),
4221 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004222
4223 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004224
4225 // An ellipsis cannot be placed outside parentheses.
4226 if (EllipsisLoc.isValid())
4227 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4228
Chris Lattneref4715c2008-04-06 05:45:57 +00004229 return;
4230 }
Mike Stump1eb44332009-09-09 15:08:12 +00004231
Chris Lattneref4715c2008-04-06 05:45:57 +00004232 // Okay, if this wasn't a grouping paren, it must be the start of a function
4233 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004234 // identifier (and remember where it would have been), then call into
4235 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004236 D.SetIdentifier(0, Tok.getLocation());
4237
David Blaikie42d6d0c2011-12-04 05:04:18 +00004238 // Enter function-declaration scope, limiting any declarators to the
4239 // function prototype scope, including parameter declarators.
4240 ParseScope PrototypeScope(this,
4241 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004242 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004243 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004244}
4245
4246/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4247/// declarator D up to a paren, which indicates that we are parsing function
4248/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004249///
Richard Smith6ee326a2012-04-10 01:32:12 +00004250/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4251/// immediately after the open paren - they should be considered to be the
4252/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004253///
Richard Smith6ee326a2012-04-10 01:32:12 +00004254/// If RequiresArg is true, then the first argument of the function is required
4255/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004256///
Richard Smith6ee326a2012-04-10 01:32:12 +00004257/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4258/// (C++11) ref-qualifier[opt], exception-specification[opt],
4259/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4260///
4261/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004262/// dynamic-exception-specification
4263/// noexcept-specification
4264///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004265void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004266 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004267 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004268 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004269 assert(getCurScope()->isFunctionPrototypeScope() &&
4270 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004271 // lparen is already consumed!
4272 assert(D.isPastIdentifier() && "Should not call before identifier!");
4273
4274 // This should be true when the function has typed arguments.
4275 // Otherwise, it is treated as a K&R-style function.
4276 bool HasProto = false;
4277 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004278 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004279 // Remember where we see an ellipsis, if any.
4280 SourceLocation EllipsisLoc;
4281
4282 DeclSpec DS(AttrFactory);
4283 bool RefQualifierIsLValueRef = true;
4284 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004285 SourceLocation ConstQualifierLoc;
4286 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004287 ExceptionSpecificationType ESpecType = EST_None;
4288 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004289 SmallVector<ParsedType, 2> DynamicExceptions;
4290 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004291 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004292 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004293 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004294
James Molloy16f1f712012-02-29 10:24:19 +00004295 Actions.ActOnStartFunctionDeclarator();
4296
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004297 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004298 if (isFunctionDeclaratorIdentifierList()) {
4299 if (RequiresArg)
4300 Diag(Tok, diag::err_argument_required_after_attribute);
4301
4302 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4303
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004304 Tracker.consumeClose();
4305 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004306 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004307 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004308 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004309 else if (RequiresArg)
4310 Diag(Tok, diag::err_argument_required_after_attribute);
4311
David Blaikie4e4d0842012-03-11 07:00:24 +00004312 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004313
4314 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004315 Tracker.consumeClose();
4316 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004317
David Blaikie4e4d0842012-03-11 07:00:24 +00004318 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004319 // FIXME: Accept these components in any order, and produce fixits to
4320 // correct the order if the user gets it wrong. Ideally we should deal
4321 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004322
4323 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004324 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4325 if (!DS.getSourceRange().getEnd().isInvalid()) {
4326 EndLoc = DS.getSourceRange().getEnd();
4327 ConstQualifierLoc = DS.getConstSpecLoc();
4328 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4329 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004330
4331 // Parse ref-qualifier[opt].
4332 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004333 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004334 diag::warn_cxx98_compat_ref_qualifier :
4335 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004336
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004337 RefQualifierIsLValueRef = Tok.is(tok::amp);
4338 RefQualifierLoc = ConsumeToken();
4339 EndLoc = RefQualifierLoc;
4340 }
4341
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004342 // C++11 [expr.prim.general]p3:
4343 // If a declaration declares a member function or member function
4344 // template of a class X, the expression this is a prvalue of type
4345 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
4346 // and the end of the function-definition, member-declarator, or
4347 // declarator.
4348 bool IsCXX11MemberFunction =
4349 getLangOpts().CPlusPlus0x &&
4350 (D.getContext() == Declarator::MemberContext ||
4351 (D.getContext() == Declarator::FileContext &&
4352 D.getCXXScopeSpec().isValid() &&
4353 Actions.CurContext->isRecord()));
4354 Sema::CXXThisScopeRAII ThisScope(Actions,
4355 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4356 DS.getTypeQualifiers(),
4357 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004358
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004359 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004360 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004361 DynamicExceptions,
4362 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004363 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004364 if (ESpecType != EST_None)
4365 EndLoc = ESpecRange.getEnd();
4366
Richard Smith6ee326a2012-04-10 01:32:12 +00004367 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4368 // after the exception-specification.
4369 MaybeParseCXX0XAttributes(FnAttrs);
4370
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004371 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004372 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004373 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004374 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004375 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004376 if (Range.getEnd().isValid())
4377 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004378 }
4379 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004380 }
4381
4382 // Remember that we parsed a function type, and remember the attributes.
4383 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4384 /*isVariadic=*/EllipsisLoc.isValid(),
4385 EllipsisLoc,
4386 ParamInfo.data(), ParamInfo.size(),
4387 DS.getTypeQualifiers(),
4388 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004389 RefQualifierLoc, ConstQualifierLoc,
4390 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004391 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004392 ESpecType, ESpecRange.getBegin(),
4393 DynamicExceptions.data(),
4394 DynamicExceptionRanges.data(),
4395 DynamicExceptions.size(),
4396 NoexceptExpr.isUsable() ?
4397 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004398 Tracker.getOpenLocation(),
4399 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004400 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004401 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004402
4403 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004404}
4405
4406/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4407/// identifier list form for a K&R-style function: void foo(a,b,c)
4408///
4409/// Note that identifier-lists are only allowed for normal declarators, not for
4410/// abstract-declarators.
4411bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004412 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004413 && Tok.is(tok::identifier)
4414 && !TryAltiVecVectorToken()
4415 // K&R identifier lists can't have typedefs as identifiers, per C99
4416 // 6.7.5.3p11.
4417 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4418 // Identifier lists follow a really simple grammar: the identifiers can
4419 // be followed *only* by a ", identifier" or ")". However, K&R
4420 // identifier lists are really rare in the brave new modern world, and
4421 // it is very common for someone to typo a type in a non-K&R style
4422 // list. If we are presented with something like: "void foo(intptr x,
4423 // float y)", we don't want to start parsing the function declarator as
4424 // though it is a K&R style declarator just because intptr is an
4425 // invalid type.
4426 //
4427 // To handle this, we check to see if the token after the first
4428 // identifier is a "," or ")". Only then do we parse it as an
4429 // identifier list.
4430 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4431}
4432
4433/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4434/// we found a K&R-style identifier list instead of a typed parameter list.
4435///
4436/// After returning, ParamInfo will hold the parsed parameters.
4437///
4438/// identifier-list: [C99 6.7.5]
4439/// identifier
4440/// identifier-list ',' identifier
4441///
4442void Parser::ParseFunctionDeclaratorIdentifierList(
4443 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004444 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004445 // If there was no identifier specified for the declarator, either we are in
4446 // an abstract-declarator, or we are in a parameter declarator which was found
4447 // to be abstract. In abstract-declarators, identifier lists are not valid:
4448 // diagnose this.
4449 if (!D.getIdentifier())
4450 Diag(Tok, diag::ext_ident_list_in_param);
4451
4452 // Maintain an efficient lookup of params we have seen so far.
4453 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4454
4455 while (1) {
4456 // If this isn't an identifier, report the error and skip until ')'.
4457 if (Tok.isNot(tok::identifier)) {
4458 Diag(Tok, diag::err_expected_ident);
4459 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4460 // Forget we parsed anything.
4461 ParamInfo.clear();
4462 return;
4463 }
4464
4465 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4466
4467 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4468 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4469 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4470
4471 // Verify that the argument identifier has not already been mentioned.
4472 if (!ParamsSoFar.insert(ParmII)) {
4473 Diag(Tok, diag::err_param_redefinition) << ParmII;
4474 } else {
4475 // Remember this identifier in ParamInfo.
4476 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4477 Tok.getLocation(),
4478 0));
4479 }
4480
4481 // Eat the identifier.
4482 ConsumeToken();
4483
4484 // The list continues if we see a comma.
4485 if (Tok.isNot(tok::comma))
4486 break;
4487 ConsumeToken();
4488 }
4489}
4490
4491/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4492/// after the opening parenthesis. This function will not parse a K&R-style
4493/// identifier list.
4494///
Richard Smith6ce48a72012-04-11 04:01:28 +00004495/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4496/// caller parsed those arguments immediately after the open paren - they should
4497/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004498///
4499/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4500/// be the location of the ellipsis, if any was parsed.
4501///
Reid Spencer5f016e22007-07-11 17:01:13 +00004502/// parameter-type-list: [C99 6.7.5]
4503/// parameter-list
4504/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004505/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004506///
4507/// parameter-list: [C99 6.7.5]
4508/// parameter-declaration
4509/// parameter-list ',' parameter-declaration
4510///
4511/// parameter-declaration: [C99 6.7.5]
4512/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004513/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004514/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004515/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004516/// declaration-specifiers abstract-declarator[opt]
4517/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004518/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004519/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004520/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004521///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004522void Parser::ParseParameterDeclarationClause(
4523 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004524 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004525 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004526 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004527
Chris Lattnerf97409f2008-04-06 06:57:35 +00004528 while (1) {
4529 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004530 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4531 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004532 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004533 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004534 }
Mike Stump1eb44332009-09-09 15:08:12 +00004535
Chris Lattnerf97409f2008-04-06 06:57:35 +00004536 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004537 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004538 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004539
Richard Smith6ce48a72012-04-11 04:01:28 +00004540 // Parse any C++11 attributes.
4541 MaybeParseCXX0XAttributes(DS.getAttributes());
4542
John McCall7f040a92010-12-24 02:08:15 +00004543 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004544 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004545 ParseMicrosoftAttributes(DS.getAttributes());
4546
4547 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004548
4549 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004550 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004551 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004552 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4553 // too much hassle.
4554 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004555
Chris Lattnere64c5492009-02-27 18:38:20 +00004556 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004557
Chris Lattnerf97409f2008-04-06 06:57:35 +00004558 // Parse the declarator. This is "PrototypeContext", because we must
4559 // accept either 'declarator' or 'abstract-declarator' here.
4560 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4561 ParseDeclarator(ParmDecl);
4562
4563 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004564 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004565
Chris Lattnerf97409f2008-04-06 06:57:35 +00004566 // Remember this parsed parameter in ParamInfo.
4567 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004568
Douglas Gregor72b505b2008-12-16 21:30:33 +00004569 // DefArgToks is used when the parsing of default arguments needs
4570 // to be delayed.
4571 CachedTokens *DefArgToks = 0;
4572
Chris Lattnerf97409f2008-04-06 06:57:35 +00004573 // If no parameter was specified, verify that *something* was specified,
4574 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004575 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4576 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004577 // Completely missing, emit error.
4578 Diag(DSStart, diag::err_missing_param);
4579 } else {
4580 // Otherwise, we have something. Add it and let semantic analysis try
4581 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004582
Chris Lattnerf97409f2008-04-06 06:57:35 +00004583 // Inform the actions module about the parameter declarator, so it gets
4584 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004585 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004586
4587 // Parse the default argument, if any. We parse the default
4588 // arguments in all dialects; the semantic analysis in
4589 // ActOnParamDefaultArgument will reject the default argument in
4590 // C.
4591 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004592 SourceLocation EqualLoc = Tok.getLocation();
4593
Chris Lattner04421082008-04-08 04:40:51 +00004594 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004595 if (D.getContext() == Declarator::MemberContext) {
4596 // If we're inside a class definition, cache the tokens
4597 // corresponding to the default argument. We'll actually parse
4598 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004599 // FIXME: Can we use a smart pointer for Toks?
4600 DefArgToks = new CachedTokens;
4601
Mike Stump1eb44332009-09-09 15:08:12 +00004602 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004603 /*StopAtSemi=*/true,
4604 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004605 delete DefArgToks;
4606 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004607 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004608 } else {
4609 // Mark the end of the default argument so that we know when to
4610 // stop when we parse it later on.
4611 Token DefArgEnd;
4612 DefArgEnd.startToken();
4613 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4614 DefArgEnd.setLocation(Tok.getLocation());
4615 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004616 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004617 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004618 }
Chris Lattner04421082008-04-08 04:40:51 +00004619 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004620 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004621 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004622
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004623 // The argument isn't actually potentially evaluated unless it is
4624 // used.
4625 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004626 Sema::PotentiallyEvaluatedIfUsed,
4627 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004628
Sebastian Redl84407ba2012-03-14 15:54:00 +00004629 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004630 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4631 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004632 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004633 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004634 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004635 if (DefArgResult.isInvalid()) {
4636 Actions.ActOnParamDefaultArgumentError(Param);
4637 SkipUntil(tok::comma, tok::r_paren, true, true);
4638 } else {
4639 // Inform the actions module about the default argument
4640 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004641 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004642 }
Chris Lattner04421082008-04-08 04:40:51 +00004643 }
4644 }
Mike Stump1eb44332009-09-09 15:08:12 +00004645
4646 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4647 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004648 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004649 }
4650
4651 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004652 if (Tok.isNot(tok::comma)) {
4653 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004654 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4655
David Blaikie4e4d0842012-03-11 07:00:24 +00004656 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004657 // We have ellipsis without a preceding ',', which is ill-formed
4658 // in C. Complain and provide the fix.
4659 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004660 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004661 }
4662 }
4663
4664 break;
4665 }
Mike Stump1eb44332009-09-09 15:08:12 +00004666
Chris Lattnerf97409f2008-04-06 06:57:35 +00004667 // Consume the comma.
4668 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004669 }
Mike Stump1eb44332009-09-09 15:08:12 +00004670
Chris Lattner66d28652008-04-06 06:34:08 +00004671}
Chris Lattneref4715c2008-04-06 05:45:57 +00004672
Reid Spencer5f016e22007-07-11 17:01:13 +00004673/// [C90] direct-declarator '[' constant-expression[opt] ']'
4674/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4675/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4676/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4677/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004678/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4679/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004680void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004681 if (CheckProhibitedCXX11Attribute())
4682 return;
4683
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004684 BalancedDelimiterTracker T(*this, tok::l_square);
4685 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004686
Chris Lattner378c7e42008-12-18 07:27:21 +00004687 // C array syntax has many features, but by-far the most common is [] and [4].
4688 // This code does a fast path to handle some of the most obvious cases.
4689 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004690 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004691 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004692 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004693
Chris Lattner378c7e42008-12-18 07:27:21 +00004694 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004695 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004696 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004697 T.getOpenLocation(),
4698 T.getCloseLocation()),
4699 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004700 return;
4701 } else if (Tok.getKind() == tok::numeric_constant &&
4702 GetLookAheadToken(1).is(tok::r_square)) {
4703 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004704 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004705 ConsumeToken();
4706
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004707 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004708 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004709 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004710
Chris Lattner378c7e42008-12-18 07:27:21 +00004711 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004712 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004713 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004714 T.getOpenLocation(),
4715 T.getCloseLocation()),
4716 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004717 return;
4718 }
Mike Stump1eb44332009-09-09 15:08:12 +00004719
Reid Spencer5f016e22007-07-11 17:01:13 +00004720 // If valid, this location is the position where we read the 'static' keyword.
4721 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004722 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004723 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004724
Reid Spencer5f016e22007-07-11 17:01:13 +00004725 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004726 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004727 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004728 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004729
Reid Spencer5f016e22007-07-11 17:01:13 +00004730 // If we haven't already read 'static', check to see if there is one after the
4731 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004732 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004733 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004734
Reid Spencer5f016e22007-07-11 17:01:13 +00004735 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4736 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004737 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004738
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004739 // Handle the case where we have '[*]' as the array size. However, a leading
4740 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4741 // the the token after the star is a ']'. Since stars in arrays are
4742 // infrequent, use of lookahead is not costly here.
4743 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004744 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004745
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004746 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004747 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004748 StaticLoc = SourceLocation(); // Drop the static.
4749 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004750 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004751 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004752 // Note, in C89, this production uses the constant-expr production instead
4753 // of assignment-expr. The only difference is that assignment-expr allows
4754 // things like '=' and '*='. Sema rejects these in C89 mode because they
4755 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004756
Douglas Gregore0762c92009-06-19 23:52:42 +00004757 // Parse the constant-expression or assignment-expression now (depending
4758 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004759 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004760 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004761 } else {
4762 EnterExpressionEvaluationContext Unevaluated(Actions,
4763 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004764 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004765 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004766 }
Mike Stump1eb44332009-09-09 15:08:12 +00004767
Reid Spencer5f016e22007-07-11 17:01:13 +00004768 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004769 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004770 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004771 // If the expression was invalid, skip it.
4772 SkipUntil(tok::r_square);
4773 return;
4774 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004775
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004776 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004777
John McCall0b7e6782011-03-24 11:26:52 +00004778 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004779 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004780
Chris Lattner378c7e42008-12-18 07:27:21 +00004781 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004782 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004783 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004784 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004785 T.getOpenLocation(),
4786 T.getCloseLocation()),
4787 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004788}
4789
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004790/// [GNU] typeof-specifier:
4791/// typeof ( expressions )
4792/// typeof ( type-name )
4793/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004794///
4795void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004796 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004797 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004798 SourceLocation StartLoc = ConsumeToken();
4799
John McCallcfb708c2010-01-13 20:03:27 +00004800 const bool hasParens = Tok.is(tok::l_paren);
4801
Eli Friedman71b8fb52012-01-21 01:01:51 +00004802 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4803
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004804 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004805 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004806 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004807 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4808 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004809 if (hasParens)
4810 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004811
4812 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004813 // FIXME: Not accurate, the range gets one token more than it should.
4814 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004815 else
4816 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004817
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004818 if (isCastExpr) {
4819 if (!CastTy) {
4820 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004821 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004822 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004823
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004824 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004825 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004826 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4827 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004828 DiagID, CastTy))
4829 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004830 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004831 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004832
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004833 // If we get here, the operand to the typeof was an expresion.
4834 if (Operand.isInvalid()) {
4835 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004836 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004837 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004838
Eli Friedman71b8fb52012-01-21 01:01:51 +00004839 // We might need to transform the operand if it is potentially evaluated.
4840 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4841 if (Operand.isInvalid()) {
4842 DS.SetTypeSpecError();
4843 return;
4844 }
4845
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004846 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004847 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004848 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4849 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004850 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004851 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004852}
Chris Lattner1b492422010-02-28 18:33:55 +00004853
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004854/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004855/// _Atomic ( type-name )
4856///
4857void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4858 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4859
4860 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004861 BalancedDelimiterTracker T(*this, tok::l_paren);
4862 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004863 SkipUntil(tok::r_paren);
4864 return;
4865 }
4866
4867 TypeResult Result = ParseTypeName();
4868 if (Result.isInvalid()) {
4869 SkipUntil(tok::r_paren);
4870 return;
4871 }
4872
4873 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004874 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004875
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004876 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004877 return;
4878
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004879 DS.setTypeofParensRange(T.getRange());
4880 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004881
4882 const char *PrevSpec = 0;
4883 unsigned DiagID;
4884 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4885 DiagID, Result.release()))
4886 Diag(StartLoc, DiagID) << PrevSpec;
4887}
4888
Chris Lattner1b492422010-02-28 18:33:55 +00004889
4890/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4891/// from TryAltiVecVectorToken.
4892bool Parser::TryAltiVecVectorTokenOutOfLine() {
4893 Token Next = NextToken();
4894 switch (Next.getKind()) {
4895 default: return false;
4896 case tok::kw_short:
4897 case tok::kw_long:
4898 case tok::kw_signed:
4899 case tok::kw_unsigned:
4900 case tok::kw_void:
4901 case tok::kw_char:
4902 case tok::kw_int:
4903 case tok::kw_float:
4904 case tok::kw_double:
4905 case tok::kw_bool:
4906 case tok::kw___pixel:
4907 Tok.setKind(tok::kw___vector);
4908 return true;
4909 case tok::identifier:
4910 if (Next.getIdentifierInfo() == Ident_pixel) {
4911 Tok.setKind(tok::kw___vector);
4912 return true;
4913 }
4914 return false;
4915 }
4916}
4917
4918bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4919 const char *&PrevSpec, unsigned &DiagID,
4920 bool &isInvalid) {
4921 if (Tok.getIdentifierInfo() == Ident_vector) {
4922 Token Next = NextToken();
4923 switch (Next.getKind()) {
4924 case tok::kw_short:
4925 case tok::kw_long:
4926 case tok::kw_signed:
4927 case tok::kw_unsigned:
4928 case tok::kw_void:
4929 case tok::kw_char:
4930 case tok::kw_int:
4931 case tok::kw_float:
4932 case tok::kw_double:
4933 case tok::kw_bool:
4934 case tok::kw___pixel:
4935 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4936 return true;
4937 case tok::identifier:
4938 if (Next.getIdentifierInfo() == Ident_pixel) {
4939 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4940 return true;
4941 }
4942 break;
4943 default:
4944 break;
4945 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004946 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004947 DS.isTypeAltiVecVector()) {
4948 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4949 return true;
4950 }
4951 return false;
4952}