blob: ee394f885bca6e9581bab856537f732340fbdd60 [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,
162 0, SourceLocation(), 0, 0);
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,
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000278 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Michael Hane53ac8a2012-03-07 00:12:16 +0000279 if (BuiltinType && attr->getKind() == AttributeList::AT_iboutletcollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000280 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000281 }
282}
283
284
Eli Friedmana23b4852009-06-08 07:21:15 +0000285/// ParseMicrosoftDeclSpec - Parse an __declspec construct
286///
287/// [MS] decl-specifier:
288/// __declspec ( extended-decl-modifier-seq )
289///
290/// [MS] extended-decl-modifier-seq:
291/// extended-decl-modifier[opt]
292/// extended-decl-modifier extended-decl-modifier-seq
293
John McCall7f040a92010-12-24 02:08:15 +0000294void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000295 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000296
Steve Narofff59e17e2008-12-24 20:59:21 +0000297 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000298 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
299 "declspec")) {
300 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000301 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000302 }
Francois Pichet373197b2011-05-07 19:04:49 +0000303
Eli Friedman290eeb02009-06-08 23:27:34 +0000304 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000305 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
306 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000307
308 // FIXME: Remove this when we have proper __declspec(property()) support.
309 // Just skip everything inside property().
310 if (AttrName->getName() == "property") {
311 ConsumeParen();
312 SkipUntil(tok::r_paren);
313 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000314 if (Tok.is(tok::l_paren)) {
315 ConsumeParen();
316 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
317 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000318 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000319 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000320 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000321 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
322 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000323 }
324 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
325 SkipUntil(tok::r_paren, false);
326 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000327 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
328 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000329 }
330 }
331 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
332 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000333 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000334}
335
John McCall7f040a92010-12-24 02:08:15 +0000336void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000337 // Treat these like attributes
338 // FIXME: Allow Sema to distinguish between these and real attributes!
339 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000340 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000341 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000342 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000343 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000344 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
345 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000346 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
347 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000348 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000349}
350
John McCall7f040a92010-12-24 02:08:15 +0000351void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000352 // Treat these like attributes
353 while (Tok.is(tok::kw___pascal)) {
354 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
355 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000356 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
357 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000358 }
John McCall7f040a92010-12-24 02:08:15 +0000359}
360
Peter Collingbournef315fa82011-02-14 01:42:53 +0000361void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
362 // Treat these like attributes
363 while (Tok.is(tok::kw___kernel)) {
364 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000365 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
366 AttrNameLoc, 0, AttrNameLoc, 0,
367 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000368 }
369}
370
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000371void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
372 SourceLocation Loc = Tok.getLocation();
373 switch(Tok.getKind()) {
374 // OpenCL qualifiers:
375 case tok::kw___private:
376 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000377 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000378 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000379 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000380 break;
381
382 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000383 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000384 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000385 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000386 break;
387
388 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000389 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000390 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000391 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000392 break;
393
394 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000395 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000396 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000397 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000398 break;
399
400 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000401 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000402 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000403 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000404 break;
405
406 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000407 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000408 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000409 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000410 break;
411
412 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000413 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000414 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000415 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000416 break;
417 default: break;
418 }
419}
420
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000421/// \brief Parse a version number.
422///
423/// version:
424/// simple-integer
425/// simple-integer ',' simple-integer
426/// simple-integer ',' simple-integer ',' simple-integer
427VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
428 Range = Tok.getLocation();
429
430 if (!Tok.is(tok::numeric_constant)) {
431 Diag(Tok, diag::err_expected_version);
432 SkipUntil(tok::comma, tok::r_paren, true, true, true);
433 return VersionTuple();
434 }
435
436 // Parse the major (and possibly minor and subminor) versions, which
437 // are stored in the numeric constant. We utilize a quirk of the
438 // lexer, which is that it handles something like 1.2.3 as a single
439 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000440 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000441 Buffer.resize(Tok.getLength()+1);
442 const char *ThisTokBegin = &Buffer[0];
443
444 // Get the spelling of the token, which eliminates trigraphs, etc.
445 bool Invalid = false;
446 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
447 if (Invalid)
448 return VersionTuple();
449
450 // Parse the major version.
451 unsigned AfterMajor = 0;
452 unsigned Major = 0;
453 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
454 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
455 ++AfterMajor;
456 }
457
458 if (AfterMajor == 0) {
459 Diag(Tok, diag::err_expected_version);
460 SkipUntil(tok::comma, tok::r_paren, true, true, true);
461 return VersionTuple();
462 }
463
464 if (AfterMajor == ActualLength) {
465 ConsumeToken();
466
467 // We only had a single version component.
468 if (Major == 0) {
469 Diag(Tok, diag::err_zero_version);
470 return VersionTuple();
471 }
472
473 return VersionTuple(Major);
474 }
475
476 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
477 Diag(Tok, diag::err_expected_version);
478 SkipUntil(tok::comma, tok::r_paren, true, true, true);
479 return VersionTuple();
480 }
481
482 // Parse the minor version.
483 unsigned AfterMinor = AfterMajor + 1;
484 unsigned Minor = 0;
485 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
486 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
487 ++AfterMinor;
488 }
489
490 if (AfterMinor == ActualLength) {
491 ConsumeToken();
492
493 // We had major.minor.
494 if (Major == 0 && Minor == 0) {
495 Diag(Tok, diag::err_zero_version);
496 return VersionTuple();
497 }
498
499 return VersionTuple(Major, Minor);
500 }
501
502 // If what follows is not a '.', we have a problem.
503 if (ThisTokBegin[AfterMinor] != '.') {
504 Diag(Tok, diag::err_expected_version);
505 SkipUntil(tok::comma, tok::r_paren, true, true, true);
506 return VersionTuple();
507 }
508
509 // Parse the subminor version.
510 unsigned AfterSubminor = AfterMinor + 1;
511 unsigned Subminor = 0;
512 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
513 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
514 ++AfterSubminor;
515 }
516
517 if (AfterSubminor != ActualLength) {
518 Diag(Tok, diag::err_expected_version);
519 SkipUntil(tok::comma, tok::r_paren, true, true, true);
520 return VersionTuple();
521 }
522 ConsumeToken();
523 return VersionTuple(Major, Minor, Subminor);
524}
525
526/// \brief Parse the contents of the "availability" attribute.
527///
528/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000529/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000530///
531/// platform:
532/// identifier
533///
534/// version-arg-list:
535/// version-arg
536/// version-arg ',' version-arg-list
537///
538/// version-arg:
539/// 'introduced' '=' version
540/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000541/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000542/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000543/// opt-message:
544/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000545void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
546 SourceLocation AvailabilityLoc,
547 ParsedAttributes &attrs,
548 SourceLocation *endLoc) {
549 SourceLocation PlatformLoc;
550 IdentifierInfo *Platform = 0;
551
552 enum { Introduced, Deprecated, Obsoleted, Unknown };
553 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000554 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000555
556 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000557 BalancedDelimiterTracker T(*this, tok::l_paren);
558 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000559 Diag(Tok, diag::err_expected_lparen);
560 return;
561 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000562
563 // Parse the platform name,
564 if (Tok.isNot(tok::identifier)) {
565 Diag(Tok, diag::err_availability_expected_platform);
566 SkipUntil(tok::r_paren);
567 return;
568 }
569 Platform = Tok.getIdentifierInfo();
570 PlatformLoc = ConsumeToken();
571
572 // Parse the ',' following the platform name.
573 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
574 return;
575
576 // If we haven't grabbed the pointers for the identifiers
577 // "introduced", "deprecated", and "obsoleted", do so now.
578 if (!Ident_introduced) {
579 Ident_introduced = PP.getIdentifierInfo("introduced");
580 Ident_deprecated = PP.getIdentifierInfo("deprecated");
581 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000582 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000583 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000584 }
585
586 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000587 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000588 do {
589 if (Tok.isNot(tok::identifier)) {
590 Diag(Tok, diag::err_availability_expected_change);
591 SkipUntil(tok::r_paren);
592 return;
593 }
594 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
595 SourceLocation KeywordLoc = ConsumeToken();
596
Douglas Gregorb53e4172011-03-26 03:35:55 +0000597 if (Keyword == Ident_unavailable) {
598 if (UnavailableLoc.isValid()) {
599 Diag(KeywordLoc, diag::err_availability_redundant)
600 << Keyword << SourceRange(UnavailableLoc);
601 }
602 UnavailableLoc = KeywordLoc;
603
604 if (Tok.isNot(tok::comma))
605 break;
606
607 ConsumeToken();
608 continue;
609 }
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000610
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000611 if (Tok.isNot(tok::equal)) {
612 Diag(Tok, diag::err_expected_equal_after)
613 << Keyword;
614 SkipUntil(tok::r_paren);
615 return;
616 }
617 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000618 if (Keyword == Ident_message) {
619 if (!isTokenStringLiteral()) {
620 Diag(Tok, diag::err_expected_string_literal);
621 SkipUntil(tok::r_paren);
622 return;
623 }
624 MessageExpr = ParseStringLiteralExpression();
625 break;
626 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000627
628 SourceRange VersionRange;
629 VersionTuple Version = ParseVersionTuple(VersionRange);
630
631 if (Version.empty()) {
632 SkipUntil(tok::r_paren);
633 return;
634 }
635
636 unsigned Index;
637 if (Keyword == Ident_introduced)
638 Index = Introduced;
639 else if (Keyword == Ident_deprecated)
640 Index = Deprecated;
641 else if (Keyword == Ident_obsoleted)
642 Index = Obsoleted;
643 else
644 Index = Unknown;
645
646 if (Index < Unknown) {
647 if (!Changes[Index].KeywordLoc.isInvalid()) {
648 Diag(KeywordLoc, diag::err_availability_redundant)
649 << Keyword
650 << SourceRange(Changes[Index].KeywordLoc,
651 Changes[Index].VersionRange.getEnd());
652 }
653
654 Changes[Index].KeywordLoc = KeywordLoc;
655 Changes[Index].Version = Version;
656 Changes[Index].VersionRange = VersionRange;
657 } else {
658 Diag(KeywordLoc, diag::err_availability_unknown_change)
659 << Keyword << VersionRange;
660 }
661
662 if (Tok.isNot(tok::comma))
663 break;
664
665 ConsumeToken();
666 } while (true);
667
668 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000669 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000670 return;
671
672 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000673 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000674
Douglas Gregorb53e4172011-03-26 03:35:55 +0000675 // The 'unavailable' availability cannot be combined with any other
676 // availability changes. Make sure that hasn't happened.
677 if (UnavailableLoc.isValid()) {
678 bool Complained = false;
679 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
680 if (Changes[Index].KeywordLoc.isValid()) {
681 if (!Complained) {
682 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
683 << SourceRange(Changes[Index].KeywordLoc,
684 Changes[Index].VersionRange.getEnd());
685 Complained = true;
686 }
687
688 // Clear out the availability.
689 Changes[Index] = AvailabilityChange();
690 }
691 }
692 }
693
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000694 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000695 attrs.addNew(&Availability,
696 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000697 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000698 Platform, PlatformLoc,
699 Changes[Introduced],
700 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000701 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000702 UnavailableLoc, MessageExpr.take(),
703 false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000704}
705
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000706
707// Late Parsed Attributes:
708// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
709
710void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
711
712void Parser::LateParsedClass::ParseLexedAttributes() {
713 Self->ParseLexedAttributes(*Class);
714}
715
716void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000717 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000718}
719
720/// Wrapper class which calls ParseLexedAttribute, after setting up the
721/// scope appropriately.
722void Parser::ParseLexedAttributes(ParsingClass &Class) {
723 // Deal with templates
724 // FIXME: Test cases to make sure this does the right thing for templates.
725 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
726 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
727 HasTemplateScope);
728 if (HasTemplateScope)
729 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
730
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000731 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000732 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000733 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000734 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
735 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
736
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000737 // Enter the scope of nested classes
738 if (!AlreadyHasClassScope)
739 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
740 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000741 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000742 // Allow 'this' within late-parsed attributes.
743 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
744 /*TypeQuals=*/0);
745
746 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
747 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
748 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000749 }
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000750
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000751 if (!AlreadyHasClassScope)
752 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
753 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000754}
755
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000756
757/// \brief Parse all attributes in LAs, and attach them to Decl D.
758void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
759 bool EnterScope, bool OnDefinition) {
760 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000761 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000762 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000763 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000764 }
765 LAs.clear();
766}
767
768
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000769/// \brief Finish parsing an attribute for which parsing was delayed.
770/// This will be called at the end of parsing a class declaration
771/// for each LateParsedAttribute. We consume the saved tokens and
772/// create an attribute with the arguments filled in. We add this
773/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000774void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
775 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000776 // Save the current token position.
777 SourceLocation OrigLoc = Tok.getLocation();
778
779 // Append the current token at the end of the new token stream so that it
780 // doesn't get lost.
781 LA.Toks.push_back(Tok);
782 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
783 // Consume the previously pushed token.
784 ConsumeAnyToken();
785
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000786 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
787 Diag(Tok, diag::warn_attribute_on_function_definition)
788 << LA.AttrName.getName();
789 }
790
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000791 ParsedAttributes Attrs(AttrFactory);
792 SourceLocation endLoc;
793
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000794 if (LA.Decls.size() == 1) {
795 Decl *D = LA.Decls[0];
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000796
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000797 // If the Decl is templatized, add template parameters to scope.
798 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
799 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
800 if (HasTemplateScope)
801 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000802
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000803 // If the Decl is on a function, add function parameters to the scope.
804 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
805 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
806 if (HasFunctionScope)
807 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
808
809 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
810
811 if (HasFunctionScope) {
812 Actions.ActOnExitFunctionContext();
813 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
814 }
815 if (HasTemplateScope) {
816 TempScope.Exit();
817 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000818 } else if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000819 // If there are multiple decls, then the decl cannot be within the
820 // function scope.
821 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000822 } else {
823 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000824 }
825
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000826 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
827 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
828 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000829
830 if (Tok.getLocation() != OrigLoc) {
831 // Due to a parsing error, we either went over the cached tokens or
832 // there are still cached tokens left, so we skip the leftover tokens.
833 // Since this is an uncommon situation that should be avoided, use the
834 // expensive isBeforeInTranslationUnit call.
835 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
836 OrigLoc))
837 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000838 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000839 }
840}
841
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000842/// \brief Wrapper around a case statement checking if AttrName is
843/// one of the thread safety attributes
844bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
845 return llvm::StringSwitch<bool>(AttrName)
846 .Case("guarded_by", true)
847 .Case("guarded_var", true)
848 .Case("pt_guarded_by", true)
849 .Case("pt_guarded_var", true)
850 .Case("lockable", true)
851 .Case("scoped_lockable", true)
852 .Case("no_thread_safety_analysis", true)
853 .Case("acquired_after", true)
854 .Case("acquired_before", true)
855 .Case("exclusive_lock_function", true)
856 .Case("shared_lock_function", true)
857 .Case("exclusive_trylock_function", true)
858 .Case("shared_trylock_function", true)
859 .Case("unlock_function", true)
860 .Case("lock_returned", true)
861 .Case("locks_excluded", true)
862 .Case("exclusive_locks_required", true)
863 .Case("shared_locks_required", true)
864 .Default(false);
865}
866
867/// \brief Parse the contents of thread safety attributes. These
868/// should always be parsed as an expression list.
869///
870/// We need to special case the parsing due to the fact that if the first token
871/// of the first argument is an identifier, the main parse loop will store
872/// that token as a "parameter" and the rest of
873/// the arguments will be added to a list of "arguments". However,
874/// subsequent tokens in the first argument are lost. We instead parse each
875/// argument as an expression and add all arguments to the list of "arguments".
876/// In future, we will take advantage of this special case to also
877/// deal with some argument scoping issues here (for example, referring to a
878/// function parameter in the attribute on that function).
879void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
880 SourceLocation AttrNameLoc,
881 ParsedAttributes &Attrs,
882 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000883 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000884
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000885 BalancedDelimiterTracker T(*this, tok::l_paren);
886 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000887
888 ExprVector ArgExprs(Actions);
889 bool ArgExprsOk = true;
890
891 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000892 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000893 ExprResult ArgExpr(ParseAssignmentExpression());
894 if (ArgExpr.isInvalid()) {
895 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000896 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000897 break;
898 } else {
899 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000900 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000901 if (Tok.isNot(tok::comma))
902 break;
903 ConsumeToken(); // Eat the comma, move to the next argument
904 }
905 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000906 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000907 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
908 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000909 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000910 if (EndLoc)
911 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000912}
913
Richard Smith6ee326a2012-04-10 01:32:12 +0000914/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
915/// of a C++11 attribute-specifier in a location where an attribute is not
916/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
917/// situation.
918///
919/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
920/// this doesn't appear to actually be an attribute-specifier, and the caller
921/// should try to parse it.
922bool Parser::DiagnoseProhibitedCXX11Attribute() {
923 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
924
925 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
926 case CAK_NotAttributeSpecifier:
927 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
928 return false;
929
930 case CAK_InvalidAttributeSpecifier:
931 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
932 return false;
933
934 case CAK_AttributeSpecifier:
935 // Parse and discard the attributes.
936 SourceLocation BeginLoc = ConsumeBracket();
937 ConsumeBracket();
938 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
939 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
940 SourceLocation EndLoc = ConsumeBracket();
941 Diag(BeginLoc, diag::err_attributes_not_allowed)
942 << SourceRange(BeginLoc, EndLoc);
943 return true;
944 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +0000945 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +0000946}
947
John McCall7f040a92010-12-24 02:08:15 +0000948void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
949 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
950 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000951}
952
Reid Spencer5f016e22007-07-11 17:01:13 +0000953/// ParseDeclaration - Parse a full 'declaration', which consists of
954/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000955/// 'Context' should be a Declarator::TheContext value. This returns the
956/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000957///
958/// declaration: [C99 6.7]
959/// block-declaration ->
960/// simple-declaration
961/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000962/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000963/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000964/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000965/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +0000966/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000967/// others... [FIXME]
968///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000969Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
970 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000971 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000972 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000973 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000974 // Must temporarily exit the objective-c container scope for
975 // parsing c none objective-c decls.
976 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000977
John McCalld226f652010-08-21 09:40:31 +0000978 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000979 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000980 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000981 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000982 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000983 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000984 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000985 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000986 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000987 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +0000988 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000989 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000990 SourceLocation InlineLoc = ConsumeToken();
991 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
992 break;
993 }
John McCall7f040a92010-12-24 02:08:15 +0000994 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000995 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000996 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000997 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000998 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000999 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001000 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001001 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001002 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001003 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001004 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001005 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001006 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001007 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001008 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001009 default:
John McCall7f040a92010-12-24 02:08:15 +00001010 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001011 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001012
Chris Lattner682bf922009-03-29 16:50:03 +00001013 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001014 // single decl, convert it now. Alias declarations can also declare a type;
1015 // include that too if it is present.
1016 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001017}
1018
1019/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1020/// declaration-specifiers init-declarator-list[opt] ';'
1021///[C90/C++]init-declarator-list ';' [TODO]
1022/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001023///
Richard Smithad762fc2011-04-14 22:09:26 +00001024/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
1025/// attribute-specifier-seq[opt] type-specifier-seq declarator
1026///
Chris Lattnercd147752009-03-29 17:27:48 +00001027/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001028/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001029///
1030/// If FRI is non-null, we might be parsing a for-range-declaration instead
1031/// of a simple-declaration. If we find that we are, we also parse the
1032/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001033Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
1034 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001035 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001036 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +00001037 bool RequireSemi,
1038 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001040 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001041 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001042
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001043 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001044 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001045
Reid Spencer5f016e22007-07-11 17:01:13 +00001046 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1047 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001048 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001049 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001050 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001051 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001052 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001053 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001054 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001056
1057 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001058}
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Richard Smith0706df42011-10-19 21:33:05 +00001060/// Returns true if this might be the start of a declarator, or a common typo
1061/// for a declarator.
1062bool Parser::MightBeDeclarator(unsigned Context) {
1063 switch (Tok.getKind()) {
1064 case tok::annot_cxxscope:
1065 case tok::annot_template_id:
1066 case tok::caret:
1067 case tok::code_completion:
1068 case tok::coloncolon:
1069 case tok::ellipsis:
1070 case tok::kw___attribute:
1071 case tok::kw_operator:
1072 case tok::l_paren:
1073 case tok::star:
1074 return true;
1075
1076 case tok::amp:
1077 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001078 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001079
Richard Smith1c94c162012-01-09 22:31:44 +00001080 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001081 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001082 NextToken().is(tok::l_square);
1083
1084 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001085 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001086
Richard Smith0706df42011-10-19 21:33:05 +00001087 case tok::identifier:
1088 switch (NextToken().getKind()) {
1089 case tok::code_completion:
1090 case tok::coloncolon:
1091 case tok::comma:
1092 case tok::equal:
1093 case tok::equalequal: // Might be a typo for '='.
1094 case tok::kw_alignas:
1095 case tok::kw_asm:
1096 case tok::kw___attribute:
1097 case tok::l_brace:
1098 case tok::l_paren:
1099 case tok::l_square:
1100 case tok::less:
1101 case tok::r_brace:
1102 case tok::r_paren:
1103 case tok::r_square:
1104 case tok::semi:
1105 return true;
1106
1107 case tok::colon:
1108 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001109 // and in block scope it's probably a label. Inside a class definition,
1110 // this is a bit-field.
1111 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001112 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001113
1114 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001115 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001116
1117 default:
1118 return false;
1119 }
1120
1121 default:
1122 return false;
1123 }
1124}
1125
Richard Smith994d73f2012-04-11 20:59:20 +00001126/// Skip until we reach something which seems like a sensible place to pick
1127/// up parsing after a malformed declaration. This will sometimes stop sooner
1128/// than SkipUntil(tok::r_brace) would, but will never stop later.
1129void Parser::SkipMalformedDecl() {
1130 while (true) {
1131 switch (Tok.getKind()) {
1132 case tok::l_brace:
1133 // Skip until matching }, then stop. We've probably skipped over
1134 // a malformed class or function definition or similar.
1135 ConsumeBrace();
1136 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1137 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1138 // This declaration isn't over yet. Keep skipping.
1139 continue;
1140 }
1141 if (Tok.is(tok::semi))
1142 ConsumeToken();
1143 return;
1144
1145 case tok::l_square:
1146 ConsumeBracket();
1147 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1148 continue;
1149
1150 case tok::l_paren:
1151 ConsumeParen();
1152 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1153 continue;
1154
1155 case tok::r_brace:
1156 return;
1157
1158 case tok::semi:
1159 ConsumeToken();
1160 return;
1161
1162 case tok::kw_inline:
1163 // 'inline namespace' at the start of a line is almost certainly
1164 // a good place to pick back up parsing.
1165 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace))
1166 return;
1167 break;
1168
1169 case tok::kw_namespace:
1170 // 'namespace' at the start of a line is almost certainly a good
1171 // place to pick back up parsing.
1172 if (Tok.isAtStartOfLine())
1173 return;
1174 break;
1175
1176 case tok::eof:
1177 return;
1178
1179 default:
1180 break;
1181 }
1182
1183 ConsumeAnyToken();
1184 }
1185}
1186
John McCalld8ac0572009-11-03 19:26:08 +00001187/// ParseDeclGroup - Having concluded that this is either a function
1188/// definition or a group of object declarations, actually parse the
1189/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001190Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1191 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001192 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001193 SourceLocation *DeclEnd,
1194 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001195 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001196 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001197 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001198
John McCalld8ac0572009-11-03 19:26:08 +00001199 // Bail out if the first declarator didn't seem well-formed.
1200 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001201 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001202 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001203 }
Mike Stump1eb44332009-09-09 15:08:12 +00001204
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001205 // Save late-parsed attributes for now; they need to be parsed in the
1206 // appropriate function scope after the function Decl has been constructed.
1207 LateParsedAttrList LateParsedAttrs;
1208 if (D.isFunctionDeclarator())
1209 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1210
Chris Lattnerc82daef2010-07-11 22:24:20 +00001211 // Check to see if we have a function *definition* which must have a body.
1212 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1213 // Look at the next token to make sure that this isn't a function
1214 // declaration. We have to check this because __attribute__ might be the
1215 // start of a function definition in GCC-extended K&R C.
1216 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001217
Chris Lattner004659a2010-07-11 22:42:07 +00001218 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001219 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1220 Diag(Tok, diag::err_function_declared_typedef);
1221
1222 // Recover by treating the 'typedef' as spurious.
1223 DS.ClearStorageClassSpecs();
1224 }
1225
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001226 Decl *TheDecl =
1227 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001228 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001229 }
1230
1231 if (isDeclarationSpecifier()) {
1232 // If there is an invalid declaration specifier right after the function
1233 // prototype, then we must be in a missing semicolon case where this isn't
1234 // actually a body. Just fall through into the code that handles it as a
1235 // prototype, and let the top-level code handle the erroneous declspec
1236 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001237 } else {
1238 Diag(Tok, diag::err_expected_fn_body);
1239 SkipUntil(tok::semi);
1240 return DeclGroupPtrTy();
1241 }
1242 }
1243
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001244 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001245 return DeclGroupPtrTy();
1246
1247 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1248 // must parse and analyze the for-range-initializer before the declaration is
1249 // analyzed.
1250 if (FRI && Tok.is(tok::colon)) {
1251 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001252 if (Tok.is(tok::l_brace))
1253 FRI->RangeExpr = ParseBraceInitializer();
1254 else
1255 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001256 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1257 Actions.ActOnCXXForRangeDecl(ThisDecl);
1258 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001259 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001260 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1261 }
1262
Chris Lattner5f9e2722011-07-23 10:55:15 +00001263 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001264 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001265 if (LateParsedAttrs.size() > 0)
1266 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001267 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001268 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001269 DeclsInGroup.push_back(FirstDecl);
1270
Richard Smith0706df42011-10-19 21:33:05 +00001271 bool ExpectSemi = Context != Declarator::ForContext;
1272
John McCalld8ac0572009-11-03 19:26:08 +00001273 // If we don't have a comma, it is either the end of the list (a ';') or an
1274 // error, bail out.
1275 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001276 SourceLocation CommaLoc = ConsumeToken();
1277
1278 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1279 // This comma was followed by a line-break and something which can't be
1280 // the start of a declarator. The comma was probably a typo for a
1281 // semicolon.
1282 Diag(CommaLoc, diag::err_expected_semi_declaration)
1283 << FixItHint::CreateReplacement(CommaLoc, ";");
1284 ExpectSemi = false;
1285 break;
1286 }
John McCalld8ac0572009-11-03 19:26:08 +00001287
1288 // Parse the next declarator.
1289 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001290 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001291
1292 // Accept attributes in an init-declarator. In the first declarator in a
1293 // declaration, these would be part of the declspec. In subsequent
1294 // declarators, they become part of the declarator itself, so that they
1295 // don't apply to declarators after *this* one. Examples:
1296 // short __attribute__((common)) var; -> declspec
1297 // short var __attribute__((common)); -> declarator
1298 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001299 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001300
1301 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001302 if (!D.isInvalidType()) {
1303 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1304 D.complete(ThisDecl);
1305 if (ThisDecl)
1306 DeclsInGroup.push_back(ThisDecl);
1307 }
John McCalld8ac0572009-11-03 19:26:08 +00001308 }
1309
1310 if (DeclEnd)
1311 *DeclEnd = Tok.getLocation();
1312
Richard Smith0706df42011-10-19 21:33:05 +00001313 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001314 ExpectAndConsumeSemi(Context == Declarator::FileContext
1315 ? diag::err_invalid_token_after_toplevel_declarator
1316 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001317 // Okay, there was no semicolon and one was expected. If we see a
1318 // declaration specifier, just assume it was missing and continue parsing.
1319 // Otherwise things are very confused and we skip to recover.
1320 if (!isDeclarationSpecifier()) {
1321 SkipUntil(tok::r_brace, true, true);
1322 if (Tok.is(tok::semi))
1323 ConsumeToken();
1324 }
John McCalld8ac0572009-11-03 19:26:08 +00001325 }
1326
Douglas Gregor23c94db2010-07-02 17:43:08 +00001327 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001328 DeclsInGroup.data(),
1329 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001330}
1331
Richard Smithad762fc2011-04-14 22:09:26 +00001332/// Parse an optional simple-asm-expr and attributes, and attach them to a
1333/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001334bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001335 // If a simple-asm-expr is present, parse it.
1336 if (Tok.is(tok::kw_asm)) {
1337 SourceLocation Loc;
1338 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1339 if (AsmLabel.isInvalid()) {
1340 SkipUntil(tok::semi, true, true);
1341 return true;
1342 }
1343
1344 D.setAsmLabel(AsmLabel.release());
1345 D.SetRangeEnd(Loc);
1346 }
1347
1348 MaybeParseGNUAttributes(D);
1349 return false;
1350}
1351
Douglas Gregor1426e532009-05-12 21:31:51 +00001352/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1353/// declarator'. This method parses the remainder of the declaration
1354/// (including any attributes or initializer, among other things) and
1355/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001356///
Reid Spencer5f016e22007-07-11 17:01:13 +00001357/// init-declarator: [C99 6.7]
1358/// declarator
1359/// declarator '=' initializer
1360/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1361/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001362/// [C++] declarator initializer[opt]
1363///
1364/// [C++] initializer:
1365/// [C++] '=' initializer-clause
1366/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001367/// [C++0x] '=' 'default' [TODO]
1368/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001369/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001370///
1371/// According to the standard grammar, =default and =delete are function
1372/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001373///
John McCalld226f652010-08-21 09:40:31 +00001374Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001375 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001376 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001377 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Richard Smithad762fc2011-04-14 22:09:26 +00001379 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1380}
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Richard Smithad762fc2011-04-14 22:09:26 +00001382Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1383 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001384 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001385 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001386 switch (TemplateInfo.Kind) {
1387 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001388 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001389 break;
1390
1391 case ParsedTemplateInfo::Template:
1392 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001393 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001394 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001395 TemplateInfo.TemplateParams->data(),
1396 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001397 D);
1398 break;
1399
1400 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001401 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001402 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001403 TemplateInfo.ExternLoc,
1404 TemplateInfo.TemplateLoc,
1405 D);
1406 if (ThisRes.isInvalid()) {
1407 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001408 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001409 }
1410
1411 ThisDecl = ThisRes.get();
1412 break;
1413 }
1414 }
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Richard Smith34b41d92011-02-20 03:19:35 +00001416 bool TypeContainsAuto =
1417 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1418
Douglas Gregor1426e532009-05-12 21:31:51 +00001419 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001420 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001421 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001422 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001423 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001424 if (D.isFunctionDeclarator())
1425 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1426 << 1 /* delete */;
1427 else
1428 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001429 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001430 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001431 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1432 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001433 else
1434 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001435 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001436 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001437 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001438 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001439 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001440
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001441 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001442 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001443 cutOffParsing();
1444 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001445 }
1446
John McCall60d7b3a2010-08-24 06:29:42 +00001447 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001448
David Blaikie4e4d0842012-03-11 07:00:24 +00001449 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001450 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001451 ExitScope();
1452 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001453
Douglas Gregor1426e532009-05-12 21:31:51 +00001454 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001455 SkipUntil(tok::comma, true, true);
1456 Actions.ActOnInitializerError(ThisDecl);
1457 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001458 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1459 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001460 }
1461 } else if (Tok.is(tok::l_paren)) {
1462 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001463 BalancedDelimiterTracker T(*this, tok::l_paren);
1464 T.consumeOpen();
1465
Douglas Gregor1426e532009-05-12 21:31:51 +00001466 ExprVector Exprs(Actions);
1467 CommaLocsTy CommaLocs;
1468
David Blaikie4e4d0842012-03-11 07:00:24 +00001469 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001470 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001471 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001472 }
1473
Douglas Gregor1426e532009-05-12 21:31:51 +00001474 if (ParseExpressionList(Exprs, CommaLocs)) {
1475 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001476
David Blaikie4e4d0842012-03-11 07:00:24 +00001477 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001478 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001479 ExitScope();
1480 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001481 } else {
1482 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001483 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001484
1485 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1486 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001487
David Blaikie4e4d0842012-03-11 07:00:24 +00001488 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001489 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001490 ExitScope();
1491 }
1492
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001493 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1494 T.getCloseLocation(),
1495 move_arg(Exprs));
1496 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1497 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001498 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001499 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001500 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001501 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1502
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001503 if (D.getCXXScopeSpec().isSet()) {
1504 EnterScope(0);
1505 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1506 }
1507
1508 ExprResult Init(ParseBraceInitializer());
1509
1510 if (D.getCXXScopeSpec().isSet()) {
1511 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1512 ExitScope();
1513 }
1514
1515 if (Init.isInvalid()) {
1516 Actions.ActOnInitializerError(ThisDecl);
1517 } else
1518 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1519 /*DirectInit=*/true, TypeContainsAuto);
1520
Douglas Gregor1426e532009-05-12 21:31:51 +00001521 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001522 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001523 }
1524
Richard Smith483b9f32011-02-21 20:05:19 +00001525 Actions.FinalizeDeclaration(ThisDecl);
1526
Douglas Gregor1426e532009-05-12 21:31:51 +00001527 return ThisDecl;
1528}
1529
Reid Spencer5f016e22007-07-11 17:01:13 +00001530/// ParseSpecifierQualifierList
1531/// specifier-qualifier-list:
1532/// type-specifier specifier-qualifier-list[opt]
1533/// type-qualifier specifier-qualifier-list[opt]
1534/// [GNU] attributes specifier-qualifier-list[opt]
1535///
Richard Smith69730c12012-03-12 07:56:15 +00001536void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1537 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001538 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1539 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001540 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001541 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001542
Reid Spencer5f016e22007-07-11 17:01:13 +00001543 // Validate declspec for type-name.
1544 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001545 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1546 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001547 Diag(Tok, diag::err_expected_type);
1548 DS.SetTypeSpecError();
1549 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1550 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001551 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001552 if (!DS.hasTypeSpecifier())
1553 DS.SetTypeSpecError();
1554 }
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Reid Spencer5f016e22007-07-11 17:01:13 +00001556 // Issue diagnostic and remove storage class if present.
1557 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1558 if (DS.getStorageClassSpecLoc().isValid())
1559 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1560 else
1561 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1562 DS.ClearStorageClassSpecs();
1563 }
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 // Issue diagnostic and remove function specfier if present.
1566 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001567 if (DS.isInlineSpecified())
1568 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1569 if (DS.isVirtualSpecified())
1570 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1571 if (DS.isExplicitSpecified())
1572 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001573 DS.ClearFunctionSpecs();
1574 }
Richard Smith69730c12012-03-12 07:56:15 +00001575
1576 // Issue diagnostic and remove constexpr specfier if present.
1577 if (DS.isConstexprSpecified()) {
1578 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1579 DS.ClearConstexprSpec();
1580 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001581}
1582
Chris Lattnerc199ab32009-04-12 20:42:31 +00001583/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1584/// specified token is valid after the identifier in a declarator which
1585/// immediately follows the declspec. For example, these things are valid:
1586///
1587/// int x [ 4]; // direct-declarator
1588/// int x ( int y); // direct-declarator
1589/// int(int x ) // direct-declarator
1590/// int x ; // simple-declaration
1591/// int x = 17; // init-declarator-list
1592/// int x , y; // init-declarator-list
1593/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001594/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001595/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001596///
1597/// This is not, because 'x' does not immediately follow the declspec (though
1598/// ')' happens to be valid anyway).
1599/// int (x)
1600///
1601static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1602 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1603 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001604 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001605}
1606
Chris Lattnere40c2952009-04-14 21:34:55 +00001607
1608/// ParseImplicitInt - This method is called when we have an non-typename
1609/// identifier in a declspec (which normally terminates the decl spec) when
1610/// the declspec has no type specifier. In this case, the declspec is either
1611/// malformed or is "implicit int" (in K&R and C89).
1612///
1613/// This method handles diagnosing this prettily and returns false if the
1614/// declspec is done being processed. If it recovers and thinks there may be
1615/// other pieces of declspec after it, it returns true.
1616///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001617bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001618 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001619 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001620 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Chris Lattnere40c2952009-04-14 21:34:55 +00001622 SourceLocation Loc = Tok.getLocation();
1623 // If we see an identifier that is not a type name, we normally would
1624 // parse it as the identifer being declared. However, when a typename
1625 // is typo'd or the definition is not included, this will incorrectly
1626 // parse the typename as the identifier name and fall over misparsing
1627 // later parts of the diagnostic.
1628 //
1629 // As such, we try to do some look-ahead in cases where this would
1630 // otherwise be an "implicit-int" case to see if this is invalid. For
1631 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1632 // an identifier with implicit int, we'd get a parse error because the
1633 // next token is obviously invalid for a type. Parse these as a case
1634 // with an invalid type specifier.
1635 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Chris Lattnere40c2952009-04-14 21:34:55 +00001637 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001638 // error, do lookahead to try to do better recovery. This never applies
1639 // within a type specifier. Outside of C++, we allow this even if the
1640 // language doesn't "officially" support implicit int -- we support
1641 // implicit int as an extension in C99 and C11. Allegedly, MS also
1642 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001643 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001644 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001645 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001646 // If this token is valid for implicit int, e.g. "static x = 4", then
1647 // we just avoid eating the identifier, so it will be parsed as the
1648 // identifier in the declarator.
1649 return false;
1650 }
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Richard Smith827adaf2012-05-15 21:01:51 +00001652 if (getLangOpts().CPlusPlus &&
1653 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1654 // Don't require a type specifier if we have the 'auto' storage class
1655 // specifier in C++98 -- we'll promote it to a type specifier.
1656 return false;
1657 }
1658
Chris Lattnere40c2952009-04-14 21:34:55 +00001659 // Otherwise, if we don't consume this token, we are going to emit an
1660 // error anyway. Try to recover from various common problems. Check
1661 // to see if this was a reference to a tag name without a tag specified.
1662 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001663 //
1664 // C++ doesn't need this, and isTagName doesn't take SS.
1665 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001666 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001667 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Douglas Gregor23c94db2010-07-02 17:43:08 +00001669 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001670 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001671 case DeclSpec::TST_enum:
1672 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1673 case DeclSpec::TST_union:
1674 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1675 case DeclSpec::TST_struct:
1676 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1677 case DeclSpec::TST_class:
1678 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001679 }
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Chris Lattnerf4382f52009-04-14 22:17:06 +00001681 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001682 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1683 LookupResult R(Actions, TokenName, SourceLocation(),
1684 Sema::LookupOrdinaryName);
1685
Chris Lattnerf4382f52009-04-14 22:17:06 +00001686 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001687 << TokenName << TagName << getLangOpts().CPlusPlus
1688 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1689
1690 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1691 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1692 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001693 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001694 << TokenName << TagName;
1695 }
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Chris Lattnerf4382f52009-04-14 22:17:06 +00001697 // Parse this as a tag as if the missing tag were present.
1698 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001699 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001700 else
Richard Smith69730c12012-03-12 07:56:15 +00001701 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1702 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001703 return true;
1704 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001705 }
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Richard Smith8f0a7e72012-05-15 21:29:55 +00001707 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001708 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001709 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1710 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001711 // Look ahead to the next token to try to figure out what this declaration
1712 // was supposed to be.
1713 switch (NextToken().getKind()) {
1714 case tok::comma:
1715 case tok::equal:
1716 case tok::kw_asm:
1717 case tok::l_brace:
1718 case tok::l_square:
1719 case tok::semi:
1720 // This looks like a variable declaration. The type is probably missing.
1721 // We're done parsing decl-specifiers.
1722 return false;
1723
1724 case tok::l_paren: {
1725 // static x(4); // 'x' is not a type
1726 // x(int n); // 'x' is not a type
1727 // x (*p)[]; // 'x' is a type
1728 //
1729 // Since we're in an error case (or the rare 'implicit int in C++' MS
1730 // extension), we can afford to perform a tentative parse to determine
1731 // which case we're in.
1732 TentativeParsingAction PA(*this);
1733 ConsumeToken();
1734 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1735 PA.Revert();
1736 if (TPR == TPResult::False())
1737 return false;
1738 // The identifier is followed by a parenthesized declarator.
1739 // It's supposed to be a type.
1740 break;
1741 }
1742
1743 default:
1744 // This is probably supposed to be a type. This includes cases like:
1745 // int f(itn);
1746 // struct S { unsinged : 4; };
1747 break;
1748 }
1749 }
1750
Douglas Gregora786fdb2009-10-13 23:27:22 +00001751 // This is almost certainly an invalid type name. Let the action emit a
1752 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001753 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001754 IdentifierInfo *II = Tok.getIdentifierInfo();
1755 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001756 // The action emitted a diagnostic, so we don't have to.
1757 if (T) {
1758 // The action has suggested that the type T could be used. Set that as
1759 // the type in the declaration specifiers, consume the would-be type
1760 // name token, and we're done.
1761 const char *PrevSpec;
1762 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001763 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001764 DS.SetRangeEnd(Tok.getLocation());
1765 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001766 // There may be other declaration specifiers after this.
1767 return true;
1768 } else if (II != Tok.getIdentifierInfo()) {
1769 // If no type was suggested, the correction is to a keyword
1770 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00001771 // There may be other declaration specifiers after this.
1772 return true;
1773 }
1774
1775 // Fall through; the action had no suggestion for us.
1776 } else {
1777 // The action did not emit a diagnostic, so emit one now.
1778 SourceRange R;
1779 if (SS) R = SS->getRange();
1780 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1781 }
Mike Stump1eb44332009-09-09 15:08:12 +00001782
Douglas Gregora786fdb2009-10-13 23:27:22 +00001783 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001784 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001785 DS.SetRangeEnd(Tok.getLocation());
1786 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Chris Lattnere40c2952009-04-14 21:34:55 +00001788 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1789 // avoid rippling error messages on subsequent uses of the same type,
1790 // could be useful if #include was forgotten.
1791 return false;
1792}
1793
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001794/// \brief Determine the declaration specifier context from the declarator
1795/// context.
1796///
1797/// \param Context the declarator context, which is one of the
1798/// Declarator::TheContext enumerator values.
1799Parser::DeclSpecContext
1800Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1801 if (Context == Declarator::MemberContext)
1802 return DSC_class;
1803 if (Context == Declarator::FileContext)
1804 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001805 if (Context == Declarator::TrailingReturnContext)
1806 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001807 return DSC_normal;
1808}
1809
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001810/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1811///
1812/// FIXME: Simply returns an alignof() expression if the argument is a
1813/// type. Ideally, the type should be propagated directly into Sema.
1814///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001815/// [C11] type-id
1816/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001817/// [C++0x] type-id ...[opt]
1818/// [C++0x] assignment-expression ...[opt]
1819ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1820 SourceLocation &EllipsisLoc) {
1821 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001822 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001823 SourceLocation TypeLoc = Tok.getLocation();
1824 ParsedType Ty = ParseTypeName().get();
1825 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001826 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1827 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001828 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001829 ER = ParseConstantExpression();
1830
David Blaikie4e4d0842012-03-11 07:00:24 +00001831 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001832 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001833
1834 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001835}
1836
1837/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1838/// attribute to Attrs.
1839///
1840/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001841/// [C11] '_Alignas' '(' type-id ')'
1842/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001843/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1844/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001845void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1846 SourceLocation *endLoc) {
1847 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1848 "Not an alignment-specifier!");
1849
1850 SourceLocation KWLoc = Tok.getLocation();
1851 ConsumeToken();
1852
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001853 BalancedDelimiterTracker T(*this, tok::l_paren);
1854 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001855 return;
1856
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001857 SourceLocation EllipsisLoc;
1858 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001859 if (ArgExpr.isInvalid()) {
1860 SkipUntil(tok::r_paren);
1861 return;
1862 }
1863
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001864 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001865 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001866 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001867
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001868 // FIXME: Handle pack-expansions here.
1869 if (EllipsisLoc.isValid()) {
1870 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1871 return;
1872 }
1873
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001874 ExprVector ArgExprs(Actions);
1875 ArgExprs.push_back(ArgExpr.release());
1876 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001877 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001878}
1879
Reid Spencer5f016e22007-07-11 17:01:13 +00001880/// ParseDeclarationSpecifiers
1881/// declaration-specifiers: [C99 6.7]
1882/// storage-class-specifier declaration-specifiers[opt]
1883/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001884/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001885/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001886/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001887/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001888///
1889/// storage-class-specifier: [C99 6.7.1]
1890/// 'typedef'
1891/// 'extern'
1892/// 'static'
1893/// 'auto'
1894/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001895/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001896/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001897/// function-specifier: [C99 6.7.4]
1898/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001899/// [C++] 'virtual'
1900/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001901/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001902/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001903/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001904
Reid Spencer5f016e22007-07-11 17:01:13 +00001905///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001906void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001907 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001908 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001909 DeclSpecContext DSContext,
1910 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001911 if (DS.getSourceRange().isInvalid()) {
1912 DS.SetRangeStart(Tok.getLocation());
1913 DS.SetRangeEnd(Tok.getLocation());
1914 }
1915
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001916 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001917 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001918 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001919 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001920 unsigned DiagID = 0;
1921
Reid Spencer5f016e22007-07-11 17:01:13 +00001922 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001923
Reid Spencer5f016e22007-07-11 17:01:13 +00001924 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001925 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001926 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001927 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1928 MaybeParseCXX0XAttributes(DS.getAttributes());
1929
Reid Spencer5f016e22007-07-11 17:01:13 +00001930 // If this is not a declaration specifier token, we're done reading decl
1931 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001932 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001933 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001934
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001935 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001936 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001937 if (DS.hasTypeSpecifier()) {
1938 bool AllowNonIdentifiers
1939 = (getCurScope()->getFlags() & (Scope::ControlScope |
1940 Scope::BlockScope |
1941 Scope::TemplateParamScope |
1942 Scope::FunctionPrototypeScope |
1943 Scope::AtCatchScope)) == 0;
1944 bool AllowNestedNameSpecifiers
1945 = DSContext == DSC_top_level ||
1946 (DSContext == DSC_class && DS.isFriendSpecified());
1947
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001948 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1949 AllowNonIdentifiers,
1950 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001951 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001952 }
1953
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001954 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1955 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1956 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001957 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1958 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001959 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001960 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001961 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001962 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001963
1964 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001965 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001966 }
1967
Chris Lattner5e02c472009-01-05 00:07:25 +00001968 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001969 // C++ scope specifier. Annotate and loop, or bail out on error.
1970 if (TryAnnotateCXXScopeToken(true)) {
1971 if (!DS.hasTypeSpecifier())
1972 DS.SetTypeSpecError();
1973 goto DoneWithDeclSpec;
1974 }
John McCall2e0a7152010-03-01 18:20:46 +00001975 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1976 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001977 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001978
1979 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00001980 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001981 goto DoneWithDeclSpec;
1982
John McCallaa87d332009-12-12 11:40:51 +00001983 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001984 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1985 Tok.getAnnotationRange(),
1986 SS);
John McCallaa87d332009-12-12 11:40:51 +00001987
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001988 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001989 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001990 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001991 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001992 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001993 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001994
1995 // C++ [class.qual]p2:
1996 // In a lookup in which the constructor is an acceptable lookup
1997 // result and the nested-name-specifier nominates a class C:
1998 //
1999 // - if the name specified after the
2000 // nested-name-specifier, when looked up in C, is the
2001 // injected-class-name of C (Clause 9), or
2002 //
2003 // - if the name specified after the nested-name-specifier
2004 // is the same as the identifier or the
2005 // simple-template-id's template-name in the last
2006 // component of the nested-name-specifier,
2007 //
2008 // the name is instead considered to name the constructor of
2009 // class C.
2010 //
2011 // Thus, if the template-name is actually the constructor
2012 // name, then the code is ill-formed; this interpretation is
2013 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002014 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002015 if ((DSContext == DSC_top_level ||
2016 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2017 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002018 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002019 if (isConstructorDeclarator()) {
2020 // The user meant this to be an out-of-line constructor
2021 // definition, but template arguments are not allowed
2022 // there. Just allow this as a constructor; we'll
2023 // complain about it later.
2024 goto DoneWithDeclSpec;
2025 }
2026
2027 // The user meant this to name a type, but it actually names
2028 // a constructor with some extraneous template
2029 // arguments. Complain, then parse it as a type as the user
2030 // intended.
2031 Diag(TemplateId->TemplateNameLoc,
2032 diag::err_out_of_line_template_id_names_constructor)
2033 << TemplateId->Name;
2034 }
2035
John McCallaa87d332009-12-12 11:40:51 +00002036 DS.getTypeSpecScope() = SS;
2037 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002038 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002039 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002040 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002041 continue;
2042 }
2043
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002044 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002045 DS.getTypeSpecScope() = SS;
2046 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002047 if (Tok.getAnnotationValue()) {
2048 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002049 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2050 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002051 PrevSpec, DiagID, T);
2052 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002053 else
2054 DS.SetTypeSpecError();
2055 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2056 ConsumeToken(); // The typename
2057 }
2058
Douglas Gregor9135c722009-03-25 15:40:00 +00002059 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002060 goto DoneWithDeclSpec;
2061
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002062 // If we're in a context where the identifier could be a class name,
2063 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002064 if ((DSContext == DSC_top_level ||
2065 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002066 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002067 &SS)) {
2068 if (isConstructorDeclarator())
2069 goto DoneWithDeclSpec;
2070
2071 // As noted in C++ [class.qual]p2 (cited above), when the name
2072 // of the class is qualified in a context where it could name
2073 // a constructor, its a constructor name. However, we've
2074 // looked at the declarator, and the user probably meant this
2075 // to be a type. Complain that it isn't supposed to be treated
2076 // as a type, then proceed to parse it as a type.
2077 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2078 << Next.getIdentifierInfo();
2079 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002080
John McCallb3d87482010-08-24 05:47:05 +00002081 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2082 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002083 getCurScope(), &SS,
2084 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002085 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002086 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002087
Chris Lattnerf4382f52009-04-14 22:17:06 +00002088 // If the referenced identifier is not a type, then this declspec is
2089 // erroneous: We already checked about that it has no type specifier, and
2090 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002091 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002092 if (TypeRep == 0) {
2093 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002094 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002095 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002096 }
Mike Stump1eb44332009-09-09 15:08:12 +00002097
John McCallaa87d332009-12-12 11:40:51 +00002098 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002099 ConsumeToken(); // The C++ scope.
2100
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002101 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002102 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002103 if (isInvalid)
2104 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002105
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002106 DS.SetRangeEnd(Tok.getLocation());
2107 ConsumeToken(); // The typename.
2108
2109 continue;
2110 }
Mike Stump1eb44332009-09-09 15:08:12 +00002111
Chris Lattner80d0c892009-01-21 19:48:37 +00002112 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002113 if (Tok.getAnnotationValue()) {
2114 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002115 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002116 DiagID, T);
2117 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002118 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00002119
2120 if (isInvalid)
2121 break;
2122
Chris Lattner80d0c892009-01-21 19:48:37 +00002123 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2124 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002125
Chris Lattner80d0c892009-01-21 19:48:37 +00002126 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2127 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002128 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002129 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002130 ParseObjCProtocolQualifiers(DS);
2131
Chris Lattner80d0c892009-01-21 19:48:37 +00002132 continue;
2133 }
Mike Stump1eb44332009-09-09 15:08:12 +00002134
Douglas Gregorbfad9152011-04-28 15:48:45 +00002135 case tok::kw___is_signed:
2136 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2137 // typically treats it as a trait. If we see __is_signed as it appears
2138 // in libstdc++, e.g.,
2139 //
2140 // static const bool __is_signed;
2141 //
2142 // then treat __is_signed as an identifier rather than as a keyword.
2143 if (DS.getTypeSpecType() == TST_bool &&
2144 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2145 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2146 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2147 Tok.setKind(tok::identifier);
2148 }
2149
2150 // We're done with the declaration-specifiers.
2151 goto DoneWithDeclSpec;
2152
Chris Lattner3bd934a2008-07-26 01:18:38 +00002153 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002154 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002155 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002156 // In C++, check to see if this is a scope specifier like foo::bar::, if
2157 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002158 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002159 if (TryAnnotateCXXScopeToken(true)) {
2160 if (!DS.hasTypeSpecifier())
2161 DS.SetTypeSpecError();
2162 goto DoneWithDeclSpec;
2163 }
2164 if (!Tok.is(tok::identifier))
2165 continue;
2166 }
Mike Stump1eb44332009-09-09 15:08:12 +00002167
Chris Lattner3bd934a2008-07-26 01:18:38 +00002168 // This identifier can only be a typedef name if we haven't already seen
2169 // a type-specifier. Without this check we misparse:
2170 // typedef int X; struct Y { short X; }; as 'short int'.
2171 if (DS.hasTypeSpecifier())
2172 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002173
John Thompson82287d12010-02-05 00:12:22 +00002174 // Check for need to substitute AltiVec keyword tokens.
2175 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2176 break;
2177
Richard Smithf63eee72012-05-09 18:56:43 +00002178 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2179 // allow the use of a typedef name as a type specifier.
2180 if (DS.isTypeAltiVecVector())
2181 goto DoneWithDeclSpec;
2182
John McCallb3d87482010-08-24 05:47:05 +00002183 ParsedType TypeRep =
2184 Actions.getTypeName(*Tok.getIdentifierInfo(),
2185 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002186
Chris Lattnerc199ab32009-04-12 20:42:31 +00002187 // If this is not a typedef name, don't parse it as part of the declspec,
2188 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002189 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002190 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002191 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002192 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002193
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002194 // If we're in a context where the identifier could be a class name,
2195 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002196 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002197 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002198 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002199 goto DoneWithDeclSpec;
2200
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002201 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002202 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002203 if (isInvalid)
2204 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002205
Chris Lattner3bd934a2008-07-26 01:18:38 +00002206 DS.SetRangeEnd(Tok.getLocation());
2207 ConsumeToken(); // The identifier
2208
2209 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2210 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002211 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002212 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002213 ParseObjCProtocolQualifiers(DS);
2214
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002215 // Need to support trailing type qualifiers (e.g. "id<p> const").
2216 // If a type specifier follows, it will be diagnosed elsewhere.
2217 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002218 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002219
2220 // type-name
2221 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002222 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002223 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002224 // This template-id does not refer to a type name, so we're
2225 // done with the type-specifiers.
2226 goto DoneWithDeclSpec;
2227 }
2228
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002229 // If we're in a context where the template-id could be a
2230 // constructor name or specialization, check whether this is a
2231 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002232 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002233 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002234 isConstructorDeclarator())
2235 goto DoneWithDeclSpec;
2236
Douglas Gregor39a8de12009-02-25 19:37:18 +00002237 // Turn the template-id annotation token into a type annotation
2238 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002239 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002240 continue;
2241 }
2242
Reid Spencer5f016e22007-07-11 17:01:13 +00002243 // GNU attributes support.
2244 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002245 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002246 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002247
2248 // Microsoft declspec support.
2249 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002250 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002251 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002252
Steve Naroff239f0732008-12-25 14:16:32 +00002253 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002254 case tok::kw___forceinline: {
2255 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2256 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
2257 SourceLocation AttrNameLoc = ConsumeToken();
2258 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
2259 SourceLocation(), 0, 0);
2260 continue;
2261 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002262
2263 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002264 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002265 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002266 case tok::kw___cdecl:
2267 case tok::kw___stdcall:
2268 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002269 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002270 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002271 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002272 continue;
2273
Dawn Perchik52fc3142010-09-03 01:29:35 +00002274 // Borland single token adornments.
2275 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002276 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002277 continue;
2278
Peter Collingbournef315fa82011-02-14 01:42:53 +00002279 // OpenCL single token adornments.
2280 case tok::kw___kernel:
2281 ParseOpenCLAttributes(DS.getAttributes());
2282 continue;
2283
Reid Spencer5f016e22007-07-11 17:01:13 +00002284 // storage-class-specifier
2285 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002286 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2287 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002288 break;
2289 case tok::kw_extern:
2290 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002291 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002292 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2293 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002294 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002295 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002296 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2297 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002298 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002299 case tok::kw_static:
2300 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002301 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002302 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2303 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002304 break;
2305 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002306 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002307 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002308 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2309 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002310 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002311 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002312 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002313 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002314 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2315 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002316 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002317 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2318 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002319 break;
2320 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002321 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2322 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002323 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002324 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002325 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2326 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002327 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002328 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002329 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002330 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002331
Reid Spencer5f016e22007-07-11 17:01:13 +00002332 // function-specifier
2333 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002334 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002335 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002336 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002337 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002338 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002339 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002340 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002341 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002342
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002343 // alignment-specifier
2344 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002345 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002346 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002347 ParseAlignmentSpecifier(DS.getAttributes());
2348 continue;
2349
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002350 // friend
2351 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002352 if (DSContext == DSC_class)
2353 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2354 else {
2355 PrevSpec = ""; // not actually used by the diagnostic
2356 DiagID = diag::err_friend_invalid_in_context;
2357 isInvalid = true;
2358 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002359 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002360
Douglas Gregor8d267c52011-09-09 02:06:17 +00002361 // Modules
2362 case tok::kw___module_private__:
2363 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2364 break;
2365
Sebastian Redl2ac67232009-11-05 15:47:02 +00002366 // constexpr
2367 case tok::kw_constexpr:
2368 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2369 break;
2370
Chris Lattner80d0c892009-01-21 19:48:37 +00002371 // type-specifier
2372 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002373 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2374 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002375 break;
2376 case tok::kw_long:
2377 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002378 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2379 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002380 else
John McCallfec54012009-08-03 20:12:06 +00002381 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2382 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002383 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002384 case tok::kw___int64:
2385 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2386 DiagID);
2387 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002388 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002389 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2390 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002391 break;
2392 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002393 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2394 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002395 break;
2396 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002397 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2398 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002399 break;
2400 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002401 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2402 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002403 break;
2404 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002405 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2406 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002407 break;
2408 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002409 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2410 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002411 break;
2412 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002413 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2414 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002415 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002416 case tok::kw___int128:
2417 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2418 DiagID);
2419 break;
2420 case tok::kw_half:
2421 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2422 DiagID);
2423 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002424 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002425 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2426 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002427 break;
2428 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002429 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2430 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002431 break;
2432 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002433 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2434 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002435 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002436 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002437 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2438 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002439 break;
2440 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002441 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2442 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002443 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002444 case tok::kw_bool:
2445 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002446 if (Tok.is(tok::kw_bool) &&
2447 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2448 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2449 PrevSpec = ""; // Not used by the diagnostic.
2450 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002451 // For better error recovery.
2452 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002453 isInvalid = true;
2454 } else {
2455 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2456 DiagID);
2457 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002458 break;
2459 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002460 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2461 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002462 break;
2463 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002464 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2465 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002466 break;
2467 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002468 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2469 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002470 break;
John Thompson82287d12010-02-05 00:12:22 +00002471 case tok::kw___vector:
2472 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2473 break;
2474 case tok::kw___pixel:
2475 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2476 break;
John McCalla5fc4722011-04-09 22:50:59 +00002477 case tok::kw___unknown_anytype:
2478 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2479 PrevSpec, DiagID);
2480 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002481
2482 // class-specifier:
2483 case tok::kw_class:
2484 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002485 case tok::kw_union: {
2486 tok::TokenKind Kind = Tok.getKind();
2487 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002488 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2489 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002490 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002491 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002492
2493 // enum-specifier:
2494 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002495 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002496 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002497 continue;
2498
2499 // cv-qualifier:
2500 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002501 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002502 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002503 break;
2504 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002505 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002506 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002507 break;
2508 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002509 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002510 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002511 break;
2512
Douglas Gregord57959a2009-03-27 23:10:48 +00002513 // C++ typename-specifier:
2514 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002515 if (TryAnnotateTypeOrScopeToken()) {
2516 DS.SetTypeSpecError();
2517 goto DoneWithDeclSpec;
2518 }
2519 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002520 continue;
2521 break;
2522
Chris Lattner80d0c892009-01-21 19:48:37 +00002523 // GNU typeof support.
2524 case tok::kw_typeof:
2525 ParseTypeofSpecifier(DS);
2526 continue;
2527
David Blaikie42d6d0c2011-12-04 05:04:18 +00002528 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002529 ParseDecltypeSpecifier(DS);
2530 continue;
2531
Sean Huntdb5d44b2011-05-19 05:37:45 +00002532 case tok::kw___underlying_type:
2533 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002534 continue;
2535
2536 case tok::kw__Atomic:
2537 ParseAtomicSpecifier(DS);
2538 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002539
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002540 // OpenCL qualifiers:
2541 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002542 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002543 goto DoneWithDeclSpec;
2544 case tok::kw___private:
2545 case tok::kw___global:
2546 case tok::kw___local:
2547 case tok::kw___constant:
2548 case tok::kw___read_only:
2549 case tok::kw___write_only:
2550 case tok::kw___read_write:
2551 ParseOpenCLQualifiers(DS);
2552 break;
2553
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002554 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002555 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002556 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2557 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002558 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002559 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002560
Douglas Gregor46f936e2010-11-19 17:10:50 +00002561 if (!ParseObjCProtocolQualifiers(DS))
2562 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2563 << FixItHint::CreateInsertion(Loc, "id")
2564 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002565
2566 // Need to support trailing type qualifiers (e.g. "id<p> const").
2567 // If a type specifier follows, it will be diagnosed elsewhere.
2568 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002569 }
John McCallfec54012009-08-03 20:12:06 +00002570 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002571 if (isInvalid) {
2572 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002573 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002574
2575 if (DiagID == diag::ext_duplicate_declspec)
2576 Diag(Tok, DiagID)
2577 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2578 else
2579 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002580 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002581
Chris Lattner81c018d2008-03-13 06:29:04 +00002582 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002583 if (DiagID != diag::err_bool_redeclaration)
2584 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002585 }
2586}
Douglas Gregoradcac882008-12-01 23:54:00 +00002587
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002588/// ParseStructDeclaration - Parse a struct declaration without the terminating
2589/// semicolon.
2590///
Reid Spencer5f016e22007-07-11 17:01:13 +00002591/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002592/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002593/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002594/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002595/// struct-declarator-list:
2596/// struct-declarator
2597/// struct-declarator-list ',' struct-declarator
2598/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2599/// struct-declarator:
2600/// declarator
2601/// [GNU] declarator attributes[opt]
2602/// declarator[opt] ':' constant-expression
2603/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2604///
Chris Lattnere1359422008-04-10 06:46:29 +00002605void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002606ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002607
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002608 if (Tok.is(tok::kw___extension__)) {
2609 // __extension__ silences extension warnings in the subexpression.
2610 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002611 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002612 return ParseStructDeclaration(DS, Fields);
2613 }
Mike Stump1eb44332009-09-09 15:08:12 +00002614
Steve Naroff28a7ca82007-08-20 22:28:22 +00002615 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002616 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002617
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002618 // If there are no declarators, this is a free-standing declaration
2619 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002620 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002621 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002622 return;
2623 }
2624
2625 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002626 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002627 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002628 while (1) {
John McCall92576642012-05-07 06:16:41 +00002629 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCallbdd563e2009-11-03 02:38:08 +00002630 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002631 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002632
2633 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002634 if (!FirstDeclarator)
2635 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002636
Steve Naroff28a7ca82007-08-20 22:28:22 +00002637 /// struct-declarator: declarator
2638 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002639 if (Tok.isNot(tok::colon)) {
2640 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2641 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002642 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002643 }
Mike Stump1eb44332009-09-09 15:08:12 +00002644
Chris Lattner04d66662007-10-09 17:33:22 +00002645 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002646 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002647 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002648 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002649 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002650 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002651 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002652 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002653
Steve Naroff28a7ca82007-08-20 22:28:22 +00002654 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002655 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002656
John McCallbdd563e2009-11-03 02:38:08 +00002657 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002658 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002659 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002660
Steve Naroff28a7ca82007-08-20 22:28:22 +00002661 // If we don't have a comma, it is either the end of the list (a ';')
2662 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002663 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002664 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002665
Steve Naroff28a7ca82007-08-20 22:28:22 +00002666 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002667 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002668
John McCallbdd563e2009-11-03 02:38:08 +00002669 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002670 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002671}
2672
2673/// ParseStructUnionBody
2674/// struct-contents:
2675/// struct-declaration-list
2676/// [EXT] empty
2677/// [GNU] "struct-declaration-list" without terminatoring ';'
2678/// struct-declaration-list:
2679/// struct-declaration
2680/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002681/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002682///
Reid Spencer5f016e22007-07-11 17:01:13 +00002683void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002684 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002685 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2686 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002687
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002688 BalancedDelimiterTracker T(*this, tok::l_brace);
2689 if (T.consumeOpen())
2690 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002691
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002692 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002693 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002694
Reid Spencer5f016e22007-07-11 17:01:13 +00002695 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2696 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002697 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002698 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2699 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2700 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002701
Chris Lattner5f9e2722011-07-23 10:55:15 +00002702 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002703
Reid Spencer5f016e22007-07-11 17:01:13 +00002704 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002705 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002706 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002707
Reid Spencer5f016e22007-07-11 17:01:13 +00002708 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002709 if (Tok.is(tok::semi)) {
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002710 ConsumeExtraSemi(InsideStruct,
2711 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Reid Spencer5f016e22007-07-11 17:01:13 +00002712 continue;
2713 }
Chris Lattnere1359422008-04-10 06:46:29 +00002714
2715 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002716 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002717
John McCallbdd563e2009-11-03 02:38:08 +00002718 if (!Tok.is(tok::at)) {
2719 struct CFieldCallback : FieldCallback {
2720 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002721 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002722 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002723
John McCalld226f652010-08-21 09:40:31 +00002724 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002725 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002726 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2727
John McCalld226f652010-08-21 09:40:31 +00002728 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002729 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002730 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002731 FD.D.getDeclSpec().getSourceRange().getBegin(),
2732 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002733 FieldDecls.push_back(Field);
2734 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002735 }
John McCallbdd563e2009-11-03 02:38:08 +00002736 } Callback(*this, TagDecl, FieldDecls);
2737
2738 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002739 } else { // Handle @defs
2740 ConsumeToken();
2741 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2742 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002743 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002744 continue;
2745 }
2746 ConsumeToken();
2747 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2748 if (!Tok.is(tok::identifier)) {
2749 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002750 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002751 continue;
2752 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002753 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002754 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002755 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002756 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2757 ConsumeToken();
2758 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002759 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002760
Chris Lattner04d66662007-10-09 17:33:22 +00002761 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002762 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002763 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002764 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002765 break;
2766 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002767 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2768 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002769 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002770 // If we stopped at a ';', eat it.
2771 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002772 }
2773 }
Mike Stump1eb44332009-09-09 15:08:12 +00002774
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002775 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002776
John McCall0b7e6782011-03-24 11:26:52 +00002777 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002778 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002779 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002780
Douglas Gregor23c94db2010-07-02 17:43:08 +00002781 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002782 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002783 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002784 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002785 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002786 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2787 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002788}
2789
Reid Spencer5f016e22007-07-11 17:01:13 +00002790/// ParseEnumSpecifier
2791/// enum-specifier: [C99 6.7.2.2]
2792/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002793///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002794/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2795/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002796/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2797/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002798/// 'enum' identifier
2799/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002800///
Richard Smith1af83c42012-03-23 03:33:32 +00002801/// [C++11] enum-head '{' enumerator-list[opt] '}'
2802/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002803///
Richard Smith1af83c42012-03-23 03:33:32 +00002804/// enum-head: [C++11]
2805/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2806/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2807/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002808///
Richard Smith1af83c42012-03-23 03:33:32 +00002809/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002810/// 'enum'
2811/// 'enum' 'class'
2812/// 'enum' 'struct'
2813///
Richard Smith1af83c42012-03-23 03:33:32 +00002814/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002815/// ':' type-specifier-seq
2816///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002817/// [C++] elaborated-type-specifier:
2818/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2819///
Chris Lattner4c97d762009-04-12 21:49:30 +00002820void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002821 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002822 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002823 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002824 if (Tok.is(tok::code_completion)) {
2825 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002826 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002827 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002828 }
John McCall57c13002011-07-06 05:58:41 +00002829
Richard Smithbdad7a22012-01-10 01:33:14 +00002830 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002831 bool IsScopedUsingClassTag = false;
2832
David Blaikie4e4d0842012-03-11 07:00:24 +00002833 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002834 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002835 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002836 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002837 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002838 }
Richard Smith1af83c42012-03-23 03:33:32 +00002839
John McCall13489672012-05-07 06:16:58 +00002840 // C++11 [temp.explicit]p12:
2841 // The usual access controls do not apply to names used to specify
2842 // explicit instantiations.
2843 // We extend this to also cover explicit specializations. Note that
2844 // we don't suppress if this turns out to be an elaborated type
2845 // specifier.
2846 bool shouldDelayDiagsInTag =
2847 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2848 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2849 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00002850
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002851 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002852 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002853 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002854
Aaron Ballman6454a022012-03-01 04:09:28 +00002855 // If declspecs exist after tag, parse them.
2856 while (Tok.is(tok::kw___declspec))
2857 ParseMicrosoftDeclSpec(attrs);
2858
Richard Smith7796eb52012-03-12 08:56:40 +00002859 // Enum definitions should not be parsed in a trailing-return-type.
2860 bool AllowDeclaration = DSC != DSC_trailing;
2861
2862 bool AllowFixedUnderlyingType = AllowDeclaration &&
2863 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2864 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002865
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002866 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002867 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002868 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2869 // if a fixed underlying type is allowed.
2870 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2871
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002872 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2873 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002874 return;
2875
2876 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002877 Diag(Tok, diag::err_expected_ident);
2878 if (Tok.isNot(tok::l_brace)) {
2879 // Has no name and is not a definition.
2880 // Skip the rest of this declarator, up until the comma or semicolon.
2881 SkipUntil(tok::comma, true);
2882 return;
2883 }
2884 }
2885 }
Mike Stump1eb44332009-09-09 15:08:12 +00002886
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002887 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002888 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002889 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002890 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002891
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002892 // Skip the rest of this declarator, up until the comma or semicolon.
2893 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002894 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002895 }
Mike Stump1eb44332009-09-09 15:08:12 +00002896
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002897 // If an identifier is present, consume and remember it.
2898 IdentifierInfo *Name = 0;
2899 SourceLocation NameLoc;
2900 if (Tok.is(tok::identifier)) {
2901 Name = Tok.getIdentifierInfo();
2902 NameLoc = ConsumeToken();
2903 }
Mike Stump1eb44332009-09-09 15:08:12 +00002904
Richard Smithbdad7a22012-01-10 01:33:14 +00002905 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002906 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2907 // declaration of a scoped enumeration.
2908 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002909 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002910 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002911 }
2912
John McCall13489672012-05-07 06:16:58 +00002913 // Okay, end the suppression area. We'll decide whether to emit the
2914 // diagnostics in a second.
2915 if (shouldDelayDiagsInTag)
2916 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00002917
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002918 TypeResult BaseType;
2919
Douglas Gregora61b3e72010-12-01 17:42:47 +00002920 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002921 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002922 bool PossibleBitfield = false;
2923 if (getCurScope()->getFlags() & Scope::ClassScope) {
2924 // If we're in class scope, this can either be an enum declaration with
2925 // an underlying type, or a declaration of a bitfield member. We try to
2926 // use a simple disambiguation scheme first to catch the common cases
2927 // (integer literal, sizeof); if it's still ambiguous, we then consider
2928 // anything that's a simple-type-specifier followed by '(' as an
2929 // expression. This suffices because function types are not valid
2930 // underlying types anyway.
2931 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2932 // If the next token starts an expression, we know we're parsing a
2933 // bit-field. This is the common case.
2934 if (TPR == TPResult::True())
2935 PossibleBitfield = true;
2936 // If the next token starts a type-specifier-seq, it may be either a
2937 // a fixed underlying type or the start of a function-style cast in C++;
2938 // lookahead one more token to see if it's obvious that we have a
2939 // fixed underlying type.
2940 else if (TPR == TPResult::False() &&
2941 GetLookAheadToken(2).getKind() == tok::semi) {
2942 // Consume the ':'.
2943 ConsumeToken();
2944 } else {
2945 // We have the start of a type-specifier-seq, so we have to perform
2946 // tentative parsing to determine whether we have an expression or a
2947 // type.
2948 TentativeParsingAction TPA(*this);
2949
2950 // Consume the ':'.
2951 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002952
2953 // If we see a type specifier followed by an open-brace, we have an
2954 // ambiguity between an underlying type and a C++11 braced
2955 // function-style cast. Resolve this by always treating it as an
2956 // underlying type.
2957 // FIXME: The standard is not entirely clear on how to disambiguate in
2958 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002959 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002960 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002961 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002962 // We'll parse this as a bitfield later.
2963 PossibleBitfield = true;
2964 TPA.Revert();
2965 } else {
2966 // We have a type-specifier-seq.
2967 TPA.Commit();
2968 }
2969 }
2970 } else {
2971 // Consume the ':'.
2972 ConsumeToken();
2973 }
2974
2975 if (!PossibleBitfield) {
2976 SourceRange Range;
2977 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002978
David Blaikie4e4d0842012-03-11 07:00:24 +00002979 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002980 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2981 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002982 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002983 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002984 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002985 }
2986
Richard Smithbdad7a22012-01-10 01:33:14 +00002987 // There are four options here. If we have 'friend enum foo;' then this is a
2988 // friend declaration, and cannot have an accompanying definition. If we have
2989 // 'enum foo;', then this is a forward declaration. If we have
2990 // 'enum foo {...' then this is a definition. Otherwise we have something
2991 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002992 //
2993 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2994 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2995 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2996 //
John McCallf312b1e2010-08-26 23:41:50 +00002997 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00002998 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00002999 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003000 } else if (Tok.is(tok::l_brace)) {
3001 if (DS.isFriendSpecified()) {
3002 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3003 << SourceRange(DS.getFriendSpecLoc());
3004 ConsumeBrace();
3005 SkipUntil(tok::r_brace);
3006 TUK = Sema::TUK_Friend;
3007 } else {
3008 TUK = Sema::TUK_Definition;
3009 }
3010 } else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) {
3011 TUK = (DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration);
3012 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003013 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003014 }
3015
3016 // If this is an elaborated type specifier, and we delayed
3017 // diagnostics before, just merge them into the current pool.
3018 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3019 diagsFromTag.redelay();
3020 }
Richard Smith1af83c42012-03-23 03:33:32 +00003021
3022 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003023 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003024 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003025 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3026 // Skip the rest of this declarator, up until the comma or semicolon.
3027 Diag(Tok, diag::err_enum_template);
3028 SkipUntil(tok::comma, true);
3029 return;
3030 }
3031
3032 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3033 // Enumerations can't be explicitly instantiated.
3034 DS.SetTypeSpecError();
3035 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3036 return;
3037 }
3038
3039 assert(TemplateInfo.TemplateParams && "no template parameters");
3040 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3041 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003042 }
Richard Smith1af83c42012-03-23 03:33:32 +00003043
Douglas Gregorb9075602011-02-22 02:55:24 +00003044 if (!Name && TUK != Sema::TUK_Definition) {
3045 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003046
Douglas Gregorb9075602011-02-22 02:55:24 +00003047 // Skip the rest of this declarator, up until the comma or semicolon.
3048 SkipUntil(tok::comma, true);
3049 return;
3050 }
Richard Smith1af83c42012-03-23 03:33:32 +00003051
Douglas Gregor402abb52009-05-28 23:31:59 +00003052 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003053 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003054 const char *PrevSpec = 0;
3055 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003056 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003057 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003058 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003059 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003060 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003061
Douglas Gregor48c89f42010-04-24 16:38:41 +00003062 if (IsDependent) {
3063 // This enum has a dependent nested-name-specifier. Handle it as a
3064 // dependent tag.
3065 if (!Name) {
3066 DS.SetTypeSpecError();
3067 Diag(Tok, diag::err_expected_type_name_after_typename);
3068 return;
3069 }
3070
Douglas Gregor23c94db2010-07-02 17:43:08 +00003071 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003072 TUK, SS, Name, StartLoc,
3073 NameLoc);
3074 if (Type.isInvalid()) {
3075 DS.SetTypeSpecError();
3076 return;
3077 }
3078
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003079 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3080 NameLoc.isValid() ? NameLoc : StartLoc,
3081 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003082 Diag(StartLoc, DiagID) << PrevSpec;
3083
3084 return;
3085 }
Mike Stump1eb44332009-09-09 15:08:12 +00003086
John McCalld226f652010-08-21 09:40:31 +00003087 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003088 // The action failed to produce an enumeration tag. If this is a
3089 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003090 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003091 ConsumeBrace();
3092 SkipUntil(tok::r_brace);
3093 }
3094
3095 DS.SetTypeSpecError();
3096 return;
3097 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003098
Richard Smith7796eb52012-03-12 08:56:40 +00003099 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
John McCall13489672012-05-07 06:16:58 +00003100 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00003101 }
Mike Stump1eb44332009-09-09 15:08:12 +00003102
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003103 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3104 NameLoc.isValid() ? NameLoc : StartLoc,
3105 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003106 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003107}
3108
3109/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3110/// enumerator-list:
3111/// enumerator
3112/// enumerator-list ',' enumerator
3113/// enumerator:
3114/// enumeration-constant
3115/// enumeration-constant '=' constant-expression
3116/// enumeration-constant:
3117/// identifier
3118///
John McCalld226f652010-08-21 09:40:31 +00003119void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003120 // Enter the scope of the enum body and start the definition.
3121 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003122 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003123
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003124 BalancedDelimiterTracker T(*this, tok::l_brace);
3125 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003126
Chris Lattner7946dd32007-08-27 17:24:30 +00003127 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003128 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003129 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003130
Chris Lattner5f9e2722011-07-23 10:55:15 +00003131 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003132
John McCalld226f652010-08-21 09:40:31 +00003133 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003134
Reid Spencer5f016e22007-07-11 17:01:13 +00003135 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003136 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003137 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3138 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003139
John McCall5b629aa2010-10-22 23:36:17 +00003140 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003141 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003142 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003143
Reid Spencer5f016e22007-07-11 17:01:13 +00003144 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003145 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003146 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003147
Chris Lattner04d66662007-10-09 17:33:22 +00003148 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003149 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003150 AssignedVal = ParseConstantExpression();
3151 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003152 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003153 }
Mike Stump1eb44332009-09-09 15:08:12 +00003154
Reid Spencer5f016e22007-07-11 17:01:13 +00003155 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003156 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3157 LastEnumConstDecl,
3158 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003159 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003160 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003161 PD.complete(EnumConstDecl);
3162
Reid Spencer5f016e22007-07-11 17:01:13 +00003163 EnumConstantDecls.push_back(EnumConstDecl);
3164 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003165
Douglas Gregor751f6922010-09-07 14:51:08 +00003166 if (Tok.is(tok::identifier)) {
3167 // We're missing a comma between enumerators.
3168 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3169 Diag(Loc, diag::err_enumerator_list_missing_comma)
3170 << FixItHint::CreateInsertion(Loc, ", ");
3171 continue;
3172 }
3173
Chris Lattner04d66662007-10-09 17:33:22 +00003174 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003175 break;
3176 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003177
Richard Smith7fe62082011-10-15 05:09:34 +00003178 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003179 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003180 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003181 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003182 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003183 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003184 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3185 << FixItHint::CreateRemoval(CommaLoc);
3186 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003187 }
Mike Stump1eb44332009-09-09 15:08:12 +00003188
Reid Spencer5f016e22007-07-11 17:01:13 +00003189 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003190 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003191
Reid Spencer5f016e22007-07-11 17:01:13 +00003192 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003193 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003194 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003195
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003196 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3197 EnumDecl, EnumConstantDecls.data(),
3198 EnumConstantDecls.size(), getCurScope(),
3199 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003200
Douglas Gregor72de6672009-01-08 20:45:30 +00003201 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003202 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3203 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003204}
3205
3206/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003207/// start of a type-qualifier-list.
3208bool Parser::isTypeQualifier() const {
3209 switch (Tok.getKind()) {
3210 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003211
3212 // type-qualifier only in OpenCL
3213 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003214 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003215
Steve Naroff5f8aa692008-02-11 23:15:56 +00003216 // type-qualifier
3217 case tok::kw_const:
3218 case tok::kw_volatile:
3219 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003220 case tok::kw___private:
3221 case tok::kw___local:
3222 case tok::kw___global:
3223 case tok::kw___constant:
3224 case tok::kw___read_only:
3225 case tok::kw___read_write:
3226 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003227 return true;
3228 }
3229}
3230
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003231/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3232/// is definitely a type-specifier. Return false if it isn't part of a type
3233/// specifier or if we're not sure.
3234bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3235 switch (Tok.getKind()) {
3236 default: return false;
3237 // type-specifiers
3238 case tok::kw_short:
3239 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003240 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003241 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003242 case tok::kw_signed:
3243 case tok::kw_unsigned:
3244 case tok::kw__Complex:
3245 case tok::kw__Imaginary:
3246 case tok::kw_void:
3247 case tok::kw_char:
3248 case tok::kw_wchar_t:
3249 case tok::kw_char16_t:
3250 case tok::kw_char32_t:
3251 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003252 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003253 case tok::kw_float:
3254 case tok::kw_double:
3255 case tok::kw_bool:
3256 case tok::kw__Bool:
3257 case tok::kw__Decimal32:
3258 case tok::kw__Decimal64:
3259 case tok::kw__Decimal128:
3260 case tok::kw___vector:
3261
3262 // struct-or-union-specifier (C99) or class-specifier (C++)
3263 case tok::kw_class:
3264 case tok::kw_struct:
3265 case tok::kw_union:
3266 // enum-specifier
3267 case tok::kw_enum:
3268
3269 // typedef-name
3270 case tok::annot_typename:
3271 return true;
3272 }
3273}
3274
Steve Naroff5f8aa692008-02-11 23:15:56 +00003275/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003276/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003277bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003278 switch (Tok.getKind()) {
3279 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003280
Chris Lattner166a8fc2009-01-04 23:41:41 +00003281 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003282 if (TryAltiVecVectorToken())
3283 return true;
3284 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003285 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003286 // Annotate typenames and C++ scope specifiers. If we get one, just
3287 // recurse to handle whatever we get.
3288 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003289 return true;
3290 if (Tok.is(tok::identifier))
3291 return false;
3292 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003293
Chris Lattner166a8fc2009-01-04 23:41:41 +00003294 case tok::coloncolon: // ::foo::bar
3295 if (NextToken().is(tok::kw_new) || // ::new
3296 NextToken().is(tok::kw_delete)) // ::delete
3297 return false;
3298
Chris Lattner166a8fc2009-01-04 23:41:41 +00003299 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003300 return true;
3301 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003302
Reid Spencer5f016e22007-07-11 17:01:13 +00003303 // GNU attributes support.
3304 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003305 // GNU typeof support.
3306 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003307
Reid Spencer5f016e22007-07-11 17:01:13 +00003308 // type-specifiers
3309 case tok::kw_short:
3310 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003311 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003312 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003313 case tok::kw_signed:
3314 case tok::kw_unsigned:
3315 case tok::kw__Complex:
3316 case tok::kw__Imaginary:
3317 case tok::kw_void:
3318 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003319 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003320 case tok::kw_char16_t:
3321 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003322 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003323 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003324 case tok::kw_float:
3325 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003326 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003327 case tok::kw__Bool:
3328 case tok::kw__Decimal32:
3329 case tok::kw__Decimal64:
3330 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003331 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003332
Chris Lattner99dc9142008-04-13 18:59:07 +00003333 // struct-or-union-specifier (C99) or class-specifier (C++)
3334 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003335 case tok::kw_struct:
3336 case tok::kw_union:
3337 // enum-specifier
3338 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003339
Reid Spencer5f016e22007-07-11 17:01:13 +00003340 // type-qualifier
3341 case tok::kw_const:
3342 case tok::kw_volatile:
3343 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003344
3345 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003346 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003347 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003348
Chris Lattner7c186be2008-10-20 00:25:30 +00003349 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3350 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003351 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003352
Steve Naroff239f0732008-12-25 14:16:32 +00003353 case tok::kw___cdecl:
3354 case tok::kw___stdcall:
3355 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003356 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003357 case tok::kw___w64:
3358 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003359 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003360 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003361 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003362
3363 case tok::kw___private:
3364 case tok::kw___local:
3365 case tok::kw___global:
3366 case tok::kw___constant:
3367 case tok::kw___read_only:
3368 case tok::kw___read_write:
3369 case tok::kw___write_only:
3370
Eli Friedman290eeb02009-06-08 23:27:34 +00003371 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003372
3373 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003374 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003375
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003376 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003377 case tok::kw__Atomic:
3378 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003379 }
3380}
3381
3382/// isDeclarationSpecifier() - Return true if the current token is part of a
3383/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003384///
3385/// \param DisambiguatingWithExpression True to indicate that the purpose of
3386/// this check is to disambiguate between an expression and a declaration.
3387bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003388 switch (Tok.getKind()) {
3389 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003390
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003391 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003392 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003393
Chris Lattner166a8fc2009-01-04 23:41:41 +00003394 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003395 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003396 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003397 return false;
John Thompson82287d12010-02-05 00:12:22 +00003398 if (TryAltiVecVectorToken())
3399 return true;
3400 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003401 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003402 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003403 // Annotate typenames and C++ scope specifiers. If we get one, just
3404 // recurse to handle whatever we get.
3405 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003406 return true;
3407 if (Tok.is(tok::identifier))
3408 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003409
3410 // If we're in Objective-C and we have an Objective-C class type followed
3411 // by an identifier and then either ':' or ']', in a place where an
3412 // expression is permitted, then this is probably a class message send
3413 // missing the initial '['. In this case, we won't consider this to be
3414 // the start of a declaration.
3415 if (DisambiguatingWithExpression &&
3416 isStartOfObjCClassMessageMissingOpenBracket())
3417 return false;
3418
John McCall9ba61662010-02-26 08:45:28 +00003419 return isDeclarationSpecifier();
3420
Chris Lattner166a8fc2009-01-04 23:41:41 +00003421 case tok::coloncolon: // ::foo::bar
3422 if (NextToken().is(tok::kw_new) || // ::new
3423 NextToken().is(tok::kw_delete)) // ::delete
3424 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003425
Chris Lattner166a8fc2009-01-04 23:41:41 +00003426 // Annotate typenames and C++ scope specifiers. If we get one, just
3427 // recurse to handle whatever we get.
3428 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003429 return true;
3430 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003431
Reid Spencer5f016e22007-07-11 17:01:13 +00003432 // storage-class-specifier
3433 case tok::kw_typedef:
3434 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003435 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003436 case tok::kw_static:
3437 case tok::kw_auto:
3438 case tok::kw_register:
3439 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003440
Douglas Gregor8d267c52011-09-09 02:06:17 +00003441 // Modules
3442 case tok::kw___module_private__:
3443
Reid Spencer5f016e22007-07-11 17:01:13 +00003444 // type-specifiers
3445 case tok::kw_short:
3446 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003447 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003448 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003449 case tok::kw_signed:
3450 case tok::kw_unsigned:
3451 case tok::kw__Complex:
3452 case tok::kw__Imaginary:
3453 case tok::kw_void:
3454 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003455 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003456 case tok::kw_char16_t:
3457 case tok::kw_char32_t:
3458
Reid Spencer5f016e22007-07-11 17:01:13 +00003459 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003460 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003461 case tok::kw_float:
3462 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003463 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003464 case tok::kw__Bool:
3465 case tok::kw__Decimal32:
3466 case tok::kw__Decimal64:
3467 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003468 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003469
Chris Lattner99dc9142008-04-13 18:59:07 +00003470 // struct-or-union-specifier (C99) or class-specifier (C++)
3471 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003472 case tok::kw_struct:
3473 case tok::kw_union:
3474 // enum-specifier
3475 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003476
Reid Spencer5f016e22007-07-11 17:01:13 +00003477 // type-qualifier
3478 case tok::kw_const:
3479 case tok::kw_volatile:
3480 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003481
Reid Spencer5f016e22007-07-11 17:01:13 +00003482 // function-specifier
3483 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003484 case tok::kw_virtual:
3485 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003486
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003487 // static_assert-declaration
3488 case tok::kw__Static_assert:
3489
Chris Lattner1ef08762007-08-09 17:01:07 +00003490 // GNU typeof support.
3491 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003492
Chris Lattner1ef08762007-08-09 17:01:07 +00003493 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003494 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003495 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003496
Francois Pichete3d49b42011-06-19 08:02:06 +00003497 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003498 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003499 return true;
3500
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003501 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003502 case tok::kw__Atomic:
3503 return true;
3504
Chris Lattnerf3948c42008-07-26 03:38:44 +00003505 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3506 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003507 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003508
Douglas Gregord9d75e52011-04-27 05:41:15 +00003509 // typedef-name
3510 case tok::annot_typename:
3511 return !DisambiguatingWithExpression ||
3512 !isStartOfObjCClassMessageMissingOpenBracket();
3513
Steve Naroff47f52092009-01-06 19:34:12 +00003514 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003515 case tok::kw___cdecl:
3516 case tok::kw___stdcall:
3517 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003518 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003519 case tok::kw___w64:
3520 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003521 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003522 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003523 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003524 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003525
3526 case tok::kw___private:
3527 case tok::kw___local:
3528 case tok::kw___global:
3529 case tok::kw___constant:
3530 case tok::kw___read_only:
3531 case tok::kw___read_write:
3532 case tok::kw___write_only:
3533
Eli Friedman290eeb02009-06-08 23:27:34 +00003534 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003535 }
3536}
3537
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003538bool Parser::isConstructorDeclarator() {
3539 TentativeParsingAction TPA(*this);
3540
3541 // Parse the C++ scope specifier.
3542 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003543 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3544 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003545 TPA.Revert();
3546 return false;
3547 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003548
3549 // Parse the constructor name.
3550 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3551 // We already know that we have a constructor name; just consume
3552 // the token.
3553 ConsumeToken();
3554 } else {
3555 TPA.Revert();
3556 return false;
3557 }
3558
Richard Smith22592862012-03-27 23:05:05 +00003559 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003560 if (Tok.isNot(tok::l_paren)) {
3561 TPA.Revert();
3562 return false;
3563 }
3564 ConsumeParen();
3565
Richard Smith22592862012-03-27 23:05:05 +00003566 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3567 // that we have a constructor.
3568 if (Tok.is(tok::r_paren) ||
3569 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003570 TPA.Revert();
3571 return true;
3572 }
3573
3574 // If we need to, enter the specified scope.
3575 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003576 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003577 DeclScopeObj.EnterDeclaratorScope();
3578
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003579 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003580 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003581 MaybeParseMicrosoftAttributes(Attrs);
3582
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003583 // Check whether the next token(s) are part of a declaration
3584 // specifier, in which case we have the start of a parameter and,
3585 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003586 bool IsConstructor = false;
3587 if (isDeclarationSpecifier())
3588 IsConstructor = true;
3589 else if (Tok.is(tok::identifier) ||
3590 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3591 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3592 // This might be a parenthesized member name, but is more likely to
3593 // be a constructor declaration with an invalid argument type. Keep
3594 // looking.
3595 if (Tok.is(tok::annot_cxxscope))
3596 ConsumeToken();
3597 ConsumeToken();
3598
3599 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003600 // which must have one of the following syntactic forms (see the
3601 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003602 switch (Tok.getKind()) {
3603 case tok::l_paren:
3604 // C(X ( int));
3605 case tok::l_square:
3606 // C(X [ 5]);
3607 // C(X [ [attribute]]);
3608 case tok::coloncolon:
3609 // C(X :: Y);
3610 // C(X :: *p);
3611 case tok::r_paren:
3612 // C(X )
3613 // Assume this isn't a constructor, rather than assuming it's a
3614 // constructor with an unnamed parameter of an ill-formed type.
3615 break;
3616
3617 default:
3618 IsConstructor = true;
3619 break;
3620 }
3621 }
3622
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003623 TPA.Revert();
3624 return IsConstructor;
3625}
Reid Spencer5f016e22007-07-11 17:01:13 +00003626
3627/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003628/// type-qualifier-list: [C99 6.7.5]
3629/// type-qualifier
3630/// [vendor] attributes
3631/// [ only if VendorAttributesAllowed=true ]
3632/// type-qualifier-list type-qualifier
3633/// [vendor] type-qualifier-list attributes
3634/// [ only if VendorAttributesAllowed=true ]
3635/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3636/// [ only if CXX0XAttributesAllowed=true ]
3637/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003638///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003639void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3640 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003641 bool CXX11AttributesAllowed) {
3642 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003643 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003644 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003645 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003646 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003647 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003648
3649 SourceLocation EndLoc;
3650
Reid Spencer5f016e22007-07-11 17:01:13 +00003651 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003652 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003653 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003654 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003655 SourceLocation Loc = Tok.getLocation();
3656
3657 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003658 case tok::code_completion:
3659 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003660 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003661
Reid Spencer5f016e22007-07-11 17:01:13 +00003662 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003663 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003664 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003665 break;
3666 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003667 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003668 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003669 break;
3670 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003671 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003672 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003673 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003674
3675 // OpenCL qualifiers:
3676 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003677 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003678 goto DoneWithTypeQuals;
3679 case tok::kw___private:
3680 case tok::kw___global:
3681 case tok::kw___local:
3682 case tok::kw___constant:
3683 case tok::kw___read_only:
3684 case tok::kw___write_only:
3685 case tok::kw___read_write:
3686 ParseOpenCLQualifiers(DS);
3687 break;
3688
Eli Friedman290eeb02009-06-08 23:27:34 +00003689 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003690 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003691 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003692 case tok::kw___cdecl:
3693 case tok::kw___stdcall:
3694 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003695 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003696 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003697 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003698 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003699 continue;
3700 }
3701 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003702 case tok::kw___pascal:
3703 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003704 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003705 continue;
3706 }
3707 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003708 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003709 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003710 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003711 continue; // do *not* consume the next token!
3712 }
3713 // otherwise, FALL THROUGH!
3714 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003715 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003716 // If this is not a type-qualifier token, we're done reading type
3717 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003718 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003719 if (EndLoc.isValid())
3720 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003721 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003722 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003723
Reid Spencer5f016e22007-07-11 17:01:13 +00003724 // If the specifier combination wasn't legal, issue a diagnostic.
3725 if (isInvalid) {
3726 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003727 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003728 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003729 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003730 }
3731}
3732
3733
3734/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3735///
3736void Parser::ParseDeclarator(Declarator &D) {
3737 /// This implements the 'declarator' production in the C grammar, then checks
3738 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003739 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003740}
3741
Richard Smith9988f282012-03-29 01:16:42 +00003742static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3743 if (Kind == tok::star || Kind == tok::caret)
3744 return true;
3745
3746 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3747 if (!Lang.CPlusPlus)
3748 return false;
3749
3750 return Kind == tok::amp || Kind == tok::ampamp;
3751}
3752
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003753/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3754/// is parsed by the function passed to it. Pass null, and the direct-declarator
3755/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003756/// ptr-operator production.
3757///
Richard Smith0706df42011-10-19 21:33:05 +00003758/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003759/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3760/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003761///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003762/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3763/// [C] pointer[opt] direct-declarator
3764/// [C++] direct-declarator
3765/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003766///
3767/// pointer: [C99 6.7.5]
3768/// '*' type-qualifier-list[opt]
3769/// '*' type-qualifier-list[opt] pointer
3770///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003771/// ptr-operator:
3772/// '*' cv-qualifier-seq[opt]
3773/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003774/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003775/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003776/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003777/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003778void Parser::ParseDeclaratorInternal(Declarator &D,
3779 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003780 if (Diags.hasAllExtensionsSilenced())
3781 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003782
Sebastian Redlf30208a2009-01-24 21:16:55 +00003783 // C++ member pointers start with a '::' or a nested-name.
3784 // Member pointers get special handling, since there's no place for the
3785 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003786 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003787 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3788 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003789 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3790 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003791 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003792 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003793
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003794 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003795 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003796 // The scope spec really belongs to the direct-declarator.
3797 D.getCXXScopeSpec() = SS;
3798 if (DirectDeclParser)
3799 (this->*DirectDeclParser)(D);
3800 return;
3801 }
3802
3803 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003804 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003805 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003806 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003807 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003808
3809 // Recurse to parse whatever is left.
3810 ParseDeclaratorInternal(D, DirectDeclParser);
3811
3812 // Sema will have to catch (syntactically invalid) pointers into global
3813 // scope. It has to catch pointers into namespace scope anyway.
3814 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003815 Loc),
3816 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003817 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003818 return;
3819 }
3820 }
3821
3822 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003823 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003824 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003825 if (DirectDeclParser)
3826 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003827 return;
3828 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003829
Sebastian Redl05532f22009-03-15 22:02:01 +00003830 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3831 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003832 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003833 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003834
Chris Lattner9af55002009-03-27 04:18:06 +00003835 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003836 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003837 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003838
Richard Smith6ee326a2012-04-10 01:32:12 +00003839 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00003840 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003841 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003842
Reid Spencer5f016e22007-07-11 17:01:13 +00003843 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003844 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003845 if (Kind == tok::star)
3846 // Remember that we parsed a pointer type, and remember the type-quals.
3847 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003848 DS.getConstSpecLoc(),
3849 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003850 DS.getRestrictSpecLoc()),
3851 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003852 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003853 else
3854 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003855 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003856 Loc),
3857 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003858 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003859 } else {
3860 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003861 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003862
Sebastian Redl743de1f2009-03-23 00:00:23 +00003863 // Complain about rvalue references in C++03, but then go on and build
3864 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003865 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003866 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003867 diag::warn_cxx98_compat_rvalue_reference :
3868 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003869
Richard Smith6ee326a2012-04-10 01:32:12 +00003870 // GNU-style and C++11 attributes are allowed here, as is restrict.
3871 ParseTypeQualifierListOpt(DS);
3872 D.ExtendWithDeclSpec(DS);
3873
Reid Spencer5f016e22007-07-11 17:01:13 +00003874 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3875 // cv-qualifiers are introduced through the use of a typedef or of a
3876 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00003877 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3878 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3879 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003880 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003881 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3882 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003883 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003884 }
3885
3886 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003887 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003888
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003889 if (D.getNumTypeObjects() > 0) {
3890 // C++ [dcl.ref]p4: There shall be no references to references.
3891 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3892 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003893 if (const IdentifierInfo *II = D.getIdentifier())
3894 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3895 << II;
3896 else
3897 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3898 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003899
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003900 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003901 // can go ahead and build the (technically ill-formed)
3902 // declarator: reference collapsing will take care of it.
3903 }
3904 }
3905
Reid Spencer5f016e22007-07-11 17:01:13 +00003906 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003907 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003908 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003909 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003910 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003911 }
3912}
3913
Richard Smith9988f282012-03-29 01:16:42 +00003914static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
3915 SourceLocation EllipsisLoc) {
3916 if (EllipsisLoc.isValid()) {
3917 FixItHint Insertion;
3918 if (!D.getEllipsisLoc().isValid()) {
3919 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
3920 D.setEllipsisLoc(EllipsisLoc);
3921 }
3922 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
3923 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
3924 }
3925}
3926
Reid Spencer5f016e22007-07-11 17:01:13 +00003927/// ParseDirectDeclarator
3928/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003929/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003930/// '(' declarator ')'
3931/// [GNU] '(' attributes declarator ')'
3932/// [C90] direct-declarator '[' constant-expression[opt] ']'
3933/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3934/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3935/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3936/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00003937/// [C++11] direct-declarator '[' constant-expression[opt] ']'
3938/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00003939/// direct-declarator '(' parameter-type-list ')'
3940/// direct-declarator '(' identifier-list[opt] ')'
3941/// [GNU] direct-declarator '(' parameter-forward-declarations
3942/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003943/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3944/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00003945/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
3946/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
3947/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003948/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00003949/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003950///
3951/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003952/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003953/// '::'[opt] nested-name-specifier[opt] type-name
3954///
3955/// id-expression: [C++ 5.1]
3956/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003957/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003958///
3959/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003960/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003961/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003962/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003963/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003964/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003965///
Richard Smith5d8388c2012-03-27 01:42:32 +00003966/// Note, any additional constructs added here may need corresponding changes
3967/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00003968void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003969 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003970
David Blaikie4e4d0842012-03-11 07:00:24 +00003971 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003972 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003973 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003974 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3975 D.getContext() == Declarator::MemberContext;
3976 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3977 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003978 }
3979
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003980 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003981 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003982 // Change the declaration context for name lookup, until this function
3983 // is exited (and the declarator has been parsed).
3984 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003985 }
3986
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003987 // C++0x [dcl.fct]p14:
3988 // There is a syntactic ambiguity when an ellipsis occurs at the end
3989 // of a parameter-declaration-clause without a preceding comma. In
3990 // this case, the ellipsis is parsed as part of the
3991 // abstract-declarator if the type of the parameter names a template
3992 // parameter pack that has not been expanded; otherwise, it is parsed
3993 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00003994 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003995 !((D.getContext() == Declarator::PrototypeContext ||
3996 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003997 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00003998 !Actions.containsUnexpandedParameterPacks(D))) {
3999 SourceLocation EllipsisLoc = ConsumeToken();
4000 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4001 // The ellipsis was put in the wrong place. Recover, and explain to
4002 // the user what they should have done.
4003 ParseDeclarator(D);
4004 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4005 return;
4006 } else
4007 D.setEllipsisLoc(EllipsisLoc);
4008
4009 // The ellipsis can't be followed by a parenthesized declarator. We
4010 // check for that in ParseParenDeclarator, after we have disambiguated
4011 // the l_paren token.
4012 }
4013
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004014 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4015 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4016 // We found something that indicates the start of an unqualified-id.
4017 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004018 bool AllowConstructorName;
4019 if (D.getDeclSpec().hasTypeSpecifier())
4020 AllowConstructorName = false;
4021 else if (D.getCXXScopeSpec().isSet())
4022 AllowConstructorName =
4023 (D.getContext() == Declarator::FileContext ||
4024 (D.getContext() == Declarator::MemberContext &&
4025 D.getDeclSpec().isFriendSpecified()));
4026 else
4027 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4028
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004029 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004030 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4031 /*EnteringContext=*/true,
4032 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004033 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004034 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004035 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004036 D.getName()) ||
4037 // Once we're past the identifier, if the scope was bad, mark the
4038 // whole declarator bad.
4039 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004040 D.SetIdentifier(0, Tok.getLocation());
4041 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004042 } else {
4043 // Parsed the unqualified-id; update range information and move along.
4044 if (D.getSourceRange().getBegin().isInvalid())
4045 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4046 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004047 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004048 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004049 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004050 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004051 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004052 "There's a C++-specific check for tok::identifier above");
4053 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4054 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4055 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004056 goto PastIdentifier;
4057 }
Richard Smith9988f282012-03-29 01:16:42 +00004058
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004059 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004060 // direct-declarator: '(' declarator ')'
4061 // direct-declarator: '(' attributes declarator ')'
4062 // Example: 'char (*X)' or 'int (*XX)(void)'
4063 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004064
4065 // If the declarator was parenthesized, we entered the declarator
4066 // scope when parsing the parenthesized declarator, then exited
4067 // the scope already. Re-enter the scope, if we need to.
4068 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004069 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004070 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004071 if (!D.isInvalidType() &&
4072 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004073 // Change the declaration context for name lookup, until this function
4074 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004075 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004076 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004077 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004078 // This could be something simple like "int" (in which case the declarator
4079 // portion is empty), if an abstract-declarator is allowed.
4080 D.SetIdentifier(0, Tok.getLocation());
4081 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00004082 if (D.getContext() == Declarator::MemberContext)
4083 Diag(Tok, diag::err_expected_member_name_or_semi)
4084 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004085 else if (getLangOpts().CPlusPlus)
4086 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004087 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004088 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004089 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004090 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004091 }
Mike Stump1eb44332009-09-09 15:08:12 +00004092
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004093 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004094 assert(D.isPastIdentifier() &&
4095 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004096
Richard Smith6ee326a2012-04-10 01:32:12 +00004097 // Don't parse attributes unless we have parsed an unparenthesized name.
4098 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004099 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004100
Reid Spencer5f016e22007-07-11 17:01:13 +00004101 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004102 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004103 // Enter function-declaration scope, limiting any declarators to the
4104 // function prototype scope, including parameter declarators.
4105 ParseScope PrototypeScope(this,
4106 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004107 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4108 // In such a case, check if we actually have a function declarator; if it
4109 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00004110 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00004111 // When not in file scope, warn for ambiguous function declarators, just
4112 // in case the author intended it as a variable definition.
4113 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4114 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4115 break;
4116 }
John McCall0b7e6782011-03-24 11:26:52 +00004117 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004118 BalancedDelimiterTracker T(*this, tok::l_paren);
4119 T.consumeOpen();
4120 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004121 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004122 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004123 ParseBracketDeclarator(D);
4124 } else {
4125 break;
4126 }
4127 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004128}
Reid Spencer5f016e22007-07-11 17:01:13 +00004129
Chris Lattneref4715c2008-04-06 05:45:57 +00004130/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4131/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004132/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004133/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4134///
4135/// direct-declarator:
4136/// '(' declarator ')'
4137/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004138/// direct-declarator '(' parameter-type-list ')'
4139/// direct-declarator '(' identifier-list[opt] ')'
4140/// [GNU] direct-declarator '(' parameter-forward-declarations
4141/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004142///
4143void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004144 BalancedDelimiterTracker T(*this, tok::l_paren);
4145 T.consumeOpen();
4146
Chris Lattneref4715c2008-04-06 05:45:57 +00004147 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004148
Chris Lattner7399ee02008-10-20 02:05:46 +00004149 // Eat any attributes before we look at whether this is a grouping or function
4150 // declarator paren. If this is a grouping paren, the attribute applies to
4151 // the type being built up, for example:
4152 // int (__attribute__(()) *x)(long y)
4153 // If this ends up not being a grouping paren, the attribute applies to the
4154 // first argument, for example:
4155 // int (__attribute__(()) int x)
4156 // In either case, we need to eat any attributes to be able to determine what
4157 // sort of paren this is.
4158 //
John McCall0b7e6782011-03-24 11:26:52 +00004159 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004160 bool RequiresArg = false;
4161 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004162 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004163
Chris Lattner7399ee02008-10-20 02:05:46 +00004164 // We require that the argument list (if this is a non-grouping paren) be
4165 // present even if the attribute list was empty.
4166 RequiresArg = true;
4167 }
Steve Naroff239f0732008-12-25 14:16:32 +00004168 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004169 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004170 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004171 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004172 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004173 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004174 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004175 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004176 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004177 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004178
Chris Lattneref4715c2008-04-06 05:45:57 +00004179 // If we haven't past the identifier yet (or where the identifier would be
4180 // stored, if this is an abstract declarator), then this is probably just
4181 // grouping parens. However, if this could be an abstract-declarator, then
4182 // this could also be the start of function arguments (consider 'void()').
4183 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004184
Chris Lattneref4715c2008-04-06 05:45:57 +00004185 if (!D.mayOmitIdentifier()) {
4186 // If this can't be an abstract-declarator, this *must* be a grouping
4187 // paren, because we haven't seen the identifier yet.
4188 isGrouping = true;
4189 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004190 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4191 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004192 isDeclarationSpecifier() || // 'int(int)' is a function.
4193 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004194 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4195 // considered to be a type, not a K&R identifier-list.
4196 isGrouping = false;
4197 } else {
4198 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4199 isGrouping = true;
4200 }
Mike Stump1eb44332009-09-09 15:08:12 +00004201
Chris Lattneref4715c2008-04-06 05:45:57 +00004202 // If this is a grouping paren, handle:
4203 // direct-declarator: '(' declarator ')'
4204 // direct-declarator: '(' attributes declarator ')'
4205 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004206 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4207 D.setEllipsisLoc(SourceLocation());
4208
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004209 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004210 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004211 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004212 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004213 T.consumeClose();
4214 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4215 T.getCloseLocation()),
4216 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004217
4218 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004219
4220 // An ellipsis cannot be placed outside parentheses.
4221 if (EllipsisLoc.isValid())
4222 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4223
Chris Lattneref4715c2008-04-06 05:45:57 +00004224 return;
4225 }
Mike Stump1eb44332009-09-09 15:08:12 +00004226
Chris Lattneref4715c2008-04-06 05:45:57 +00004227 // Okay, if this wasn't a grouping paren, it must be the start of a function
4228 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004229 // identifier (and remember where it would have been), then call into
4230 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004231 D.SetIdentifier(0, Tok.getLocation());
4232
David Blaikie42d6d0c2011-12-04 05:04:18 +00004233 // Enter function-declaration scope, limiting any declarators to the
4234 // function prototype scope, including parameter declarators.
4235 ParseScope PrototypeScope(this,
4236 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004237 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004238 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004239}
4240
4241/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4242/// declarator D up to a paren, which indicates that we are parsing function
4243/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004244///
Richard Smith6ee326a2012-04-10 01:32:12 +00004245/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4246/// immediately after the open paren - they should be considered to be the
4247/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004248///
Richard Smith6ee326a2012-04-10 01:32:12 +00004249/// If RequiresArg is true, then the first argument of the function is required
4250/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004251///
Richard Smith6ee326a2012-04-10 01:32:12 +00004252/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4253/// (C++11) ref-qualifier[opt], exception-specification[opt],
4254/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4255///
4256/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004257/// dynamic-exception-specification
4258/// noexcept-specification
4259///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004260void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004261 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004262 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004263 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004264 assert(getCurScope()->isFunctionPrototypeScope() &&
4265 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004266 // lparen is already consumed!
4267 assert(D.isPastIdentifier() && "Should not call before identifier!");
4268
4269 // This should be true when the function has typed arguments.
4270 // Otherwise, it is treated as a K&R-style function.
4271 bool HasProto = false;
4272 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004273 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004274 // Remember where we see an ellipsis, if any.
4275 SourceLocation EllipsisLoc;
4276
4277 DeclSpec DS(AttrFactory);
4278 bool RefQualifierIsLValueRef = true;
4279 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004280 SourceLocation ConstQualifierLoc;
4281 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004282 ExceptionSpecificationType ESpecType = EST_None;
4283 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004284 SmallVector<ParsedType, 2> DynamicExceptions;
4285 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004286 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004287 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004288 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004289
James Molloy16f1f712012-02-29 10:24:19 +00004290 Actions.ActOnStartFunctionDeclarator();
4291
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004292 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004293 if (isFunctionDeclaratorIdentifierList()) {
4294 if (RequiresArg)
4295 Diag(Tok, diag::err_argument_required_after_attribute);
4296
4297 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4298
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004299 Tracker.consumeClose();
4300 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004301 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004302 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004303 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004304 else if (RequiresArg)
4305 Diag(Tok, diag::err_argument_required_after_attribute);
4306
David Blaikie4e4d0842012-03-11 07:00:24 +00004307 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004308
4309 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004310 Tracker.consumeClose();
4311 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004312
David Blaikie4e4d0842012-03-11 07:00:24 +00004313 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004314 // FIXME: Accept these components in any order, and produce fixits to
4315 // correct the order if the user gets it wrong. Ideally we should deal
4316 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004317
4318 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004319 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4320 if (!DS.getSourceRange().getEnd().isInvalid()) {
4321 EndLoc = DS.getSourceRange().getEnd();
4322 ConstQualifierLoc = DS.getConstSpecLoc();
4323 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4324 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004325
4326 // Parse ref-qualifier[opt].
4327 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004328 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004329 diag::warn_cxx98_compat_ref_qualifier :
4330 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004331
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004332 RefQualifierIsLValueRef = Tok.is(tok::amp);
4333 RefQualifierLoc = ConsumeToken();
4334 EndLoc = RefQualifierLoc;
4335 }
4336
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004337 // C++11 [expr.prim.general]p3:
4338 // If a declaration declares a member function or member function
4339 // template of a class X, the expression this is a prvalue of type
4340 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
4341 // and the end of the function-definition, member-declarator, or
4342 // declarator.
4343 bool IsCXX11MemberFunction =
4344 getLangOpts().CPlusPlus0x &&
4345 (D.getContext() == Declarator::MemberContext ||
4346 (D.getContext() == Declarator::FileContext &&
4347 D.getCXXScopeSpec().isValid() &&
4348 Actions.CurContext->isRecord()));
4349 Sema::CXXThisScopeRAII ThisScope(Actions,
4350 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4351 DS.getTypeQualifiers(),
4352 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004353
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004354 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004355 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004356 DynamicExceptions,
4357 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004358 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004359 if (ESpecType != EST_None)
4360 EndLoc = ESpecRange.getEnd();
4361
Richard Smith6ee326a2012-04-10 01:32:12 +00004362 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4363 // after the exception-specification.
4364 MaybeParseCXX0XAttributes(FnAttrs);
4365
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004366 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004367 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004368 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004369 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004370 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004371 if (Range.getEnd().isValid())
4372 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004373 }
4374 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004375 }
4376
4377 // Remember that we parsed a function type, and remember the attributes.
4378 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4379 /*isVariadic=*/EllipsisLoc.isValid(),
4380 EllipsisLoc,
4381 ParamInfo.data(), ParamInfo.size(),
4382 DS.getTypeQualifiers(),
4383 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004384 RefQualifierLoc, ConstQualifierLoc,
4385 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004386 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004387 ESpecType, ESpecRange.getBegin(),
4388 DynamicExceptions.data(),
4389 DynamicExceptionRanges.data(),
4390 DynamicExceptions.size(),
4391 NoexceptExpr.isUsable() ?
4392 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004393 Tracker.getOpenLocation(),
4394 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004395 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004396 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004397
4398 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004399}
4400
4401/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4402/// identifier list form for a K&R-style function: void foo(a,b,c)
4403///
4404/// Note that identifier-lists are only allowed for normal declarators, not for
4405/// abstract-declarators.
4406bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004407 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004408 && Tok.is(tok::identifier)
4409 && !TryAltiVecVectorToken()
4410 // K&R identifier lists can't have typedefs as identifiers, per C99
4411 // 6.7.5.3p11.
4412 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4413 // Identifier lists follow a really simple grammar: the identifiers can
4414 // be followed *only* by a ", identifier" or ")". However, K&R
4415 // identifier lists are really rare in the brave new modern world, and
4416 // it is very common for someone to typo a type in a non-K&R style
4417 // list. If we are presented with something like: "void foo(intptr x,
4418 // float y)", we don't want to start parsing the function declarator as
4419 // though it is a K&R style declarator just because intptr is an
4420 // invalid type.
4421 //
4422 // To handle this, we check to see if the token after the first
4423 // identifier is a "," or ")". Only then do we parse it as an
4424 // identifier list.
4425 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4426}
4427
4428/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4429/// we found a K&R-style identifier list instead of a typed parameter list.
4430///
4431/// After returning, ParamInfo will hold the parsed parameters.
4432///
4433/// identifier-list: [C99 6.7.5]
4434/// identifier
4435/// identifier-list ',' identifier
4436///
4437void Parser::ParseFunctionDeclaratorIdentifierList(
4438 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004439 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004440 // If there was no identifier specified for the declarator, either we are in
4441 // an abstract-declarator, or we are in a parameter declarator which was found
4442 // to be abstract. In abstract-declarators, identifier lists are not valid:
4443 // diagnose this.
4444 if (!D.getIdentifier())
4445 Diag(Tok, diag::ext_ident_list_in_param);
4446
4447 // Maintain an efficient lookup of params we have seen so far.
4448 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4449
4450 while (1) {
4451 // If this isn't an identifier, report the error and skip until ')'.
4452 if (Tok.isNot(tok::identifier)) {
4453 Diag(Tok, diag::err_expected_ident);
4454 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4455 // Forget we parsed anything.
4456 ParamInfo.clear();
4457 return;
4458 }
4459
4460 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4461
4462 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4463 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4464 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4465
4466 // Verify that the argument identifier has not already been mentioned.
4467 if (!ParamsSoFar.insert(ParmII)) {
4468 Diag(Tok, diag::err_param_redefinition) << ParmII;
4469 } else {
4470 // Remember this identifier in ParamInfo.
4471 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4472 Tok.getLocation(),
4473 0));
4474 }
4475
4476 // Eat the identifier.
4477 ConsumeToken();
4478
4479 // The list continues if we see a comma.
4480 if (Tok.isNot(tok::comma))
4481 break;
4482 ConsumeToken();
4483 }
4484}
4485
4486/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4487/// after the opening parenthesis. This function will not parse a K&R-style
4488/// identifier list.
4489///
Richard Smith6ce48a72012-04-11 04:01:28 +00004490/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4491/// caller parsed those arguments immediately after the open paren - they should
4492/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004493///
4494/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4495/// be the location of the ellipsis, if any was parsed.
4496///
Reid Spencer5f016e22007-07-11 17:01:13 +00004497/// parameter-type-list: [C99 6.7.5]
4498/// parameter-list
4499/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004500/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004501///
4502/// parameter-list: [C99 6.7.5]
4503/// parameter-declaration
4504/// parameter-list ',' parameter-declaration
4505///
4506/// parameter-declaration: [C99 6.7.5]
4507/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004508/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004509/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004510/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004511/// declaration-specifiers abstract-declarator[opt]
4512/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004513/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004514/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004515/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004516///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004517void Parser::ParseParameterDeclarationClause(
4518 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004519 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004520 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004521 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004522
Chris Lattnerf97409f2008-04-06 06:57:35 +00004523 while (1) {
4524 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004525 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4526 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004527 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004528 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004529 }
Mike Stump1eb44332009-09-09 15:08:12 +00004530
Chris Lattnerf97409f2008-04-06 06:57:35 +00004531 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004532 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004533 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004534
Richard Smith6ce48a72012-04-11 04:01:28 +00004535 // Parse any C++11 attributes.
4536 MaybeParseCXX0XAttributes(DS.getAttributes());
4537
John McCall7f040a92010-12-24 02:08:15 +00004538 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004539 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004540 ParseMicrosoftAttributes(DS.getAttributes());
4541
4542 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004543
4544 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004545 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004546 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004547 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4548 // too much hassle.
4549 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004550
Chris Lattnere64c5492009-02-27 18:38:20 +00004551 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004552
Chris Lattnerf97409f2008-04-06 06:57:35 +00004553 // Parse the declarator. This is "PrototypeContext", because we must
4554 // accept either 'declarator' or 'abstract-declarator' here.
4555 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4556 ParseDeclarator(ParmDecl);
4557
4558 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004559 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004560
Chris Lattnerf97409f2008-04-06 06:57:35 +00004561 // Remember this parsed parameter in ParamInfo.
4562 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004563
Douglas Gregor72b505b2008-12-16 21:30:33 +00004564 // DefArgToks is used when the parsing of default arguments needs
4565 // to be delayed.
4566 CachedTokens *DefArgToks = 0;
4567
Chris Lattnerf97409f2008-04-06 06:57:35 +00004568 // If no parameter was specified, verify that *something* was specified,
4569 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004570 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4571 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004572 // Completely missing, emit error.
4573 Diag(DSStart, diag::err_missing_param);
4574 } else {
4575 // Otherwise, we have something. Add it and let semantic analysis try
4576 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004577
Chris Lattnerf97409f2008-04-06 06:57:35 +00004578 // Inform the actions module about the parameter declarator, so it gets
4579 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004580 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004581
4582 // Parse the default argument, if any. We parse the default
4583 // arguments in all dialects; the semantic analysis in
4584 // ActOnParamDefaultArgument will reject the default argument in
4585 // C.
4586 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004587 SourceLocation EqualLoc = Tok.getLocation();
4588
Chris Lattner04421082008-04-08 04:40:51 +00004589 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004590 if (D.getContext() == Declarator::MemberContext) {
4591 // If we're inside a class definition, cache the tokens
4592 // corresponding to the default argument. We'll actually parse
4593 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004594 // FIXME: Can we use a smart pointer for Toks?
4595 DefArgToks = new CachedTokens;
4596
Mike Stump1eb44332009-09-09 15:08:12 +00004597 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004598 /*StopAtSemi=*/true,
4599 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004600 delete DefArgToks;
4601 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004602 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004603 } else {
4604 // Mark the end of the default argument so that we know when to
4605 // stop when we parse it later on.
4606 Token DefArgEnd;
4607 DefArgEnd.startToken();
4608 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4609 DefArgEnd.setLocation(Tok.getLocation());
4610 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004611 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004612 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004613 }
Chris Lattner04421082008-04-08 04:40:51 +00004614 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004615 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004616 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004617
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004618 // The argument isn't actually potentially evaluated unless it is
4619 // used.
4620 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004621 Sema::PotentiallyEvaluatedIfUsed,
4622 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004623
Sebastian Redl84407ba2012-03-14 15:54:00 +00004624 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004625 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4626 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004627 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004628 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004629 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004630 if (DefArgResult.isInvalid()) {
4631 Actions.ActOnParamDefaultArgumentError(Param);
4632 SkipUntil(tok::comma, tok::r_paren, true, true);
4633 } else {
4634 // Inform the actions module about the default argument
4635 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004636 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004637 }
Chris Lattner04421082008-04-08 04:40:51 +00004638 }
4639 }
Mike Stump1eb44332009-09-09 15:08:12 +00004640
4641 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4642 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004643 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004644 }
4645
4646 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004647 if (Tok.isNot(tok::comma)) {
4648 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004649 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4650
David Blaikie4e4d0842012-03-11 07:00:24 +00004651 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004652 // We have ellipsis without a preceding ',', which is ill-formed
4653 // in C. Complain and provide the fix.
4654 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004655 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004656 }
4657 }
4658
4659 break;
4660 }
Mike Stump1eb44332009-09-09 15:08:12 +00004661
Chris Lattnerf97409f2008-04-06 06:57:35 +00004662 // Consume the comma.
4663 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004664 }
Mike Stump1eb44332009-09-09 15:08:12 +00004665
Chris Lattner66d28652008-04-06 06:34:08 +00004666}
Chris Lattneref4715c2008-04-06 05:45:57 +00004667
Reid Spencer5f016e22007-07-11 17:01:13 +00004668/// [C90] direct-declarator '[' constant-expression[opt] ']'
4669/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4670/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4671/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4672/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004673/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4674/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004675void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004676 if (CheckProhibitedCXX11Attribute())
4677 return;
4678
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004679 BalancedDelimiterTracker T(*this, tok::l_square);
4680 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004681
Chris Lattner378c7e42008-12-18 07:27:21 +00004682 // C array syntax has many features, but by-far the most common is [] and [4].
4683 // This code does a fast path to handle some of the most obvious cases.
4684 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004685 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004686 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004687 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004688
Chris Lattner378c7e42008-12-18 07:27:21 +00004689 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004690 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004691 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004692 T.getOpenLocation(),
4693 T.getCloseLocation()),
4694 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004695 return;
4696 } else if (Tok.getKind() == tok::numeric_constant &&
4697 GetLookAheadToken(1).is(tok::r_square)) {
4698 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004699 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004700 ConsumeToken();
4701
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004702 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004703 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004704 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004705
Chris Lattner378c7e42008-12-18 07:27:21 +00004706 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004707 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004708 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004709 T.getOpenLocation(),
4710 T.getCloseLocation()),
4711 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004712 return;
4713 }
Mike Stump1eb44332009-09-09 15:08:12 +00004714
Reid Spencer5f016e22007-07-11 17:01:13 +00004715 // If valid, this location is the position where we read the 'static' keyword.
4716 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004717 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004718 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004719
Reid Spencer5f016e22007-07-11 17:01:13 +00004720 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004721 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004722 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004723 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004724
Reid Spencer5f016e22007-07-11 17:01:13 +00004725 // If we haven't already read 'static', check to see if there is one after the
4726 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004727 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004728 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004729
Reid Spencer5f016e22007-07-11 17:01:13 +00004730 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4731 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004732 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004733
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004734 // Handle the case where we have '[*]' as the array size. However, a leading
4735 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4736 // the the token after the star is a ']'. Since stars in arrays are
4737 // infrequent, use of lookahead is not costly here.
4738 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004739 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004740
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004741 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004742 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004743 StaticLoc = SourceLocation(); // Drop the static.
4744 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004745 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004746 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004747 // Note, in C89, this production uses the constant-expr production instead
4748 // of assignment-expr. The only difference is that assignment-expr allows
4749 // things like '=' and '*='. Sema rejects these in C89 mode because they
4750 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004751
Douglas Gregore0762c92009-06-19 23:52:42 +00004752 // Parse the constant-expression or assignment-expression now (depending
4753 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004754 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004755 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004756 } else {
4757 EnterExpressionEvaluationContext Unevaluated(Actions,
4758 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004759 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004760 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004761 }
Mike Stump1eb44332009-09-09 15:08:12 +00004762
Reid Spencer5f016e22007-07-11 17:01:13 +00004763 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004764 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004765 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004766 // If the expression was invalid, skip it.
4767 SkipUntil(tok::r_square);
4768 return;
4769 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004770
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004771 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004772
John McCall0b7e6782011-03-24 11:26:52 +00004773 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004774 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004775
Chris Lattner378c7e42008-12-18 07:27:21 +00004776 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004777 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004778 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004779 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004780 T.getOpenLocation(),
4781 T.getCloseLocation()),
4782 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004783}
4784
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004785/// [GNU] typeof-specifier:
4786/// typeof ( expressions )
4787/// typeof ( type-name )
4788/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004789///
4790void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004791 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004792 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004793 SourceLocation StartLoc = ConsumeToken();
4794
John McCallcfb708c2010-01-13 20:03:27 +00004795 const bool hasParens = Tok.is(tok::l_paren);
4796
Eli Friedman71b8fb52012-01-21 01:01:51 +00004797 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4798
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004799 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004800 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004801 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004802 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4803 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004804 if (hasParens)
4805 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004806
4807 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004808 // FIXME: Not accurate, the range gets one token more than it should.
4809 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004810 else
4811 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004812
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004813 if (isCastExpr) {
4814 if (!CastTy) {
4815 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004816 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004817 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004818
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004819 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004820 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004821 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4822 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004823 DiagID, CastTy))
4824 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004825 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004826 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004827
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004828 // If we get here, the operand to the typeof was an expresion.
4829 if (Operand.isInvalid()) {
4830 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004831 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004832 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004833
Eli Friedman71b8fb52012-01-21 01:01:51 +00004834 // We might need to transform the operand if it is potentially evaluated.
4835 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4836 if (Operand.isInvalid()) {
4837 DS.SetTypeSpecError();
4838 return;
4839 }
4840
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004841 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004842 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004843 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4844 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004845 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004846 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004847}
Chris Lattner1b492422010-02-28 18:33:55 +00004848
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004849/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004850/// _Atomic ( type-name )
4851///
4852void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4853 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4854
4855 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004856 BalancedDelimiterTracker T(*this, tok::l_paren);
4857 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004858 SkipUntil(tok::r_paren);
4859 return;
4860 }
4861
4862 TypeResult Result = ParseTypeName();
4863 if (Result.isInvalid()) {
4864 SkipUntil(tok::r_paren);
4865 return;
4866 }
4867
4868 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004869 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004870
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004871 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004872 return;
4873
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004874 DS.setTypeofParensRange(T.getRange());
4875 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004876
4877 const char *PrevSpec = 0;
4878 unsigned DiagID;
4879 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4880 DiagID, Result.release()))
4881 Diag(StartLoc, DiagID) << PrevSpec;
4882}
4883
Chris Lattner1b492422010-02-28 18:33:55 +00004884
4885/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4886/// from TryAltiVecVectorToken.
4887bool Parser::TryAltiVecVectorTokenOutOfLine() {
4888 Token Next = NextToken();
4889 switch (Next.getKind()) {
4890 default: return false;
4891 case tok::kw_short:
4892 case tok::kw_long:
4893 case tok::kw_signed:
4894 case tok::kw_unsigned:
4895 case tok::kw_void:
4896 case tok::kw_char:
4897 case tok::kw_int:
4898 case tok::kw_float:
4899 case tok::kw_double:
4900 case tok::kw_bool:
4901 case tok::kw___pixel:
4902 Tok.setKind(tok::kw___vector);
4903 return true;
4904 case tok::identifier:
4905 if (Next.getIdentifierInfo() == Ident_pixel) {
4906 Tok.setKind(tok::kw___vector);
4907 return true;
4908 }
4909 return false;
4910 }
4911}
4912
4913bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4914 const char *&PrevSpec, unsigned &DiagID,
4915 bool &isInvalid) {
4916 if (Tok.getIdentifierInfo() == Ident_vector) {
4917 Token Next = NextToken();
4918 switch (Next.getKind()) {
4919 case tok::kw_short:
4920 case tok::kw_long:
4921 case tok::kw_signed:
4922 case tok::kw_unsigned:
4923 case tok::kw_void:
4924 case tok::kw_char:
4925 case tok::kw_int:
4926 case tok::kw_float:
4927 case tok::kw_double:
4928 case tok::kw_bool:
4929 case tok::kw___pixel:
4930 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4931 return true;
4932 case tok::identifier:
4933 if (Next.getIdentifierInfo() == Ident_pixel) {
4934 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4935 return true;
4936 }
4937 break;
4938 default:
4939 break;
4940 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004941 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004942 DS.isTypeAltiVecVector()) {
4943 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4944 return true;
4945 }
4946 return false;
4947}