blob: 3eeef7bff34ced5e77ce6042702444358ba0c10b [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;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001754 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001755 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();
1766
1767 // There may be other declaration specifiers after this.
1768 return true;
1769 }
1770
1771 // Fall through; the action had no suggestion for us.
1772 } else {
1773 // The action did not emit a diagnostic, so emit one now.
1774 SourceRange R;
1775 if (SS) R = SS->getRange();
1776 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1777 }
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Douglas Gregora786fdb2009-10-13 23:27:22 +00001779 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001780 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001781 DS.SetRangeEnd(Tok.getLocation());
1782 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Chris Lattnere40c2952009-04-14 21:34:55 +00001784 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1785 // avoid rippling error messages on subsequent uses of the same type,
1786 // could be useful if #include was forgotten.
1787 return false;
1788}
1789
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001790/// \brief Determine the declaration specifier context from the declarator
1791/// context.
1792///
1793/// \param Context the declarator context, which is one of the
1794/// Declarator::TheContext enumerator values.
1795Parser::DeclSpecContext
1796Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1797 if (Context == Declarator::MemberContext)
1798 return DSC_class;
1799 if (Context == Declarator::FileContext)
1800 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001801 if (Context == Declarator::TrailingReturnContext)
1802 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001803 return DSC_normal;
1804}
1805
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001806/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1807///
1808/// FIXME: Simply returns an alignof() expression if the argument is a
1809/// type. Ideally, the type should be propagated directly into Sema.
1810///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001811/// [C11] type-id
1812/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001813/// [C++0x] type-id ...[opt]
1814/// [C++0x] assignment-expression ...[opt]
1815ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1816 SourceLocation &EllipsisLoc) {
1817 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001818 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001819 SourceLocation TypeLoc = Tok.getLocation();
1820 ParsedType Ty = ParseTypeName().get();
1821 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001822 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1823 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001824 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001825 ER = ParseConstantExpression();
1826
David Blaikie4e4d0842012-03-11 07:00:24 +00001827 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001828 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001829
1830 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001831}
1832
1833/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1834/// attribute to Attrs.
1835///
1836/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001837/// [C11] '_Alignas' '(' type-id ')'
1838/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001839/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1840/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001841void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1842 SourceLocation *endLoc) {
1843 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1844 "Not an alignment-specifier!");
1845
1846 SourceLocation KWLoc = Tok.getLocation();
1847 ConsumeToken();
1848
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001849 BalancedDelimiterTracker T(*this, tok::l_paren);
1850 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001851 return;
1852
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001853 SourceLocation EllipsisLoc;
1854 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001855 if (ArgExpr.isInvalid()) {
1856 SkipUntil(tok::r_paren);
1857 return;
1858 }
1859
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001860 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001861 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001862 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001863
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001864 // FIXME: Handle pack-expansions here.
1865 if (EllipsisLoc.isValid()) {
1866 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1867 return;
1868 }
1869
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001870 ExprVector ArgExprs(Actions);
1871 ArgExprs.push_back(ArgExpr.release());
1872 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001873 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001874}
1875
Reid Spencer5f016e22007-07-11 17:01:13 +00001876/// ParseDeclarationSpecifiers
1877/// declaration-specifiers: [C99 6.7]
1878/// storage-class-specifier declaration-specifiers[opt]
1879/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001880/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001881/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001882/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001883/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001884///
1885/// storage-class-specifier: [C99 6.7.1]
1886/// 'typedef'
1887/// 'extern'
1888/// 'static'
1889/// 'auto'
1890/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001891/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001892/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001893/// function-specifier: [C99 6.7.4]
1894/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001895/// [C++] 'virtual'
1896/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001897/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001898/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001899/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001900
Reid Spencer5f016e22007-07-11 17:01:13 +00001901///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001902void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001903 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001904 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001905 DeclSpecContext DSContext,
1906 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001907 if (DS.getSourceRange().isInvalid()) {
1908 DS.SetRangeStart(Tok.getLocation());
1909 DS.SetRangeEnd(Tok.getLocation());
1910 }
1911
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001912 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001913 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001914 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001915 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001916 unsigned DiagID = 0;
1917
Reid Spencer5f016e22007-07-11 17:01:13 +00001918 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001919
Reid Spencer5f016e22007-07-11 17:01:13 +00001920 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001921 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001922 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001923 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1924 MaybeParseCXX0XAttributes(DS.getAttributes());
1925
Reid Spencer5f016e22007-07-11 17:01:13 +00001926 // If this is not a declaration specifier token, we're done reading decl
1927 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001928 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001929 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001931 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001932 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001933 if (DS.hasTypeSpecifier()) {
1934 bool AllowNonIdentifiers
1935 = (getCurScope()->getFlags() & (Scope::ControlScope |
1936 Scope::BlockScope |
1937 Scope::TemplateParamScope |
1938 Scope::FunctionPrototypeScope |
1939 Scope::AtCatchScope)) == 0;
1940 bool AllowNestedNameSpecifiers
1941 = DSContext == DSC_top_level ||
1942 (DSContext == DSC_class && DS.isFriendSpecified());
1943
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001944 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1945 AllowNonIdentifiers,
1946 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001947 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001948 }
1949
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001950 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1951 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1952 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001953 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1954 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001955 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001956 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001957 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001958 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001959
1960 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001961 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001962 }
1963
Chris Lattner5e02c472009-01-05 00:07:25 +00001964 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001965 // C++ scope specifier. Annotate and loop, or bail out on error.
1966 if (TryAnnotateCXXScopeToken(true)) {
1967 if (!DS.hasTypeSpecifier())
1968 DS.SetTypeSpecError();
1969 goto DoneWithDeclSpec;
1970 }
John McCall2e0a7152010-03-01 18:20:46 +00001971 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1972 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001973 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001974
1975 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00001976 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001977 goto DoneWithDeclSpec;
1978
John McCallaa87d332009-12-12 11:40:51 +00001979 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001980 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1981 Tok.getAnnotationRange(),
1982 SS);
John McCallaa87d332009-12-12 11:40:51 +00001983
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001984 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001985 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001986 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001987 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001988 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001989 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001990
1991 // C++ [class.qual]p2:
1992 // In a lookup in which the constructor is an acceptable lookup
1993 // result and the nested-name-specifier nominates a class C:
1994 //
1995 // - if the name specified after the
1996 // nested-name-specifier, when looked up in C, is the
1997 // injected-class-name of C (Clause 9), or
1998 //
1999 // - if the name specified after the nested-name-specifier
2000 // is the same as the identifier or the
2001 // simple-template-id's template-name in the last
2002 // component of the nested-name-specifier,
2003 //
2004 // the name is instead considered to name the constructor of
2005 // class C.
2006 //
2007 // Thus, if the template-name is actually the constructor
2008 // name, then the code is ill-formed; this interpretation is
2009 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002010 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002011 if ((DSContext == DSC_top_level ||
2012 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2013 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002014 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002015 if (isConstructorDeclarator()) {
2016 // The user meant this to be an out-of-line constructor
2017 // definition, but template arguments are not allowed
2018 // there. Just allow this as a constructor; we'll
2019 // complain about it later.
2020 goto DoneWithDeclSpec;
2021 }
2022
2023 // The user meant this to name a type, but it actually names
2024 // a constructor with some extraneous template
2025 // arguments. Complain, then parse it as a type as the user
2026 // intended.
2027 Diag(TemplateId->TemplateNameLoc,
2028 diag::err_out_of_line_template_id_names_constructor)
2029 << TemplateId->Name;
2030 }
2031
John McCallaa87d332009-12-12 11:40:51 +00002032 DS.getTypeSpecScope() = SS;
2033 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002034 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002035 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002036 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002037 continue;
2038 }
2039
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002040 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002041 DS.getTypeSpecScope() = SS;
2042 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002043 if (Tok.getAnnotationValue()) {
2044 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002045 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2046 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002047 PrevSpec, DiagID, T);
2048 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002049 else
2050 DS.SetTypeSpecError();
2051 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2052 ConsumeToken(); // The typename
2053 }
2054
Douglas Gregor9135c722009-03-25 15:40:00 +00002055 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002056 goto DoneWithDeclSpec;
2057
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002058 // If we're in a context where the identifier could be a class name,
2059 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002060 if ((DSContext == DSC_top_level ||
2061 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002062 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002063 &SS)) {
2064 if (isConstructorDeclarator())
2065 goto DoneWithDeclSpec;
2066
2067 // As noted in C++ [class.qual]p2 (cited above), when the name
2068 // of the class is qualified in a context where it could name
2069 // a constructor, its a constructor name. However, we've
2070 // looked at the declarator, and the user probably meant this
2071 // to be a type. Complain that it isn't supposed to be treated
2072 // as a type, then proceed to parse it as a type.
2073 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2074 << Next.getIdentifierInfo();
2075 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002076
John McCallb3d87482010-08-24 05:47:05 +00002077 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2078 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002079 getCurScope(), &SS,
2080 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002081 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002082 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002083
Chris Lattnerf4382f52009-04-14 22:17:06 +00002084 // If the referenced identifier is not a type, then this declspec is
2085 // erroneous: We already checked about that it has no type specifier, and
2086 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002087 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002088 if (TypeRep == 0) {
2089 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002090 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002091 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002092 }
Mike Stump1eb44332009-09-09 15:08:12 +00002093
John McCallaa87d332009-12-12 11:40:51 +00002094 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002095 ConsumeToken(); // The C++ scope.
2096
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002097 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002098 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002099 if (isInvalid)
2100 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002101
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002102 DS.SetRangeEnd(Tok.getLocation());
2103 ConsumeToken(); // The typename.
2104
2105 continue;
2106 }
Mike Stump1eb44332009-09-09 15:08:12 +00002107
Chris Lattner80d0c892009-01-21 19:48:37 +00002108 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002109 if (Tok.getAnnotationValue()) {
2110 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002111 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002112 DiagID, T);
2113 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002114 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00002115
2116 if (isInvalid)
2117 break;
2118
Chris Lattner80d0c892009-01-21 19:48:37 +00002119 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2120 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002121
Chris Lattner80d0c892009-01-21 19:48:37 +00002122 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2123 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002124 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002125 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002126 ParseObjCProtocolQualifiers(DS);
2127
Chris Lattner80d0c892009-01-21 19:48:37 +00002128 continue;
2129 }
Mike Stump1eb44332009-09-09 15:08:12 +00002130
Douglas Gregorbfad9152011-04-28 15:48:45 +00002131 case tok::kw___is_signed:
2132 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2133 // typically treats it as a trait. If we see __is_signed as it appears
2134 // in libstdc++, e.g.,
2135 //
2136 // static const bool __is_signed;
2137 //
2138 // then treat __is_signed as an identifier rather than as a keyword.
2139 if (DS.getTypeSpecType() == TST_bool &&
2140 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2141 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2142 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2143 Tok.setKind(tok::identifier);
2144 }
2145
2146 // We're done with the declaration-specifiers.
2147 goto DoneWithDeclSpec;
2148
Chris Lattner3bd934a2008-07-26 01:18:38 +00002149 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002150 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002151 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002152 // In C++, check to see if this is a scope specifier like foo::bar::, if
2153 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002154 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002155 if (TryAnnotateCXXScopeToken(true)) {
2156 if (!DS.hasTypeSpecifier())
2157 DS.SetTypeSpecError();
2158 goto DoneWithDeclSpec;
2159 }
2160 if (!Tok.is(tok::identifier))
2161 continue;
2162 }
Mike Stump1eb44332009-09-09 15:08:12 +00002163
Chris Lattner3bd934a2008-07-26 01:18:38 +00002164 // This identifier can only be a typedef name if we haven't already seen
2165 // a type-specifier. Without this check we misparse:
2166 // typedef int X; struct Y { short X; }; as 'short int'.
2167 if (DS.hasTypeSpecifier())
2168 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002169
John Thompson82287d12010-02-05 00:12:22 +00002170 // Check for need to substitute AltiVec keyword tokens.
2171 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2172 break;
2173
Richard Smithf63eee72012-05-09 18:56:43 +00002174 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2175 // allow the use of a typedef name as a type specifier.
2176 if (DS.isTypeAltiVecVector())
2177 goto DoneWithDeclSpec;
2178
John McCallb3d87482010-08-24 05:47:05 +00002179 ParsedType TypeRep =
2180 Actions.getTypeName(*Tok.getIdentifierInfo(),
2181 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002182
Chris Lattnerc199ab32009-04-12 20:42:31 +00002183 // If this is not a typedef name, don't parse it as part of the declspec,
2184 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002185 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002186 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002187 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002188 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002189
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002190 // If we're in a context where the identifier could be a class name,
2191 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002192 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002193 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002194 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002195 goto DoneWithDeclSpec;
2196
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002197 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002198 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002199 if (isInvalid)
2200 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002201
Chris Lattner3bd934a2008-07-26 01:18:38 +00002202 DS.SetRangeEnd(Tok.getLocation());
2203 ConsumeToken(); // The identifier
2204
2205 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2206 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002207 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002208 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002209 ParseObjCProtocolQualifiers(DS);
2210
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002211 // Need to support trailing type qualifiers (e.g. "id<p> const").
2212 // If a type specifier follows, it will be diagnosed elsewhere.
2213 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002214 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002215
2216 // type-name
2217 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002218 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002219 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002220 // This template-id does not refer to a type name, so we're
2221 // done with the type-specifiers.
2222 goto DoneWithDeclSpec;
2223 }
2224
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002225 // If we're in a context where the template-id could be a
2226 // constructor name or specialization, check whether this is a
2227 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002228 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002229 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002230 isConstructorDeclarator())
2231 goto DoneWithDeclSpec;
2232
Douglas Gregor39a8de12009-02-25 19:37:18 +00002233 // Turn the template-id annotation token into a type annotation
2234 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002235 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002236 continue;
2237 }
2238
Reid Spencer5f016e22007-07-11 17:01:13 +00002239 // GNU attributes support.
2240 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002241 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002242 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002243
2244 // Microsoft declspec support.
2245 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002246 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002247 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002248
Steve Naroff239f0732008-12-25 14:16:32 +00002249 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002250 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002251 // FIXME: Add handling here!
2252 break;
2253
2254 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002255 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002256 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002257 case tok::kw___cdecl:
2258 case tok::kw___stdcall:
2259 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002260 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002261 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002262 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002263 continue;
2264
Dawn Perchik52fc3142010-09-03 01:29:35 +00002265 // Borland single token adornments.
2266 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002267 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002268 continue;
2269
Peter Collingbournef315fa82011-02-14 01:42:53 +00002270 // OpenCL single token adornments.
2271 case tok::kw___kernel:
2272 ParseOpenCLAttributes(DS.getAttributes());
2273 continue;
2274
Reid Spencer5f016e22007-07-11 17:01:13 +00002275 // storage-class-specifier
2276 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002277 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2278 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002279 break;
2280 case tok::kw_extern:
2281 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002282 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002283 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2284 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002285 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002286 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002287 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2288 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002289 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002290 case tok::kw_static:
2291 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002292 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002293 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2294 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002295 break;
2296 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002297 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002298 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002299 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2300 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002301 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002302 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002303 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002304 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002305 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2306 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002307 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002308 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2309 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002310 break;
2311 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002312 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2313 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002314 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002315 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002316 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2317 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002318 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002319 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002320 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002321 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002322
Reid Spencer5f016e22007-07-11 17:01:13 +00002323 // function-specifier
2324 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002325 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002326 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002327 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002328 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002329 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002330 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002331 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002332 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002333
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002334 // alignment-specifier
2335 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002336 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002337 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002338 ParseAlignmentSpecifier(DS.getAttributes());
2339 continue;
2340
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002341 // friend
2342 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002343 if (DSContext == DSC_class)
2344 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2345 else {
2346 PrevSpec = ""; // not actually used by the diagnostic
2347 DiagID = diag::err_friend_invalid_in_context;
2348 isInvalid = true;
2349 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002350 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002351
Douglas Gregor8d267c52011-09-09 02:06:17 +00002352 // Modules
2353 case tok::kw___module_private__:
2354 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2355 break;
2356
Sebastian Redl2ac67232009-11-05 15:47:02 +00002357 // constexpr
2358 case tok::kw_constexpr:
2359 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2360 break;
2361
Chris Lattner80d0c892009-01-21 19:48:37 +00002362 // type-specifier
2363 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002364 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2365 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002366 break;
2367 case tok::kw_long:
2368 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002369 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2370 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002371 else
John McCallfec54012009-08-03 20:12:06 +00002372 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2373 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002374 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002375 case tok::kw___int64:
2376 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2377 DiagID);
2378 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002379 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002380 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2381 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002382 break;
2383 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002384 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2385 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002386 break;
2387 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002388 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2389 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002390 break;
2391 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002392 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2393 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002394 break;
2395 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002396 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2397 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002398 break;
2399 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002400 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2401 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002402 break;
2403 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002404 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2405 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002406 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002407 case tok::kw___int128:
2408 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2409 DiagID);
2410 break;
2411 case tok::kw_half:
2412 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2413 DiagID);
2414 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002415 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002416 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2417 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002418 break;
2419 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002420 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2421 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002422 break;
2423 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002424 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2425 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002426 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002427 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002428 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2429 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002430 break;
2431 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002432 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2433 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002434 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002435 case tok::kw_bool:
2436 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002437 if (Tok.is(tok::kw_bool) &&
2438 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2439 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2440 PrevSpec = ""; // Not used by the diagnostic.
2441 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002442 // For better error recovery.
2443 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002444 isInvalid = true;
2445 } else {
2446 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2447 DiagID);
2448 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002449 break;
2450 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002451 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2452 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002453 break;
2454 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002455 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2456 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002457 break;
2458 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002459 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2460 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002461 break;
John Thompson82287d12010-02-05 00:12:22 +00002462 case tok::kw___vector:
2463 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2464 break;
2465 case tok::kw___pixel:
2466 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2467 break;
John McCalla5fc4722011-04-09 22:50:59 +00002468 case tok::kw___unknown_anytype:
2469 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2470 PrevSpec, DiagID);
2471 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002472
2473 // class-specifier:
2474 case tok::kw_class:
2475 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002476 case tok::kw_union: {
2477 tok::TokenKind Kind = Tok.getKind();
2478 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002479 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2480 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002481 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002482 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002483
2484 // enum-specifier:
2485 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002486 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002487 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002488 continue;
2489
2490 // cv-qualifier:
2491 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002492 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002493 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002494 break;
2495 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002496 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002497 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002498 break;
2499 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002500 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002501 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002502 break;
2503
Douglas Gregord57959a2009-03-27 23:10:48 +00002504 // C++ typename-specifier:
2505 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002506 if (TryAnnotateTypeOrScopeToken()) {
2507 DS.SetTypeSpecError();
2508 goto DoneWithDeclSpec;
2509 }
2510 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002511 continue;
2512 break;
2513
Chris Lattner80d0c892009-01-21 19:48:37 +00002514 // GNU typeof support.
2515 case tok::kw_typeof:
2516 ParseTypeofSpecifier(DS);
2517 continue;
2518
David Blaikie42d6d0c2011-12-04 05:04:18 +00002519 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002520 ParseDecltypeSpecifier(DS);
2521 continue;
2522
Sean Huntdb5d44b2011-05-19 05:37:45 +00002523 case tok::kw___underlying_type:
2524 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002525 continue;
2526
2527 case tok::kw__Atomic:
2528 ParseAtomicSpecifier(DS);
2529 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002530
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002531 // OpenCL qualifiers:
2532 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002533 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002534 goto DoneWithDeclSpec;
2535 case tok::kw___private:
2536 case tok::kw___global:
2537 case tok::kw___local:
2538 case tok::kw___constant:
2539 case tok::kw___read_only:
2540 case tok::kw___write_only:
2541 case tok::kw___read_write:
2542 ParseOpenCLQualifiers(DS);
2543 break;
2544
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002545 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002546 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002547 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2548 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002549 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002550 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002551
Douglas Gregor46f936e2010-11-19 17:10:50 +00002552 if (!ParseObjCProtocolQualifiers(DS))
2553 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2554 << FixItHint::CreateInsertion(Loc, "id")
2555 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002556
2557 // Need to support trailing type qualifiers (e.g. "id<p> const").
2558 // If a type specifier follows, it will be diagnosed elsewhere.
2559 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002560 }
John McCallfec54012009-08-03 20:12:06 +00002561 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002562 if (isInvalid) {
2563 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002564 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002565
2566 if (DiagID == diag::ext_duplicate_declspec)
2567 Diag(Tok, DiagID)
2568 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2569 else
2570 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002571 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002572
Chris Lattner81c018d2008-03-13 06:29:04 +00002573 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002574 if (DiagID != diag::err_bool_redeclaration)
2575 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002576 }
2577}
Douglas Gregoradcac882008-12-01 23:54:00 +00002578
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002579/// ParseStructDeclaration - Parse a struct declaration without the terminating
2580/// semicolon.
2581///
Reid Spencer5f016e22007-07-11 17:01:13 +00002582/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002583/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002584/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002585/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002586/// struct-declarator-list:
2587/// struct-declarator
2588/// struct-declarator-list ',' struct-declarator
2589/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2590/// struct-declarator:
2591/// declarator
2592/// [GNU] declarator attributes[opt]
2593/// declarator[opt] ':' constant-expression
2594/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2595///
Chris Lattnere1359422008-04-10 06:46:29 +00002596void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002597ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002598
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002599 if (Tok.is(tok::kw___extension__)) {
2600 // __extension__ silences extension warnings in the subexpression.
2601 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002602 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002603 return ParseStructDeclaration(DS, Fields);
2604 }
Mike Stump1eb44332009-09-09 15:08:12 +00002605
Steve Naroff28a7ca82007-08-20 22:28:22 +00002606 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002607 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002608
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002609 // If there are no declarators, this is a free-standing declaration
2610 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002611 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002612 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002613 return;
2614 }
2615
2616 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002617 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002618 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002619 while (1) {
John McCall92576642012-05-07 06:16:41 +00002620 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCallbdd563e2009-11-03 02:38:08 +00002621 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002622 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002623
2624 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002625 if (!FirstDeclarator)
2626 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002627
Steve Naroff28a7ca82007-08-20 22:28:22 +00002628 /// struct-declarator: declarator
2629 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002630 if (Tok.isNot(tok::colon)) {
2631 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2632 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002633 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002634 }
Mike Stump1eb44332009-09-09 15:08:12 +00002635
Chris Lattner04d66662007-10-09 17:33:22 +00002636 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002637 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002638 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002639 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002640 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002641 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002642 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002643 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002644
Steve Naroff28a7ca82007-08-20 22:28:22 +00002645 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002646 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002647
John McCallbdd563e2009-11-03 02:38:08 +00002648 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002649 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002650 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002651
Steve Naroff28a7ca82007-08-20 22:28:22 +00002652 // If we don't have a comma, it is either the end of the list (a ';')
2653 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002654 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002655 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002656
Steve Naroff28a7ca82007-08-20 22:28:22 +00002657 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002658 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002659
John McCallbdd563e2009-11-03 02:38:08 +00002660 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002661 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002662}
2663
2664/// ParseStructUnionBody
2665/// struct-contents:
2666/// struct-declaration-list
2667/// [EXT] empty
2668/// [GNU] "struct-declaration-list" without terminatoring ';'
2669/// struct-declaration-list:
2670/// struct-declaration
2671/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002672/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002673///
Reid Spencer5f016e22007-07-11 17:01:13 +00002674void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002675 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002676 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2677 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002678
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002679 BalancedDelimiterTracker T(*this, tok::l_brace);
2680 if (T.consumeOpen())
2681 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002682
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002683 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002684 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002685
Reid Spencer5f016e22007-07-11 17:01:13 +00002686 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2687 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002688 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002689 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2690 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2691 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002692
Chris Lattner5f9e2722011-07-23 10:55:15 +00002693 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002694
Reid Spencer5f016e22007-07-11 17:01:13 +00002695 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002696 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002697 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002698
Reid Spencer5f016e22007-07-11 17:01:13 +00002699 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002700 if (Tok.is(tok::semi)) {
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002701 ConsumeExtraSemi(InsideStruct,
2702 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Reid Spencer5f016e22007-07-11 17:01:13 +00002703 continue;
2704 }
Chris Lattnere1359422008-04-10 06:46:29 +00002705
2706 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002707 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002708
John McCallbdd563e2009-11-03 02:38:08 +00002709 if (!Tok.is(tok::at)) {
2710 struct CFieldCallback : FieldCallback {
2711 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002712 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002713 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002714
John McCalld226f652010-08-21 09:40:31 +00002715 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002716 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002717 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2718
John McCalld226f652010-08-21 09:40:31 +00002719 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002720 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002721 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002722 FD.D.getDeclSpec().getSourceRange().getBegin(),
2723 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002724 FieldDecls.push_back(Field);
2725 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002726 }
John McCallbdd563e2009-11-03 02:38:08 +00002727 } Callback(*this, TagDecl, FieldDecls);
2728
2729 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002730 } else { // Handle @defs
2731 ConsumeToken();
2732 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2733 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002734 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002735 continue;
2736 }
2737 ConsumeToken();
2738 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2739 if (!Tok.is(tok::identifier)) {
2740 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002741 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002742 continue;
2743 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002744 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002745 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002746 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002747 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2748 ConsumeToken();
2749 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002750 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002751
Chris Lattner04d66662007-10-09 17:33:22 +00002752 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002753 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002754 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002755 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002756 break;
2757 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002758 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2759 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002760 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002761 // If we stopped at a ';', eat it.
2762 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002763 }
2764 }
Mike Stump1eb44332009-09-09 15:08:12 +00002765
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002766 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002767
John McCall0b7e6782011-03-24 11:26:52 +00002768 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002769 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002770 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002771
Douglas Gregor23c94db2010-07-02 17:43:08 +00002772 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002773 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002774 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002775 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002776 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002777 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2778 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002779}
2780
Reid Spencer5f016e22007-07-11 17:01:13 +00002781/// ParseEnumSpecifier
2782/// enum-specifier: [C99 6.7.2.2]
2783/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002784///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002785/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2786/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002787/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2788/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002789/// 'enum' identifier
2790/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002791///
Richard Smith1af83c42012-03-23 03:33:32 +00002792/// [C++11] enum-head '{' enumerator-list[opt] '}'
2793/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002794///
Richard Smith1af83c42012-03-23 03:33:32 +00002795/// enum-head: [C++11]
2796/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2797/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2798/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002799///
Richard Smith1af83c42012-03-23 03:33:32 +00002800/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002801/// 'enum'
2802/// 'enum' 'class'
2803/// 'enum' 'struct'
2804///
Richard Smith1af83c42012-03-23 03:33:32 +00002805/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002806/// ':' type-specifier-seq
2807///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002808/// [C++] elaborated-type-specifier:
2809/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2810///
Chris Lattner4c97d762009-04-12 21:49:30 +00002811void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002812 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002813 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002814 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002815 if (Tok.is(tok::code_completion)) {
2816 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002817 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002818 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002819 }
John McCall57c13002011-07-06 05:58:41 +00002820
Richard Smithbdad7a22012-01-10 01:33:14 +00002821 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002822 bool IsScopedUsingClassTag = false;
2823
David Blaikie4e4d0842012-03-11 07:00:24 +00002824 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002825 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002826 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002827 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002828 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002829 }
Richard Smith1af83c42012-03-23 03:33:32 +00002830
John McCall13489672012-05-07 06:16:58 +00002831 // C++11 [temp.explicit]p12:
2832 // The usual access controls do not apply to names used to specify
2833 // explicit instantiations.
2834 // We extend this to also cover explicit specializations. Note that
2835 // we don't suppress if this turns out to be an elaborated type
2836 // specifier.
2837 bool shouldDelayDiagsInTag =
2838 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2839 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2840 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00002841
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002842 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002843 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002844 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002845
Aaron Ballman6454a022012-03-01 04:09:28 +00002846 // If declspecs exist after tag, parse them.
2847 while (Tok.is(tok::kw___declspec))
2848 ParseMicrosoftDeclSpec(attrs);
2849
Richard Smith7796eb52012-03-12 08:56:40 +00002850 // Enum definitions should not be parsed in a trailing-return-type.
2851 bool AllowDeclaration = DSC != DSC_trailing;
2852
2853 bool AllowFixedUnderlyingType = AllowDeclaration &&
2854 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2855 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002856
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002857 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002858 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002859 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2860 // if a fixed underlying type is allowed.
2861 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2862
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002863 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2864 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002865 return;
2866
2867 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002868 Diag(Tok, diag::err_expected_ident);
2869 if (Tok.isNot(tok::l_brace)) {
2870 // Has no name and is not a definition.
2871 // Skip the rest of this declarator, up until the comma or semicolon.
2872 SkipUntil(tok::comma, true);
2873 return;
2874 }
2875 }
2876 }
Mike Stump1eb44332009-09-09 15:08:12 +00002877
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002878 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002879 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002880 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002881 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002882
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002883 // Skip the rest of this declarator, up until the comma or semicolon.
2884 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002885 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002886 }
Mike Stump1eb44332009-09-09 15:08:12 +00002887
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002888 // If an identifier is present, consume and remember it.
2889 IdentifierInfo *Name = 0;
2890 SourceLocation NameLoc;
2891 if (Tok.is(tok::identifier)) {
2892 Name = Tok.getIdentifierInfo();
2893 NameLoc = ConsumeToken();
2894 }
Mike Stump1eb44332009-09-09 15:08:12 +00002895
Richard Smithbdad7a22012-01-10 01:33:14 +00002896 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002897 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2898 // declaration of a scoped enumeration.
2899 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002900 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002901 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002902 }
2903
John McCall13489672012-05-07 06:16:58 +00002904 // Okay, end the suppression area. We'll decide whether to emit the
2905 // diagnostics in a second.
2906 if (shouldDelayDiagsInTag)
2907 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00002908
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002909 TypeResult BaseType;
2910
Douglas Gregora61b3e72010-12-01 17:42:47 +00002911 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002912 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002913 bool PossibleBitfield = false;
2914 if (getCurScope()->getFlags() & Scope::ClassScope) {
2915 // If we're in class scope, this can either be an enum declaration with
2916 // an underlying type, or a declaration of a bitfield member. We try to
2917 // use a simple disambiguation scheme first to catch the common cases
2918 // (integer literal, sizeof); if it's still ambiguous, we then consider
2919 // anything that's a simple-type-specifier followed by '(' as an
2920 // expression. This suffices because function types are not valid
2921 // underlying types anyway.
2922 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2923 // If the next token starts an expression, we know we're parsing a
2924 // bit-field. This is the common case.
2925 if (TPR == TPResult::True())
2926 PossibleBitfield = true;
2927 // If the next token starts a type-specifier-seq, it may be either a
2928 // a fixed underlying type or the start of a function-style cast in C++;
2929 // lookahead one more token to see if it's obvious that we have a
2930 // fixed underlying type.
2931 else if (TPR == TPResult::False() &&
2932 GetLookAheadToken(2).getKind() == tok::semi) {
2933 // Consume the ':'.
2934 ConsumeToken();
2935 } else {
2936 // We have the start of a type-specifier-seq, so we have to perform
2937 // tentative parsing to determine whether we have an expression or a
2938 // type.
2939 TentativeParsingAction TPA(*this);
2940
2941 // Consume the ':'.
2942 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002943
2944 // If we see a type specifier followed by an open-brace, we have an
2945 // ambiguity between an underlying type and a C++11 braced
2946 // function-style cast. Resolve this by always treating it as an
2947 // underlying type.
2948 // FIXME: The standard is not entirely clear on how to disambiguate in
2949 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002950 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002951 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002952 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002953 // We'll parse this as a bitfield later.
2954 PossibleBitfield = true;
2955 TPA.Revert();
2956 } else {
2957 // We have a type-specifier-seq.
2958 TPA.Commit();
2959 }
2960 }
2961 } else {
2962 // Consume the ':'.
2963 ConsumeToken();
2964 }
2965
2966 if (!PossibleBitfield) {
2967 SourceRange Range;
2968 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002969
David Blaikie4e4d0842012-03-11 07:00:24 +00002970 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002971 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2972 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002973 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002974 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002975 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002976 }
2977
Richard Smithbdad7a22012-01-10 01:33:14 +00002978 // There are four options here. If we have 'friend enum foo;' then this is a
2979 // friend declaration, and cannot have an accompanying definition. If we have
2980 // 'enum foo;', then this is a forward declaration. If we have
2981 // 'enum foo {...' then this is a definition. Otherwise we have something
2982 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002983 //
2984 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2985 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2986 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2987 //
John McCallf312b1e2010-08-26 23:41:50 +00002988 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00002989 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00002990 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00002991 } else if (Tok.is(tok::l_brace)) {
2992 if (DS.isFriendSpecified()) {
2993 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
2994 << SourceRange(DS.getFriendSpecLoc());
2995 ConsumeBrace();
2996 SkipUntil(tok::r_brace);
2997 TUK = Sema::TUK_Friend;
2998 } else {
2999 TUK = Sema::TUK_Definition;
3000 }
3001 } else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) {
3002 TUK = (DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration);
3003 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003004 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003005 }
3006
3007 // If this is an elaborated type specifier, and we delayed
3008 // diagnostics before, just merge them into the current pool.
3009 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3010 diagsFromTag.redelay();
3011 }
Richard Smith1af83c42012-03-23 03:33:32 +00003012
3013 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003014 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003015 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003016 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3017 // Skip the rest of this declarator, up until the comma or semicolon.
3018 Diag(Tok, diag::err_enum_template);
3019 SkipUntil(tok::comma, true);
3020 return;
3021 }
3022
3023 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3024 // Enumerations can't be explicitly instantiated.
3025 DS.SetTypeSpecError();
3026 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3027 return;
3028 }
3029
3030 assert(TemplateInfo.TemplateParams && "no template parameters");
3031 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3032 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003033 }
Richard Smith1af83c42012-03-23 03:33:32 +00003034
Douglas Gregorb9075602011-02-22 02:55:24 +00003035 if (!Name && TUK != Sema::TUK_Definition) {
3036 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003037
Douglas Gregorb9075602011-02-22 02:55:24 +00003038 // Skip the rest of this declarator, up until the comma or semicolon.
3039 SkipUntil(tok::comma, true);
3040 return;
3041 }
Richard Smith1af83c42012-03-23 03:33:32 +00003042
Douglas Gregor402abb52009-05-28 23:31:59 +00003043 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003044 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003045 const char *PrevSpec = 0;
3046 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003047 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003048 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003049 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003050 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003051 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003052
Douglas Gregor48c89f42010-04-24 16:38:41 +00003053 if (IsDependent) {
3054 // This enum has a dependent nested-name-specifier. Handle it as a
3055 // dependent tag.
3056 if (!Name) {
3057 DS.SetTypeSpecError();
3058 Diag(Tok, diag::err_expected_type_name_after_typename);
3059 return;
3060 }
3061
Douglas Gregor23c94db2010-07-02 17:43:08 +00003062 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003063 TUK, SS, Name, StartLoc,
3064 NameLoc);
3065 if (Type.isInvalid()) {
3066 DS.SetTypeSpecError();
3067 return;
3068 }
3069
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003070 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3071 NameLoc.isValid() ? NameLoc : StartLoc,
3072 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003073 Diag(StartLoc, DiagID) << PrevSpec;
3074
3075 return;
3076 }
Mike Stump1eb44332009-09-09 15:08:12 +00003077
John McCalld226f652010-08-21 09:40:31 +00003078 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003079 // The action failed to produce an enumeration tag. If this is a
3080 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003081 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003082 ConsumeBrace();
3083 SkipUntil(tok::r_brace);
3084 }
3085
3086 DS.SetTypeSpecError();
3087 return;
3088 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003089
Richard Smith7796eb52012-03-12 08:56:40 +00003090 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
John McCall13489672012-05-07 06:16:58 +00003091 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00003092 }
Mike Stump1eb44332009-09-09 15:08:12 +00003093
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003094 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3095 NameLoc.isValid() ? NameLoc : StartLoc,
3096 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003097 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003098}
3099
3100/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3101/// enumerator-list:
3102/// enumerator
3103/// enumerator-list ',' enumerator
3104/// enumerator:
3105/// enumeration-constant
3106/// enumeration-constant '=' constant-expression
3107/// enumeration-constant:
3108/// identifier
3109///
John McCalld226f652010-08-21 09:40:31 +00003110void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003111 // Enter the scope of the enum body and start the definition.
3112 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003113 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003114
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003115 BalancedDelimiterTracker T(*this, tok::l_brace);
3116 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003117
Chris Lattner7946dd32007-08-27 17:24:30 +00003118 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003119 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003120 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003121
Chris Lattner5f9e2722011-07-23 10:55:15 +00003122 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003123
John McCalld226f652010-08-21 09:40:31 +00003124 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003125
Reid Spencer5f016e22007-07-11 17:01:13 +00003126 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003127 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003128 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3129 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003130
John McCall5b629aa2010-10-22 23:36:17 +00003131 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003132 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003133 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003134
Reid Spencer5f016e22007-07-11 17:01:13 +00003135 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003136 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003137 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003138
Chris Lattner04d66662007-10-09 17:33:22 +00003139 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003140 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003141 AssignedVal = ParseConstantExpression();
3142 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003143 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003144 }
Mike Stump1eb44332009-09-09 15:08:12 +00003145
Reid Spencer5f016e22007-07-11 17:01:13 +00003146 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003147 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3148 LastEnumConstDecl,
3149 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003150 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003151 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003152 PD.complete(EnumConstDecl);
3153
Reid Spencer5f016e22007-07-11 17:01:13 +00003154 EnumConstantDecls.push_back(EnumConstDecl);
3155 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003156
Douglas Gregor751f6922010-09-07 14:51:08 +00003157 if (Tok.is(tok::identifier)) {
3158 // We're missing a comma between enumerators.
3159 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3160 Diag(Loc, diag::err_enumerator_list_missing_comma)
3161 << FixItHint::CreateInsertion(Loc, ", ");
3162 continue;
3163 }
3164
Chris Lattner04d66662007-10-09 17:33:22 +00003165 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003166 break;
3167 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003168
Richard Smith7fe62082011-10-15 05:09:34 +00003169 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003170 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003171 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003172 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003173 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003174 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003175 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3176 << FixItHint::CreateRemoval(CommaLoc);
3177 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003178 }
Mike Stump1eb44332009-09-09 15:08:12 +00003179
Reid Spencer5f016e22007-07-11 17:01:13 +00003180 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003181 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003182
Reid Spencer5f016e22007-07-11 17:01:13 +00003183 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003184 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003185 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003186
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003187 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3188 EnumDecl, EnumConstantDecls.data(),
3189 EnumConstantDecls.size(), getCurScope(),
3190 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003191
Douglas Gregor72de6672009-01-08 20:45:30 +00003192 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003193 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3194 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003195}
3196
3197/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003198/// start of a type-qualifier-list.
3199bool Parser::isTypeQualifier() const {
3200 switch (Tok.getKind()) {
3201 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003202
3203 // type-qualifier only in OpenCL
3204 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003205 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003206
Steve Naroff5f8aa692008-02-11 23:15:56 +00003207 // type-qualifier
3208 case tok::kw_const:
3209 case tok::kw_volatile:
3210 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003211 case tok::kw___private:
3212 case tok::kw___local:
3213 case tok::kw___global:
3214 case tok::kw___constant:
3215 case tok::kw___read_only:
3216 case tok::kw___read_write:
3217 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003218 return true;
3219 }
3220}
3221
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003222/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3223/// is definitely a type-specifier. Return false if it isn't part of a type
3224/// specifier or if we're not sure.
3225bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3226 switch (Tok.getKind()) {
3227 default: return false;
3228 // type-specifiers
3229 case tok::kw_short:
3230 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003231 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003232 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003233 case tok::kw_signed:
3234 case tok::kw_unsigned:
3235 case tok::kw__Complex:
3236 case tok::kw__Imaginary:
3237 case tok::kw_void:
3238 case tok::kw_char:
3239 case tok::kw_wchar_t:
3240 case tok::kw_char16_t:
3241 case tok::kw_char32_t:
3242 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003243 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003244 case tok::kw_float:
3245 case tok::kw_double:
3246 case tok::kw_bool:
3247 case tok::kw__Bool:
3248 case tok::kw__Decimal32:
3249 case tok::kw__Decimal64:
3250 case tok::kw__Decimal128:
3251 case tok::kw___vector:
3252
3253 // struct-or-union-specifier (C99) or class-specifier (C++)
3254 case tok::kw_class:
3255 case tok::kw_struct:
3256 case tok::kw_union:
3257 // enum-specifier
3258 case tok::kw_enum:
3259
3260 // typedef-name
3261 case tok::annot_typename:
3262 return true;
3263 }
3264}
3265
Steve Naroff5f8aa692008-02-11 23:15:56 +00003266/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003267/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003268bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003269 switch (Tok.getKind()) {
3270 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003271
Chris Lattner166a8fc2009-01-04 23:41:41 +00003272 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003273 if (TryAltiVecVectorToken())
3274 return true;
3275 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003276 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003277 // Annotate typenames and C++ scope specifiers. If we get one, just
3278 // recurse to handle whatever we get.
3279 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003280 return true;
3281 if (Tok.is(tok::identifier))
3282 return false;
3283 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003284
Chris Lattner166a8fc2009-01-04 23:41:41 +00003285 case tok::coloncolon: // ::foo::bar
3286 if (NextToken().is(tok::kw_new) || // ::new
3287 NextToken().is(tok::kw_delete)) // ::delete
3288 return false;
3289
Chris Lattner166a8fc2009-01-04 23:41:41 +00003290 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003291 return true;
3292 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003293
Reid Spencer5f016e22007-07-11 17:01:13 +00003294 // GNU attributes support.
3295 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003296 // GNU typeof support.
3297 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003298
Reid Spencer5f016e22007-07-11 17:01:13 +00003299 // type-specifiers
3300 case tok::kw_short:
3301 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003302 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003303 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003304 case tok::kw_signed:
3305 case tok::kw_unsigned:
3306 case tok::kw__Complex:
3307 case tok::kw__Imaginary:
3308 case tok::kw_void:
3309 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003310 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003311 case tok::kw_char16_t:
3312 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003313 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003314 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003315 case tok::kw_float:
3316 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003317 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003318 case tok::kw__Bool:
3319 case tok::kw__Decimal32:
3320 case tok::kw__Decimal64:
3321 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003322 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003323
Chris Lattner99dc9142008-04-13 18:59:07 +00003324 // struct-or-union-specifier (C99) or class-specifier (C++)
3325 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003326 case tok::kw_struct:
3327 case tok::kw_union:
3328 // enum-specifier
3329 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003330
Reid Spencer5f016e22007-07-11 17:01:13 +00003331 // type-qualifier
3332 case tok::kw_const:
3333 case tok::kw_volatile:
3334 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003335
3336 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003337 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003338 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003339
Chris Lattner7c186be2008-10-20 00:25:30 +00003340 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3341 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003342 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003343
Steve Naroff239f0732008-12-25 14:16:32 +00003344 case tok::kw___cdecl:
3345 case tok::kw___stdcall:
3346 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003347 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003348 case tok::kw___w64:
3349 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003350 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003351 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003352 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003353
3354 case tok::kw___private:
3355 case tok::kw___local:
3356 case tok::kw___global:
3357 case tok::kw___constant:
3358 case tok::kw___read_only:
3359 case tok::kw___read_write:
3360 case tok::kw___write_only:
3361
Eli Friedman290eeb02009-06-08 23:27:34 +00003362 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003363
3364 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003365 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003366
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003367 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003368 case tok::kw__Atomic:
3369 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003370 }
3371}
3372
3373/// isDeclarationSpecifier() - Return true if the current token is part of a
3374/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003375///
3376/// \param DisambiguatingWithExpression True to indicate that the purpose of
3377/// this check is to disambiguate between an expression and a declaration.
3378bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003379 switch (Tok.getKind()) {
3380 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003381
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003382 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003383 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003384
Chris Lattner166a8fc2009-01-04 23:41:41 +00003385 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003386 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003387 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003388 return false;
John Thompson82287d12010-02-05 00:12:22 +00003389 if (TryAltiVecVectorToken())
3390 return true;
3391 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003392 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003393 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003394 // Annotate typenames and C++ scope specifiers. If we get one, just
3395 // recurse to handle whatever we get.
3396 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003397 return true;
3398 if (Tok.is(tok::identifier))
3399 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003400
3401 // If we're in Objective-C and we have an Objective-C class type followed
3402 // by an identifier and then either ':' or ']', in a place where an
3403 // expression is permitted, then this is probably a class message send
3404 // missing the initial '['. In this case, we won't consider this to be
3405 // the start of a declaration.
3406 if (DisambiguatingWithExpression &&
3407 isStartOfObjCClassMessageMissingOpenBracket())
3408 return false;
3409
John McCall9ba61662010-02-26 08:45:28 +00003410 return isDeclarationSpecifier();
3411
Chris Lattner166a8fc2009-01-04 23:41:41 +00003412 case tok::coloncolon: // ::foo::bar
3413 if (NextToken().is(tok::kw_new) || // ::new
3414 NextToken().is(tok::kw_delete)) // ::delete
3415 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003416
Chris Lattner166a8fc2009-01-04 23:41:41 +00003417 // Annotate typenames and C++ scope specifiers. If we get one, just
3418 // recurse to handle whatever we get.
3419 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003420 return true;
3421 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003422
Reid Spencer5f016e22007-07-11 17:01:13 +00003423 // storage-class-specifier
3424 case tok::kw_typedef:
3425 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003426 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003427 case tok::kw_static:
3428 case tok::kw_auto:
3429 case tok::kw_register:
3430 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003431
Douglas Gregor8d267c52011-09-09 02:06:17 +00003432 // Modules
3433 case tok::kw___module_private__:
3434
Reid Spencer5f016e22007-07-11 17:01:13 +00003435 // type-specifiers
3436 case tok::kw_short:
3437 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003438 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003439 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003440 case tok::kw_signed:
3441 case tok::kw_unsigned:
3442 case tok::kw__Complex:
3443 case tok::kw__Imaginary:
3444 case tok::kw_void:
3445 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003446 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003447 case tok::kw_char16_t:
3448 case tok::kw_char32_t:
3449
Reid Spencer5f016e22007-07-11 17:01:13 +00003450 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003451 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003452 case tok::kw_float:
3453 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003454 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003455 case tok::kw__Bool:
3456 case tok::kw__Decimal32:
3457 case tok::kw__Decimal64:
3458 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003459 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003460
Chris Lattner99dc9142008-04-13 18:59:07 +00003461 // struct-or-union-specifier (C99) or class-specifier (C++)
3462 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003463 case tok::kw_struct:
3464 case tok::kw_union:
3465 // enum-specifier
3466 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003467
Reid Spencer5f016e22007-07-11 17:01:13 +00003468 // type-qualifier
3469 case tok::kw_const:
3470 case tok::kw_volatile:
3471 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003472
Reid Spencer5f016e22007-07-11 17:01:13 +00003473 // function-specifier
3474 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003475 case tok::kw_virtual:
3476 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003477
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003478 // static_assert-declaration
3479 case tok::kw__Static_assert:
3480
Chris Lattner1ef08762007-08-09 17:01:07 +00003481 // GNU typeof support.
3482 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003483
Chris Lattner1ef08762007-08-09 17:01:07 +00003484 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003485 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003486 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003487
Francois Pichete3d49b42011-06-19 08:02:06 +00003488 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003489 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003490 return true;
3491
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003492 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003493 case tok::kw__Atomic:
3494 return true;
3495
Chris Lattnerf3948c42008-07-26 03:38:44 +00003496 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3497 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003498 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003499
Douglas Gregord9d75e52011-04-27 05:41:15 +00003500 // typedef-name
3501 case tok::annot_typename:
3502 return !DisambiguatingWithExpression ||
3503 !isStartOfObjCClassMessageMissingOpenBracket();
3504
Steve Naroff47f52092009-01-06 19:34:12 +00003505 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003506 case tok::kw___cdecl:
3507 case tok::kw___stdcall:
3508 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003509 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003510 case tok::kw___w64:
3511 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003512 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003513 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003514 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003515 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003516
3517 case tok::kw___private:
3518 case tok::kw___local:
3519 case tok::kw___global:
3520 case tok::kw___constant:
3521 case tok::kw___read_only:
3522 case tok::kw___read_write:
3523 case tok::kw___write_only:
3524
Eli Friedman290eeb02009-06-08 23:27:34 +00003525 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003526 }
3527}
3528
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003529bool Parser::isConstructorDeclarator() {
3530 TentativeParsingAction TPA(*this);
3531
3532 // Parse the C++ scope specifier.
3533 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003534 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3535 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003536 TPA.Revert();
3537 return false;
3538 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003539
3540 // Parse the constructor name.
3541 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3542 // We already know that we have a constructor name; just consume
3543 // the token.
3544 ConsumeToken();
3545 } else {
3546 TPA.Revert();
3547 return false;
3548 }
3549
Richard Smith22592862012-03-27 23:05:05 +00003550 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003551 if (Tok.isNot(tok::l_paren)) {
3552 TPA.Revert();
3553 return false;
3554 }
3555 ConsumeParen();
3556
Richard Smith22592862012-03-27 23:05:05 +00003557 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3558 // that we have a constructor.
3559 if (Tok.is(tok::r_paren) ||
3560 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003561 TPA.Revert();
3562 return true;
3563 }
3564
3565 // If we need to, enter the specified scope.
3566 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003567 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003568 DeclScopeObj.EnterDeclaratorScope();
3569
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003570 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003571 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003572 MaybeParseMicrosoftAttributes(Attrs);
3573
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003574 // Check whether the next token(s) are part of a declaration
3575 // specifier, in which case we have the start of a parameter and,
3576 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003577 bool IsConstructor = false;
3578 if (isDeclarationSpecifier())
3579 IsConstructor = true;
3580 else if (Tok.is(tok::identifier) ||
3581 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3582 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3583 // This might be a parenthesized member name, but is more likely to
3584 // be a constructor declaration with an invalid argument type. Keep
3585 // looking.
3586 if (Tok.is(tok::annot_cxxscope))
3587 ConsumeToken();
3588 ConsumeToken();
3589
3590 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003591 // which must have one of the following syntactic forms (see the
3592 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003593 switch (Tok.getKind()) {
3594 case tok::l_paren:
3595 // C(X ( int));
3596 case tok::l_square:
3597 // C(X [ 5]);
3598 // C(X [ [attribute]]);
3599 case tok::coloncolon:
3600 // C(X :: Y);
3601 // C(X :: *p);
3602 case tok::r_paren:
3603 // C(X )
3604 // Assume this isn't a constructor, rather than assuming it's a
3605 // constructor with an unnamed parameter of an ill-formed type.
3606 break;
3607
3608 default:
3609 IsConstructor = true;
3610 break;
3611 }
3612 }
3613
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003614 TPA.Revert();
3615 return IsConstructor;
3616}
Reid Spencer5f016e22007-07-11 17:01:13 +00003617
3618/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003619/// type-qualifier-list: [C99 6.7.5]
3620/// type-qualifier
3621/// [vendor] attributes
3622/// [ only if VendorAttributesAllowed=true ]
3623/// type-qualifier-list type-qualifier
3624/// [vendor] type-qualifier-list attributes
3625/// [ only if VendorAttributesAllowed=true ]
3626/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3627/// [ only if CXX0XAttributesAllowed=true ]
3628/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003629///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003630void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3631 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003632 bool CXX11AttributesAllowed) {
3633 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003634 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003635 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003636 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003637 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003638 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003639
3640 SourceLocation EndLoc;
3641
Reid Spencer5f016e22007-07-11 17:01:13 +00003642 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003643 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003644 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003645 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003646 SourceLocation Loc = Tok.getLocation();
3647
3648 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003649 case tok::code_completion:
3650 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003651 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003652
Reid Spencer5f016e22007-07-11 17:01:13 +00003653 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003654 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003655 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003656 break;
3657 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003658 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003659 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003660 break;
3661 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003662 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003663 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003664 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003665
3666 // OpenCL qualifiers:
3667 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003668 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003669 goto DoneWithTypeQuals;
3670 case tok::kw___private:
3671 case tok::kw___global:
3672 case tok::kw___local:
3673 case tok::kw___constant:
3674 case tok::kw___read_only:
3675 case tok::kw___write_only:
3676 case tok::kw___read_write:
3677 ParseOpenCLQualifiers(DS);
3678 break;
3679
Eli Friedman290eeb02009-06-08 23:27:34 +00003680 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003681 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003682 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003683 case tok::kw___cdecl:
3684 case tok::kw___stdcall:
3685 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003686 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003687 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003688 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003689 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003690 continue;
3691 }
3692 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003693 case tok::kw___pascal:
3694 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003695 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003696 continue;
3697 }
3698 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003699 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003700 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003701 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003702 continue; // do *not* consume the next token!
3703 }
3704 // otherwise, FALL THROUGH!
3705 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003706 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003707 // If this is not a type-qualifier token, we're done reading type
3708 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003709 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003710 if (EndLoc.isValid())
3711 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003712 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003713 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003714
Reid Spencer5f016e22007-07-11 17:01:13 +00003715 // If the specifier combination wasn't legal, issue a diagnostic.
3716 if (isInvalid) {
3717 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003718 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003719 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003720 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003721 }
3722}
3723
3724
3725/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3726///
3727void Parser::ParseDeclarator(Declarator &D) {
3728 /// This implements the 'declarator' production in the C grammar, then checks
3729 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003730 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003731}
3732
Richard Smith9988f282012-03-29 01:16:42 +00003733static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3734 if (Kind == tok::star || Kind == tok::caret)
3735 return true;
3736
3737 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3738 if (!Lang.CPlusPlus)
3739 return false;
3740
3741 return Kind == tok::amp || Kind == tok::ampamp;
3742}
3743
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003744/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3745/// is parsed by the function passed to it. Pass null, and the direct-declarator
3746/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003747/// ptr-operator production.
3748///
Richard Smith0706df42011-10-19 21:33:05 +00003749/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003750/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3751/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003752///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003753/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3754/// [C] pointer[opt] direct-declarator
3755/// [C++] direct-declarator
3756/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003757///
3758/// pointer: [C99 6.7.5]
3759/// '*' type-qualifier-list[opt]
3760/// '*' type-qualifier-list[opt] pointer
3761///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003762/// ptr-operator:
3763/// '*' cv-qualifier-seq[opt]
3764/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003765/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003766/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003767/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003768/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003769void Parser::ParseDeclaratorInternal(Declarator &D,
3770 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003771 if (Diags.hasAllExtensionsSilenced())
3772 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003773
Sebastian Redlf30208a2009-01-24 21:16:55 +00003774 // C++ member pointers start with a '::' or a nested-name.
3775 // Member pointers get special handling, since there's no place for the
3776 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003777 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003778 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3779 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003780 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3781 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003782 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003783 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003784
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003785 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003786 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003787 // The scope spec really belongs to the direct-declarator.
3788 D.getCXXScopeSpec() = SS;
3789 if (DirectDeclParser)
3790 (this->*DirectDeclParser)(D);
3791 return;
3792 }
3793
3794 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003795 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003796 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003797 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003798 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003799
3800 // Recurse to parse whatever is left.
3801 ParseDeclaratorInternal(D, DirectDeclParser);
3802
3803 // Sema will have to catch (syntactically invalid) pointers into global
3804 // scope. It has to catch pointers into namespace scope anyway.
3805 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003806 Loc),
3807 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003808 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003809 return;
3810 }
3811 }
3812
3813 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003814 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003815 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003816 if (DirectDeclParser)
3817 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003818 return;
3819 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003820
Sebastian Redl05532f22009-03-15 22:02:01 +00003821 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3822 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003823 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003824 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003825
Chris Lattner9af55002009-03-27 04:18:06 +00003826 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003827 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003828 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003829
Richard Smith6ee326a2012-04-10 01:32:12 +00003830 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00003831 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003832 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003833
Reid Spencer5f016e22007-07-11 17:01:13 +00003834 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003835 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003836 if (Kind == tok::star)
3837 // Remember that we parsed a pointer type, and remember the type-quals.
3838 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003839 DS.getConstSpecLoc(),
3840 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003841 DS.getRestrictSpecLoc()),
3842 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003843 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003844 else
3845 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003846 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003847 Loc),
3848 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003849 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003850 } else {
3851 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003852 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003853
Sebastian Redl743de1f2009-03-23 00:00:23 +00003854 // Complain about rvalue references in C++03, but then go on and build
3855 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003856 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003857 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003858 diag::warn_cxx98_compat_rvalue_reference :
3859 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003860
Richard Smith6ee326a2012-04-10 01:32:12 +00003861 // GNU-style and C++11 attributes are allowed here, as is restrict.
3862 ParseTypeQualifierListOpt(DS);
3863 D.ExtendWithDeclSpec(DS);
3864
Reid Spencer5f016e22007-07-11 17:01:13 +00003865 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3866 // cv-qualifiers are introduced through the use of a typedef or of a
3867 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00003868 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3869 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3870 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003871 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003872 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3873 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003874 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003875 }
3876
3877 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003878 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003879
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003880 if (D.getNumTypeObjects() > 0) {
3881 // C++ [dcl.ref]p4: There shall be no references to references.
3882 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3883 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003884 if (const IdentifierInfo *II = D.getIdentifier())
3885 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3886 << II;
3887 else
3888 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3889 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003890
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003891 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003892 // can go ahead and build the (technically ill-formed)
3893 // declarator: reference collapsing will take care of it.
3894 }
3895 }
3896
Reid Spencer5f016e22007-07-11 17:01:13 +00003897 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003898 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003899 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003900 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003901 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003902 }
3903}
3904
Richard Smith9988f282012-03-29 01:16:42 +00003905static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
3906 SourceLocation EllipsisLoc) {
3907 if (EllipsisLoc.isValid()) {
3908 FixItHint Insertion;
3909 if (!D.getEllipsisLoc().isValid()) {
3910 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
3911 D.setEllipsisLoc(EllipsisLoc);
3912 }
3913 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
3914 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
3915 }
3916}
3917
Reid Spencer5f016e22007-07-11 17:01:13 +00003918/// ParseDirectDeclarator
3919/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003920/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003921/// '(' declarator ')'
3922/// [GNU] '(' attributes declarator ')'
3923/// [C90] direct-declarator '[' constant-expression[opt] ']'
3924/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3925/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3926/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3927/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00003928/// [C++11] direct-declarator '[' constant-expression[opt] ']'
3929/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00003930/// direct-declarator '(' parameter-type-list ')'
3931/// direct-declarator '(' identifier-list[opt] ')'
3932/// [GNU] direct-declarator '(' parameter-forward-declarations
3933/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003934/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3935/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00003936/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
3937/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
3938/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003939/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00003940/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003941///
3942/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003943/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003944/// '::'[opt] nested-name-specifier[opt] type-name
3945///
3946/// id-expression: [C++ 5.1]
3947/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003948/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003949///
3950/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003951/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003952/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003953/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003954/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003955/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003956///
Richard Smith5d8388c2012-03-27 01:42:32 +00003957/// Note, any additional constructs added here may need corresponding changes
3958/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00003959void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003960 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003961
David Blaikie4e4d0842012-03-11 07:00:24 +00003962 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003963 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003964 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003965 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3966 D.getContext() == Declarator::MemberContext;
3967 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3968 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003969 }
3970
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003971 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003972 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003973 // Change the declaration context for name lookup, until this function
3974 // is exited (and the declarator has been parsed).
3975 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003976 }
3977
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003978 // C++0x [dcl.fct]p14:
3979 // There is a syntactic ambiguity when an ellipsis occurs at the end
3980 // of a parameter-declaration-clause without a preceding comma. In
3981 // this case, the ellipsis is parsed as part of the
3982 // abstract-declarator if the type of the parameter names a template
3983 // parameter pack that has not been expanded; otherwise, it is parsed
3984 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00003985 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003986 !((D.getContext() == Declarator::PrototypeContext ||
3987 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003988 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00003989 !Actions.containsUnexpandedParameterPacks(D))) {
3990 SourceLocation EllipsisLoc = ConsumeToken();
3991 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
3992 // The ellipsis was put in the wrong place. Recover, and explain to
3993 // the user what they should have done.
3994 ParseDeclarator(D);
3995 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
3996 return;
3997 } else
3998 D.setEllipsisLoc(EllipsisLoc);
3999
4000 // The ellipsis can't be followed by a parenthesized declarator. We
4001 // check for that in ParseParenDeclarator, after we have disambiguated
4002 // the l_paren token.
4003 }
4004
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004005 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4006 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4007 // We found something that indicates the start of an unqualified-id.
4008 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004009 bool AllowConstructorName;
4010 if (D.getDeclSpec().hasTypeSpecifier())
4011 AllowConstructorName = false;
4012 else if (D.getCXXScopeSpec().isSet())
4013 AllowConstructorName =
4014 (D.getContext() == Declarator::FileContext ||
4015 (D.getContext() == Declarator::MemberContext &&
4016 D.getDeclSpec().isFriendSpecified()));
4017 else
4018 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4019
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004020 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004021 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4022 /*EnteringContext=*/true,
4023 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004024 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004025 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004026 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004027 D.getName()) ||
4028 // Once we're past the identifier, if the scope was bad, mark the
4029 // whole declarator bad.
4030 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004031 D.SetIdentifier(0, Tok.getLocation());
4032 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004033 } else {
4034 // Parsed the unqualified-id; update range information and move along.
4035 if (D.getSourceRange().getBegin().isInvalid())
4036 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4037 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004038 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004039 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004040 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004041 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004042 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004043 "There's a C++-specific check for tok::identifier above");
4044 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4045 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4046 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004047 goto PastIdentifier;
4048 }
Richard Smith9988f282012-03-29 01:16:42 +00004049
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004050 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004051 // direct-declarator: '(' declarator ')'
4052 // direct-declarator: '(' attributes declarator ')'
4053 // Example: 'char (*X)' or 'int (*XX)(void)'
4054 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004055
4056 // If the declarator was parenthesized, we entered the declarator
4057 // scope when parsing the parenthesized declarator, then exited
4058 // the scope already. Re-enter the scope, if we need to.
4059 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004060 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004061 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004062 if (!D.isInvalidType() &&
4063 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004064 // Change the declaration context for name lookup, until this function
4065 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004066 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004067 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004068 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004069 // This could be something simple like "int" (in which case the declarator
4070 // portion is empty), if an abstract-declarator is allowed.
4071 D.SetIdentifier(0, Tok.getLocation());
4072 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00004073 if (D.getContext() == Declarator::MemberContext)
4074 Diag(Tok, diag::err_expected_member_name_or_semi)
4075 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004076 else if (getLangOpts().CPlusPlus)
4077 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004078 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004079 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004080 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004081 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004082 }
Mike Stump1eb44332009-09-09 15:08:12 +00004083
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004084 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004085 assert(D.isPastIdentifier() &&
4086 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004087
Richard Smith6ee326a2012-04-10 01:32:12 +00004088 // Don't parse attributes unless we have parsed an unparenthesized name.
4089 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004090 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004091
Reid Spencer5f016e22007-07-11 17:01:13 +00004092 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004093 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004094 // Enter function-declaration scope, limiting any declarators to the
4095 // function prototype scope, including parameter declarators.
4096 ParseScope PrototypeScope(this,
4097 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004098 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4099 // In such a case, check if we actually have a function declarator; if it
4100 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00004101 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00004102 // When not in file scope, warn for ambiguous function declarators, just
4103 // in case the author intended it as a variable definition.
4104 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4105 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4106 break;
4107 }
John McCall0b7e6782011-03-24 11:26:52 +00004108 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004109 BalancedDelimiterTracker T(*this, tok::l_paren);
4110 T.consumeOpen();
4111 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004112 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004113 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004114 ParseBracketDeclarator(D);
4115 } else {
4116 break;
4117 }
4118 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004119}
Reid Spencer5f016e22007-07-11 17:01:13 +00004120
Chris Lattneref4715c2008-04-06 05:45:57 +00004121/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4122/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004123/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004124/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4125///
4126/// direct-declarator:
4127/// '(' declarator ')'
4128/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004129/// direct-declarator '(' parameter-type-list ')'
4130/// direct-declarator '(' identifier-list[opt] ')'
4131/// [GNU] direct-declarator '(' parameter-forward-declarations
4132/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004133///
4134void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004135 BalancedDelimiterTracker T(*this, tok::l_paren);
4136 T.consumeOpen();
4137
Chris Lattneref4715c2008-04-06 05:45:57 +00004138 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004139
Chris Lattner7399ee02008-10-20 02:05:46 +00004140 // Eat any attributes before we look at whether this is a grouping or function
4141 // declarator paren. If this is a grouping paren, the attribute applies to
4142 // the type being built up, for example:
4143 // int (__attribute__(()) *x)(long y)
4144 // If this ends up not being a grouping paren, the attribute applies to the
4145 // first argument, for example:
4146 // int (__attribute__(()) int x)
4147 // In either case, we need to eat any attributes to be able to determine what
4148 // sort of paren this is.
4149 //
John McCall0b7e6782011-03-24 11:26:52 +00004150 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004151 bool RequiresArg = false;
4152 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004153 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004154
Chris Lattner7399ee02008-10-20 02:05:46 +00004155 // We require that the argument list (if this is a non-grouping paren) be
4156 // present even if the attribute list was empty.
4157 RequiresArg = true;
4158 }
Steve Naroff239f0732008-12-25 14:16:32 +00004159 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004160 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004161 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004162 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004163 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004164 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004165 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004166 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004167 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004168 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004169
Chris Lattneref4715c2008-04-06 05:45:57 +00004170 // If we haven't past the identifier yet (or where the identifier would be
4171 // stored, if this is an abstract declarator), then this is probably just
4172 // grouping parens. However, if this could be an abstract-declarator, then
4173 // this could also be the start of function arguments (consider 'void()').
4174 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004175
Chris Lattneref4715c2008-04-06 05:45:57 +00004176 if (!D.mayOmitIdentifier()) {
4177 // If this can't be an abstract-declarator, this *must* be a grouping
4178 // paren, because we haven't seen the identifier yet.
4179 isGrouping = true;
4180 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004181 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4182 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004183 isDeclarationSpecifier() || // 'int(int)' is a function.
4184 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004185 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4186 // considered to be a type, not a K&R identifier-list.
4187 isGrouping = false;
4188 } else {
4189 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4190 isGrouping = true;
4191 }
Mike Stump1eb44332009-09-09 15:08:12 +00004192
Chris Lattneref4715c2008-04-06 05:45:57 +00004193 // If this is a grouping paren, handle:
4194 // direct-declarator: '(' declarator ')'
4195 // direct-declarator: '(' attributes declarator ')'
4196 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004197 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4198 D.setEllipsisLoc(SourceLocation());
4199
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004200 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004201 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004202 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004203 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004204 T.consumeClose();
4205 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4206 T.getCloseLocation()),
4207 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004208
4209 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004210
4211 // An ellipsis cannot be placed outside parentheses.
4212 if (EllipsisLoc.isValid())
4213 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4214
Chris Lattneref4715c2008-04-06 05:45:57 +00004215 return;
4216 }
Mike Stump1eb44332009-09-09 15:08:12 +00004217
Chris Lattneref4715c2008-04-06 05:45:57 +00004218 // Okay, if this wasn't a grouping paren, it must be the start of a function
4219 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004220 // identifier (and remember where it would have been), then call into
4221 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004222 D.SetIdentifier(0, Tok.getLocation());
4223
David Blaikie42d6d0c2011-12-04 05:04:18 +00004224 // Enter function-declaration scope, limiting any declarators to the
4225 // function prototype scope, including parameter declarators.
4226 ParseScope PrototypeScope(this,
4227 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004228 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004229 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004230}
4231
4232/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4233/// declarator D up to a paren, which indicates that we are parsing function
4234/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004235///
Richard Smith6ee326a2012-04-10 01:32:12 +00004236/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4237/// immediately after the open paren - they should be considered to be the
4238/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004239///
Richard Smith6ee326a2012-04-10 01:32:12 +00004240/// If RequiresArg is true, then the first argument of the function is required
4241/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004242///
Richard Smith6ee326a2012-04-10 01:32:12 +00004243/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4244/// (C++11) ref-qualifier[opt], exception-specification[opt],
4245/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4246///
4247/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004248/// dynamic-exception-specification
4249/// noexcept-specification
4250///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004251void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004252 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004253 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004254 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004255 assert(getCurScope()->isFunctionPrototypeScope() &&
4256 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004257 // lparen is already consumed!
4258 assert(D.isPastIdentifier() && "Should not call before identifier!");
4259
4260 // This should be true when the function has typed arguments.
4261 // Otherwise, it is treated as a K&R-style function.
4262 bool HasProto = false;
4263 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004264 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004265 // Remember where we see an ellipsis, if any.
4266 SourceLocation EllipsisLoc;
4267
4268 DeclSpec DS(AttrFactory);
4269 bool RefQualifierIsLValueRef = true;
4270 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004271 SourceLocation ConstQualifierLoc;
4272 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004273 ExceptionSpecificationType ESpecType = EST_None;
4274 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004275 SmallVector<ParsedType, 2> DynamicExceptions;
4276 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004277 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004278 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004279 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004280
James Molloy16f1f712012-02-29 10:24:19 +00004281 Actions.ActOnStartFunctionDeclarator();
4282
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004283 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004284 if (isFunctionDeclaratorIdentifierList()) {
4285 if (RequiresArg)
4286 Diag(Tok, diag::err_argument_required_after_attribute);
4287
4288 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4289
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004290 Tracker.consumeClose();
4291 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004292 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004293 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004294 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004295 else if (RequiresArg)
4296 Diag(Tok, diag::err_argument_required_after_attribute);
4297
David Blaikie4e4d0842012-03-11 07:00:24 +00004298 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004299
4300 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004301 Tracker.consumeClose();
4302 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004303
David Blaikie4e4d0842012-03-11 07:00:24 +00004304 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004305 // FIXME: Accept these components in any order, and produce fixits to
4306 // correct the order if the user gets it wrong. Ideally we should deal
4307 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004308
4309 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004310 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4311 if (!DS.getSourceRange().getEnd().isInvalid()) {
4312 EndLoc = DS.getSourceRange().getEnd();
4313 ConstQualifierLoc = DS.getConstSpecLoc();
4314 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4315 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004316
4317 // Parse ref-qualifier[opt].
4318 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004319 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004320 diag::warn_cxx98_compat_ref_qualifier :
4321 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004322
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004323 RefQualifierIsLValueRef = Tok.is(tok::amp);
4324 RefQualifierLoc = ConsumeToken();
4325 EndLoc = RefQualifierLoc;
4326 }
4327
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004328 // C++11 [expr.prim.general]p3:
4329 // If a declaration declares a member function or member function
4330 // template of a class X, the expression this is a prvalue of type
4331 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
4332 // and the end of the function-definition, member-declarator, or
4333 // declarator.
4334 bool IsCXX11MemberFunction =
4335 getLangOpts().CPlusPlus0x &&
4336 (D.getContext() == Declarator::MemberContext ||
4337 (D.getContext() == Declarator::FileContext &&
4338 D.getCXXScopeSpec().isValid() &&
4339 Actions.CurContext->isRecord()));
4340 Sema::CXXThisScopeRAII ThisScope(Actions,
4341 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4342 DS.getTypeQualifiers(),
4343 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004344
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004345 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004346 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004347 DynamicExceptions,
4348 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004349 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004350 if (ESpecType != EST_None)
4351 EndLoc = ESpecRange.getEnd();
4352
Richard Smith6ee326a2012-04-10 01:32:12 +00004353 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4354 // after the exception-specification.
4355 MaybeParseCXX0XAttributes(FnAttrs);
4356
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004357 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004358 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004359 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004360 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004361 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004362 if (Range.getEnd().isValid())
4363 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004364 }
4365 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004366 }
4367
4368 // Remember that we parsed a function type, and remember the attributes.
4369 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4370 /*isVariadic=*/EllipsisLoc.isValid(),
4371 EllipsisLoc,
4372 ParamInfo.data(), ParamInfo.size(),
4373 DS.getTypeQualifiers(),
4374 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004375 RefQualifierLoc, ConstQualifierLoc,
4376 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004377 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004378 ESpecType, ESpecRange.getBegin(),
4379 DynamicExceptions.data(),
4380 DynamicExceptionRanges.data(),
4381 DynamicExceptions.size(),
4382 NoexceptExpr.isUsable() ?
4383 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004384 Tracker.getOpenLocation(),
4385 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004386 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004387 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004388
4389 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004390}
4391
4392/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4393/// identifier list form for a K&R-style function: void foo(a,b,c)
4394///
4395/// Note that identifier-lists are only allowed for normal declarators, not for
4396/// abstract-declarators.
4397bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004398 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004399 && Tok.is(tok::identifier)
4400 && !TryAltiVecVectorToken()
4401 // K&R identifier lists can't have typedefs as identifiers, per C99
4402 // 6.7.5.3p11.
4403 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4404 // Identifier lists follow a really simple grammar: the identifiers can
4405 // be followed *only* by a ", identifier" or ")". However, K&R
4406 // identifier lists are really rare in the brave new modern world, and
4407 // it is very common for someone to typo a type in a non-K&R style
4408 // list. If we are presented with something like: "void foo(intptr x,
4409 // float y)", we don't want to start parsing the function declarator as
4410 // though it is a K&R style declarator just because intptr is an
4411 // invalid type.
4412 //
4413 // To handle this, we check to see if the token after the first
4414 // identifier is a "," or ")". Only then do we parse it as an
4415 // identifier list.
4416 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4417}
4418
4419/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4420/// we found a K&R-style identifier list instead of a typed parameter list.
4421///
4422/// After returning, ParamInfo will hold the parsed parameters.
4423///
4424/// identifier-list: [C99 6.7.5]
4425/// identifier
4426/// identifier-list ',' identifier
4427///
4428void Parser::ParseFunctionDeclaratorIdentifierList(
4429 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004430 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004431 // If there was no identifier specified for the declarator, either we are in
4432 // an abstract-declarator, or we are in a parameter declarator which was found
4433 // to be abstract. In abstract-declarators, identifier lists are not valid:
4434 // diagnose this.
4435 if (!D.getIdentifier())
4436 Diag(Tok, diag::ext_ident_list_in_param);
4437
4438 // Maintain an efficient lookup of params we have seen so far.
4439 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4440
4441 while (1) {
4442 // If this isn't an identifier, report the error and skip until ')'.
4443 if (Tok.isNot(tok::identifier)) {
4444 Diag(Tok, diag::err_expected_ident);
4445 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4446 // Forget we parsed anything.
4447 ParamInfo.clear();
4448 return;
4449 }
4450
4451 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4452
4453 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4454 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4455 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4456
4457 // Verify that the argument identifier has not already been mentioned.
4458 if (!ParamsSoFar.insert(ParmII)) {
4459 Diag(Tok, diag::err_param_redefinition) << ParmII;
4460 } else {
4461 // Remember this identifier in ParamInfo.
4462 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4463 Tok.getLocation(),
4464 0));
4465 }
4466
4467 // Eat the identifier.
4468 ConsumeToken();
4469
4470 // The list continues if we see a comma.
4471 if (Tok.isNot(tok::comma))
4472 break;
4473 ConsumeToken();
4474 }
4475}
4476
4477/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4478/// after the opening parenthesis. This function will not parse a K&R-style
4479/// identifier list.
4480///
Richard Smith6ce48a72012-04-11 04:01:28 +00004481/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4482/// caller parsed those arguments immediately after the open paren - they should
4483/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004484///
4485/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4486/// be the location of the ellipsis, if any was parsed.
4487///
Reid Spencer5f016e22007-07-11 17:01:13 +00004488/// parameter-type-list: [C99 6.7.5]
4489/// parameter-list
4490/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004491/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004492///
4493/// parameter-list: [C99 6.7.5]
4494/// parameter-declaration
4495/// parameter-list ',' parameter-declaration
4496///
4497/// parameter-declaration: [C99 6.7.5]
4498/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004499/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004500/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004501/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004502/// declaration-specifiers abstract-declarator[opt]
4503/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004504/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004505/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004506/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004507///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004508void Parser::ParseParameterDeclarationClause(
4509 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004510 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004511 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004512 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004513
Chris Lattnerf97409f2008-04-06 06:57:35 +00004514 while (1) {
4515 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004516 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4517 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004518 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004519 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004520 }
Mike Stump1eb44332009-09-09 15:08:12 +00004521
Chris Lattnerf97409f2008-04-06 06:57:35 +00004522 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004523 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004524 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004525
Richard Smith6ce48a72012-04-11 04:01:28 +00004526 // Parse any C++11 attributes.
4527 MaybeParseCXX0XAttributes(DS.getAttributes());
4528
John McCall7f040a92010-12-24 02:08:15 +00004529 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004530 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004531 ParseMicrosoftAttributes(DS.getAttributes());
4532
4533 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004534
4535 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004536 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004537 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004538 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4539 // too much hassle.
4540 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004541
Chris Lattnere64c5492009-02-27 18:38:20 +00004542 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004543
Chris Lattnerf97409f2008-04-06 06:57:35 +00004544 // Parse the declarator. This is "PrototypeContext", because we must
4545 // accept either 'declarator' or 'abstract-declarator' here.
4546 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4547 ParseDeclarator(ParmDecl);
4548
4549 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004550 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004551
Chris Lattnerf97409f2008-04-06 06:57:35 +00004552 // Remember this parsed parameter in ParamInfo.
4553 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004554
Douglas Gregor72b505b2008-12-16 21:30:33 +00004555 // DefArgToks is used when the parsing of default arguments needs
4556 // to be delayed.
4557 CachedTokens *DefArgToks = 0;
4558
Chris Lattnerf97409f2008-04-06 06:57:35 +00004559 // If no parameter was specified, verify that *something* was specified,
4560 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004561 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4562 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004563 // Completely missing, emit error.
4564 Diag(DSStart, diag::err_missing_param);
4565 } else {
4566 // Otherwise, we have something. Add it and let semantic analysis try
4567 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004568
Chris Lattnerf97409f2008-04-06 06:57:35 +00004569 // Inform the actions module about the parameter declarator, so it gets
4570 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004571 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004572
4573 // Parse the default argument, if any. We parse the default
4574 // arguments in all dialects; the semantic analysis in
4575 // ActOnParamDefaultArgument will reject the default argument in
4576 // C.
4577 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004578 SourceLocation EqualLoc = Tok.getLocation();
4579
Chris Lattner04421082008-04-08 04:40:51 +00004580 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004581 if (D.getContext() == Declarator::MemberContext) {
4582 // If we're inside a class definition, cache the tokens
4583 // corresponding to the default argument. We'll actually parse
4584 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004585 // FIXME: Can we use a smart pointer for Toks?
4586 DefArgToks = new CachedTokens;
4587
Mike Stump1eb44332009-09-09 15:08:12 +00004588 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004589 /*StopAtSemi=*/true,
4590 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004591 delete DefArgToks;
4592 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004593 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004594 } else {
4595 // Mark the end of the default argument so that we know when to
4596 // stop when we parse it later on.
4597 Token DefArgEnd;
4598 DefArgEnd.startToken();
4599 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4600 DefArgEnd.setLocation(Tok.getLocation());
4601 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004602 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004603 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004604 }
Chris Lattner04421082008-04-08 04:40:51 +00004605 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004606 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004607 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004608
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004609 // The argument isn't actually potentially evaluated unless it is
4610 // used.
4611 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004612 Sema::PotentiallyEvaluatedIfUsed,
4613 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004614
Sebastian Redl84407ba2012-03-14 15:54:00 +00004615 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004616 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4617 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004618 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004619 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004620 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004621 if (DefArgResult.isInvalid()) {
4622 Actions.ActOnParamDefaultArgumentError(Param);
4623 SkipUntil(tok::comma, tok::r_paren, true, true);
4624 } else {
4625 // Inform the actions module about the default argument
4626 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004627 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004628 }
Chris Lattner04421082008-04-08 04:40:51 +00004629 }
4630 }
Mike Stump1eb44332009-09-09 15:08:12 +00004631
4632 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4633 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004634 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004635 }
4636
4637 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004638 if (Tok.isNot(tok::comma)) {
4639 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004640 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4641
David Blaikie4e4d0842012-03-11 07:00:24 +00004642 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004643 // We have ellipsis without a preceding ',', which is ill-formed
4644 // in C. Complain and provide the fix.
4645 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004646 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004647 }
4648 }
4649
4650 break;
4651 }
Mike Stump1eb44332009-09-09 15:08:12 +00004652
Chris Lattnerf97409f2008-04-06 06:57:35 +00004653 // Consume the comma.
4654 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004655 }
Mike Stump1eb44332009-09-09 15:08:12 +00004656
Chris Lattner66d28652008-04-06 06:34:08 +00004657}
Chris Lattneref4715c2008-04-06 05:45:57 +00004658
Reid Spencer5f016e22007-07-11 17:01:13 +00004659/// [C90] direct-declarator '[' constant-expression[opt] ']'
4660/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4661/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4662/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4663/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004664/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4665/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004666void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004667 if (CheckProhibitedCXX11Attribute())
4668 return;
4669
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004670 BalancedDelimiterTracker T(*this, tok::l_square);
4671 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004672
Chris Lattner378c7e42008-12-18 07:27:21 +00004673 // C array syntax has many features, but by-far the most common is [] and [4].
4674 // This code does a fast path to handle some of the most obvious cases.
4675 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004676 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004677 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004678 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004679
Chris Lattner378c7e42008-12-18 07:27:21 +00004680 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004681 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004682 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004683 T.getOpenLocation(),
4684 T.getCloseLocation()),
4685 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004686 return;
4687 } else if (Tok.getKind() == tok::numeric_constant &&
4688 GetLookAheadToken(1).is(tok::r_square)) {
4689 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004690 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004691 ConsumeToken();
4692
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004693 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004694 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004695 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004696
Chris Lattner378c7e42008-12-18 07:27:21 +00004697 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004698 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004699 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004700 T.getOpenLocation(),
4701 T.getCloseLocation()),
4702 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004703 return;
4704 }
Mike Stump1eb44332009-09-09 15:08:12 +00004705
Reid Spencer5f016e22007-07-11 17:01:13 +00004706 // If valid, this location is the position where we read the 'static' keyword.
4707 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004708 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004709 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004710
Reid Spencer5f016e22007-07-11 17:01:13 +00004711 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004712 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004713 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004714 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004715
Reid Spencer5f016e22007-07-11 17:01:13 +00004716 // If we haven't already read 'static', check to see if there is one after the
4717 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004718 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004719 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004720
Reid Spencer5f016e22007-07-11 17:01:13 +00004721 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4722 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004723 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004724
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004725 // Handle the case where we have '[*]' as the array size. However, a leading
4726 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4727 // the the token after the star is a ']'. Since stars in arrays are
4728 // infrequent, use of lookahead is not costly here.
4729 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004730 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004731
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004732 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004733 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004734 StaticLoc = SourceLocation(); // Drop the static.
4735 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004736 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004737 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004738 // Note, in C89, this production uses the constant-expr production instead
4739 // of assignment-expr. The only difference is that assignment-expr allows
4740 // things like '=' and '*='. Sema rejects these in C89 mode because they
4741 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004742
Douglas Gregore0762c92009-06-19 23:52:42 +00004743 // Parse the constant-expression or assignment-expression now (depending
4744 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004745 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004746 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004747 } else {
4748 EnterExpressionEvaluationContext Unevaluated(Actions,
4749 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004750 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004751 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004752 }
Mike Stump1eb44332009-09-09 15:08:12 +00004753
Reid Spencer5f016e22007-07-11 17:01:13 +00004754 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004755 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004756 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004757 // If the expression was invalid, skip it.
4758 SkipUntil(tok::r_square);
4759 return;
4760 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004761
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004762 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004763
John McCall0b7e6782011-03-24 11:26:52 +00004764 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004765 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004766
Chris Lattner378c7e42008-12-18 07:27:21 +00004767 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004768 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004769 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004770 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004771 T.getOpenLocation(),
4772 T.getCloseLocation()),
4773 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004774}
4775
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004776/// [GNU] typeof-specifier:
4777/// typeof ( expressions )
4778/// typeof ( type-name )
4779/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004780///
4781void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004782 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004783 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004784 SourceLocation StartLoc = ConsumeToken();
4785
John McCallcfb708c2010-01-13 20:03:27 +00004786 const bool hasParens = Tok.is(tok::l_paren);
4787
Eli Friedman71b8fb52012-01-21 01:01:51 +00004788 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4789
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004790 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004791 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004792 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004793 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4794 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004795 if (hasParens)
4796 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004797
4798 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004799 // FIXME: Not accurate, the range gets one token more than it should.
4800 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004801 else
4802 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004803
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004804 if (isCastExpr) {
4805 if (!CastTy) {
4806 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004807 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004808 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004809
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004810 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004811 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004812 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4813 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004814 DiagID, CastTy))
4815 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004816 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004817 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004818
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004819 // If we get here, the operand to the typeof was an expresion.
4820 if (Operand.isInvalid()) {
4821 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004822 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004823 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004824
Eli Friedman71b8fb52012-01-21 01:01:51 +00004825 // We might need to transform the operand if it is potentially evaluated.
4826 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4827 if (Operand.isInvalid()) {
4828 DS.SetTypeSpecError();
4829 return;
4830 }
4831
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004832 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004833 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004834 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4835 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004836 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004837 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004838}
Chris Lattner1b492422010-02-28 18:33:55 +00004839
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004840/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004841/// _Atomic ( type-name )
4842///
4843void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4844 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4845
4846 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004847 BalancedDelimiterTracker T(*this, tok::l_paren);
4848 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004849 SkipUntil(tok::r_paren);
4850 return;
4851 }
4852
4853 TypeResult Result = ParseTypeName();
4854 if (Result.isInvalid()) {
4855 SkipUntil(tok::r_paren);
4856 return;
4857 }
4858
4859 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004860 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004861
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004862 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004863 return;
4864
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004865 DS.setTypeofParensRange(T.getRange());
4866 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004867
4868 const char *PrevSpec = 0;
4869 unsigned DiagID;
4870 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4871 DiagID, Result.release()))
4872 Diag(StartLoc, DiagID) << PrevSpec;
4873}
4874
Chris Lattner1b492422010-02-28 18:33:55 +00004875
4876/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4877/// from TryAltiVecVectorToken.
4878bool Parser::TryAltiVecVectorTokenOutOfLine() {
4879 Token Next = NextToken();
4880 switch (Next.getKind()) {
4881 default: return false;
4882 case tok::kw_short:
4883 case tok::kw_long:
4884 case tok::kw_signed:
4885 case tok::kw_unsigned:
4886 case tok::kw_void:
4887 case tok::kw_char:
4888 case tok::kw_int:
4889 case tok::kw_float:
4890 case tok::kw_double:
4891 case tok::kw_bool:
4892 case tok::kw___pixel:
4893 Tok.setKind(tok::kw___vector);
4894 return true;
4895 case tok::identifier:
4896 if (Next.getIdentifierInfo() == Ident_pixel) {
4897 Tok.setKind(tok::kw___vector);
4898 return true;
4899 }
4900 return false;
4901 }
4902}
4903
4904bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4905 const char *&PrevSpec, unsigned &DiagID,
4906 bool &isInvalid) {
4907 if (Tok.getIdentifierInfo() == Ident_vector) {
4908 Token Next = NextToken();
4909 switch (Next.getKind()) {
4910 case tok::kw_short:
4911 case tok::kw_long:
4912 case tok::kw_signed:
4913 case tok::kw_unsigned:
4914 case tok::kw_void:
4915 case tok::kw_char:
4916 case tok::kw_int:
4917 case tok::kw_float:
4918 case tok::kw_double:
4919 case tok::kw_bool:
4920 case tok::kw___pixel:
4921 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4922 return true;
4923 case tok::identifier:
4924 if (Next.getIdentifierInfo() == Ident_pixel) {
4925 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4926 return true;
4927 }
4928 break;
4929 default:
4930 break;
4931 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004932 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004933 DS.isTypeAltiVecVector()) {
4934 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4935 return true;
4936 }
4937 return false;
4938}