blob: c3ffd9e135d151e311197304827afedbe694a960 [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();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000346 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
347 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000348 // FIXME: Support these properly!
349 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000350 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
351 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000352 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000353}
354
John McCall7f040a92010-12-24 02:08:15 +0000355void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000356 // Treat these like attributes
357 while (Tok.is(tok::kw___pascal)) {
358 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
359 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000360 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
361 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000362 }
John McCall7f040a92010-12-24 02:08:15 +0000363}
364
Peter Collingbournef315fa82011-02-14 01:42:53 +0000365void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
366 // Treat these like attributes
367 while (Tok.is(tok::kw___kernel)) {
368 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000369 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
370 AttrNameLoc, 0, AttrNameLoc, 0,
371 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000372 }
373}
374
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000375void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
376 SourceLocation Loc = Tok.getLocation();
377 switch(Tok.getKind()) {
378 // OpenCL qualifiers:
379 case tok::kw___private:
380 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000381 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000382 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000383 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000384 break;
385
386 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000387 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000388 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000389 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000390 break;
391
392 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000393 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000394 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000395 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000396 break;
397
398 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000399 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000400 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000401 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000402 break;
403
404 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000405 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000406 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000407 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000408 break;
409
410 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000411 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000412 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000413 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000414 break;
415
416 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000417 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000418 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000419 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000420 break;
421 default: break;
422 }
423}
424
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000425/// \brief Parse a version number.
426///
427/// version:
428/// simple-integer
429/// simple-integer ',' simple-integer
430/// simple-integer ',' simple-integer ',' simple-integer
431VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
432 Range = Tok.getLocation();
433
434 if (!Tok.is(tok::numeric_constant)) {
435 Diag(Tok, diag::err_expected_version);
436 SkipUntil(tok::comma, tok::r_paren, true, true, true);
437 return VersionTuple();
438 }
439
440 // Parse the major (and possibly minor and subminor) versions, which
441 // are stored in the numeric constant. We utilize a quirk of the
442 // lexer, which is that it handles something like 1.2.3 as a single
443 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000444 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000445 Buffer.resize(Tok.getLength()+1);
446 const char *ThisTokBegin = &Buffer[0];
447
448 // Get the spelling of the token, which eliminates trigraphs, etc.
449 bool Invalid = false;
450 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
451 if (Invalid)
452 return VersionTuple();
453
454 // Parse the major version.
455 unsigned AfterMajor = 0;
456 unsigned Major = 0;
457 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
458 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
459 ++AfterMajor;
460 }
461
462 if (AfterMajor == 0) {
463 Diag(Tok, diag::err_expected_version);
464 SkipUntil(tok::comma, tok::r_paren, true, true, true);
465 return VersionTuple();
466 }
467
468 if (AfterMajor == ActualLength) {
469 ConsumeToken();
470
471 // We only had a single version component.
472 if (Major == 0) {
473 Diag(Tok, diag::err_zero_version);
474 return VersionTuple();
475 }
476
477 return VersionTuple(Major);
478 }
479
480 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
481 Diag(Tok, diag::err_expected_version);
482 SkipUntil(tok::comma, tok::r_paren, true, true, true);
483 return VersionTuple();
484 }
485
486 // Parse the minor version.
487 unsigned AfterMinor = AfterMajor + 1;
488 unsigned Minor = 0;
489 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
490 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
491 ++AfterMinor;
492 }
493
494 if (AfterMinor == ActualLength) {
495 ConsumeToken();
496
497 // We had major.minor.
498 if (Major == 0 && Minor == 0) {
499 Diag(Tok, diag::err_zero_version);
500 return VersionTuple();
501 }
502
503 return VersionTuple(Major, Minor);
504 }
505
506 // If what follows is not a '.', we have a problem.
507 if (ThisTokBegin[AfterMinor] != '.') {
508 Diag(Tok, diag::err_expected_version);
509 SkipUntil(tok::comma, tok::r_paren, true, true, true);
510 return VersionTuple();
511 }
512
513 // Parse the subminor version.
514 unsigned AfterSubminor = AfterMinor + 1;
515 unsigned Subminor = 0;
516 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
517 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
518 ++AfterSubminor;
519 }
520
521 if (AfterSubminor != ActualLength) {
522 Diag(Tok, diag::err_expected_version);
523 SkipUntil(tok::comma, tok::r_paren, true, true, true);
524 return VersionTuple();
525 }
526 ConsumeToken();
527 return VersionTuple(Major, Minor, Subminor);
528}
529
530/// \brief Parse the contents of the "availability" attribute.
531///
532/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000533/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000534///
535/// platform:
536/// identifier
537///
538/// version-arg-list:
539/// version-arg
540/// version-arg ',' version-arg-list
541///
542/// version-arg:
543/// 'introduced' '=' version
544/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000545/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000546/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000547/// opt-message:
548/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000549void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
550 SourceLocation AvailabilityLoc,
551 ParsedAttributes &attrs,
552 SourceLocation *endLoc) {
553 SourceLocation PlatformLoc;
554 IdentifierInfo *Platform = 0;
555
556 enum { Introduced, Deprecated, Obsoleted, Unknown };
557 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000558 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000559
560 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000561 BalancedDelimiterTracker T(*this, tok::l_paren);
562 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000563 Diag(Tok, diag::err_expected_lparen);
564 return;
565 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000566
567 // Parse the platform name,
568 if (Tok.isNot(tok::identifier)) {
569 Diag(Tok, diag::err_availability_expected_platform);
570 SkipUntil(tok::r_paren);
571 return;
572 }
573 Platform = Tok.getIdentifierInfo();
574 PlatformLoc = ConsumeToken();
575
576 // Parse the ',' following the platform name.
577 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
578 return;
579
580 // If we haven't grabbed the pointers for the identifiers
581 // "introduced", "deprecated", and "obsoleted", do so now.
582 if (!Ident_introduced) {
583 Ident_introduced = PP.getIdentifierInfo("introduced");
584 Ident_deprecated = PP.getIdentifierInfo("deprecated");
585 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000586 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000587 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000588 }
589
590 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000591 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000592 do {
593 if (Tok.isNot(tok::identifier)) {
594 Diag(Tok, diag::err_availability_expected_change);
595 SkipUntil(tok::r_paren);
596 return;
597 }
598 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
599 SourceLocation KeywordLoc = ConsumeToken();
600
Douglas Gregorb53e4172011-03-26 03:35:55 +0000601 if (Keyword == Ident_unavailable) {
602 if (UnavailableLoc.isValid()) {
603 Diag(KeywordLoc, diag::err_availability_redundant)
604 << Keyword << SourceRange(UnavailableLoc);
605 }
606 UnavailableLoc = KeywordLoc;
607
608 if (Tok.isNot(tok::comma))
609 break;
610
611 ConsumeToken();
612 continue;
613 }
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000614
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000615 if (Tok.isNot(tok::equal)) {
616 Diag(Tok, diag::err_expected_equal_after)
617 << Keyword;
618 SkipUntil(tok::r_paren);
619 return;
620 }
621 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000622 if (Keyword == Ident_message) {
623 if (!isTokenStringLiteral()) {
624 Diag(Tok, diag::err_expected_string_literal);
625 SkipUntil(tok::r_paren);
626 return;
627 }
628 MessageExpr = ParseStringLiteralExpression();
629 break;
630 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000631
632 SourceRange VersionRange;
633 VersionTuple Version = ParseVersionTuple(VersionRange);
634
635 if (Version.empty()) {
636 SkipUntil(tok::r_paren);
637 return;
638 }
639
640 unsigned Index;
641 if (Keyword == Ident_introduced)
642 Index = Introduced;
643 else if (Keyword == Ident_deprecated)
644 Index = Deprecated;
645 else if (Keyword == Ident_obsoleted)
646 Index = Obsoleted;
647 else
648 Index = Unknown;
649
650 if (Index < Unknown) {
651 if (!Changes[Index].KeywordLoc.isInvalid()) {
652 Diag(KeywordLoc, diag::err_availability_redundant)
653 << Keyword
654 << SourceRange(Changes[Index].KeywordLoc,
655 Changes[Index].VersionRange.getEnd());
656 }
657
658 Changes[Index].KeywordLoc = KeywordLoc;
659 Changes[Index].Version = Version;
660 Changes[Index].VersionRange = VersionRange;
661 } else {
662 Diag(KeywordLoc, diag::err_availability_unknown_change)
663 << Keyword << VersionRange;
664 }
665
666 if (Tok.isNot(tok::comma))
667 break;
668
669 ConsumeToken();
670 } while (true);
671
672 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000673 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000674 return;
675
676 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000677 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000678
Douglas Gregorb53e4172011-03-26 03:35:55 +0000679 // The 'unavailable' availability cannot be combined with any other
680 // availability changes. Make sure that hasn't happened.
681 if (UnavailableLoc.isValid()) {
682 bool Complained = false;
683 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
684 if (Changes[Index].KeywordLoc.isValid()) {
685 if (!Complained) {
686 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
687 << SourceRange(Changes[Index].KeywordLoc,
688 Changes[Index].VersionRange.getEnd());
689 Complained = true;
690 }
691
692 // Clear out the availability.
693 Changes[Index] = AvailabilityChange();
694 }
695 }
696 }
697
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000698 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000699 attrs.addNew(&Availability,
700 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000701 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000702 Platform, PlatformLoc,
703 Changes[Introduced],
704 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000705 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000706 UnavailableLoc, MessageExpr.take(),
707 false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000708}
709
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000710
711// Late Parsed Attributes:
712// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
713
714void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
715
716void Parser::LateParsedClass::ParseLexedAttributes() {
717 Self->ParseLexedAttributes(*Class);
718}
719
720void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000721 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000722}
723
724/// Wrapper class which calls ParseLexedAttribute, after setting up the
725/// scope appropriately.
726void Parser::ParseLexedAttributes(ParsingClass &Class) {
727 // Deal with templates
728 // FIXME: Test cases to make sure this does the right thing for templates.
729 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
730 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
731 HasTemplateScope);
732 if (HasTemplateScope)
733 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
734
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000735 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000736 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000737 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000738 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
739 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
740
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000741 // Enter the scope of nested classes
742 if (!AlreadyHasClassScope)
743 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
744 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000745 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000746 // Allow 'this' within late-parsed attributes.
747 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
748 /*TypeQuals=*/0);
749
750 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
751 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
752 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000753 }
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000754
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000755 if (!AlreadyHasClassScope)
756 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
757 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000758}
759
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000760
761/// \brief Parse all attributes in LAs, and attach them to Decl D.
762void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
763 bool EnterScope, bool OnDefinition) {
764 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000765 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000766 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000767 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000768 }
769 LAs.clear();
770}
771
772
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000773/// \brief Finish parsing an attribute for which parsing was delayed.
774/// This will be called at the end of parsing a class declaration
775/// for each LateParsedAttribute. We consume the saved tokens and
776/// create an attribute with the arguments filled in. We add this
777/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000778void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
779 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000780 // Save the current token position.
781 SourceLocation OrigLoc = Tok.getLocation();
782
783 // Append the current token at the end of the new token stream so that it
784 // doesn't get lost.
785 LA.Toks.push_back(Tok);
786 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
787 // Consume the previously pushed token.
788 ConsumeAnyToken();
789
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000790 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
791 Diag(Tok, diag::warn_attribute_on_function_definition)
792 << LA.AttrName.getName();
793 }
794
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000795 ParsedAttributes Attrs(AttrFactory);
796 SourceLocation endLoc;
797
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000798 if (LA.Decls.size() == 1) {
799 Decl *D = LA.Decls[0];
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000800
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000801 // If the Decl is templatized, add template parameters to scope.
802 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
803 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
804 if (HasTemplateScope)
805 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000806
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000807 // If the Decl is on a function, add function parameters to the scope.
808 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
809 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
810 if (HasFunctionScope)
811 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
812
813 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
814
815 if (HasFunctionScope) {
816 Actions.ActOnExitFunctionContext();
817 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
818 }
819 if (HasTemplateScope) {
820 TempScope.Exit();
821 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000822 } else if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000823 // If there are multiple decls, then the decl cannot be within the
824 // function scope.
825 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000826 } else {
827 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000828 }
829
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000830 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
831 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
832 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000833
834 if (Tok.getLocation() != OrigLoc) {
835 // Due to a parsing error, we either went over the cached tokens or
836 // there are still cached tokens left, so we skip the leftover tokens.
837 // Since this is an uncommon situation that should be avoided, use the
838 // expensive isBeforeInTranslationUnit call.
839 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
840 OrigLoc))
841 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000842 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000843 }
844}
845
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000846/// \brief Wrapper around a case statement checking if AttrName is
847/// one of the thread safety attributes
848bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
849 return llvm::StringSwitch<bool>(AttrName)
850 .Case("guarded_by", true)
851 .Case("guarded_var", true)
852 .Case("pt_guarded_by", true)
853 .Case("pt_guarded_var", true)
854 .Case("lockable", true)
855 .Case("scoped_lockable", true)
856 .Case("no_thread_safety_analysis", true)
857 .Case("acquired_after", true)
858 .Case("acquired_before", true)
859 .Case("exclusive_lock_function", true)
860 .Case("shared_lock_function", true)
861 .Case("exclusive_trylock_function", true)
862 .Case("shared_trylock_function", true)
863 .Case("unlock_function", true)
864 .Case("lock_returned", true)
865 .Case("locks_excluded", true)
866 .Case("exclusive_locks_required", true)
867 .Case("shared_locks_required", true)
868 .Default(false);
869}
870
871/// \brief Parse the contents of thread safety attributes. These
872/// should always be parsed as an expression list.
873///
874/// We need to special case the parsing due to the fact that if the first token
875/// of the first argument is an identifier, the main parse loop will store
876/// that token as a "parameter" and the rest of
877/// the arguments will be added to a list of "arguments". However,
878/// subsequent tokens in the first argument are lost. We instead parse each
879/// argument as an expression and add all arguments to the list of "arguments".
880/// In future, we will take advantage of this special case to also
881/// deal with some argument scoping issues here (for example, referring to a
882/// function parameter in the attribute on that function).
883void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
884 SourceLocation AttrNameLoc,
885 ParsedAttributes &Attrs,
886 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000887 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000888
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000889 BalancedDelimiterTracker T(*this, tok::l_paren);
890 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000891
892 ExprVector ArgExprs(Actions);
893 bool ArgExprsOk = true;
894
895 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000896 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000897 ExprResult ArgExpr(ParseAssignmentExpression());
898 if (ArgExpr.isInvalid()) {
899 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000900 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000901 break;
902 } else {
903 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000904 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000905 if (Tok.isNot(tok::comma))
906 break;
907 ConsumeToken(); // Eat the comma, move to the next argument
908 }
909 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000910 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000911 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
912 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000913 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000914 if (EndLoc)
915 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000916}
917
Richard Smith6ee326a2012-04-10 01:32:12 +0000918/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
919/// of a C++11 attribute-specifier in a location where an attribute is not
920/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
921/// situation.
922///
923/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
924/// this doesn't appear to actually be an attribute-specifier, and the caller
925/// should try to parse it.
926bool Parser::DiagnoseProhibitedCXX11Attribute() {
927 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
928
929 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
930 case CAK_NotAttributeSpecifier:
931 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
932 return false;
933
934 case CAK_InvalidAttributeSpecifier:
935 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
936 return false;
937
938 case CAK_AttributeSpecifier:
939 // Parse and discard the attributes.
940 SourceLocation BeginLoc = ConsumeBracket();
941 ConsumeBracket();
942 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
943 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
944 SourceLocation EndLoc = ConsumeBracket();
945 Diag(BeginLoc, diag::err_attributes_not_allowed)
946 << SourceRange(BeginLoc, EndLoc);
947 return true;
948 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +0000949 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +0000950}
951
John McCall7f040a92010-12-24 02:08:15 +0000952void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
953 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
954 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000955}
956
Reid Spencer5f016e22007-07-11 17:01:13 +0000957/// ParseDeclaration - Parse a full 'declaration', which consists of
958/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000959/// 'Context' should be a Declarator::TheContext value. This returns the
960/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000961///
962/// declaration: [C99 6.7]
963/// block-declaration ->
964/// simple-declaration
965/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000966/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000967/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000968/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000969/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +0000970/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000971/// others... [FIXME]
972///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000973Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
974 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000975 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000976 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000977 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000978 // Must temporarily exit the objective-c container scope for
979 // parsing c none objective-c decls.
980 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000981
John McCalld226f652010-08-21 09:40:31 +0000982 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000983 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000984 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000985 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000986 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000987 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000988 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000989 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000990 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000991 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +0000992 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000993 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000994 SourceLocation InlineLoc = ConsumeToken();
995 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
996 break;
997 }
John McCall7f040a92010-12-24 02:08:15 +0000998 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000999 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001000 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001001 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001002 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001003 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001004 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001005 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001006 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001007 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001008 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001009 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001010 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001011 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001012 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001013 default:
John McCall7f040a92010-12-24 02:08:15 +00001014 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001015 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001016
Chris Lattner682bf922009-03-29 16:50:03 +00001017 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001018 // single decl, convert it now. Alias declarations can also declare a type;
1019 // include that too if it is present.
1020 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001021}
1022
1023/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1024/// declaration-specifiers init-declarator-list[opt] ';'
1025///[C90/C++]init-declarator-list ';' [TODO]
1026/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001027///
Richard Smithad762fc2011-04-14 22:09:26 +00001028/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
1029/// attribute-specifier-seq[opt] type-specifier-seq declarator
1030///
Chris Lattnercd147752009-03-29 17:27:48 +00001031/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001032/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001033///
1034/// If FRI is non-null, we might be parsing a for-range-declaration instead
1035/// of a simple-declaration. If we find that we are, we also parse the
1036/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001037Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
1038 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001039 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001040 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +00001041 bool RequireSemi,
1042 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001044 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001045 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001046
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001047 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001048 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001049
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1051 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001052 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001053 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001054 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001055 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001056 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001057 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001058 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001060
1061 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001062}
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Richard Smith0706df42011-10-19 21:33:05 +00001064/// Returns true if this might be the start of a declarator, or a common typo
1065/// for a declarator.
1066bool Parser::MightBeDeclarator(unsigned Context) {
1067 switch (Tok.getKind()) {
1068 case tok::annot_cxxscope:
1069 case tok::annot_template_id:
1070 case tok::caret:
1071 case tok::code_completion:
1072 case tok::coloncolon:
1073 case tok::ellipsis:
1074 case tok::kw___attribute:
1075 case tok::kw_operator:
1076 case tok::l_paren:
1077 case tok::star:
1078 return true;
1079
1080 case tok::amp:
1081 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001082 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001083
Richard Smith1c94c162012-01-09 22:31:44 +00001084 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001085 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001086 NextToken().is(tok::l_square);
1087
1088 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001089 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001090
Richard Smith0706df42011-10-19 21:33:05 +00001091 case tok::identifier:
1092 switch (NextToken().getKind()) {
1093 case tok::code_completion:
1094 case tok::coloncolon:
1095 case tok::comma:
1096 case tok::equal:
1097 case tok::equalequal: // Might be a typo for '='.
1098 case tok::kw_alignas:
1099 case tok::kw_asm:
1100 case tok::kw___attribute:
1101 case tok::l_brace:
1102 case tok::l_paren:
1103 case tok::l_square:
1104 case tok::less:
1105 case tok::r_brace:
1106 case tok::r_paren:
1107 case tok::r_square:
1108 case tok::semi:
1109 return true;
1110
1111 case tok::colon:
1112 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001113 // and in block scope it's probably a label. Inside a class definition,
1114 // this is a bit-field.
1115 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001116 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001117
1118 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001119 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001120
1121 default:
1122 return false;
1123 }
1124
1125 default:
1126 return false;
1127 }
1128}
1129
Richard Smith994d73f2012-04-11 20:59:20 +00001130/// Skip until we reach something which seems like a sensible place to pick
1131/// up parsing after a malformed declaration. This will sometimes stop sooner
1132/// than SkipUntil(tok::r_brace) would, but will never stop later.
1133void Parser::SkipMalformedDecl() {
1134 while (true) {
1135 switch (Tok.getKind()) {
1136 case tok::l_brace:
1137 // Skip until matching }, then stop. We've probably skipped over
1138 // a malformed class or function definition or similar.
1139 ConsumeBrace();
1140 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1141 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1142 // This declaration isn't over yet. Keep skipping.
1143 continue;
1144 }
1145 if (Tok.is(tok::semi))
1146 ConsumeToken();
1147 return;
1148
1149 case tok::l_square:
1150 ConsumeBracket();
1151 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1152 continue;
1153
1154 case tok::l_paren:
1155 ConsumeParen();
1156 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1157 continue;
1158
1159 case tok::r_brace:
1160 return;
1161
1162 case tok::semi:
1163 ConsumeToken();
1164 return;
1165
1166 case tok::kw_inline:
1167 // 'inline namespace' at the start of a line is almost certainly
1168 // a good place to pick back up parsing.
1169 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace))
1170 return;
1171 break;
1172
1173 case tok::kw_namespace:
1174 // 'namespace' at the start of a line is almost certainly a good
1175 // place to pick back up parsing.
1176 if (Tok.isAtStartOfLine())
1177 return;
1178 break;
1179
1180 case tok::eof:
1181 return;
1182
1183 default:
1184 break;
1185 }
1186
1187 ConsumeAnyToken();
1188 }
1189}
1190
John McCalld8ac0572009-11-03 19:26:08 +00001191/// ParseDeclGroup - Having concluded that this is either a function
1192/// definition or a group of object declarations, actually parse the
1193/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001194Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1195 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001196 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001197 SourceLocation *DeclEnd,
1198 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001199 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001200 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001201 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001202
John McCalld8ac0572009-11-03 19:26:08 +00001203 // Bail out if the first declarator didn't seem well-formed.
1204 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001205 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001206 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001207 }
Mike Stump1eb44332009-09-09 15:08:12 +00001208
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001209 // Save late-parsed attributes for now; they need to be parsed in the
1210 // appropriate function scope after the function Decl has been constructed.
1211 LateParsedAttrList LateParsedAttrs;
1212 if (D.isFunctionDeclarator())
1213 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1214
Chris Lattnerc82daef2010-07-11 22:24:20 +00001215 // Check to see if we have a function *definition* which must have a body.
1216 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1217 // Look at the next token to make sure that this isn't a function
1218 // declaration. We have to check this because __attribute__ might be the
1219 // start of a function definition in GCC-extended K&R C.
1220 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001221
Chris Lattner004659a2010-07-11 22:42:07 +00001222 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001223 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1224 Diag(Tok, diag::err_function_declared_typedef);
1225
1226 // Recover by treating the 'typedef' as spurious.
1227 DS.ClearStorageClassSpecs();
1228 }
1229
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001230 Decl *TheDecl =
1231 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001232 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001233 }
1234
1235 if (isDeclarationSpecifier()) {
1236 // If there is an invalid declaration specifier right after the function
1237 // prototype, then we must be in a missing semicolon case where this isn't
1238 // actually a body. Just fall through into the code that handles it as a
1239 // prototype, and let the top-level code handle the erroneous declspec
1240 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001241 } else {
1242 Diag(Tok, diag::err_expected_fn_body);
1243 SkipUntil(tok::semi);
1244 return DeclGroupPtrTy();
1245 }
1246 }
1247
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001248 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001249 return DeclGroupPtrTy();
1250
1251 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1252 // must parse and analyze the for-range-initializer before the declaration is
1253 // analyzed.
1254 if (FRI && Tok.is(tok::colon)) {
1255 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001256 if (Tok.is(tok::l_brace))
1257 FRI->RangeExpr = ParseBraceInitializer();
1258 else
1259 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001260 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1261 Actions.ActOnCXXForRangeDecl(ThisDecl);
1262 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001263 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001264 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1265 }
1266
Chris Lattner5f9e2722011-07-23 10:55:15 +00001267 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001268 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001269 if (LateParsedAttrs.size() > 0)
1270 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001271 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001272 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001273 DeclsInGroup.push_back(FirstDecl);
1274
Richard Smith0706df42011-10-19 21:33:05 +00001275 bool ExpectSemi = Context != Declarator::ForContext;
1276
John McCalld8ac0572009-11-03 19:26:08 +00001277 // If we don't have a comma, it is either the end of the list (a ';') or an
1278 // error, bail out.
1279 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001280 SourceLocation CommaLoc = ConsumeToken();
1281
1282 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1283 // This comma was followed by a line-break and something which can't be
1284 // the start of a declarator. The comma was probably a typo for a
1285 // semicolon.
1286 Diag(CommaLoc, diag::err_expected_semi_declaration)
1287 << FixItHint::CreateReplacement(CommaLoc, ";");
1288 ExpectSemi = false;
1289 break;
1290 }
John McCalld8ac0572009-11-03 19:26:08 +00001291
1292 // Parse the next declarator.
1293 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001294 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001295
1296 // Accept attributes in an init-declarator. In the first declarator in a
1297 // declaration, these would be part of the declspec. In subsequent
1298 // declarators, they become part of the declarator itself, so that they
1299 // don't apply to declarators after *this* one. Examples:
1300 // short __attribute__((common)) var; -> declspec
1301 // short var __attribute__((common)); -> declarator
1302 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001303 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001304
1305 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001306 if (!D.isInvalidType()) {
1307 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1308 D.complete(ThisDecl);
1309 if (ThisDecl)
1310 DeclsInGroup.push_back(ThisDecl);
1311 }
John McCalld8ac0572009-11-03 19:26:08 +00001312 }
1313
1314 if (DeclEnd)
1315 *DeclEnd = Tok.getLocation();
1316
Richard Smith0706df42011-10-19 21:33:05 +00001317 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001318 ExpectAndConsumeSemi(Context == Declarator::FileContext
1319 ? diag::err_invalid_token_after_toplevel_declarator
1320 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001321 // Okay, there was no semicolon and one was expected. If we see a
1322 // declaration specifier, just assume it was missing and continue parsing.
1323 // Otherwise things are very confused and we skip to recover.
1324 if (!isDeclarationSpecifier()) {
1325 SkipUntil(tok::r_brace, true, true);
1326 if (Tok.is(tok::semi))
1327 ConsumeToken();
1328 }
John McCalld8ac0572009-11-03 19:26:08 +00001329 }
1330
Douglas Gregor23c94db2010-07-02 17:43:08 +00001331 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001332 DeclsInGroup.data(),
1333 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001334}
1335
Richard Smithad762fc2011-04-14 22:09:26 +00001336/// Parse an optional simple-asm-expr and attributes, and attach them to a
1337/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001338bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001339 // If a simple-asm-expr is present, parse it.
1340 if (Tok.is(tok::kw_asm)) {
1341 SourceLocation Loc;
1342 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1343 if (AsmLabel.isInvalid()) {
1344 SkipUntil(tok::semi, true, true);
1345 return true;
1346 }
1347
1348 D.setAsmLabel(AsmLabel.release());
1349 D.SetRangeEnd(Loc);
1350 }
1351
1352 MaybeParseGNUAttributes(D);
1353 return false;
1354}
1355
Douglas Gregor1426e532009-05-12 21:31:51 +00001356/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1357/// declarator'. This method parses the remainder of the declaration
1358/// (including any attributes or initializer, among other things) and
1359/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001360///
Reid Spencer5f016e22007-07-11 17:01:13 +00001361/// init-declarator: [C99 6.7]
1362/// declarator
1363/// declarator '=' initializer
1364/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1365/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001366/// [C++] declarator initializer[opt]
1367///
1368/// [C++] initializer:
1369/// [C++] '=' initializer-clause
1370/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001371/// [C++0x] '=' 'default' [TODO]
1372/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001373/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001374///
1375/// According to the standard grammar, =default and =delete are function
1376/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001377///
John McCalld226f652010-08-21 09:40:31 +00001378Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001379 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001380 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001381 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Richard Smithad762fc2011-04-14 22:09:26 +00001383 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1384}
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Richard Smithad762fc2011-04-14 22:09:26 +00001386Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1387 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001388 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001389 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001390 switch (TemplateInfo.Kind) {
1391 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001392 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001393 break;
1394
1395 case ParsedTemplateInfo::Template:
1396 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001397 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001398 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001399 TemplateInfo.TemplateParams->data(),
1400 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001401 D);
1402 break;
1403
1404 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001405 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001406 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001407 TemplateInfo.ExternLoc,
1408 TemplateInfo.TemplateLoc,
1409 D);
1410 if (ThisRes.isInvalid()) {
1411 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001412 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001413 }
1414
1415 ThisDecl = ThisRes.get();
1416 break;
1417 }
1418 }
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Richard Smith34b41d92011-02-20 03:19:35 +00001420 bool TypeContainsAuto =
1421 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1422
Douglas Gregor1426e532009-05-12 21:31:51 +00001423 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001424 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001425 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001426 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001427 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001428 if (D.isFunctionDeclarator())
1429 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1430 << 1 /* delete */;
1431 else
1432 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001433 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001434 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001435 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1436 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001437 else
1438 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001439 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001440 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001441 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001442 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001443 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001444
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001445 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001446 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001447 cutOffParsing();
1448 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001449 }
1450
John McCall60d7b3a2010-08-24 06:29:42 +00001451 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001452
David Blaikie4e4d0842012-03-11 07:00:24 +00001453 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001454 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001455 ExitScope();
1456 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001457
Douglas Gregor1426e532009-05-12 21:31:51 +00001458 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001459 SkipUntil(tok::comma, true, true);
1460 Actions.ActOnInitializerError(ThisDecl);
1461 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001462 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1463 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001464 }
1465 } else if (Tok.is(tok::l_paren)) {
1466 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001467 BalancedDelimiterTracker T(*this, tok::l_paren);
1468 T.consumeOpen();
1469
Douglas Gregor1426e532009-05-12 21:31:51 +00001470 ExprVector Exprs(Actions);
1471 CommaLocsTy CommaLocs;
1472
David Blaikie4e4d0842012-03-11 07:00:24 +00001473 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001474 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001475 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001476 }
1477
Douglas Gregor1426e532009-05-12 21:31:51 +00001478 if (ParseExpressionList(Exprs, CommaLocs)) {
1479 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001480
David Blaikie4e4d0842012-03-11 07:00:24 +00001481 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001482 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001483 ExitScope();
1484 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001485 } else {
1486 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001487 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001488
1489 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1490 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001491
David Blaikie4e4d0842012-03-11 07:00:24 +00001492 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001493 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001494 ExitScope();
1495 }
1496
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001497 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1498 T.getCloseLocation(),
1499 move_arg(Exprs));
1500 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1501 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001502 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001503 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001504 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001505 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1506
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001507 if (D.getCXXScopeSpec().isSet()) {
1508 EnterScope(0);
1509 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1510 }
1511
1512 ExprResult Init(ParseBraceInitializer());
1513
1514 if (D.getCXXScopeSpec().isSet()) {
1515 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1516 ExitScope();
1517 }
1518
1519 if (Init.isInvalid()) {
1520 Actions.ActOnInitializerError(ThisDecl);
1521 } else
1522 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1523 /*DirectInit=*/true, TypeContainsAuto);
1524
Douglas Gregor1426e532009-05-12 21:31:51 +00001525 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001526 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001527 }
1528
Richard Smith483b9f32011-02-21 20:05:19 +00001529 Actions.FinalizeDeclaration(ThisDecl);
1530
Douglas Gregor1426e532009-05-12 21:31:51 +00001531 return ThisDecl;
1532}
1533
Reid Spencer5f016e22007-07-11 17:01:13 +00001534/// ParseSpecifierQualifierList
1535/// specifier-qualifier-list:
1536/// type-specifier specifier-qualifier-list[opt]
1537/// type-qualifier specifier-qualifier-list[opt]
1538/// [GNU] attributes specifier-qualifier-list[opt]
1539///
Richard Smith69730c12012-03-12 07:56:15 +00001540void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1541 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001542 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1543 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001544 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001545 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Reid Spencer5f016e22007-07-11 17:01:13 +00001547 // Validate declspec for type-name.
1548 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001549 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1550 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001551 Diag(Tok, diag::err_expected_type);
1552 DS.SetTypeSpecError();
1553 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1554 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001555 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001556 if (!DS.hasTypeSpecifier())
1557 DS.SetTypeSpecError();
1558 }
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Reid Spencer5f016e22007-07-11 17:01:13 +00001560 // Issue diagnostic and remove storage class if present.
1561 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1562 if (DS.getStorageClassSpecLoc().isValid())
1563 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1564 else
1565 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1566 DS.ClearStorageClassSpecs();
1567 }
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Reid Spencer5f016e22007-07-11 17:01:13 +00001569 // Issue diagnostic and remove function specfier if present.
1570 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001571 if (DS.isInlineSpecified())
1572 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1573 if (DS.isVirtualSpecified())
1574 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1575 if (DS.isExplicitSpecified())
1576 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001577 DS.ClearFunctionSpecs();
1578 }
Richard Smith69730c12012-03-12 07:56:15 +00001579
1580 // Issue diagnostic and remove constexpr specfier if present.
1581 if (DS.isConstexprSpecified()) {
1582 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1583 DS.ClearConstexprSpec();
1584 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001585}
1586
Chris Lattnerc199ab32009-04-12 20:42:31 +00001587/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1588/// specified token is valid after the identifier in a declarator which
1589/// immediately follows the declspec. For example, these things are valid:
1590///
1591/// int x [ 4]; // direct-declarator
1592/// int x ( int y); // direct-declarator
1593/// int(int x ) // direct-declarator
1594/// int x ; // simple-declaration
1595/// int x = 17; // init-declarator-list
1596/// int x , y; // init-declarator-list
1597/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001598/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001599/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001600///
1601/// This is not, because 'x' does not immediately follow the declspec (though
1602/// ')' happens to be valid anyway).
1603/// int (x)
1604///
1605static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1606 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1607 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001608 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001609}
1610
Chris Lattnere40c2952009-04-14 21:34:55 +00001611
1612/// ParseImplicitInt - This method is called when we have an non-typename
1613/// identifier in a declspec (which normally terminates the decl spec) when
1614/// the declspec has no type specifier. In this case, the declspec is either
1615/// malformed or is "implicit int" (in K&R and C89).
1616///
1617/// This method handles diagnosing this prettily and returns false if the
1618/// declspec is done being processed. If it recovers and thinks there may be
1619/// other pieces of declspec after it, it returns true.
1620///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001621bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001622 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001623 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001624 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Chris Lattnere40c2952009-04-14 21:34:55 +00001626 SourceLocation Loc = Tok.getLocation();
1627 // If we see an identifier that is not a type name, we normally would
1628 // parse it as the identifer being declared. However, when a typename
1629 // is typo'd or the definition is not included, this will incorrectly
1630 // parse the typename as the identifier name and fall over misparsing
1631 // later parts of the diagnostic.
1632 //
1633 // As such, we try to do some look-ahead in cases where this would
1634 // otherwise be an "implicit-int" case to see if this is invalid. For
1635 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1636 // an identifier with implicit int, we'd get a parse error because the
1637 // next token is obviously invalid for a type. Parse these as a case
1638 // with an invalid type specifier.
1639 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001640
Chris Lattnere40c2952009-04-14 21:34:55 +00001641 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001642 // error, do lookahead to try to do better recovery. This never applies
1643 // within a type specifier. Outside of C++, we allow this even if the
1644 // language doesn't "officially" support implicit int -- we support
1645 // implicit int as an extension in C99 and C11. Allegedly, MS also
1646 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001647 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001648 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001649 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001650 // If this token is valid for implicit int, e.g. "static x = 4", then
1651 // we just avoid eating the identifier, so it will be parsed as the
1652 // identifier in the declarator.
1653 return false;
1654 }
Mike Stump1eb44332009-09-09 15:08:12 +00001655
Richard Smith827adaf2012-05-15 21:01:51 +00001656 if (getLangOpts().CPlusPlus &&
1657 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1658 // Don't require a type specifier if we have the 'auto' storage class
1659 // specifier in C++98 -- we'll promote it to a type specifier.
1660 return false;
1661 }
1662
Chris Lattnere40c2952009-04-14 21:34:55 +00001663 // Otherwise, if we don't consume this token, we are going to emit an
1664 // error anyway. Try to recover from various common problems. Check
1665 // to see if this was a reference to a tag name without a tag specified.
1666 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001667 //
1668 // C++ doesn't need this, and isTagName doesn't take SS.
1669 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001670 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001671 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Douglas Gregor23c94db2010-07-02 17:43:08 +00001673 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001674 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001675 case DeclSpec::TST_enum:
1676 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1677 case DeclSpec::TST_union:
1678 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1679 case DeclSpec::TST_struct:
1680 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1681 case DeclSpec::TST_class:
1682 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001683 }
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Chris Lattnerf4382f52009-04-14 22:17:06 +00001685 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001686 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1687 LookupResult R(Actions, TokenName, SourceLocation(),
1688 Sema::LookupOrdinaryName);
1689
Chris Lattnerf4382f52009-04-14 22:17:06 +00001690 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001691 << TokenName << TagName << getLangOpts().CPlusPlus
1692 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1693
1694 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1695 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1696 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001697 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001698 << TokenName << TagName;
1699 }
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Chris Lattnerf4382f52009-04-14 22:17:06 +00001701 // Parse this as a tag as if the missing tag were present.
1702 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001703 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001704 else
Richard Smith69730c12012-03-12 07:56:15 +00001705 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1706 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001707 return true;
1708 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001709 }
Mike Stump1eb44332009-09-09 15:08:12 +00001710
Richard Smith8f0a7e72012-05-15 21:29:55 +00001711 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001712 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001713 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1714 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001715 // Look ahead to the next token to try to figure out what this declaration
1716 // was supposed to be.
1717 switch (NextToken().getKind()) {
1718 case tok::comma:
1719 case tok::equal:
1720 case tok::kw_asm:
1721 case tok::l_brace:
1722 case tok::l_square:
1723 case tok::semi:
1724 // This looks like a variable declaration. The type is probably missing.
1725 // We're done parsing decl-specifiers.
1726 return false;
1727
1728 case tok::l_paren: {
1729 // static x(4); // 'x' is not a type
1730 // x(int n); // 'x' is not a type
1731 // x (*p)[]; // 'x' is a type
1732 //
1733 // Since we're in an error case (or the rare 'implicit int in C++' MS
1734 // extension), we can afford to perform a tentative parse to determine
1735 // which case we're in.
1736 TentativeParsingAction PA(*this);
1737 ConsumeToken();
1738 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1739 PA.Revert();
1740 if (TPR == TPResult::False())
1741 return false;
1742 // The identifier is followed by a parenthesized declarator.
1743 // It's supposed to be a type.
1744 break;
1745 }
1746
1747 default:
1748 // This is probably supposed to be a type. This includes cases like:
1749 // int f(itn);
1750 // struct S { unsinged : 4; };
1751 break;
1752 }
1753 }
1754
Douglas Gregora786fdb2009-10-13 23:27:22 +00001755 // This is almost certainly an invalid type name. Let the action emit a
1756 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001757 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001758 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001759 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001760 // The action emitted a diagnostic, so we don't have to.
1761 if (T) {
1762 // The action has suggested that the type T could be used. Set that as
1763 // the type in the declaration specifiers, consume the would-be type
1764 // name token, and we're done.
1765 const char *PrevSpec;
1766 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001767 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001768 DS.SetRangeEnd(Tok.getLocation());
1769 ConsumeToken();
1770
1771 // There may be other declaration specifiers after this.
1772 return true;
1773 }
1774
1775 // Fall through; the action had no suggestion for us.
1776 } else {
1777 // The action did not emit a diagnostic, so emit one now.
1778 SourceRange R;
1779 if (SS) R = SS->getRange();
1780 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1781 }
Mike Stump1eb44332009-09-09 15:08:12 +00001782
Douglas Gregora786fdb2009-10-13 23:27:22 +00001783 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001784 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001785 DS.SetRangeEnd(Tok.getLocation());
1786 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Chris Lattnere40c2952009-04-14 21:34:55 +00001788 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1789 // avoid rippling error messages on subsequent uses of the same type,
1790 // could be useful if #include was forgotten.
1791 return false;
1792}
1793
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001794/// \brief Determine the declaration specifier context from the declarator
1795/// context.
1796///
1797/// \param Context the declarator context, which is one of the
1798/// Declarator::TheContext enumerator values.
1799Parser::DeclSpecContext
1800Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1801 if (Context == Declarator::MemberContext)
1802 return DSC_class;
1803 if (Context == Declarator::FileContext)
1804 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001805 if (Context == Declarator::TrailingReturnContext)
1806 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001807 return DSC_normal;
1808}
1809
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001810/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1811///
1812/// FIXME: Simply returns an alignof() expression if the argument is a
1813/// type. Ideally, the type should be propagated directly into Sema.
1814///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001815/// [C11] type-id
1816/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001817/// [C++0x] type-id ...[opt]
1818/// [C++0x] assignment-expression ...[opt]
1819ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1820 SourceLocation &EllipsisLoc) {
1821 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001822 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001823 SourceLocation TypeLoc = Tok.getLocation();
1824 ParsedType Ty = ParseTypeName().get();
1825 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001826 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1827 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001828 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001829 ER = ParseConstantExpression();
1830
David Blaikie4e4d0842012-03-11 07:00:24 +00001831 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001832 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001833
1834 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001835}
1836
1837/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1838/// attribute to Attrs.
1839///
1840/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001841/// [C11] '_Alignas' '(' type-id ')'
1842/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001843/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1844/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001845void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1846 SourceLocation *endLoc) {
1847 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1848 "Not an alignment-specifier!");
1849
1850 SourceLocation KWLoc = Tok.getLocation();
1851 ConsumeToken();
1852
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001853 BalancedDelimiterTracker T(*this, tok::l_paren);
1854 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001855 return;
1856
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001857 SourceLocation EllipsisLoc;
1858 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001859 if (ArgExpr.isInvalid()) {
1860 SkipUntil(tok::r_paren);
1861 return;
1862 }
1863
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001864 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001865 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001866 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001867
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001868 // FIXME: Handle pack-expansions here.
1869 if (EllipsisLoc.isValid()) {
1870 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1871 return;
1872 }
1873
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001874 ExprVector ArgExprs(Actions);
1875 ArgExprs.push_back(ArgExpr.release());
1876 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001877 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001878}
1879
Reid Spencer5f016e22007-07-11 17:01:13 +00001880/// ParseDeclarationSpecifiers
1881/// declaration-specifiers: [C99 6.7]
1882/// storage-class-specifier declaration-specifiers[opt]
1883/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001884/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001885/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001886/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001887/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001888///
1889/// storage-class-specifier: [C99 6.7.1]
1890/// 'typedef'
1891/// 'extern'
1892/// 'static'
1893/// 'auto'
1894/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001895/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001896/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001897/// function-specifier: [C99 6.7.4]
1898/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001899/// [C++] 'virtual'
1900/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001901/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001902/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001903/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001904
Reid Spencer5f016e22007-07-11 17:01:13 +00001905///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001906void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001907 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001908 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001909 DeclSpecContext DSContext,
1910 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001911 if (DS.getSourceRange().isInvalid()) {
1912 DS.SetRangeStart(Tok.getLocation());
1913 DS.SetRangeEnd(Tok.getLocation());
1914 }
1915
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001916 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001917 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001918 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001919 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001920 unsigned DiagID = 0;
1921
Reid Spencer5f016e22007-07-11 17:01:13 +00001922 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001923
Reid Spencer5f016e22007-07-11 17:01:13 +00001924 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001925 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001926 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001927 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1928 MaybeParseCXX0XAttributes(DS.getAttributes());
1929
Reid Spencer5f016e22007-07-11 17:01:13 +00001930 // If this is not a declaration specifier token, we're done reading decl
1931 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001932 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001933 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001934
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001935 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001936 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001937 if (DS.hasTypeSpecifier()) {
1938 bool AllowNonIdentifiers
1939 = (getCurScope()->getFlags() & (Scope::ControlScope |
1940 Scope::BlockScope |
1941 Scope::TemplateParamScope |
1942 Scope::FunctionPrototypeScope |
1943 Scope::AtCatchScope)) == 0;
1944 bool AllowNestedNameSpecifiers
1945 = DSContext == DSC_top_level ||
1946 (DSContext == DSC_class && DS.isFriendSpecified());
1947
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001948 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1949 AllowNonIdentifiers,
1950 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001951 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001952 }
1953
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001954 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1955 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1956 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001957 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1958 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001959 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001960 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001961 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001962 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001963
1964 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001965 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001966 }
1967
Chris Lattner5e02c472009-01-05 00:07:25 +00001968 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001969 // C++ scope specifier. Annotate and loop, or bail out on error.
1970 if (TryAnnotateCXXScopeToken(true)) {
1971 if (!DS.hasTypeSpecifier())
1972 DS.SetTypeSpecError();
1973 goto DoneWithDeclSpec;
1974 }
John McCall2e0a7152010-03-01 18:20:46 +00001975 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1976 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001977 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001978
1979 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00001980 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001981 goto DoneWithDeclSpec;
1982
John McCallaa87d332009-12-12 11:40:51 +00001983 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001984 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1985 Tok.getAnnotationRange(),
1986 SS);
John McCallaa87d332009-12-12 11:40:51 +00001987
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001988 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001989 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001990 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001991 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001992 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001993 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001994
1995 // C++ [class.qual]p2:
1996 // In a lookup in which the constructor is an acceptable lookup
1997 // result and the nested-name-specifier nominates a class C:
1998 //
1999 // - if the name specified after the
2000 // nested-name-specifier, when looked up in C, is the
2001 // injected-class-name of C (Clause 9), or
2002 //
2003 // - if the name specified after the nested-name-specifier
2004 // is the same as the identifier or the
2005 // simple-template-id's template-name in the last
2006 // component of the nested-name-specifier,
2007 //
2008 // the name is instead considered to name the constructor of
2009 // class C.
2010 //
2011 // Thus, if the template-name is actually the constructor
2012 // name, then the code is ill-formed; this interpretation is
2013 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002014 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002015 if ((DSContext == DSC_top_level ||
2016 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2017 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002018 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002019 if (isConstructorDeclarator()) {
2020 // The user meant this to be an out-of-line constructor
2021 // definition, but template arguments are not allowed
2022 // there. Just allow this as a constructor; we'll
2023 // complain about it later.
2024 goto DoneWithDeclSpec;
2025 }
2026
2027 // The user meant this to name a type, but it actually names
2028 // a constructor with some extraneous template
2029 // arguments. Complain, then parse it as a type as the user
2030 // intended.
2031 Diag(TemplateId->TemplateNameLoc,
2032 diag::err_out_of_line_template_id_names_constructor)
2033 << TemplateId->Name;
2034 }
2035
John McCallaa87d332009-12-12 11:40:51 +00002036 DS.getTypeSpecScope() = SS;
2037 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002038 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002039 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002040 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002041 continue;
2042 }
2043
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002044 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002045 DS.getTypeSpecScope() = SS;
2046 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002047 if (Tok.getAnnotationValue()) {
2048 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002049 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2050 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002051 PrevSpec, DiagID, T);
2052 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002053 else
2054 DS.SetTypeSpecError();
2055 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2056 ConsumeToken(); // The typename
2057 }
2058
Douglas Gregor9135c722009-03-25 15:40:00 +00002059 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002060 goto DoneWithDeclSpec;
2061
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002062 // If we're in a context where the identifier could be a class name,
2063 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002064 if ((DSContext == DSC_top_level ||
2065 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002066 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002067 &SS)) {
2068 if (isConstructorDeclarator())
2069 goto DoneWithDeclSpec;
2070
2071 // As noted in C++ [class.qual]p2 (cited above), when the name
2072 // of the class is qualified in a context where it could name
2073 // a constructor, its a constructor name. However, we've
2074 // looked at the declarator, and the user probably meant this
2075 // to be a type. Complain that it isn't supposed to be treated
2076 // as a type, then proceed to parse it as a type.
2077 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2078 << Next.getIdentifierInfo();
2079 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002080
John McCallb3d87482010-08-24 05:47:05 +00002081 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2082 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002083 getCurScope(), &SS,
2084 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002085 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002086 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002087
Chris Lattnerf4382f52009-04-14 22:17:06 +00002088 // If the referenced identifier is not a type, then this declspec is
2089 // erroneous: We already checked about that it has no type specifier, and
2090 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002091 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002092 if (TypeRep == 0) {
2093 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002094 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002095 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002096 }
Mike Stump1eb44332009-09-09 15:08:12 +00002097
John McCallaa87d332009-12-12 11:40:51 +00002098 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002099 ConsumeToken(); // The C++ scope.
2100
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002101 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002102 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002103 if (isInvalid)
2104 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002105
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002106 DS.SetRangeEnd(Tok.getLocation());
2107 ConsumeToken(); // The typename.
2108
2109 continue;
2110 }
Mike Stump1eb44332009-09-09 15:08:12 +00002111
Chris Lattner80d0c892009-01-21 19:48:37 +00002112 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002113 if (Tok.getAnnotationValue()) {
2114 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002115 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002116 DiagID, T);
2117 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002118 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00002119
2120 if (isInvalid)
2121 break;
2122
Chris Lattner80d0c892009-01-21 19:48:37 +00002123 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2124 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002125
Chris Lattner80d0c892009-01-21 19:48:37 +00002126 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2127 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002128 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002129 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002130 ParseObjCProtocolQualifiers(DS);
2131
Chris Lattner80d0c892009-01-21 19:48:37 +00002132 continue;
2133 }
Mike Stump1eb44332009-09-09 15:08:12 +00002134
Douglas Gregorbfad9152011-04-28 15:48:45 +00002135 case tok::kw___is_signed:
2136 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2137 // typically treats it as a trait. If we see __is_signed as it appears
2138 // in libstdc++, e.g.,
2139 //
2140 // static const bool __is_signed;
2141 //
2142 // then treat __is_signed as an identifier rather than as a keyword.
2143 if (DS.getTypeSpecType() == TST_bool &&
2144 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2145 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2146 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2147 Tok.setKind(tok::identifier);
2148 }
2149
2150 // We're done with the declaration-specifiers.
2151 goto DoneWithDeclSpec;
2152
Chris Lattner3bd934a2008-07-26 01:18:38 +00002153 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002154 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002155 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002156 // In C++, check to see if this is a scope specifier like foo::bar::, if
2157 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002158 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002159 if (TryAnnotateCXXScopeToken(true)) {
2160 if (!DS.hasTypeSpecifier())
2161 DS.SetTypeSpecError();
2162 goto DoneWithDeclSpec;
2163 }
2164 if (!Tok.is(tok::identifier))
2165 continue;
2166 }
Mike Stump1eb44332009-09-09 15:08:12 +00002167
Chris Lattner3bd934a2008-07-26 01:18:38 +00002168 // This identifier can only be a typedef name if we haven't already seen
2169 // a type-specifier. Without this check we misparse:
2170 // typedef int X; struct Y { short X; }; as 'short int'.
2171 if (DS.hasTypeSpecifier())
2172 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002173
John Thompson82287d12010-02-05 00:12:22 +00002174 // Check for need to substitute AltiVec keyword tokens.
2175 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2176 break;
2177
Richard Smithf63eee72012-05-09 18:56:43 +00002178 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2179 // allow the use of a typedef name as a type specifier.
2180 if (DS.isTypeAltiVecVector())
2181 goto DoneWithDeclSpec;
2182
John McCallb3d87482010-08-24 05:47:05 +00002183 ParsedType TypeRep =
2184 Actions.getTypeName(*Tok.getIdentifierInfo(),
2185 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002186
Chris Lattnerc199ab32009-04-12 20:42:31 +00002187 // If this is not a typedef name, don't parse it as part of the declspec,
2188 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002189 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002190 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002191 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002192 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002193
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002194 // If we're in a context where the identifier could be a class name,
2195 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002196 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002197 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002198 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002199 goto DoneWithDeclSpec;
2200
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002201 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002202 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002203 if (isInvalid)
2204 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002205
Chris Lattner3bd934a2008-07-26 01:18:38 +00002206 DS.SetRangeEnd(Tok.getLocation());
2207 ConsumeToken(); // The identifier
2208
2209 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2210 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002211 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002212 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002213 ParseObjCProtocolQualifiers(DS);
2214
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002215 // Need to support trailing type qualifiers (e.g. "id<p> const").
2216 // If a type specifier follows, it will be diagnosed elsewhere.
2217 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002218 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002219
2220 // type-name
2221 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002222 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002223 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002224 // This template-id does not refer to a type name, so we're
2225 // done with the type-specifiers.
2226 goto DoneWithDeclSpec;
2227 }
2228
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002229 // If we're in a context where the template-id could be a
2230 // constructor name or specialization, check whether this is a
2231 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002232 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002233 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002234 isConstructorDeclarator())
2235 goto DoneWithDeclSpec;
2236
Douglas Gregor39a8de12009-02-25 19:37:18 +00002237 // Turn the template-id annotation token into a type annotation
2238 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002239 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002240 continue;
2241 }
2242
Reid Spencer5f016e22007-07-11 17:01:13 +00002243 // GNU attributes support.
2244 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002245 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002246 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002247
2248 // Microsoft declspec support.
2249 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002250 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002251 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002252
Steve Naroff239f0732008-12-25 14:16:32 +00002253 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002254 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002255 // FIXME: Add handling here!
2256 break;
2257
2258 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002259 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002260 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002261 case tok::kw___cdecl:
2262 case tok::kw___stdcall:
2263 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002264 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002265 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002266 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002267 continue;
2268
Dawn Perchik52fc3142010-09-03 01:29:35 +00002269 // Borland single token adornments.
2270 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002271 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002272 continue;
2273
Peter Collingbournef315fa82011-02-14 01:42:53 +00002274 // OpenCL single token adornments.
2275 case tok::kw___kernel:
2276 ParseOpenCLAttributes(DS.getAttributes());
2277 continue;
2278
Reid Spencer5f016e22007-07-11 17:01:13 +00002279 // storage-class-specifier
2280 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002281 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2282 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002283 break;
2284 case tok::kw_extern:
2285 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002286 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002287 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2288 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002289 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002290 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002291 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2292 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002293 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002294 case tok::kw_static:
2295 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002296 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002297 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2298 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002299 break;
2300 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002301 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002302 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002303 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2304 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002305 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002306 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002307 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002308 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002309 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2310 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002311 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002312 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2313 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002314 break;
2315 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002316 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2317 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002318 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002319 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002320 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2321 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002322 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002323 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002324 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002325 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002326
Reid Spencer5f016e22007-07-11 17:01:13 +00002327 // function-specifier
2328 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002329 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002330 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002331 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002332 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002333 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002334 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002335 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002336 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002337
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002338 // alignment-specifier
2339 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002340 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002341 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002342 ParseAlignmentSpecifier(DS.getAttributes());
2343 continue;
2344
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002345 // friend
2346 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002347 if (DSContext == DSC_class)
2348 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2349 else {
2350 PrevSpec = ""; // not actually used by the diagnostic
2351 DiagID = diag::err_friend_invalid_in_context;
2352 isInvalid = true;
2353 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002354 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002355
Douglas Gregor8d267c52011-09-09 02:06:17 +00002356 // Modules
2357 case tok::kw___module_private__:
2358 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2359 break;
2360
Sebastian Redl2ac67232009-11-05 15:47:02 +00002361 // constexpr
2362 case tok::kw_constexpr:
2363 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2364 break;
2365
Chris Lattner80d0c892009-01-21 19:48:37 +00002366 // type-specifier
2367 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002368 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2369 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002370 break;
2371 case tok::kw_long:
2372 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002373 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2374 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002375 else
John McCallfec54012009-08-03 20:12:06 +00002376 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2377 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002378 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002379 case tok::kw___int64:
2380 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2381 DiagID);
2382 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002383 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002384 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2385 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002386 break;
2387 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002388 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2389 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002390 break;
2391 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002392 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2393 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002394 break;
2395 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002396 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2397 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002398 break;
2399 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002400 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2401 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002402 break;
2403 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002404 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2405 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002406 break;
2407 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002408 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2409 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002410 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002411 case tok::kw___int128:
2412 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2413 DiagID);
2414 break;
2415 case tok::kw_half:
2416 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2417 DiagID);
2418 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002419 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002420 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2421 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002422 break;
2423 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002424 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2425 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002426 break;
2427 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002428 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2429 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002430 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002431 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002432 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2433 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002434 break;
2435 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002436 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2437 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002438 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002439 case tok::kw_bool:
2440 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002441 if (Tok.is(tok::kw_bool) &&
2442 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2443 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2444 PrevSpec = ""; // Not used by the diagnostic.
2445 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002446 // For better error recovery.
2447 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002448 isInvalid = true;
2449 } else {
2450 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2451 DiagID);
2452 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002453 break;
2454 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002455 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2456 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002457 break;
2458 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002459 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2460 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002461 break;
2462 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002463 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2464 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002465 break;
John Thompson82287d12010-02-05 00:12:22 +00002466 case tok::kw___vector:
2467 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2468 break;
2469 case tok::kw___pixel:
2470 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2471 break;
John McCalla5fc4722011-04-09 22:50:59 +00002472 case tok::kw___unknown_anytype:
2473 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2474 PrevSpec, DiagID);
2475 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002476
2477 // class-specifier:
2478 case tok::kw_class:
2479 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002480 case tok::kw_union: {
2481 tok::TokenKind Kind = Tok.getKind();
2482 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002483 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2484 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002485 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002486 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002487
2488 // enum-specifier:
2489 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002490 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002491 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002492 continue;
2493
2494 // cv-qualifier:
2495 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002496 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, 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_volatile:
John McCallfec54012009-08-03 20:12:06 +00002500 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002501 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002502 break;
2503 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002504 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002505 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002506 break;
2507
Douglas Gregord57959a2009-03-27 23:10:48 +00002508 // C++ typename-specifier:
2509 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002510 if (TryAnnotateTypeOrScopeToken()) {
2511 DS.SetTypeSpecError();
2512 goto DoneWithDeclSpec;
2513 }
2514 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002515 continue;
2516 break;
2517
Chris Lattner80d0c892009-01-21 19:48:37 +00002518 // GNU typeof support.
2519 case tok::kw_typeof:
2520 ParseTypeofSpecifier(DS);
2521 continue;
2522
David Blaikie42d6d0c2011-12-04 05:04:18 +00002523 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002524 ParseDecltypeSpecifier(DS);
2525 continue;
2526
Sean Huntdb5d44b2011-05-19 05:37:45 +00002527 case tok::kw___underlying_type:
2528 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002529 continue;
2530
2531 case tok::kw__Atomic:
2532 ParseAtomicSpecifier(DS);
2533 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002534
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002535 // OpenCL qualifiers:
2536 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002537 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002538 goto DoneWithDeclSpec;
2539 case tok::kw___private:
2540 case tok::kw___global:
2541 case tok::kw___local:
2542 case tok::kw___constant:
2543 case tok::kw___read_only:
2544 case tok::kw___write_only:
2545 case tok::kw___read_write:
2546 ParseOpenCLQualifiers(DS);
2547 break;
2548
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002549 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002550 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002551 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2552 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002553 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002554 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002555
Douglas Gregor46f936e2010-11-19 17:10:50 +00002556 if (!ParseObjCProtocolQualifiers(DS))
2557 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2558 << FixItHint::CreateInsertion(Loc, "id")
2559 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002560
2561 // Need to support trailing type qualifiers (e.g. "id<p> const").
2562 // If a type specifier follows, it will be diagnosed elsewhere.
2563 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002564 }
John McCallfec54012009-08-03 20:12:06 +00002565 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002566 if (isInvalid) {
2567 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002568 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002569
2570 if (DiagID == diag::ext_duplicate_declspec)
2571 Diag(Tok, DiagID)
2572 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2573 else
2574 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002575 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002576
Chris Lattner81c018d2008-03-13 06:29:04 +00002577 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002578 if (DiagID != diag::err_bool_redeclaration)
2579 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002580 }
2581}
Douglas Gregoradcac882008-12-01 23:54:00 +00002582
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002583/// ParseStructDeclaration - Parse a struct declaration without the terminating
2584/// semicolon.
2585///
Reid Spencer5f016e22007-07-11 17:01:13 +00002586/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002587/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002588/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002589/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002590/// struct-declarator-list:
2591/// struct-declarator
2592/// struct-declarator-list ',' struct-declarator
2593/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2594/// struct-declarator:
2595/// declarator
2596/// [GNU] declarator attributes[opt]
2597/// declarator[opt] ':' constant-expression
2598/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2599///
Chris Lattnere1359422008-04-10 06:46:29 +00002600void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002601ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002602
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002603 if (Tok.is(tok::kw___extension__)) {
2604 // __extension__ silences extension warnings in the subexpression.
2605 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002606 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002607 return ParseStructDeclaration(DS, Fields);
2608 }
Mike Stump1eb44332009-09-09 15:08:12 +00002609
Steve Naroff28a7ca82007-08-20 22:28:22 +00002610 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002611 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002612
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002613 // If there are no declarators, this is a free-standing declaration
2614 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002615 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002616 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002617 return;
2618 }
2619
2620 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002621 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002622 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002623 while (1) {
John McCall92576642012-05-07 06:16:41 +00002624 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCallbdd563e2009-11-03 02:38:08 +00002625 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002626 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002627
2628 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002629 if (!FirstDeclarator)
2630 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002631
Steve Naroff28a7ca82007-08-20 22:28:22 +00002632 /// struct-declarator: declarator
2633 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002634 if (Tok.isNot(tok::colon)) {
2635 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2636 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002637 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002638 }
Mike Stump1eb44332009-09-09 15:08:12 +00002639
Chris Lattner04d66662007-10-09 17:33:22 +00002640 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002641 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002642 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002643 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002644 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002645 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002646 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002647 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002648
Steve Naroff28a7ca82007-08-20 22:28:22 +00002649 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002650 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002651
John McCallbdd563e2009-11-03 02:38:08 +00002652 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002653 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002654 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002655
Steve Naroff28a7ca82007-08-20 22:28:22 +00002656 // If we don't have a comma, it is either the end of the list (a ';')
2657 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002658 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002659 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002660
Steve Naroff28a7ca82007-08-20 22:28:22 +00002661 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002662 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002663
John McCallbdd563e2009-11-03 02:38:08 +00002664 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002665 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002666}
2667
2668/// ParseStructUnionBody
2669/// struct-contents:
2670/// struct-declaration-list
2671/// [EXT] empty
2672/// [GNU] "struct-declaration-list" without terminatoring ';'
2673/// struct-declaration-list:
2674/// struct-declaration
2675/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002676/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002677///
Reid Spencer5f016e22007-07-11 17:01:13 +00002678void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002679 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002680 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2681 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002682
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002683 BalancedDelimiterTracker T(*this, tok::l_brace);
2684 if (T.consumeOpen())
2685 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002686
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002687 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002688 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002689
Reid Spencer5f016e22007-07-11 17:01:13 +00002690 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2691 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002692 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002693 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2694 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2695 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002696
Chris Lattner5f9e2722011-07-23 10:55:15 +00002697 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002698
Reid Spencer5f016e22007-07-11 17:01:13 +00002699 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002700 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002701 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002702
Reid Spencer5f016e22007-07-11 17:01:13 +00002703 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002704 if (Tok.is(tok::semi)) {
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002705 ConsumeExtraSemi(InsideStruct,
2706 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Reid Spencer5f016e22007-07-11 17:01:13 +00002707 continue;
2708 }
Chris Lattnere1359422008-04-10 06:46:29 +00002709
2710 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002711 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002712
John McCallbdd563e2009-11-03 02:38:08 +00002713 if (!Tok.is(tok::at)) {
2714 struct CFieldCallback : FieldCallback {
2715 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002716 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002717 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002718
John McCalld226f652010-08-21 09:40:31 +00002719 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002720 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002721 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2722
John McCalld226f652010-08-21 09:40:31 +00002723 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002724 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002725 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002726 FD.D.getDeclSpec().getSourceRange().getBegin(),
2727 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002728 FieldDecls.push_back(Field);
2729 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002730 }
John McCallbdd563e2009-11-03 02:38:08 +00002731 } Callback(*this, TagDecl, FieldDecls);
2732
2733 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002734 } else { // Handle @defs
2735 ConsumeToken();
2736 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2737 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002738 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002739 continue;
2740 }
2741 ConsumeToken();
2742 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2743 if (!Tok.is(tok::identifier)) {
2744 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002745 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002746 continue;
2747 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002748 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002749 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002750 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002751 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2752 ConsumeToken();
2753 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002754 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002755
Chris Lattner04d66662007-10-09 17:33:22 +00002756 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002757 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002758 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002759 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002760 break;
2761 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002762 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2763 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002764 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002765 // If we stopped at a ';', eat it.
2766 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002767 }
2768 }
Mike Stump1eb44332009-09-09 15:08:12 +00002769
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002770 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002771
John McCall0b7e6782011-03-24 11:26:52 +00002772 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002773 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002774 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002775
Douglas Gregor23c94db2010-07-02 17:43:08 +00002776 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002777 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002778 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002779 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002780 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002781 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2782 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002783}
2784
Reid Spencer5f016e22007-07-11 17:01:13 +00002785/// ParseEnumSpecifier
2786/// enum-specifier: [C99 6.7.2.2]
2787/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002788///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002789/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2790/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002791/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2792/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002793/// 'enum' identifier
2794/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002795///
Richard Smith1af83c42012-03-23 03:33:32 +00002796/// [C++11] enum-head '{' enumerator-list[opt] '}'
2797/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002798///
Richard Smith1af83c42012-03-23 03:33:32 +00002799/// enum-head: [C++11]
2800/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2801/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2802/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002803///
Richard Smith1af83c42012-03-23 03:33:32 +00002804/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002805/// 'enum'
2806/// 'enum' 'class'
2807/// 'enum' 'struct'
2808///
Richard Smith1af83c42012-03-23 03:33:32 +00002809/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002810/// ':' type-specifier-seq
2811///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002812/// [C++] elaborated-type-specifier:
2813/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2814///
Chris Lattner4c97d762009-04-12 21:49:30 +00002815void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002816 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002817 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002818 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002819 if (Tok.is(tok::code_completion)) {
2820 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002821 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002822 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002823 }
John McCall57c13002011-07-06 05:58:41 +00002824
Richard Smithbdad7a22012-01-10 01:33:14 +00002825 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002826 bool IsScopedUsingClassTag = false;
2827
David Blaikie4e4d0842012-03-11 07:00:24 +00002828 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002829 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002830 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002831 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002832 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002833 }
Richard Smith1af83c42012-03-23 03:33:32 +00002834
John McCall13489672012-05-07 06:16:58 +00002835 // C++11 [temp.explicit]p12:
2836 // The usual access controls do not apply to names used to specify
2837 // explicit instantiations.
2838 // We extend this to also cover explicit specializations. Note that
2839 // we don't suppress if this turns out to be an elaborated type
2840 // specifier.
2841 bool shouldDelayDiagsInTag =
2842 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2843 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2844 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00002845
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002846 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002847 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002848 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002849
Aaron Ballman6454a022012-03-01 04:09:28 +00002850 // If declspecs exist after tag, parse them.
2851 while (Tok.is(tok::kw___declspec))
2852 ParseMicrosoftDeclSpec(attrs);
2853
Richard Smith7796eb52012-03-12 08:56:40 +00002854 // Enum definitions should not be parsed in a trailing-return-type.
2855 bool AllowDeclaration = DSC != DSC_trailing;
2856
2857 bool AllowFixedUnderlyingType = AllowDeclaration &&
2858 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2859 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002860
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002861 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002862 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002863 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2864 // if a fixed underlying type is allowed.
2865 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2866
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002867 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2868 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002869 return;
2870
2871 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002872 Diag(Tok, diag::err_expected_ident);
2873 if (Tok.isNot(tok::l_brace)) {
2874 // Has no name and is not a definition.
2875 // Skip the rest of this declarator, up until the comma or semicolon.
2876 SkipUntil(tok::comma, true);
2877 return;
2878 }
2879 }
2880 }
Mike Stump1eb44332009-09-09 15:08:12 +00002881
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002882 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002883 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002884 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002885 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002886
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002887 // Skip the rest of this declarator, up until the comma or semicolon.
2888 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002889 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002890 }
Mike Stump1eb44332009-09-09 15:08:12 +00002891
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002892 // If an identifier is present, consume and remember it.
2893 IdentifierInfo *Name = 0;
2894 SourceLocation NameLoc;
2895 if (Tok.is(tok::identifier)) {
2896 Name = Tok.getIdentifierInfo();
2897 NameLoc = ConsumeToken();
2898 }
Mike Stump1eb44332009-09-09 15:08:12 +00002899
Richard Smithbdad7a22012-01-10 01:33:14 +00002900 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002901 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2902 // declaration of a scoped enumeration.
2903 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002904 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002905 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002906 }
2907
John McCall13489672012-05-07 06:16:58 +00002908 // Okay, end the suppression area. We'll decide whether to emit the
2909 // diagnostics in a second.
2910 if (shouldDelayDiagsInTag)
2911 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00002912
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002913 TypeResult BaseType;
2914
Douglas Gregora61b3e72010-12-01 17:42:47 +00002915 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002916 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002917 bool PossibleBitfield = false;
2918 if (getCurScope()->getFlags() & Scope::ClassScope) {
2919 // If we're in class scope, this can either be an enum declaration with
2920 // an underlying type, or a declaration of a bitfield member. We try to
2921 // use a simple disambiguation scheme first to catch the common cases
2922 // (integer literal, sizeof); if it's still ambiguous, we then consider
2923 // anything that's a simple-type-specifier followed by '(' as an
2924 // expression. This suffices because function types are not valid
2925 // underlying types anyway.
2926 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2927 // If the next token starts an expression, we know we're parsing a
2928 // bit-field. This is the common case.
2929 if (TPR == TPResult::True())
2930 PossibleBitfield = true;
2931 // If the next token starts a type-specifier-seq, it may be either a
2932 // a fixed underlying type or the start of a function-style cast in C++;
2933 // lookahead one more token to see if it's obvious that we have a
2934 // fixed underlying type.
2935 else if (TPR == TPResult::False() &&
2936 GetLookAheadToken(2).getKind() == tok::semi) {
2937 // Consume the ':'.
2938 ConsumeToken();
2939 } else {
2940 // We have the start of a type-specifier-seq, so we have to perform
2941 // tentative parsing to determine whether we have an expression or a
2942 // type.
2943 TentativeParsingAction TPA(*this);
2944
2945 // Consume the ':'.
2946 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002947
2948 // If we see a type specifier followed by an open-brace, we have an
2949 // ambiguity between an underlying type and a C++11 braced
2950 // function-style cast. Resolve this by always treating it as an
2951 // underlying type.
2952 // FIXME: The standard is not entirely clear on how to disambiguate in
2953 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002954 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002955 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002956 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002957 // We'll parse this as a bitfield later.
2958 PossibleBitfield = true;
2959 TPA.Revert();
2960 } else {
2961 // We have a type-specifier-seq.
2962 TPA.Commit();
2963 }
2964 }
2965 } else {
2966 // Consume the ':'.
2967 ConsumeToken();
2968 }
2969
2970 if (!PossibleBitfield) {
2971 SourceRange Range;
2972 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002973
David Blaikie4e4d0842012-03-11 07:00:24 +00002974 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002975 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2976 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002977 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002978 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002979 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002980 }
2981
Richard Smithbdad7a22012-01-10 01:33:14 +00002982 // There are four options here. If we have 'friend enum foo;' then this is a
2983 // friend declaration, and cannot have an accompanying definition. If we have
2984 // 'enum foo;', then this is a forward declaration. If we have
2985 // 'enum foo {...' then this is a definition. Otherwise we have something
2986 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002987 //
2988 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2989 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2990 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2991 //
John McCallf312b1e2010-08-26 23:41:50 +00002992 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00002993 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00002994 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00002995 } else if (Tok.is(tok::l_brace)) {
2996 if (DS.isFriendSpecified()) {
2997 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
2998 << SourceRange(DS.getFriendSpecLoc());
2999 ConsumeBrace();
3000 SkipUntil(tok::r_brace);
3001 TUK = Sema::TUK_Friend;
3002 } else {
3003 TUK = Sema::TUK_Definition;
3004 }
3005 } else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) {
3006 TUK = (DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration);
3007 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003008 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003009 }
3010
3011 // If this is an elaborated type specifier, and we delayed
3012 // diagnostics before, just merge them into the current pool.
3013 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3014 diagsFromTag.redelay();
3015 }
Richard Smith1af83c42012-03-23 03:33:32 +00003016
3017 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003018 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003019 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003020 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3021 // Skip the rest of this declarator, up until the comma or semicolon.
3022 Diag(Tok, diag::err_enum_template);
3023 SkipUntil(tok::comma, true);
3024 return;
3025 }
3026
3027 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3028 // Enumerations can't be explicitly instantiated.
3029 DS.SetTypeSpecError();
3030 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3031 return;
3032 }
3033
3034 assert(TemplateInfo.TemplateParams && "no template parameters");
3035 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3036 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003037 }
Richard Smith1af83c42012-03-23 03:33:32 +00003038
Douglas Gregorb9075602011-02-22 02:55:24 +00003039 if (!Name && TUK != Sema::TUK_Definition) {
3040 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003041
Douglas Gregorb9075602011-02-22 02:55:24 +00003042 // Skip the rest of this declarator, up until the comma or semicolon.
3043 SkipUntil(tok::comma, true);
3044 return;
3045 }
Richard Smith1af83c42012-03-23 03:33:32 +00003046
Douglas Gregor402abb52009-05-28 23:31:59 +00003047 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003048 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003049 const char *PrevSpec = 0;
3050 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003051 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003052 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003053 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003054 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003055 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003056
Douglas Gregor48c89f42010-04-24 16:38:41 +00003057 if (IsDependent) {
3058 // This enum has a dependent nested-name-specifier. Handle it as a
3059 // dependent tag.
3060 if (!Name) {
3061 DS.SetTypeSpecError();
3062 Diag(Tok, diag::err_expected_type_name_after_typename);
3063 return;
3064 }
3065
Douglas Gregor23c94db2010-07-02 17:43:08 +00003066 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003067 TUK, SS, Name, StartLoc,
3068 NameLoc);
3069 if (Type.isInvalid()) {
3070 DS.SetTypeSpecError();
3071 return;
3072 }
3073
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003074 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3075 NameLoc.isValid() ? NameLoc : StartLoc,
3076 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003077 Diag(StartLoc, DiagID) << PrevSpec;
3078
3079 return;
3080 }
Mike Stump1eb44332009-09-09 15:08:12 +00003081
John McCalld226f652010-08-21 09:40:31 +00003082 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003083 // The action failed to produce an enumeration tag. If this is a
3084 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003085 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003086 ConsumeBrace();
3087 SkipUntil(tok::r_brace);
3088 }
3089
3090 DS.SetTypeSpecError();
3091 return;
3092 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003093
Richard Smith7796eb52012-03-12 08:56:40 +00003094 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
John McCall13489672012-05-07 06:16:58 +00003095 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00003096 }
Mike Stump1eb44332009-09-09 15:08:12 +00003097
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003098 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3099 NameLoc.isValid() ? NameLoc : StartLoc,
3100 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003101 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003102}
3103
3104/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3105/// enumerator-list:
3106/// enumerator
3107/// enumerator-list ',' enumerator
3108/// enumerator:
3109/// enumeration-constant
3110/// enumeration-constant '=' constant-expression
3111/// enumeration-constant:
3112/// identifier
3113///
John McCalld226f652010-08-21 09:40:31 +00003114void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003115 // Enter the scope of the enum body and start the definition.
3116 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003117 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003118
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003119 BalancedDelimiterTracker T(*this, tok::l_brace);
3120 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003121
Chris Lattner7946dd32007-08-27 17:24:30 +00003122 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003123 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003124 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003125
Chris Lattner5f9e2722011-07-23 10:55:15 +00003126 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003127
John McCalld226f652010-08-21 09:40:31 +00003128 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003129
Reid Spencer5f016e22007-07-11 17:01:13 +00003130 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003131 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003132 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3133 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003134
John McCall5b629aa2010-10-22 23:36:17 +00003135 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003136 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003137 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003138
Reid Spencer5f016e22007-07-11 17:01:13 +00003139 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003140 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003141 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003142
Chris Lattner04d66662007-10-09 17:33:22 +00003143 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003144 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003145 AssignedVal = ParseConstantExpression();
3146 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003147 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003148 }
Mike Stump1eb44332009-09-09 15:08:12 +00003149
Reid Spencer5f016e22007-07-11 17:01:13 +00003150 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003151 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3152 LastEnumConstDecl,
3153 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003154 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003155 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003156 PD.complete(EnumConstDecl);
3157
Reid Spencer5f016e22007-07-11 17:01:13 +00003158 EnumConstantDecls.push_back(EnumConstDecl);
3159 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003160
Douglas Gregor751f6922010-09-07 14:51:08 +00003161 if (Tok.is(tok::identifier)) {
3162 // We're missing a comma between enumerators.
3163 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3164 Diag(Loc, diag::err_enumerator_list_missing_comma)
3165 << FixItHint::CreateInsertion(Loc, ", ");
3166 continue;
3167 }
3168
Chris Lattner04d66662007-10-09 17:33:22 +00003169 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003170 break;
3171 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003172
Richard Smith7fe62082011-10-15 05:09:34 +00003173 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003174 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003175 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003176 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003177 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003178 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003179 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3180 << FixItHint::CreateRemoval(CommaLoc);
3181 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003182 }
Mike Stump1eb44332009-09-09 15:08:12 +00003183
Reid Spencer5f016e22007-07-11 17:01:13 +00003184 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003185 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003186
Reid Spencer5f016e22007-07-11 17:01:13 +00003187 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003188 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003189 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003190
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003191 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3192 EnumDecl, EnumConstantDecls.data(),
3193 EnumConstantDecls.size(), getCurScope(),
3194 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003195
Douglas Gregor72de6672009-01-08 20:45:30 +00003196 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003197 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3198 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003199}
3200
3201/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003202/// start of a type-qualifier-list.
3203bool Parser::isTypeQualifier() const {
3204 switch (Tok.getKind()) {
3205 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003206
3207 // type-qualifier only in OpenCL
3208 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003209 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003210
Steve Naroff5f8aa692008-02-11 23:15:56 +00003211 // type-qualifier
3212 case tok::kw_const:
3213 case tok::kw_volatile:
3214 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003215 case tok::kw___private:
3216 case tok::kw___local:
3217 case tok::kw___global:
3218 case tok::kw___constant:
3219 case tok::kw___read_only:
3220 case tok::kw___read_write:
3221 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003222 return true;
3223 }
3224}
3225
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003226/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3227/// is definitely a type-specifier. Return false if it isn't part of a type
3228/// specifier or if we're not sure.
3229bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3230 switch (Tok.getKind()) {
3231 default: return false;
3232 // type-specifiers
3233 case tok::kw_short:
3234 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003235 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003236 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003237 case tok::kw_signed:
3238 case tok::kw_unsigned:
3239 case tok::kw__Complex:
3240 case tok::kw__Imaginary:
3241 case tok::kw_void:
3242 case tok::kw_char:
3243 case tok::kw_wchar_t:
3244 case tok::kw_char16_t:
3245 case tok::kw_char32_t:
3246 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003247 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003248 case tok::kw_float:
3249 case tok::kw_double:
3250 case tok::kw_bool:
3251 case tok::kw__Bool:
3252 case tok::kw__Decimal32:
3253 case tok::kw__Decimal64:
3254 case tok::kw__Decimal128:
3255 case tok::kw___vector:
3256
3257 // struct-or-union-specifier (C99) or class-specifier (C++)
3258 case tok::kw_class:
3259 case tok::kw_struct:
3260 case tok::kw_union:
3261 // enum-specifier
3262 case tok::kw_enum:
3263
3264 // typedef-name
3265 case tok::annot_typename:
3266 return true;
3267 }
3268}
3269
Steve Naroff5f8aa692008-02-11 23:15:56 +00003270/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003271/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003272bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003273 switch (Tok.getKind()) {
3274 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003275
Chris Lattner166a8fc2009-01-04 23:41:41 +00003276 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003277 if (TryAltiVecVectorToken())
3278 return true;
3279 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003280 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003281 // Annotate typenames and C++ scope specifiers. If we get one, just
3282 // recurse to handle whatever we get.
3283 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003284 return true;
3285 if (Tok.is(tok::identifier))
3286 return false;
3287 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003288
Chris Lattner166a8fc2009-01-04 23:41:41 +00003289 case tok::coloncolon: // ::foo::bar
3290 if (NextToken().is(tok::kw_new) || // ::new
3291 NextToken().is(tok::kw_delete)) // ::delete
3292 return false;
3293
Chris Lattner166a8fc2009-01-04 23:41:41 +00003294 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003295 return true;
3296 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003297
Reid Spencer5f016e22007-07-11 17:01:13 +00003298 // GNU attributes support.
3299 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003300 // GNU typeof support.
3301 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003302
Reid Spencer5f016e22007-07-11 17:01:13 +00003303 // type-specifiers
3304 case tok::kw_short:
3305 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003306 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003307 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003308 case tok::kw_signed:
3309 case tok::kw_unsigned:
3310 case tok::kw__Complex:
3311 case tok::kw__Imaginary:
3312 case tok::kw_void:
3313 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003314 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003315 case tok::kw_char16_t:
3316 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003317 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003318 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003319 case tok::kw_float:
3320 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003321 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003322 case tok::kw__Bool:
3323 case tok::kw__Decimal32:
3324 case tok::kw__Decimal64:
3325 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003326 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003327
Chris Lattner99dc9142008-04-13 18:59:07 +00003328 // struct-or-union-specifier (C99) or class-specifier (C++)
3329 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003330 case tok::kw_struct:
3331 case tok::kw_union:
3332 // enum-specifier
3333 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003334
Reid Spencer5f016e22007-07-11 17:01:13 +00003335 // type-qualifier
3336 case tok::kw_const:
3337 case tok::kw_volatile:
3338 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003339
3340 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003341 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003342 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003343
Chris Lattner7c186be2008-10-20 00:25:30 +00003344 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3345 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003346 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003347
Steve Naroff239f0732008-12-25 14:16:32 +00003348 case tok::kw___cdecl:
3349 case tok::kw___stdcall:
3350 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003351 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003352 case tok::kw___w64:
3353 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003354 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003355 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003356 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003357
3358 case tok::kw___private:
3359 case tok::kw___local:
3360 case tok::kw___global:
3361 case tok::kw___constant:
3362 case tok::kw___read_only:
3363 case tok::kw___read_write:
3364 case tok::kw___write_only:
3365
Eli Friedman290eeb02009-06-08 23:27:34 +00003366 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003367
3368 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003369 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003370
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003371 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003372 case tok::kw__Atomic:
3373 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003374 }
3375}
3376
3377/// isDeclarationSpecifier() - Return true if the current token is part of a
3378/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003379///
3380/// \param DisambiguatingWithExpression True to indicate that the purpose of
3381/// this check is to disambiguate between an expression and a declaration.
3382bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003383 switch (Tok.getKind()) {
3384 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003385
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003386 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003387 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003388
Chris Lattner166a8fc2009-01-04 23:41:41 +00003389 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003390 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003391 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003392 return false;
John Thompson82287d12010-02-05 00:12:22 +00003393 if (TryAltiVecVectorToken())
3394 return true;
3395 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003396 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003397 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003398 // Annotate typenames and C++ scope specifiers. If we get one, just
3399 // recurse to handle whatever we get.
3400 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003401 return true;
3402 if (Tok.is(tok::identifier))
3403 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003404
3405 // If we're in Objective-C and we have an Objective-C class type followed
3406 // by an identifier and then either ':' or ']', in a place where an
3407 // expression is permitted, then this is probably a class message send
3408 // missing the initial '['. In this case, we won't consider this to be
3409 // the start of a declaration.
3410 if (DisambiguatingWithExpression &&
3411 isStartOfObjCClassMessageMissingOpenBracket())
3412 return false;
3413
John McCall9ba61662010-02-26 08:45:28 +00003414 return isDeclarationSpecifier();
3415
Chris Lattner166a8fc2009-01-04 23:41:41 +00003416 case tok::coloncolon: // ::foo::bar
3417 if (NextToken().is(tok::kw_new) || // ::new
3418 NextToken().is(tok::kw_delete)) // ::delete
3419 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003420
Chris Lattner166a8fc2009-01-04 23:41:41 +00003421 // Annotate typenames and C++ scope specifiers. If we get one, just
3422 // recurse to handle whatever we get.
3423 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003424 return true;
3425 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003426
Reid Spencer5f016e22007-07-11 17:01:13 +00003427 // storage-class-specifier
3428 case tok::kw_typedef:
3429 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003430 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003431 case tok::kw_static:
3432 case tok::kw_auto:
3433 case tok::kw_register:
3434 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003435
Douglas Gregor8d267c52011-09-09 02:06:17 +00003436 // Modules
3437 case tok::kw___module_private__:
3438
Reid Spencer5f016e22007-07-11 17:01:13 +00003439 // type-specifiers
3440 case tok::kw_short:
3441 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003442 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003443 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003444 case tok::kw_signed:
3445 case tok::kw_unsigned:
3446 case tok::kw__Complex:
3447 case tok::kw__Imaginary:
3448 case tok::kw_void:
3449 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003450 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003451 case tok::kw_char16_t:
3452 case tok::kw_char32_t:
3453
Reid Spencer5f016e22007-07-11 17:01:13 +00003454 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003455 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003456 case tok::kw_float:
3457 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003458 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003459 case tok::kw__Bool:
3460 case tok::kw__Decimal32:
3461 case tok::kw__Decimal64:
3462 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003463 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003464
Chris Lattner99dc9142008-04-13 18:59:07 +00003465 // struct-or-union-specifier (C99) or class-specifier (C++)
3466 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003467 case tok::kw_struct:
3468 case tok::kw_union:
3469 // enum-specifier
3470 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003471
Reid Spencer5f016e22007-07-11 17:01:13 +00003472 // type-qualifier
3473 case tok::kw_const:
3474 case tok::kw_volatile:
3475 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003476
Reid Spencer5f016e22007-07-11 17:01:13 +00003477 // function-specifier
3478 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003479 case tok::kw_virtual:
3480 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003481
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003482 // static_assert-declaration
3483 case tok::kw__Static_assert:
3484
Chris Lattner1ef08762007-08-09 17:01:07 +00003485 // GNU typeof support.
3486 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003487
Chris Lattner1ef08762007-08-09 17:01:07 +00003488 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003489 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003490 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003491
Francois Pichete3d49b42011-06-19 08:02:06 +00003492 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003493 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003494 return true;
3495
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003496 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003497 case tok::kw__Atomic:
3498 return true;
3499
Chris Lattnerf3948c42008-07-26 03:38:44 +00003500 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3501 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003502 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003503
Douglas Gregord9d75e52011-04-27 05:41:15 +00003504 // typedef-name
3505 case tok::annot_typename:
3506 return !DisambiguatingWithExpression ||
3507 !isStartOfObjCClassMessageMissingOpenBracket();
3508
Steve Naroff47f52092009-01-06 19:34:12 +00003509 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003510 case tok::kw___cdecl:
3511 case tok::kw___stdcall:
3512 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003513 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003514 case tok::kw___w64:
3515 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003516 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003517 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003518 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003519 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003520
3521 case tok::kw___private:
3522 case tok::kw___local:
3523 case tok::kw___global:
3524 case tok::kw___constant:
3525 case tok::kw___read_only:
3526 case tok::kw___read_write:
3527 case tok::kw___write_only:
3528
Eli Friedman290eeb02009-06-08 23:27:34 +00003529 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003530 }
3531}
3532
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003533bool Parser::isConstructorDeclarator() {
3534 TentativeParsingAction TPA(*this);
3535
3536 // Parse the C++ scope specifier.
3537 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003538 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3539 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003540 TPA.Revert();
3541 return false;
3542 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003543
3544 // Parse the constructor name.
3545 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3546 // We already know that we have a constructor name; just consume
3547 // the token.
3548 ConsumeToken();
3549 } else {
3550 TPA.Revert();
3551 return false;
3552 }
3553
Richard Smith22592862012-03-27 23:05:05 +00003554 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003555 if (Tok.isNot(tok::l_paren)) {
3556 TPA.Revert();
3557 return false;
3558 }
3559 ConsumeParen();
3560
Richard Smith22592862012-03-27 23:05:05 +00003561 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3562 // that we have a constructor.
3563 if (Tok.is(tok::r_paren) ||
3564 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003565 TPA.Revert();
3566 return true;
3567 }
3568
3569 // If we need to, enter the specified scope.
3570 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003571 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003572 DeclScopeObj.EnterDeclaratorScope();
3573
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003574 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003575 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003576 MaybeParseMicrosoftAttributes(Attrs);
3577
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003578 // Check whether the next token(s) are part of a declaration
3579 // specifier, in which case we have the start of a parameter and,
3580 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003581 bool IsConstructor = false;
3582 if (isDeclarationSpecifier())
3583 IsConstructor = true;
3584 else if (Tok.is(tok::identifier) ||
3585 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3586 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3587 // This might be a parenthesized member name, but is more likely to
3588 // be a constructor declaration with an invalid argument type. Keep
3589 // looking.
3590 if (Tok.is(tok::annot_cxxscope))
3591 ConsumeToken();
3592 ConsumeToken();
3593
3594 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003595 // which must have one of the following syntactic forms (see the
3596 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003597 switch (Tok.getKind()) {
3598 case tok::l_paren:
3599 // C(X ( int));
3600 case tok::l_square:
3601 // C(X [ 5]);
3602 // C(X [ [attribute]]);
3603 case tok::coloncolon:
3604 // C(X :: Y);
3605 // C(X :: *p);
3606 case tok::r_paren:
3607 // C(X )
3608 // Assume this isn't a constructor, rather than assuming it's a
3609 // constructor with an unnamed parameter of an ill-formed type.
3610 break;
3611
3612 default:
3613 IsConstructor = true;
3614 break;
3615 }
3616 }
3617
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003618 TPA.Revert();
3619 return IsConstructor;
3620}
Reid Spencer5f016e22007-07-11 17:01:13 +00003621
3622/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003623/// type-qualifier-list: [C99 6.7.5]
3624/// type-qualifier
3625/// [vendor] attributes
3626/// [ only if VendorAttributesAllowed=true ]
3627/// type-qualifier-list type-qualifier
3628/// [vendor] type-qualifier-list attributes
3629/// [ only if VendorAttributesAllowed=true ]
3630/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3631/// [ only if CXX0XAttributesAllowed=true ]
3632/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003633///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003634void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3635 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003636 bool CXX11AttributesAllowed) {
3637 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003638 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003639 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003640 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003641 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003642 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003643
3644 SourceLocation EndLoc;
3645
Reid Spencer5f016e22007-07-11 17:01:13 +00003646 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003647 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003648 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003649 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003650 SourceLocation Loc = Tok.getLocation();
3651
3652 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003653 case tok::code_completion:
3654 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003655 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003656
Reid Spencer5f016e22007-07-11 17:01:13 +00003657 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003658 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , 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_volatile:
John McCallfec54012009-08-03 20:12:06 +00003662 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003663 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003664 break;
3665 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003666 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003667 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003668 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003669
3670 // OpenCL qualifiers:
3671 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003672 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003673 goto DoneWithTypeQuals;
3674 case tok::kw___private:
3675 case tok::kw___global:
3676 case tok::kw___local:
3677 case tok::kw___constant:
3678 case tok::kw___read_only:
3679 case tok::kw___write_only:
3680 case tok::kw___read_write:
3681 ParseOpenCLQualifiers(DS);
3682 break;
3683
Eli Friedman290eeb02009-06-08 23:27:34 +00003684 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003685 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003686 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003687 case tok::kw___cdecl:
3688 case tok::kw___stdcall:
3689 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003690 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003691 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003692 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003693 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003694 continue;
3695 }
3696 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003697 case tok::kw___pascal:
3698 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003699 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003700 continue;
3701 }
3702 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003703 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003704 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003705 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003706 continue; // do *not* consume the next token!
3707 }
3708 // otherwise, FALL THROUGH!
3709 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003710 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003711 // If this is not a type-qualifier token, we're done reading type
3712 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003713 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003714 if (EndLoc.isValid())
3715 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003716 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003717 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003718
Reid Spencer5f016e22007-07-11 17:01:13 +00003719 // If the specifier combination wasn't legal, issue a diagnostic.
3720 if (isInvalid) {
3721 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003722 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003723 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003724 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003725 }
3726}
3727
3728
3729/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3730///
3731void Parser::ParseDeclarator(Declarator &D) {
3732 /// This implements the 'declarator' production in the C grammar, then checks
3733 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003734 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003735}
3736
Richard Smith9988f282012-03-29 01:16:42 +00003737static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3738 if (Kind == tok::star || Kind == tok::caret)
3739 return true;
3740
3741 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3742 if (!Lang.CPlusPlus)
3743 return false;
3744
3745 return Kind == tok::amp || Kind == tok::ampamp;
3746}
3747
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003748/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3749/// is parsed by the function passed to it. Pass null, and the direct-declarator
3750/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003751/// ptr-operator production.
3752///
Richard Smith0706df42011-10-19 21:33:05 +00003753/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003754/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3755/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003756///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003757/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3758/// [C] pointer[opt] direct-declarator
3759/// [C++] direct-declarator
3760/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003761///
3762/// pointer: [C99 6.7.5]
3763/// '*' type-qualifier-list[opt]
3764/// '*' type-qualifier-list[opt] pointer
3765///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003766/// ptr-operator:
3767/// '*' cv-qualifier-seq[opt]
3768/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003769/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003770/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003771/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003772/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003773void Parser::ParseDeclaratorInternal(Declarator &D,
3774 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003775 if (Diags.hasAllExtensionsSilenced())
3776 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003777
Sebastian Redlf30208a2009-01-24 21:16:55 +00003778 // C++ member pointers start with a '::' or a nested-name.
3779 // Member pointers get special handling, since there's no place for the
3780 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003781 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003782 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3783 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003784 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3785 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003786 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003787 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003788
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003789 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003790 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003791 // The scope spec really belongs to the direct-declarator.
3792 D.getCXXScopeSpec() = SS;
3793 if (DirectDeclParser)
3794 (this->*DirectDeclParser)(D);
3795 return;
3796 }
3797
3798 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003799 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003800 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003801 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003802 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003803
3804 // Recurse to parse whatever is left.
3805 ParseDeclaratorInternal(D, DirectDeclParser);
3806
3807 // Sema will have to catch (syntactically invalid) pointers into global
3808 // scope. It has to catch pointers into namespace scope anyway.
3809 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003810 Loc),
3811 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003812 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003813 return;
3814 }
3815 }
3816
3817 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003818 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003819 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003820 if (DirectDeclParser)
3821 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003822 return;
3823 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003824
Sebastian Redl05532f22009-03-15 22:02:01 +00003825 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3826 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003827 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003828 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003829
Chris Lattner9af55002009-03-27 04:18:06 +00003830 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003831 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003832 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003833
Richard Smith6ee326a2012-04-10 01:32:12 +00003834 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00003835 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003836 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003837
Reid Spencer5f016e22007-07-11 17:01:13 +00003838 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003839 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003840 if (Kind == tok::star)
3841 // Remember that we parsed a pointer type, and remember the type-quals.
3842 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003843 DS.getConstSpecLoc(),
3844 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003845 DS.getRestrictSpecLoc()),
3846 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003847 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003848 else
3849 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003850 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003851 Loc),
3852 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003853 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003854 } else {
3855 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003856 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003857
Sebastian Redl743de1f2009-03-23 00:00:23 +00003858 // Complain about rvalue references in C++03, but then go on and build
3859 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003860 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003861 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003862 diag::warn_cxx98_compat_rvalue_reference :
3863 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003864
Richard Smith6ee326a2012-04-10 01:32:12 +00003865 // GNU-style and C++11 attributes are allowed here, as is restrict.
3866 ParseTypeQualifierListOpt(DS);
3867 D.ExtendWithDeclSpec(DS);
3868
Reid Spencer5f016e22007-07-11 17:01:13 +00003869 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3870 // cv-qualifiers are introduced through the use of a typedef or of a
3871 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00003872 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3873 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3874 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003875 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003876 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3877 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003878 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003879 }
3880
3881 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003882 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003883
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003884 if (D.getNumTypeObjects() > 0) {
3885 // C++ [dcl.ref]p4: There shall be no references to references.
3886 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3887 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003888 if (const IdentifierInfo *II = D.getIdentifier())
3889 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3890 << II;
3891 else
3892 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3893 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003894
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003895 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003896 // can go ahead and build the (technically ill-formed)
3897 // declarator: reference collapsing will take care of it.
3898 }
3899 }
3900
Reid Spencer5f016e22007-07-11 17:01:13 +00003901 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003902 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003903 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003904 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003905 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003906 }
3907}
3908
Richard Smith9988f282012-03-29 01:16:42 +00003909static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
3910 SourceLocation EllipsisLoc) {
3911 if (EllipsisLoc.isValid()) {
3912 FixItHint Insertion;
3913 if (!D.getEllipsisLoc().isValid()) {
3914 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
3915 D.setEllipsisLoc(EllipsisLoc);
3916 }
3917 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
3918 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
3919 }
3920}
3921
Reid Spencer5f016e22007-07-11 17:01:13 +00003922/// ParseDirectDeclarator
3923/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003924/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003925/// '(' declarator ')'
3926/// [GNU] '(' attributes declarator ')'
3927/// [C90] direct-declarator '[' constant-expression[opt] ']'
3928/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3929/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3930/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3931/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00003932/// [C++11] direct-declarator '[' constant-expression[opt] ']'
3933/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00003934/// direct-declarator '(' parameter-type-list ')'
3935/// direct-declarator '(' identifier-list[opt] ')'
3936/// [GNU] direct-declarator '(' parameter-forward-declarations
3937/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003938/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3939/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00003940/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
3941/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
3942/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003943/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00003944/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003945///
3946/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003947/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003948/// '::'[opt] nested-name-specifier[opt] type-name
3949///
3950/// id-expression: [C++ 5.1]
3951/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003952/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003953///
3954/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003955/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003956/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003957/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003958/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003959/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003960///
Richard Smith5d8388c2012-03-27 01:42:32 +00003961/// Note, any additional constructs added here may need corresponding changes
3962/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00003963void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003964 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003965
David Blaikie4e4d0842012-03-11 07:00:24 +00003966 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003967 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003968 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003969 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3970 D.getContext() == Declarator::MemberContext;
3971 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3972 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003973 }
3974
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003975 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003976 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003977 // Change the declaration context for name lookup, until this function
3978 // is exited (and the declarator has been parsed).
3979 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003980 }
3981
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003982 // C++0x [dcl.fct]p14:
3983 // There is a syntactic ambiguity when an ellipsis occurs at the end
3984 // of a parameter-declaration-clause without a preceding comma. In
3985 // this case, the ellipsis is parsed as part of the
3986 // abstract-declarator if the type of the parameter names a template
3987 // parameter pack that has not been expanded; otherwise, it is parsed
3988 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00003989 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003990 !((D.getContext() == Declarator::PrototypeContext ||
3991 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003992 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00003993 !Actions.containsUnexpandedParameterPacks(D))) {
3994 SourceLocation EllipsisLoc = ConsumeToken();
3995 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
3996 // The ellipsis was put in the wrong place. Recover, and explain to
3997 // the user what they should have done.
3998 ParseDeclarator(D);
3999 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4000 return;
4001 } else
4002 D.setEllipsisLoc(EllipsisLoc);
4003
4004 // The ellipsis can't be followed by a parenthesized declarator. We
4005 // check for that in ParseParenDeclarator, after we have disambiguated
4006 // the l_paren token.
4007 }
4008
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004009 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4010 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4011 // We found something that indicates the start of an unqualified-id.
4012 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004013 bool AllowConstructorName;
4014 if (D.getDeclSpec().hasTypeSpecifier())
4015 AllowConstructorName = false;
4016 else if (D.getCXXScopeSpec().isSet())
4017 AllowConstructorName =
4018 (D.getContext() == Declarator::FileContext ||
4019 (D.getContext() == Declarator::MemberContext &&
4020 D.getDeclSpec().isFriendSpecified()));
4021 else
4022 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4023
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004024 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004025 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4026 /*EnteringContext=*/true,
4027 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004028 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004029 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004030 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004031 D.getName()) ||
4032 // Once we're past the identifier, if the scope was bad, mark the
4033 // whole declarator bad.
4034 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004035 D.SetIdentifier(0, Tok.getLocation());
4036 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004037 } else {
4038 // Parsed the unqualified-id; update range information and move along.
4039 if (D.getSourceRange().getBegin().isInvalid())
4040 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4041 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004042 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004043 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004044 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004045 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004046 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004047 "There's a C++-specific check for tok::identifier above");
4048 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4049 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4050 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004051 goto PastIdentifier;
4052 }
Richard Smith9988f282012-03-29 01:16:42 +00004053
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004054 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004055 // direct-declarator: '(' declarator ')'
4056 // direct-declarator: '(' attributes declarator ')'
4057 // Example: 'char (*X)' or 'int (*XX)(void)'
4058 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004059
4060 // If the declarator was parenthesized, we entered the declarator
4061 // scope when parsing the parenthesized declarator, then exited
4062 // the scope already. Re-enter the scope, if we need to.
4063 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004064 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004065 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004066 if (!D.isInvalidType() &&
4067 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004068 // Change the declaration context for name lookup, until this function
4069 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004070 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004071 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004072 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004073 // This could be something simple like "int" (in which case the declarator
4074 // portion is empty), if an abstract-declarator is allowed.
4075 D.SetIdentifier(0, Tok.getLocation());
4076 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00004077 if (D.getContext() == Declarator::MemberContext)
4078 Diag(Tok, diag::err_expected_member_name_or_semi)
4079 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004080 else if (getLangOpts().CPlusPlus)
4081 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004082 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004083 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004084 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004085 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004086 }
Mike Stump1eb44332009-09-09 15:08:12 +00004087
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004088 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004089 assert(D.isPastIdentifier() &&
4090 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004091
Richard Smith6ee326a2012-04-10 01:32:12 +00004092 // Don't parse attributes unless we have parsed an unparenthesized name.
4093 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004094 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004095
Reid Spencer5f016e22007-07-11 17:01:13 +00004096 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004097 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004098 // Enter function-declaration scope, limiting any declarators to the
4099 // function prototype scope, including parameter declarators.
4100 ParseScope PrototypeScope(this,
4101 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004102 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4103 // In such a case, check if we actually have a function declarator; if it
4104 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00004105 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00004106 // When not in file scope, warn for ambiguous function declarators, just
4107 // in case the author intended it as a variable definition.
4108 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4109 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4110 break;
4111 }
John McCall0b7e6782011-03-24 11:26:52 +00004112 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004113 BalancedDelimiterTracker T(*this, tok::l_paren);
4114 T.consumeOpen();
4115 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004116 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004117 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004118 ParseBracketDeclarator(D);
4119 } else {
4120 break;
4121 }
4122 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004123}
Reid Spencer5f016e22007-07-11 17:01:13 +00004124
Chris Lattneref4715c2008-04-06 05:45:57 +00004125/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4126/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004127/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004128/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4129///
4130/// direct-declarator:
4131/// '(' declarator ')'
4132/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004133/// direct-declarator '(' parameter-type-list ')'
4134/// direct-declarator '(' identifier-list[opt] ')'
4135/// [GNU] direct-declarator '(' parameter-forward-declarations
4136/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004137///
4138void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004139 BalancedDelimiterTracker T(*this, tok::l_paren);
4140 T.consumeOpen();
4141
Chris Lattneref4715c2008-04-06 05:45:57 +00004142 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004143
Chris Lattner7399ee02008-10-20 02:05:46 +00004144 // Eat any attributes before we look at whether this is a grouping or function
4145 // declarator paren. If this is a grouping paren, the attribute applies to
4146 // the type being built up, for example:
4147 // int (__attribute__(()) *x)(long y)
4148 // If this ends up not being a grouping paren, the attribute applies to the
4149 // first argument, for example:
4150 // int (__attribute__(()) int x)
4151 // In either case, we need to eat any attributes to be able to determine what
4152 // sort of paren this is.
4153 //
John McCall0b7e6782011-03-24 11:26:52 +00004154 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004155 bool RequiresArg = false;
4156 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004157 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004158
Chris Lattner7399ee02008-10-20 02:05:46 +00004159 // We require that the argument list (if this is a non-grouping paren) be
4160 // present even if the attribute list was empty.
4161 RequiresArg = true;
4162 }
Steve Naroff239f0732008-12-25 14:16:32 +00004163 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004164 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004165 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004166 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004167 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004168 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004169 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004170 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004171 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004172 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004173
Chris Lattneref4715c2008-04-06 05:45:57 +00004174 // If we haven't past the identifier yet (or where the identifier would be
4175 // stored, if this is an abstract declarator), then this is probably just
4176 // grouping parens. However, if this could be an abstract-declarator, then
4177 // this could also be the start of function arguments (consider 'void()').
4178 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004179
Chris Lattneref4715c2008-04-06 05:45:57 +00004180 if (!D.mayOmitIdentifier()) {
4181 // If this can't be an abstract-declarator, this *must* be a grouping
4182 // paren, because we haven't seen the identifier yet.
4183 isGrouping = true;
4184 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004185 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4186 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004187 isDeclarationSpecifier() || // 'int(int)' is a function.
4188 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004189 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4190 // considered to be a type, not a K&R identifier-list.
4191 isGrouping = false;
4192 } else {
4193 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4194 isGrouping = true;
4195 }
Mike Stump1eb44332009-09-09 15:08:12 +00004196
Chris Lattneref4715c2008-04-06 05:45:57 +00004197 // If this is a grouping paren, handle:
4198 // direct-declarator: '(' declarator ')'
4199 // direct-declarator: '(' attributes declarator ')'
4200 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004201 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4202 D.setEllipsisLoc(SourceLocation());
4203
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004204 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004205 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004206 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004207 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004208 T.consumeClose();
4209 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4210 T.getCloseLocation()),
4211 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004212
4213 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004214
4215 // An ellipsis cannot be placed outside parentheses.
4216 if (EllipsisLoc.isValid())
4217 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4218
Chris Lattneref4715c2008-04-06 05:45:57 +00004219 return;
4220 }
Mike Stump1eb44332009-09-09 15:08:12 +00004221
Chris Lattneref4715c2008-04-06 05:45:57 +00004222 // Okay, if this wasn't a grouping paren, it must be the start of a function
4223 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004224 // identifier (and remember where it would have been), then call into
4225 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004226 D.SetIdentifier(0, Tok.getLocation());
4227
David Blaikie42d6d0c2011-12-04 05:04:18 +00004228 // Enter function-declaration scope, limiting any declarators to the
4229 // function prototype scope, including parameter declarators.
4230 ParseScope PrototypeScope(this,
4231 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004232 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004233 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004234}
4235
4236/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4237/// declarator D up to a paren, which indicates that we are parsing function
4238/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004239///
Richard Smith6ee326a2012-04-10 01:32:12 +00004240/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4241/// immediately after the open paren - they should be considered to be the
4242/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004243///
Richard Smith6ee326a2012-04-10 01:32:12 +00004244/// If RequiresArg is true, then the first argument of the function is required
4245/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004246///
Richard Smith6ee326a2012-04-10 01:32:12 +00004247/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4248/// (C++11) ref-qualifier[opt], exception-specification[opt],
4249/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4250///
4251/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004252/// dynamic-exception-specification
4253/// noexcept-specification
4254///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004255void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004256 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004257 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004258 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004259 assert(getCurScope()->isFunctionPrototypeScope() &&
4260 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004261 // lparen is already consumed!
4262 assert(D.isPastIdentifier() && "Should not call before identifier!");
4263
4264 // This should be true when the function has typed arguments.
4265 // Otherwise, it is treated as a K&R-style function.
4266 bool HasProto = false;
4267 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004268 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004269 // Remember where we see an ellipsis, if any.
4270 SourceLocation EllipsisLoc;
4271
4272 DeclSpec DS(AttrFactory);
4273 bool RefQualifierIsLValueRef = true;
4274 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004275 SourceLocation ConstQualifierLoc;
4276 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004277 ExceptionSpecificationType ESpecType = EST_None;
4278 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004279 SmallVector<ParsedType, 2> DynamicExceptions;
4280 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004281 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004282 ParsedAttributes FnAttrs(AttrFactory);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004283 ParsedType TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004284
James Molloy16f1f712012-02-29 10:24:19 +00004285 Actions.ActOnStartFunctionDeclarator();
4286
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004287 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004288 if (isFunctionDeclaratorIdentifierList()) {
4289 if (RequiresArg)
4290 Diag(Tok, diag::err_argument_required_after_attribute);
4291
4292 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4293
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004294 Tracker.consumeClose();
4295 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004296 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004297 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004298 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004299 else if (RequiresArg)
4300 Diag(Tok, diag::err_argument_required_after_attribute);
4301
David Blaikie4e4d0842012-03-11 07:00:24 +00004302 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004303
4304 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004305 Tracker.consumeClose();
4306 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004307
David Blaikie4e4d0842012-03-11 07:00:24 +00004308 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004309 // FIXME: Accept these components in any order, and produce fixits to
4310 // correct the order if the user gets it wrong. Ideally we should deal
4311 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004312
4313 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004314 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4315 if (!DS.getSourceRange().getEnd().isInvalid()) {
4316 EndLoc = DS.getSourceRange().getEnd();
4317 ConstQualifierLoc = DS.getConstSpecLoc();
4318 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4319 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004320
4321 // Parse ref-qualifier[opt].
4322 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004323 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004324 diag::warn_cxx98_compat_ref_qualifier :
4325 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004326
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004327 RefQualifierIsLValueRef = Tok.is(tok::amp);
4328 RefQualifierLoc = ConsumeToken();
4329 EndLoc = RefQualifierLoc;
4330 }
4331
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004332 // C++11 [expr.prim.general]p3:
4333 // If a declaration declares a member function or member function
4334 // template of a class X, the expression this is a prvalue of type
4335 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
4336 // and the end of the function-definition, member-declarator, or
4337 // declarator.
4338 bool IsCXX11MemberFunction =
4339 getLangOpts().CPlusPlus0x &&
4340 (D.getContext() == Declarator::MemberContext ||
4341 (D.getContext() == Declarator::FileContext &&
4342 D.getCXXScopeSpec().isValid() &&
4343 Actions.CurContext->isRecord()));
4344 Sema::CXXThisScopeRAII ThisScope(Actions,
4345 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4346 DS.getTypeQualifiers(),
4347 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004348
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004349 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004350 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004351 DynamicExceptions,
4352 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004353 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004354 if (ESpecType != EST_None)
4355 EndLoc = ESpecRange.getEnd();
4356
Richard Smith6ee326a2012-04-10 01:32:12 +00004357 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4358 // after the exception-specification.
4359 MaybeParseCXX0XAttributes(FnAttrs);
4360
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004361 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004362 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004363 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004364 SourceRange Range;
4365 TrailingReturnType = ParseTrailingReturnType(Range).get();
4366 if (Range.getEnd().isValid())
4367 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004368 }
4369 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004370 }
4371
4372 // Remember that we parsed a function type, and remember the attributes.
4373 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4374 /*isVariadic=*/EllipsisLoc.isValid(),
4375 EllipsisLoc,
4376 ParamInfo.data(), ParamInfo.size(),
4377 DS.getTypeQualifiers(),
4378 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004379 RefQualifierLoc, ConstQualifierLoc,
4380 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004381 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004382 ESpecType, ESpecRange.getBegin(),
4383 DynamicExceptions.data(),
4384 DynamicExceptionRanges.data(),
4385 DynamicExceptions.size(),
4386 NoexceptExpr.isUsable() ?
4387 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004388 Tracker.getOpenLocation(),
4389 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004390 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004391 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004392
4393 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004394}
4395
4396/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4397/// identifier list form for a K&R-style function: void foo(a,b,c)
4398///
4399/// Note that identifier-lists are only allowed for normal declarators, not for
4400/// abstract-declarators.
4401bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004402 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004403 && Tok.is(tok::identifier)
4404 && !TryAltiVecVectorToken()
4405 // K&R identifier lists can't have typedefs as identifiers, per C99
4406 // 6.7.5.3p11.
4407 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4408 // Identifier lists follow a really simple grammar: the identifiers can
4409 // be followed *only* by a ", identifier" or ")". However, K&R
4410 // identifier lists are really rare in the brave new modern world, and
4411 // it is very common for someone to typo a type in a non-K&R style
4412 // list. If we are presented with something like: "void foo(intptr x,
4413 // float y)", we don't want to start parsing the function declarator as
4414 // though it is a K&R style declarator just because intptr is an
4415 // invalid type.
4416 //
4417 // To handle this, we check to see if the token after the first
4418 // identifier is a "," or ")". Only then do we parse it as an
4419 // identifier list.
4420 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4421}
4422
4423/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4424/// we found a K&R-style identifier list instead of a typed parameter list.
4425///
4426/// After returning, ParamInfo will hold the parsed parameters.
4427///
4428/// identifier-list: [C99 6.7.5]
4429/// identifier
4430/// identifier-list ',' identifier
4431///
4432void Parser::ParseFunctionDeclaratorIdentifierList(
4433 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004434 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004435 // If there was no identifier specified for the declarator, either we are in
4436 // an abstract-declarator, or we are in a parameter declarator which was found
4437 // to be abstract. In abstract-declarators, identifier lists are not valid:
4438 // diagnose this.
4439 if (!D.getIdentifier())
4440 Diag(Tok, diag::ext_ident_list_in_param);
4441
4442 // Maintain an efficient lookup of params we have seen so far.
4443 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4444
4445 while (1) {
4446 // If this isn't an identifier, report the error and skip until ')'.
4447 if (Tok.isNot(tok::identifier)) {
4448 Diag(Tok, diag::err_expected_ident);
4449 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4450 // Forget we parsed anything.
4451 ParamInfo.clear();
4452 return;
4453 }
4454
4455 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4456
4457 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4458 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4459 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4460
4461 // Verify that the argument identifier has not already been mentioned.
4462 if (!ParamsSoFar.insert(ParmII)) {
4463 Diag(Tok, diag::err_param_redefinition) << ParmII;
4464 } else {
4465 // Remember this identifier in ParamInfo.
4466 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4467 Tok.getLocation(),
4468 0));
4469 }
4470
4471 // Eat the identifier.
4472 ConsumeToken();
4473
4474 // The list continues if we see a comma.
4475 if (Tok.isNot(tok::comma))
4476 break;
4477 ConsumeToken();
4478 }
4479}
4480
4481/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4482/// after the opening parenthesis. This function will not parse a K&R-style
4483/// identifier list.
4484///
Richard Smith6ce48a72012-04-11 04:01:28 +00004485/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4486/// caller parsed those arguments immediately after the open paren - they should
4487/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004488///
4489/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4490/// be the location of the ellipsis, if any was parsed.
4491///
Reid Spencer5f016e22007-07-11 17:01:13 +00004492/// parameter-type-list: [C99 6.7.5]
4493/// parameter-list
4494/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004495/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004496///
4497/// parameter-list: [C99 6.7.5]
4498/// parameter-declaration
4499/// parameter-list ',' parameter-declaration
4500///
4501/// parameter-declaration: [C99 6.7.5]
4502/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004503/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004504/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004505/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004506/// declaration-specifiers abstract-declarator[opt]
4507/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004508/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004509/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004510/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004511///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004512void Parser::ParseParameterDeclarationClause(
4513 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004514 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004515 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004516 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004517
Chris Lattnerf97409f2008-04-06 06:57:35 +00004518 while (1) {
4519 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004520 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4521 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004522 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004523 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004524 }
Mike Stump1eb44332009-09-09 15:08:12 +00004525
Chris Lattnerf97409f2008-04-06 06:57:35 +00004526 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004527 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004528 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004529
Richard Smith6ce48a72012-04-11 04:01:28 +00004530 // Parse any C++11 attributes.
4531 MaybeParseCXX0XAttributes(DS.getAttributes());
4532
John McCall7f040a92010-12-24 02:08:15 +00004533 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004534 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004535 ParseMicrosoftAttributes(DS.getAttributes());
4536
4537 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004538
4539 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004540 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004541 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004542 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4543 // too much hassle.
4544 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004545
Chris Lattnere64c5492009-02-27 18:38:20 +00004546 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004547
Chris Lattnerf97409f2008-04-06 06:57:35 +00004548 // Parse the declarator. This is "PrototypeContext", because we must
4549 // accept either 'declarator' or 'abstract-declarator' here.
4550 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4551 ParseDeclarator(ParmDecl);
4552
4553 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004554 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004555
Chris Lattnerf97409f2008-04-06 06:57:35 +00004556 // Remember this parsed parameter in ParamInfo.
4557 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004558
Douglas Gregor72b505b2008-12-16 21:30:33 +00004559 // DefArgToks is used when the parsing of default arguments needs
4560 // to be delayed.
4561 CachedTokens *DefArgToks = 0;
4562
Chris Lattnerf97409f2008-04-06 06:57:35 +00004563 // If no parameter was specified, verify that *something* was specified,
4564 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004565 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4566 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004567 // Completely missing, emit error.
4568 Diag(DSStart, diag::err_missing_param);
4569 } else {
4570 // Otherwise, we have something. Add it and let semantic analysis try
4571 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004572
Chris Lattnerf97409f2008-04-06 06:57:35 +00004573 // Inform the actions module about the parameter declarator, so it gets
4574 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004575 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004576
4577 // Parse the default argument, if any. We parse the default
4578 // arguments in all dialects; the semantic analysis in
4579 // ActOnParamDefaultArgument will reject the default argument in
4580 // C.
4581 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004582 SourceLocation EqualLoc = Tok.getLocation();
4583
Chris Lattner04421082008-04-08 04:40:51 +00004584 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004585 if (D.getContext() == Declarator::MemberContext) {
4586 // If we're inside a class definition, cache the tokens
4587 // corresponding to the default argument. We'll actually parse
4588 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004589 // FIXME: Can we use a smart pointer for Toks?
4590 DefArgToks = new CachedTokens;
4591
Mike Stump1eb44332009-09-09 15:08:12 +00004592 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004593 /*StopAtSemi=*/true,
4594 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004595 delete DefArgToks;
4596 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004597 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004598 } else {
4599 // Mark the end of the default argument so that we know when to
4600 // stop when we parse it later on.
4601 Token DefArgEnd;
4602 DefArgEnd.startToken();
4603 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4604 DefArgEnd.setLocation(Tok.getLocation());
4605 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004606 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004607 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004608 }
Chris Lattner04421082008-04-08 04:40:51 +00004609 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004610 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004611 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004612
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004613 // The argument isn't actually potentially evaluated unless it is
4614 // used.
4615 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004616 Sema::PotentiallyEvaluatedIfUsed,
4617 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004618
Sebastian Redl84407ba2012-03-14 15:54:00 +00004619 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004620 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4621 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004622 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004623 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004624 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004625 if (DefArgResult.isInvalid()) {
4626 Actions.ActOnParamDefaultArgumentError(Param);
4627 SkipUntil(tok::comma, tok::r_paren, true, true);
4628 } else {
4629 // Inform the actions module about the default argument
4630 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004631 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004632 }
Chris Lattner04421082008-04-08 04:40:51 +00004633 }
4634 }
Mike Stump1eb44332009-09-09 15:08:12 +00004635
4636 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4637 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004638 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004639 }
4640
4641 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004642 if (Tok.isNot(tok::comma)) {
4643 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004644 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4645
David Blaikie4e4d0842012-03-11 07:00:24 +00004646 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004647 // We have ellipsis without a preceding ',', which is ill-formed
4648 // in C. Complain and provide the fix.
4649 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004650 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004651 }
4652 }
4653
4654 break;
4655 }
Mike Stump1eb44332009-09-09 15:08:12 +00004656
Chris Lattnerf97409f2008-04-06 06:57:35 +00004657 // Consume the comma.
4658 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004659 }
Mike Stump1eb44332009-09-09 15:08:12 +00004660
Chris Lattner66d28652008-04-06 06:34:08 +00004661}
Chris Lattneref4715c2008-04-06 05:45:57 +00004662
Reid Spencer5f016e22007-07-11 17:01:13 +00004663/// [C90] direct-declarator '[' constant-expression[opt] ']'
4664/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4665/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4666/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4667/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004668/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4669/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004670void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004671 if (CheckProhibitedCXX11Attribute())
4672 return;
4673
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004674 BalancedDelimiterTracker T(*this, tok::l_square);
4675 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004676
Chris Lattner378c7e42008-12-18 07:27:21 +00004677 // C array syntax has many features, but by-far the most common is [] and [4].
4678 // This code does a fast path to handle some of the most obvious cases.
4679 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004680 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004681 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004682 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004683
Chris Lattner378c7e42008-12-18 07:27:21 +00004684 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004685 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004686 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004687 T.getOpenLocation(),
4688 T.getCloseLocation()),
4689 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004690 return;
4691 } else if (Tok.getKind() == tok::numeric_constant &&
4692 GetLookAheadToken(1).is(tok::r_square)) {
4693 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004694 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004695 ConsumeToken();
4696
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004697 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004698 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004699 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004700
Chris Lattner378c7e42008-12-18 07:27:21 +00004701 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004702 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004703 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004704 T.getOpenLocation(),
4705 T.getCloseLocation()),
4706 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004707 return;
4708 }
Mike Stump1eb44332009-09-09 15:08:12 +00004709
Reid Spencer5f016e22007-07-11 17:01:13 +00004710 // If valid, this location is the position where we read the 'static' keyword.
4711 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004712 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004713 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004714
Reid Spencer5f016e22007-07-11 17:01:13 +00004715 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004716 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004717 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004718 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004719
Reid Spencer5f016e22007-07-11 17:01:13 +00004720 // If we haven't already read 'static', check to see if there is one after the
4721 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004722 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004723 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004724
Reid Spencer5f016e22007-07-11 17:01:13 +00004725 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4726 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004727 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004728
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004729 // Handle the case where we have '[*]' as the array size. However, a leading
4730 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4731 // the the token after the star is a ']'. Since stars in arrays are
4732 // infrequent, use of lookahead is not costly here.
4733 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004734 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004735
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004736 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004737 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004738 StaticLoc = SourceLocation(); // Drop the static.
4739 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004740 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004741 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004742 // Note, in C89, this production uses the constant-expr production instead
4743 // of assignment-expr. The only difference is that assignment-expr allows
4744 // things like '=' and '*='. Sema rejects these in C89 mode because they
4745 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004746
Douglas Gregore0762c92009-06-19 23:52:42 +00004747 // Parse the constant-expression or assignment-expression now (depending
4748 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004749 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004750 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004751 } else {
4752 EnterExpressionEvaluationContext Unevaluated(Actions,
4753 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004754 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004755 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004756 }
Mike Stump1eb44332009-09-09 15:08:12 +00004757
Reid Spencer5f016e22007-07-11 17:01:13 +00004758 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004759 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004760 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004761 // If the expression was invalid, skip it.
4762 SkipUntil(tok::r_square);
4763 return;
4764 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004765
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004766 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004767
John McCall0b7e6782011-03-24 11:26:52 +00004768 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004769 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004770
Chris Lattner378c7e42008-12-18 07:27:21 +00004771 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004772 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004773 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004774 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004775 T.getOpenLocation(),
4776 T.getCloseLocation()),
4777 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004778}
4779
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004780/// [GNU] typeof-specifier:
4781/// typeof ( expressions )
4782/// typeof ( type-name )
4783/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004784///
4785void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004786 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004787 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004788 SourceLocation StartLoc = ConsumeToken();
4789
John McCallcfb708c2010-01-13 20:03:27 +00004790 const bool hasParens = Tok.is(tok::l_paren);
4791
Eli Friedman71b8fb52012-01-21 01:01:51 +00004792 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4793
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004794 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004795 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004796 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004797 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4798 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004799 if (hasParens)
4800 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004801
4802 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004803 // FIXME: Not accurate, the range gets one token more than it should.
4804 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004805 else
4806 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004807
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004808 if (isCastExpr) {
4809 if (!CastTy) {
4810 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004811 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004812 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004813
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004814 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004815 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004816 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4817 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004818 DiagID, CastTy))
4819 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004820 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004821 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004822
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004823 // If we get here, the operand to the typeof was an expresion.
4824 if (Operand.isInvalid()) {
4825 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004826 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004827 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004828
Eli Friedman71b8fb52012-01-21 01:01:51 +00004829 // We might need to transform the operand if it is potentially evaluated.
4830 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4831 if (Operand.isInvalid()) {
4832 DS.SetTypeSpecError();
4833 return;
4834 }
4835
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004836 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004837 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004838 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4839 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004840 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004841 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004842}
Chris Lattner1b492422010-02-28 18:33:55 +00004843
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004844/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004845/// _Atomic ( type-name )
4846///
4847void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4848 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4849
4850 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004851 BalancedDelimiterTracker T(*this, tok::l_paren);
4852 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004853 SkipUntil(tok::r_paren);
4854 return;
4855 }
4856
4857 TypeResult Result = ParseTypeName();
4858 if (Result.isInvalid()) {
4859 SkipUntil(tok::r_paren);
4860 return;
4861 }
4862
4863 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004864 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004865
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004866 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004867 return;
4868
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004869 DS.setTypeofParensRange(T.getRange());
4870 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004871
4872 const char *PrevSpec = 0;
4873 unsigned DiagID;
4874 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4875 DiagID, Result.release()))
4876 Diag(StartLoc, DiagID) << PrevSpec;
4877}
4878
Chris Lattner1b492422010-02-28 18:33:55 +00004879
4880/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4881/// from TryAltiVecVectorToken.
4882bool Parser::TryAltiVecVectorTokenOutOfLine() {
4883 Token Next = NextToken();
4884 switch (Next.getKind()) {
4885 default: return false;
4886 case tok::kw_short:
4887 case tok::kw_long:
4888 case tok::kw_signed:
4889 case tok::kw_unsigned:
4890 case tok::kw_void:
4891 case tok::kw_char:
4892 case tok::kw_int:
4893 case tok::kw_float:
4894 case tok::kw_double:
4895 case tok::kw_bool:
4896 case tok::kw___pixel:
4897 Tok.setKind(tok::kw___vector);
4898 return true;
4899 case tok::identifier:
4900 if (Next.getIdentifierInfo() == Ident_pixel) {
4901 Tok.setKind(tok::kw___vector);
4902 return true;
4903 }
4904 return false;
4905 }
4906}
4907
4908bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4909 const char *&PrevSpec, unsigned &DiagID,
4910 bool &isInvalid) {
4911 if (Tok.getIdentifierInfo() == Ident_vector) {
4912 Token Next = NextToken();
4913 switch (Next.getKind()) {
4914 case tok::kw_short:
4915 case tok::kw_long:
4916 case tok::kw_signed:
4917 case tok::kw_unsigned:
4918 case tok::kw_void:
4919 case tok::kw_char:
4920 case tok::kw_int:
4921 case tok::kw_float:
4922 case tok::kw_double:
4923 case tok::kw_bool:
4924 case tok::kw___pixel:
4925 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4926 return true;
4927 case tok::identifier:
4928 if (Next.getIdentifierInfo() == Ident_pixel) {
4929 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4930 return true;
4931 }
4932 break;
4933 default:
4934 break;
4935 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004936 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004937 DS.isTypeAltiVecVector()) {
4938 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4939 return true;
4940 }
4941 return false;
4942}